-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathreset_password.php
353 lines (280 loc) · 10.4 KB
/
reset_password.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
// This file is part of the Huygens Remote Manager
// Copyright and license notice: see license.txt
use hrm\Mail;
use hrm\Nav;
use hrm\user\proxy\ProxyFactory;
use hrm\user\UserConstants;
use hrm\Util;
use hrm\Validator;
use hrm\user\UserManager;
require_once dirname(__FILE__) . '/inc/bootstrap.php';
global $email_sender;
global $hrm_url;
/* *****************************************************************************
*
* START SESSION, CHECK LOGIN STATE, INITIALIZE WHAT NEEDED
*
**************************************************************************** */
session_start();
$message = "";
// Exit?
if (isset($_GET["exited"])) {
header("Location: " . "login.php");
exit();
}
// Clean array where to store sanitized input
if (isset($_SESSION['sanizited_input'])) {
$clean = $_SESSION['sanizited_input'];
} else {
$clean = array();
}
/* *****************************************************************************
*
* Is there some request in $_GET?
*
**************************************************************************** */
// Default action
$actionMode = "askForUserName";
if (!isset($_POST['modifypassword']) && isset($_GET['user']) && isset($_GET['seed'])) {
// Username
if (isset($_GET["user"])) {
if (Validator::isUserNameValid($_GET['user'])) {
$clean['username'] = $_GET['user'];
}
}
// Seed
if (isset($_GET["seed"])) {
if (Validator::isStringSanitized($_GET['seed'])) {
$clean['seed'] = $_GET['seed'];
}
}
// Is there a matching user with a password request?
if (UserManager::existsUserPasswordResetRequestWithSeed($clean['username'], $clean['seed'])) {
// Store the parameters
$_SESSION['sanizited_input'] = $clean;
// Action
$actionMode = "askForNewPassword";
} else {
$actionMode = "requestFromEmailFailed";
}
}
// Check that the user exists and can change his password, and e-mail the link
if (isset($_POST['requestusername']) && isset($_POST['username'])) {
// Username
if (Validator::isUserNameValid($_POST['username'])) {
$clean['username'] = $_POST['username'];
}
// Is there such a user?
if (UserManager::existsUserWithName($clean['username'])) {
// Retrieve the authentication mechanism for the user
$authProxy = ProxyFactory::getProxy($clean['username']);
if (!$authProxy->canModifyPassword()) {
$message = "The " . $authProxy->friendlyName() . " authentication does not " .
"allow changing the password for user " . $clean['username'] . "!";
} else {
// Retrieve the user e-mail
$email = $authProxy->getEmailAddress($clean['username']);
if ($email == "") {
$message = "Cannot retrieve e-mail address for user " . $clean['username'] . "!";
} else {
# Mark the
$seed = UserManager::generateAndSetSeed($clean['username']);
if ($seed == "") {
$message = "Could not mark the user to password reset!";
} else {
# Email the user with instructions
$mail = new Mail($email_sender);
$tmp_username = $clean['username'];
$text = "Please reset your password here: $hrm_url/reset_password.php?user=$tmp_username&seed=$seed";
$mail->setReceiver($email);
$mail->setSubject("HRM's password reset");
$mail->setMessage($text);
if (!$mail->send()) {
$notice = "Could not send an email to user $tmp_username! " .
"Please contact your administrator.";
$actionMode = "genericErrorWithMessage";
} else {
$actionMode = "emailSentSuccessfully";
}
}
}
}
} else {
$message = "Sorry, could not find user " . $clean['username'] . ".";
}
}
// If the new password has been submitted already, check it and set it
if (isset($_POST['modifypassword']) && isset($_POST['pass1']) &&
isset($_POST['pass2']) && isset($_SESSION['sanizited_input'])) {
// Retrieve the sanitized input
$clean = $_SESSION['sanizited_input'];
// Passwords
if (Validator::isPasswordValid($_POST["pass1"])) {
$clean["pass1"] = $_POST["pass1"];
}
if (Validator::isPasswordValid($_POST["pass2"])) {
$clean["pass2"] = $_POST["pass2"];
}
if ($clean["pass1"] != $clean["pass2"]) {
$message = "Passwords do not match!";
# Ask again
$actionMode = "askForNewPassword";
} else {
// Update the user
if (UserManager::changeUserPassword($clean['username'], $clean['pass1'])) {
// Make the user active
UserManager::setUserStatus($clean['username'], UserConstants::STATUS_ACTIVE);
$actionMode = "passwordUpdateSuccessful";
} else {
$actionMode = "passwordUpdateFailed";
}
// In any case, we reset the seed
UserManager::resetSeed($clean['username']);
}
}
/* *****************************************************************************
*
* DISPLAY PAGE
*
**************************************************************************** */
include("header.inc.php");
?>
<!--
Tooltips
-->
<span class="toolTip" id="ttSpanCancel">Discard changes and go back to your home page.</span>
<span class="toolTip" id="ttSpanContinue">Query the user.</span>
<span class="toolTip" id="ttSpanSave">Update the password.</span>
<div id="nav">
<div id="navleft">
<ul>
<?php
echo(Nav::linkWikiPage('HuygensRemoteManagerHelpLogin'));
?>
</ul>
</div>
<div id="navright">
<ul>
<li>
<?php
echo(Nav::exitToLogin());
?>
</li>
</ul>
</div>
<div class="clear"></div>
</div>
<div id="content">
<h3><img alt="Reset your password" src="./images/forgot_pwd.png"
width="40"/> Reset your password</h3>
<?php
/* ======================================================================
*
* DISPLAY FORM TO ASK FOR USERNAME
*
* ====================================================================== */
if ($actionMode == "askForUserName") {
?>
<form method="post" action="" id="askforusername">
<div id="password_reset">
<label for="username">Enter your user name:</label>
<input name="username" id="username" type="text" value=""/>
<input name="requestusername" type="hidden" value="requestusername"/>
</div>
<div id="controls">
<input type="button" name="cancel" value=""
class="icon cancel"
onmouseover="TagToTip('ttSpanCancel')"
onmouseout="UnTip()"
onclick="document.location.href='login.php'"/>
<input type="button" name="search" value=""
class="icon next"
onmouseover="TagToTip('ttSpanContinue')"
onmouseout="UnTip()"
onclick="document.forms['askforusername'].submit()"/>
</div>
</form>
<?php
} else if ($actionMode == "askForNewPassword") {
?>
<form method="post" action="" id="resetpassword">
<div id="adduser">
<p>You can change the password for user '<?php echo($clean['username']); ?>'.</p>
<label for="pass1">New password:</label>
<input name="pass1" id="pass1" type="password"/>
<br/>
<label for="pass2">(verify) New password: </label>
<input name="pass2" id="pass2" type="password"/>
<input name="modifypassword" type="hidden" value="modifypassword"/>
</div>
<div id="controls">
<input type="button" name="cancel" value=""
class="icon cancel"
onmouseover="TagToTip('ttSpanCancel')"
onmouseout="UnTip()"
onclick="document.location.href='login.php'"/>
<input type="button" name="save" value=""
class="icon save"
onmouseover="TagToTip('ttSpanSave')"
onmouseout="UnTip()"
onclick="document.forms['resetpassword'].submit()"/>
</div>
</form>
<?php
} else if ($actionMode == "emailSentSuccessfully") {
?>
<p><b>Congratulations!</b></p>
<p>Instructions have been sent to the e-mail associated to the user!</p>
<?php
} else if ($actionMode == "requestFromEmailFailed") {
?>
<p><b>Sorry!</b></p>
<p>There is no matching password reset request!<br />
Have you already changed your password?</p>
<?php
} else if ($actionMode == "genericErrorWithMessage") {
?>
<p><b>Sorry!</b></p>
<p>Could not complete the request!<br />
Please contact your administrator.</p>
<?php
} else if ($actionMode == "passwordUpdateSuccessful") {
?>
<p><b>Congratulations!</b></p>
<p>The password was updated successfully!</p>
<?php
} else if ($actionMode == "passwordUpdateFailed") {
?>
<p><b>Sorry!</b></p>
<p>Could not update the password!<br />
Please contact your administrator.</p>
<?php
} else {
?>
<p><b>Oooops!</b></p>
<p>Something went wrong. Please submit a bug report!</p>
<?php
}
?>
</div> <!-- content -->
<div id="rightpanel">
<div id="info">
<h3>Quick help</h3>
<p>Here you can change your password.</p>
<p>Please notice that this is possible only if the
authentication method assigned to you allows the
HRM to change the password.</p>
<p>If it is not possible, you will be informed.
In that case, please contact your institution's
IT or Human Resources department.</p>
</div>
<div id="message">
<?php
echo "<p>$message</p>";
?>
</div>
</div> <!-- rightpanel -->
<?php
include("footer.inc.php");
?>