forked from atutor/ATutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password_reminder.php
180 lines (140 loc) · 5.67 KB
/
password_reminder.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
<?php
/****************************************************************/
/* ATutor */
/****************************************************************/
/* Copyright (c) 2002-2010 */
/* Inclusive Design Institute */
/* http://atutor.ca */
/* */
/* This program is free software. You can redistribute it and/or*/
/* modify it under the terms of the GNU General Public License */
/* as published by the Free Software Foundation. */
/****************************************************************/
// $Id$
$_user_location = 'public';
define('AT_INCLUDE_PATH', 'include/');
require (AT_INCLUDE_PATH.'vitals.inc.php');
if (isset($_POST['cancel'])) {
$msg->addFeedback('CANCELLED');
header('Location: login.php');
exit;
} else if (isset($_POST['form_password_reminder'])) {
//get database info to create & email change-password-link
$_POST['form_email'] = $addslashes($_POST['form_email']);
$sql = "SELECT member_id, login, first_name, password, email FROM %smembers WHERE email='%s'";
$row = queryDB($sql,array(TABLE_PREFIX, $_POST['form_email']), TRUE);
if (isset($row['member_id']) && $row['member_id'] != '') {
//date link was generated (# days since epoch)
$gen = intval(((time()/60)/60)/24);
$hash = sha1($row['member_id'] + $gen + $row['password']);
$hash_bit = substr($hash, 5, 15);
$change_link = $_base_href.'password_reminder.php?id='.$row['member_id'].'&g='.$gen.'&h='.$hash_bit;
if($row['first_name'] != ''){
$reply_name = $row['first_name'];
}else{
$reply_name = $row['login'];
}
$tmp_message = _AT(array('password_request2',$reply_name, $row['login'], AT_PASSWORD_REMINDER_EXPIRY, $change_link));
//send email
require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
$mail = new ATutorMailer;
$mail->From = $_config['contact_email'];
$mail->AddAddress($row['email']);
$mail->Subject = $_config['site_name'] . ': ' . _AT('password_forgot');
$mail->Body = $tmp_message;
if(!$mail->Send()) {
$msg->addError('SENDING_ERROR');
$savant->display('password_reminder_feedback.tmpl.php');
exit;
}
$msg->addFeedback('CONFIRM_EMAIL2');
unset($mail);
header('Location:index.php');
//$savant->display('password_reminder_feedback.tmpl.php');
} else {
$msg->addError('EMAIL_NOT_FOUND');
$savant->display('password_reminder.tmpl.php');
}
} else if (isset($_REQUEST['id']) && isset($_REQUEST['g']) && isset($_REQUEST['h'])) {
//coming from an email link
//check if expired
$current = intval(((time()/60)/60)/24);
$expiry_date = $_REQUEST['g'] + AT_PASSWORD_REMINDER_EXPIRY; //2 days after creation
if ($current > $expiry_date) {
$msg->addError('INVALID_LINK');
$savant->display('password_reminder_feedback.tmpl.php');
exit;
}
/* check if already visited (possibley add a "last login" field to members table)... if password was changed, won't work anyway. do later. */
//check for valid hash
$sql = "SELECT password, email FROM %smembers WHERE member_id=%d";
$row = queryDB($sql, array(TABLE_PREFIX, $_REQUEST['id']), TRUE);
if (isset($row['email']) && $row['email'] != '') {
$email = $row['email'];
$hash = sha1($_REQUEST['id'] + $_REQUEST['g'] + $row['password']);
$hash_bit = substr($hash, 5, 15);
if ($_REQUEST['h'] !== $hash_bit) {
$msg->addError('INVALID_LINK');
} else if (($_REQUEST['h'] == $hash_bit) && !isset($_POST['form_change'])) {
$savant->assign('id', $_REQUEST['id']);
$savant->assign('g', $_REQUEST['g']);
$savant->assign('h', $_REQUEST['h']);
$savant->display('password_change.tmpl.php');
}
} else {
$msg->addError('INVALID_LINK');
$savant->display('password_reminder_feedback.tmpl.php');
exit;
}
//changing the password
if (isset($_POST['form_change'])) {
/* password check: password is verified front end by javascript. here is to handle the errors from javascript */
if ($_POST['password_error'] <> "")
{
$pwd_errors = explode(",", $_POST['password_error']);
foreach ($pwd_errors as $pwd_error)
{
if ($pwd_error == "missing_password")
$missing_fields[] = _AT('password');
else
$msg->addError($pwd_error);
}
}
if (!$msg->containsErrors()) {
//save data
$password = $addslashes($_POST['form_password_hidden']);
$sql = "UPDATE %smembers SET password='%s', last_login=last_login, creation_date=creation_date WHERE member_id=%d";
$result = queryDB($sql,array(TABLE_PREFIX, $password, $_REQUEST['id']));
//reset login attempts
if (isset($result)){
$sql = "SELECT login FROM %smembers WHERE member_id=%d";
$row = queryDB($sql, array(TABLE_PREFIX, $_REQUEST['id']), TRUE);
$sql = "DELETE FROM %smember_login_attempt WHERE login='%s'";
queryDB($sql, array(TABLE_PREFIX, $row['login']));
}
//send confirmation email
require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
$tmp_message = _AT(array('password_change_confirm', $_config['site_name'], $_base_href))."\n\n";
$mail = new ATutorMailer;
$mail->From = $_config['contact_email'];
$mail->AddAddress($email);
$mail->Subject = $_config['site_name'] . ': ' . _AT('password_forgot');
$mail->Body = $tmp_message;
if(!$mail->Send()) {
$msg->printErrors('SENDING_ERROR');
exit;
}
$msg->addFeedback('PASSWORD_CHANGED');
unset($mail);
header('Location:index.php');
} else {
$savant->assign('id', $_REQUEST['id']);
$savant->assign('g', $_REQUEST['g']);
$savant->assign('h', $_REQUEST['h']);
$savant->display('password_change.tmpl.php');
}
}
} else {
$savant->display('password_reminder.tmpl.php');
}
?>