|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\UserModel; |
|
6
|
|
|
use Core\BlogocException; |
|
7
|
|
|
use Core\Controller; |
|
8
|
|
|
use Core\Container; |
|
9
|
|
|
use Core\Traits\PasswordFunctions; |
|
10
|
|
|
use Core\Traits\StringFunctions; |
|
11
|
|
|
|
|
12
|
|
|
class Password extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
use PasswordFunctions; |
|
16
|
|
|
use StringFunctions; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
protected $siteConfig; |
|
19
|
|
|
protected $sendMail; |
|
20
|
|
|
|
|
21
|
|
|
private $userModel; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(Container $container) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->loadModules[] = 'SiteConfig'; |
|
26
|
|
|
$this->loadModules[] = 'SendMail'; |
|
27
|
|
|
parent::__construct($container); |
|
28
|
|
|
|
|
29
|
|
|
$this->userModel = new UserModel($this->container); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function index() |
|
33
|
|
|
{ |
|
34
|
|
|
if ($this->session->isParamSet("user")) { |
|
35
|
|
|
//we are already connected, redirect |
|
36
|
|
|
$this->response->redirect(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->data['configs'] = $this->siteConfig->getSiteConfig(); |
|
40
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
|
41
|
|
|
|
|
42
|
|
|
$this->renderView('forgotPassword'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function reset($get) |
|
46
|
|
|
{ |
|
47
|
|
|
//only get messages here |
|
48
|
|
|
if(!$this->startsWith(strtolower($get),"get")) |
|
49
|
|
|
{ |
|
50
|
|
|
throw new \Exception("invalid call"); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
//grab the token and ID |
|
54
|
|
|
$token = $this->request->getData("token"); |
|
55
|
|
|
$userId = $this->request->getData("userId"); |
|
56
|
|
|
|
|
57
|
|
|
//verify if token is valid |
|
58
|
|
|
if(!$this->isHexa($token)|| !$this->isInt($userId)) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$this->alertBox->setAlert('Badly formatted Token', 'error'); |
|
61
|
|
|
$this->response->redirect(); |
|
62
|
|
|
} |
|
63
|
|
|
if(!$this->userModel->getUserDetailsByToken($token, $userId)) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$this->alertBox->setAlert('Invalid reset token, please request a new password', 'error'); |
|
66
|
|
|
$this->response->redirect(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->data['configs'] = $this->siteConfig->getSiteConfig(); |
|
70
|
|
|
$this->data['navigation'] = $this->siteConfig->getMenu(); |
|
71
|
|
|
|
|
72
|
|
|
$this->data["token"] = $token; |
|
73
|
|
|
$this->data["userId"] = $userId; |
|
74
|
|
|
$this->renderView('resetPassword'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function resetPassword() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->onlyPost(); |
|
80
|
|
|
$request = $this->request->getDataFull(); |
|
81
|
|
|
$password = $request["forgotPassword"]; |
|
82
|
|
|
$confirm = $request["forgotConfirm"]; |
|
83
|
|
|
$token = $request["token"]; |
|
84
|
|
|
$userId = $request["userId"]; |
|
85
|
|
|
|
|
86
|
|
|
if(!$this->isHexa($token) || !$this->isInt($userId) ) |
|
87
|
|
|
{ |
|
88
|
|
|
throw new \Exception("Bad Token or ID request"); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$error = false; |
|
92
|
|
|
$registerErrors = new \stdClass(); |
|
93
|
|
|
if($password !== $confirm) |
|
94
|
|
|
{ |
|
95
|
|
|
$error = true; |
|
96
|
|
|
$registerErrors->forgotPassword = "password and confirmation do not match"; |
|
97
|
|
|
$registerErrors->forgotConfirm = "password and confirmation do not match"; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$passwordError = $this->isPasswordComplex($password); |
|
101
|
|
|
if (!$passwordError["success"]) { |
|
102
|
|
|
$error = true; |
|
103
|
|
|
$registerErrors->forgotPassword = $passwordError["message"]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($error) { |
|
107
|
|
|
$this->session->set("registrationErrors", $registerErrors); |
|
108
|
|
|
$this->response->redirect('/password/reset/get?token='.$token); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$user = $this->userModel->getUserDetailsByToken($token, $userId); |
|
112
|
|
|
if (!$user) { |
|
113
|
|
|
|
|
114
|
|
|
$this->alertBox->setAlert('Invalid reset token', 'error'); |
|
115
|
|
|
$this->response->redirect(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$this->userModel->resetPassword($user->idusers, $password); |
|
119
|
|
|
|
|
120
|
|
|
$this->alertBox->setAlert('Password reset, please login'); |
|
121
|
|
|
$this->response->redirect("/login"); |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @throws \Exception |
|
127
|
|
|
*/ |
|
128
|
|
|
public function sendResetMail() |
|
129
|
|
|
{ |
|
130
|
|
|
$this->onlyPost(); |
|
131
|
|
|
$request = $this->request->getDataFull(); |
|
132
|
|
|
$email = $request["forgotEmail"]; |
|
133
|
|
|
|
|
134
|
|
|
$error = false; |
|
135
|
|
|
$registerErrors = new \stdClass(); |
|
136
|
|
|
$user = false; |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
try { |
|
139
|
|
|
$user = $this->userModel->getUserDetailsByEmail($email); |
|
140
|
|
|
if (!$user) { |
|
141
|
|
|
$error = true; |
|
142
|
|
|
$registerErrors->forgotEmail = "email not found"; |
|
143
|
|
|
} |
|
144
|
|
|
} catch (BlogocException $e) { |
|
145
|
|
|
$error = true; |
|
146
|
|
|
$registerErrors->forgotEmail = $e->getMessage(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if ($error) { |
|
150
|
|
|
$this->session->set("registrationInfo", $request); |
|
151
|
|
|
$this->session->set("registrationErrors", $registerErrors); |
|
152
|
|
|
$this->response->redirect('/password'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$token = $this->userModel->generatePasswordHash($user->idusers); |
|
156
|
|
|
$this->sendMail->sendResetPasswordMail($email, $token, $user->idusers); |
|
157
|
|
|
|
|
158
|
|
|
$this->alertBox->setAlert('Password reset link sent to your mailbox'); |
|
159
|
|
|
$this->response->redirect(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
} |