|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\Pages; |
|
10
|
|
|
|
|
11
|
|
|
use Waca\DataObjects\User; |
|
12
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
|
13
|
|
|
use Waca\PdoDatabase; |
|
14
|
|
|
use Waca\SessionAlert; |
|
15
|
|
|
use Waca\Tasks\InternalPageBase; |
|
16
|
|
|
use Waca\WebRequest; |
|
17
|
|
|
|
|
18
|
|
|
class PageForgotPassword extends InternalPageBase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Main function for this page, when no specific actions are called. |
|
22
|
|
|
* |
|
23
|
|
|
* This is the forgotten password reset form |
|
24
|
|
|
* @category Security-Critical |
|
25
|
|
|
*/ |
|
|
|
|
|
|
26
|
|
|
protected function main() |
|
27
|
|
|
{ |
|
28
|
|
|
if (WebRequest::wasPosted()) { |
|
29
|
|
|
$this->validateCSRFToken(); |
|
30
|
|
|
$username = WebRequest::postString('username'); |
|
31
|
|
|
$email = WebRequest::postEmail('email'); |
|
32
|
|
|
$database = $this->getDatabase(); |
|
33
|
|
|
|
|
34
|
|
|
if ($username === null || trim($username) === "" || $email === null || trim($email) === "") { |
|
|
|
|
|
|
35
|
|
|
throw new ApplicationLogicException("Both username and email address must be specified!"); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$user = User::getByUsername($username, $database); |
|
39
|
|
|
$this->sendResetMail($user, $email); |
|
40
|
|
|
|
|
41
|
|
|
SessionAlert::success('<strong>Your password reset request has been completed.</strong> Please check your e-mail.'); |
|
42
|
|
|
|
|
43
|
|
|
$this->redirect('login'); |
|
44
|
|
|
} |
|
45
|
|
|
else { |
|
46
|
|
|
$this->assignCSRFToken(); |
|
47
|
|
|
$this->setTemplate('forgot-password/forgotpw.tpl'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Sends a reset email if the user is authenticated |
|
53
|
|
|
* |
|
54
|
|
|
* @param User|boolean $user The user located from the database, or false. Doesn't really matter, since we do the |
|
55
|
|
|
* check anyway within this method and silently skip if we don't have a user. |
|
56
|
|
|
* @param string $email The provided email address |
|
57
|
|
|
*/ |
|
58
|
|
|
private function sendResetMail($user, $email) |
|
59
|
|
|
{ |
|
60
|
|
|
// If the user isn't found, or the email address is wrong, skip sending the details silently. |
|
61
|
|
|
if (!$user instanceof User) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (strtolower($user->getEmail()) === strtolower($email)) { |
|
66
|
|
|
$clientIp = $this->getXffTrustProvider() |
|
67
|
|
|
->getTrustedClientIp(WebRequest::remoteAddress(), WebRequest::forwardedAddress()); |
|
68
|
|
|
|
|
69
|
|
|
$this->assign("user", $user); |
|
|
|
|
|
|
70
|
|
|
$this->assign("hash", $user->getForgottenPasswordHash()); |
|
|
|
|
|
|
71
|
|
|
$this->assign("remoteAddress", $clientIp); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$emailContent = $this->fetchTemplate('forgot-password/reset-mail.tpl'); |
|
74
|
|
|
|
|
75
|
|
|
$this->getEmailHelper()->sendMail($user->getEmail(), "", $emailContent); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Entry point for the reset action |
|
81
|
|
|
* |
|
82
|
|
|
* This is the reset password part of the form. |
|
83
|
|
|
* @category Security-Critical |
|
84
|
|
|
*/ |
|
|
|
|
|
|
85
|
|
|
protected function reset() |
|
86
|
|
|
{ |
|
87
|
|
|
$si = WebRequest::getString('si'); |
|
88
|
|
|
$id = WebRequest::getString('id'); |
|
89
|
|
|
|
|
90
|
|
|
if ($si === null || trim($si) === "" || $id === null || trim($id) === "") { |
|
|
|
|
|
|
91
|
|
|
throw new ApplicationLogicException("Link not valid, please ensure it has copied correctly"); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$database = $this->getDatabase(); |
|
95
|
|
|
$user = $this->getResettingUser($id, $database, $si); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
// Dual mode |
|
98
|
|
|
if (WebRequest::wasPosted()) { |
|
99
|
|
|
$this->validateCSRFToken(); |
|
100
|
|
|
try { |
|
101
|
|
|
$this->doReset($user); |
|
102
|
|
|
} |
|
103
|
|
|
catch (ApplicationLogicException $ex) { |
|
104
|
|
|
SessionAlert::error($ex->getMessage()); |
|
105
|
|
|
$this->redirect('forgotPassword', 'reset', array('si' => $si, 'id' => $id)); |
|
106
|
|
|
|
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
else { |
|
111
|
|
|
$this->assignCSRFToken(); |
|
112
|
|
|
$this->assign('user', $user); |
|
113
|
|
|
$this->setTemplate('forgot-password/forgotpwreset.tpl'); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Gets the user resetting their password from the database, or throwing an exception if that is not possible. |
|
119
|
|
|
* |
|
120
|
|
|
* @param integer $id The ID of the user to retrieve |
|
121
|
|
|
* @param PdoDatabase $database The database object to use |
|
122
|
|
|
* @param string $si The reset hash provided |
|
123
|
|
|
* |
|
124
|
|
|
* @return User |
|
125
|
|
|
* @throws ApplicationLogicException |
|
126
|
|
|
*/ |
|
127
|
|
|
private function getResettingUser($id, $database, $si) |
|
128
|
|
|
{ |
|
129
|
|
|
$user = User::getById($id, $database); |
|
130
|
|
|
|
|
131
|
|
|
if ($user === false || $user->getForgottenPasswordHash() !== $si || $user->isCommunityUser()) { |
|
132
|
|
|
throw new ApplicationLogicException("User not found"); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $user; |
|
136
|
|
|
} |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Performs the setting of the new password |
|
140
|
|
|
* |
|
141
|
|
|
* @param User $user The user to set the password for |
|
142
|
|
|
* |
|
143
|
|
|
* @throws ApplicationLogicException |
|
144
|
|
|
*/ |
|
145
|
|
|
private function doReset(User $user) |
|
146
|
|
|
{ |
|
147
|
|
|
$pw = WebRequest::postString('pw'); |
|
148
|
|
|
$pw2 = WebRequest::postString('pw2'); |
|
149
|
|
|
|
|
150
|
|
|
if ($pw !== $pw2) { |
|
151
|
|
|
throw new ApplicationLogicException('Passwords do not match!'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$user->setPassword($pw); |
|
155
|
|
|
$user->save(); |
|
156
|
|
|
|
|
157
|
|
|
SessionAlert::success('You may now log in!'); |
|
158
|
|
|
$this->redirect('login'); |
|
159
|
|
|
} |
|
|
|
|
|
|
160
|
|
|
|
|
161
|
|
|
protected function isProtectedPage() |
|
162
|
|
|
{ |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
|
|
|
|
|
165
|
|
|
} |
|
|
|
|
|
|
166
|
|
|
|