-
Notifications
You must be signed in to change notification settings - Fork 349
/
PasswordReset.php
485 lines (389 loc) · 11.7 KB
/
PasswordReset.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php
/**
* Password Reset
*
* @since 2.9
*
* @package RosarioSIS
*/
require_once 'Warehouse.php';
/**
* Send email with password reset link.
* Activate debug mode for error messages.
*/
if ( isset( $_POST['email'] )
&& ! empty( $_REQUEST['email'] ) )
{
if ( ! filter_var( $_REQUEST['email'], FILTER_VALIDATE_EMAIL ) )
{
if ( ROSARIO_DEBUG )
{
$error[] = 'Invalid email.';
}
}
else
{
// SQL Handle case when multiple users have same email: order by Failed Login.
$user_RET = DBGet( "SELECT STAFF_ID AS ID,EMAIL,USERNAME,'staff' AS USER_TYPE
FROM staff
WHERE LOWER(EMAIL)=LOWER('" . $_REQUEST['email'] . "')
AND SYEAR='" . Config( 'SYEAR' ) . "'
ORDER BY FAILED_LOGIN IS NULL,FAILED_LOGIN DESC
LIMIT 1" );
if ( ! $user_RET
&& Config( 'STUDENTS_EMAIL_FIELD' ) )
{
$email_field = Config( 'STUDENTS_EMAIL_FIELD' ) === 'USERNAME' ?
'USERNAME' : 'CUSTOM_' . (int) Config( 'STUDENTS_EMAIL_FIELD' );
// SQL Handle case when multiple users have same email: order by Failed Login.
$user_RET = DBGet( "SELECT s.STUDENT_ID AS ID,s.USERNAME,'student' AS USER_TYPE,
s." . DBEscapeIdentifier( $email_field ) . " AS EMAIL
FROM students s,student_enrollment ssm
WHERE LOWER(s." . DBEscapeIdentifier( $email_field ) . ")=LOWER('" . $_REQUEST['email'] . "')
AND s.STUDENT_ID=ssm.STUDENT_ID
AND ssm.SYEAR='" . Config( 'SYEAR' ) . "'
AND ('" . DBDate() . "'>=ssm.START_DATE
AND (ssm.END_DATE IS NULL
OR '" . DBDate() . "'<=ssm.END_DATE ) )
ORDER BY s.FAILED_LOGIN IS NULL,s.FAILED_LOGIN DESC
LIMIT 1" );
}
if ( ! $user_RET )
{
if ( ROSARIO_DEBUG )
{
$error[] = 'No account with this email were found.';
}
}
elseif ( ! $user_RET[1]['USERNAME'] )
{
if ( ROSARIO_DEBUG )
{
$error[] = 'No username with this account were found.';
}
}
else
{
$email_sent = _sendPasswordResetEmail(
$user_RET[1]['ID'],
$user_RET[1]['USER_TYPE'],
$user_RET[1]['EMAIL']
);
if ( ! $email_sent )
{
if ( ROSARIO_DEBUG )
{
$error[] = 'Password reset email could not be sent.';
}
}
elseif ( ROSARIO_DEBUG )
{
$note[] = 'Success!';
}
}
}
if ( ! ROSARIO_DEBUG )
{
// Redirect to login page.
header( 'Location: index.php?modfunc=logout&reason=password_reset&token=' . $_SESSION['token'] );
exit;
}
}
// Password reset form.
if ( ! empty( $_REQUEST['h'] )
&& ( mb_strlen( $_REQUEST['h'] ) == 106
|| mb_strlen( $_REQUEST['h'] ) == 105 )
&& mb_substr( $_REQUEST['h'], 0, 3 ) == '$6$' )
{
// Select Staff where last login > now.
$staff_RET = DBGet( "SELECT STAFF_ID AS ID, USERNAME, PASSWORD, EMAIL,
" . DisplayNameSQL() . " AS FULL_NAME, LAST_LOGIN, PROFILE_ID
FROM staff
WHERE LAST_LOGIN > CURRENT_TIMESTAMP
AND SYEAR='" . Config( 'SYEAR' ) . "'" );
$student_RET = [];
if ( Config( 'STUDENTS_EMAIL_FIELD' ) )
{
$email_field = Config( 'STUDENTS_EMAIL_FIELD' ) === 'USERNAME' ?
'USERNAME' : 'CUSTOM_' . (int) Config( 'STUDENTS_EMAIL_FIELD' );
// Select Students where last login > now & enrolled.
$student_RET = DBGet( "SELECT s.STUDENT_ID AS ID, s.USERNAME, s.PASSWORD,
s." . DBEscapeIdentifier( $email_field ) . " AS EMAIL,
" . DisplayNameSQL( 's' ) . " AS FULL_NAME, s.LAST_LOGIN
FROM students s, student_enrollment se
WHERE s.LAST_LOGIN > CURRENT_TIMESTAMP
AND se.SYEAR='" . Config( 'SYEAR' ) . "'
AND se.STUDENT_ID=s.STUDENT_ID
AND ('" . DBDate() . "'>=se.START_DATE
AND ('" . DBDate() . "'<=se.END_DATE
OR se.END_DATE IS NULL ) )" );
}
if ( ! $staff_RET
&& ! $student_RET )
{
$error[] = _( 'Please enter your email again.' );
}
else
{
$hash_matched = false;
foreach ( (array) $staff_RET as $staff )
{
// Generate plain hash from user ID, username, name, password, email & last login.
$plain_hash = $staff['ID'] . $staff['USERNAME'] . $staff['FULL_NAME'] .
$staff['PASSWORD'] . $staff['EMAIL'] . $staff['LAST_LOGIN'];
if ( match_password( $_REQUEST['h'], $plain_hash ) )
{
$hash_matched = true;
$user_id = $staff['ID'];
$user_type = 'staff';
$user_profile = $staff['PROFILE_ID'];
// @since 11.1 Prevent using App name, username, or email in the password
$_ROSARIO['PasswordInput']['user_inputs'] = [
$staff['USERNAME'],
$staff['EMAIL'],
];
break;
}
}
foreach ( (array) $student_RET as $student )
{
// Generate plain hash from user ID, username, name, password, email & last login.
$plain_hash = $student['ID'] . $student['USERNAME'] . $student['FULL_NAME'] .
$student['PASSWORD'] . $student['EMAIL'] . $student['LAST_LOGIN'];
if ( match_password( $_REQUEST['h'], $plain_hash ) )
{
$hash_matched = true;
$user_id = $student['ID'];
$user_type = 'student';
// @since 11.1 Prevent using App name, username, or email in the password
$_ROSARIO['PasswordInput']['user_inputs'] = [
$student['USERNAME'],
$student['EMAIL'],
];
break;
}
}
if ( $hash_matched )
{
// Verify new password if any.
if ( isset( $_POST['PASSWORD'] )
&& $_REQUEST['PASSWORD'] !== '' )
{
$new_password = $_POST['PASSWORD'];
if ( $user_type === 'staff' )
{
/**
* SQL update User password for all school years
*
* @since 12.0
*/
// Update password.
DBQuery( "UPDATE staff SET PASSWORD='" .
encrypt_password( $new_password ) . "'
WHERE USERNAME='" . DBEscapeString( $staff['USERNAME'] ) . "'" );
// If admin, send notification email to server admin.
if ( $user_profile == 1 )
{
_notifyServerAdminPasswordReset( $user_id );
}
}
elseif ( $user_type === 'student' )
{
// Update password.
DBQuery( "UPDATE students SET PASSWORD='" .
encrypt_password( $new_password ) . "'
WHERE STUDENT_ID='" . (int) $user_id . "'" );
}
unset(
$_POST['PASSWORD'],
$_REQUEST['PASSWORD']
);
// Redirect to login page.
header( 'Location: index.php' );
exit;
}
_passwordResetForm( $_REQUEST['h'], $user_id );
exit;
}
else
{
$error[] = _( 'Please enter your email again.' );
}
}
}
// If Student email field config option not set,
// notify that no student can use the password reset.
if ( ! Config( 'STUDENTS_EMAIL_FIELD' ) )
{
$note[] = _( 'Password reset is not activated for students.' );
}
// Forgot your password? form.
_printPageHead( _( 'Forgot your password?' ) );
?>
<form action="PasswordReset.php" method="POST" target="_top">
<?php PopTable( 'header', _( 'Forgot your password?' ) ); ?>
<label><input type="email" name="email" id="email" class="width-100p" maxlength="255" tabindex="1" required autofocus />
<?php echo _( 'Email' ); ?></label>
<br />
<br />
<?php echo Buttons( _( 'Send password reset instructions' ) ); ?>
<?php PopTable( 'footer' ); ?>
</form>
<?php
Warehouse( 'footer' );
/**
* Send Password Reset email
*
* @param string $user_id User ID (Staff or Student).
* @param string $user_type 'staff'|'student'.
* @param string $email Email address.
*
* @return boolean true if email sent, else false
*/
function _sendPasswordResetEmail( $user_id, $user_type, $email )
{
global $DatabaseType;
if ( ! $user_id )
{
return false;
}
if ( $user_type === 'staff' )
{
// Get Staff email, password.
$staff_RET = DBGet( "SELECT USERNAME, PASSWORD,
" . DisplayNameSQL() . " AS FULL_NAME
FROM staff
WHERE STAFF_ID='" . (int) $user_id . "'
AND SYEAR='" . Config( 'SYEAR' ) . "'" );
$username = $staff_RET[1]['USERNAME'];
$name = $staff_RET[1]['FULL_NAME'];
$password = $staff_RET[1]['PASSWORD']; // Can be NULL!
}
elseif ( $user_type === 'student' )
{
// Get Student username, password, name.
$student_RET = DBGet( "SELECT USERNAME,PASSWORD,
" . DisplayNameSQL( 's' ) . " AS FULL_NAME
FROM students s,student_enrollment ssm
WHERE s.STUDENT_ID='" . (int) $user_id . "'
AND s.STUDENT_ID=ssm.STUDENT_ID
AND ssm.SYEAR='" . Config( 'SYEAR' ) . "'
AND ('" . DBDate() . "'>=ssm.START_DATE
AND (ssm.END_DATE IS NULL
OR '" . DBDate() . "'<=ssm.END_DATE ) )" );
$username = $student_RET[1]['USERNAME'];
$name = $student_RET[1]['FULL_NAME'];
$password = $student_RET[1]['PASSWORD']; // Can be NULL!
}
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL )
|| ! $username )
{
return false;
}
// Last login = now + 2 hours.
$last_login = DBGetOne( "SELECT
CAST(CURRENT_TIMESTAMP + INTERVAL " . ( $DatabaseType === 'mysql' ? '2 hour' : "'2 hour'" ) . " AS char(19)) AS LAST_LOGIN" );
// Generate hash from user ID, username, name, password, email & last login.
$hash = encrypt_password( $user_id . $username . $name . $password . $email . $last_login );
// Generate link.
$link = RosarioURL( 'script' ) . '?h=' . $hash;
// Send email.
require_once 'ProgramFunctions/SendEmail.fnc.php';
$message = _( 'Please visit the following link to reset your password' ) . ':<br />
<a href="' . URLEscape( $link ) . '">' . $link . '</a>
<br />' . _( 'Username' ) . ': ' . $username . '
<br /><br />' .
_( 'Please permanently delete this email once you are done.' );
$email_sent = SendEmail( $email, _( 'Password Reset' ), $message );
if ( ! $email_sent )
{
return false;
}
if ( $user_type === 'staff' )
{
// Update Last login = now + 2 hours.
DBQuery( "UPDATE staff
SET LAST_LOGIN='" . $last_login . "'
WHERE STAFF_ID='" . (int) $user_id . "'" ); // CURRENT_TIMESTAMP + interval '2 hour'.
}
elseif ( $user_type === 'student' )
{
// Update Last login = now + 2 hours.
DBQuery( "UPDATE students
SET LAST_LOGIN='" . $last_login . "'
WHERE STUDENT_ID='" . (int) $user_id . "'" ); // CURRENT_TIMESTAMP + interval '2 hour'.
}
return true;
}
function _passwordResetForm( $hash, $user_id )
{
global $_ROSARIO;
if ( ! $hash
|| ! $user_id )
{
return;
}
_printPageHead( _( 'Reset your password' ) );
?>
<form action="PasswordReset.php" method="POST" target="_top">
<input type="hidden" name="h" value="<?php echo AttrEscape( $hash ); ?>" />
<?php PopTable( 'header', _( 'Reset your password' ) ); ?>
<?php
$_ROSARIO['allow_edit'] = true;
echo PasswordInput(
'',
'PASSWORD',
_( 'New Password' ),
'strength maxlength="42" tabindex="1" required'
);
$_ROSARIO['allow_edit'] = false;
?>
<br />
<div class="center"><?php echo Buttons( _( 'Submit' ) ); ?></div>
<?php PopTable( 'footer' ); ?>
</form>
<?php
Warehouse( 'footer' );
}
function _printPageHead( $title )
{
global $locale,
$error,
$note,
$_ROSARIO;
$_ROSARIO['page'] = 'password-reset';
Warehouse( 'header' );
$_ROSARIO['HeaderIcon'] = 'misc';
DrawHeader( _( 'Password help' ) );
echo ErrorMessage( $error );
echo ErrorMessage( $note, 'note' );
}
function _notifyServerAdminPasswordReset( $user_id )
{
global $RosarioNotifyAddress;
// Notify the network admin that a new admin has been created.
if ( ! filter_var( $RosarioNotifyAddress, FILTER_VALIDATE_EMAIL )
|| ! $user_id )
{
return false;
}
$staff_RET = DBGet( "SELECT USERNAME," . DisplayNameSQL() . " AS FULL_NAME,PROFILE
FROM staff
WHERE STAFF_ID='" . (int) $user_id . "'
AND SYEAR='" . Config( 'SYEAR' ) . "'" );
// FJ add SendEmail function.
require_once 'ProgramFunctions/SendEmail.fnc.php';
$to = $RosarioNotifyAddress;
$name = $staff_RET[1]['FULL_NAME'];
$subject = sprintf( 'Password Reset: %s', $name );
$profile = $staff_RET[1]['PROFILE'];
$username = $staff_RET[1]['USERNAME'];
$ip = ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] )
// Filter IP, HTTP_* headers can be forged.
&& filter_var( $_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP ) ?
$_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] );
$message = sprintf( 'Password Reset for: %s
Profile: %s
Remote IP: %s', $username, $profile, $ip );
return SendEmail( $to, $subject, $message );
}