-
Notifications
You must be signed in to change notification settings - Fork 2
/
forgotpass.php
248 lines (217 loc) · 8.61 KB
/
forgotpass.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
<?php
/**
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('PUBLIC', 1);
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'site');
define('SECTION_PAGE', 'forgotpass');
require('init.php');
require_once('pieforms/pieform.php');
if (!session_id()) {
session_start();
}
if (!empty($_SESSION['pwchangerequested'])) {
unset($_SESSION['pwchangerequested']);
die_info(get_string('pwchangerequestsent'));
}
if (isset($_GET['key'])) {
$_SESSION['forgotpasskey'] = $_GET['key'];
redirect('/forgotpass.php');
}
if (isset($_SESSION['forgotpasskey'])) {
define('TITLE', get_string('changepassword'));
if (!$pwrequest = get_record('usr_password_request', 'key', $_SESSION['forgotpasskey'])) {
unset($_SESSION['forgotpasskey']);
die_info(get_string('nosuchpasswordrequest'));
}
if (strtotime($pwrequest->expiry) < time()) {
unset($_SESSION['forgotpasskey']);
die_info(get_string('passwordresetexpired'));
}
$form = array(
'name' => 'forgotpasschange',
'method' => 'post',
'action' => '',
'autofocus' => true,
'elements' => array(
'password1' => array(
'type' => 'password',
'title' => get_string('password'),
'description' => get_string('yournewpassword'),
'rules' => array(
'required' => true
)
),
'password2' => array(
'type' => 'password',
'title' => get_string('confirmpassword'),
'rules' => array(
'required' => true
)
),
'user' => array(
'type' => 'hidden',
'value' => $pwrequest->usr
),
'submit' => array(
'type' => 'submit',
'value' => get_string('change')
)
)
);
$smarty = smarty();
$smarty->assign('forgotpasschange_form', pieform($form));
$smarty->assign('heading', get_string('changepassword'));
$smarty->display('forgotpass.tpl');
exit;
}
else {
define('TITLE', get_string('forgotusernamepassword'));
}
$form = array(
'name' => 'forgotpass',
'method' => 'post',
'action' => '',
'autofocus' => true,
'elements' => array(
'emailusername' => array(
'type' => 'text',
'title' => get_string('emailaddressorusername'),
'description' => get_string('emailaddressdescription'),
'rules' => array(
'required' => true,
)
),
'submit' => array(
'type' => 'submit',
'value' => get_string('sendrequest')
)
)
);
function forgotpass_validate(Pieform $form, $values) {
// See if the user input an email address or a username. We favour email addresses
if (!$form->get_error('emailusername')) {
// Check if the user who associates to username or email address is using the external authentication
if (record_exists_sql('SELECT u.authinstance
FROM {usr} u INNER JOIN {auth_instance} ai ON (u.authinstance = ai.id)
WHERE (LOWER(u.email) = ? OR LOWER(u.username) = ?)
AND ((ai.authname != \'internal\') AND (ai.authname != \'none\'))', array_fill(0, 2, strtolower($values['emailusername'])))) {
$form->set_error('emailusername', get_string('forgotpassuserusingexternalauthentication', 'mahara', get_config('wwwroot') . 'contact.php'));
}
else {
if (!($authinstance = get_field_sql('SELECT u.authinstance
FROM {usr} u INNER JOIN {auth_instance} ai ON (u.authinstance = ai.id)
WHERE (LOWER(u.email) = ? OR LOWER(u.username) = ?)
AND ai.authname = \'internal\'', array_fill(0, 2, strtolower($values['emailusername']))))) {
$form->set_error('emailusername', get_string('forgotpassnosuchemailaddressorusername'));
}
}
}
if ($form->get_error('emailusername')) {
return;
}
$authobj = AuthFactory::create($authinstance);
if (!method_exists($authobj, 'change_password')) {
die_info(get_string('cantchangepassword'));
}
}
function forgotpass_submit(Pieform $form, $values) {
global $SESSION;
try {
if (!($user = get_record_sql('SELECT u.* FROM {usr} u
INNER JOIN {auth_instance} ai ON (u.authinstance = ai.id)
WHERE (LOWER(u.email) = ? OR LOWER(u.username) = ?)
AND ai.authname = \'internal\'', array_fill(0, 2, strtolower($values['emailusername']))))) {
die_info(get_string('forgotpassnosuchemailaddressorusername'));
}
$pwrequest = new StdClass;
$pwrequest->usr = $user->id;
$pwrequest->expiry = db_format_timestamp(time() + 86400);
$pwrequest->key = get_random_key();
$sitename = get_config('sitename');
$fullname = display_name($user);
// Override the disabled status of this e-mail address
$user->ignoredisabled = true;
email_user($user, null,
get_string('forgotusernamepasswordemailsubject', 'mahara', $sitename),
get_string('forgotusernamepasswordemailmessagetext', 'mahara',
$fullname,
$sitename,
$user->username,
get_config('wwwroot') . 'forgotpass.php?key=' . $pwrequest->key,
get_config('wwwroot') . 'contact.php',
$sitename),
get_string('forgotusernamepasswordemailmessagehtml', 'mahara',
$fullname,
$sitename,
$user->username,
get_config('wwwroot') . 'forgotpass.php?key=' . $pwrequest->key,
get_config('wwwroot') . 'forgotpass.php?key=' . $pwrequest->key,
get_config('wwwroot') . 'contact.php',
$sitename));
insert_record('usr_password_request', $pwrequest);
}
catch (SQLException $e) {
die_info(get_string('forgotpassemailsendunsuccessful'));
}
catch (EmailException $e) {
die_info(get_string('forgotpassemailsendunsuccessful'));
}
// Add a note if this e-mail address is over the bounce threshold to
// warn users that they may not receive the e-mail
if ($mailinfo = get_record_select('artefact_internal_profile_email', '"owner" = ? AND principal = 1', array($user->id))) {
if (check_overcount($mailinfo)) {
$SESSION->add_info_msg(get_string('forgotpassemailsentanyway1', 'mahara', get_config('sitename')));
}
}
// Unsetting disabled status overriding
unset($user->ignoredisabled);
// Add a marker in the session to say that the user has registered
$_SESSION['pwchangerequested'] = true;
redirect('/forgotpass.php');
}
function forgotpasschange_validate(Pieform $form, $values) {
$user = new User();
$user->find_by_id($values['user']);
password_validate($form, $values, $user);
}
// TODO:
// password_validate to maharalib, use it in places specified, test with a drop/create run
// support autofocus => (true|'id'), remove stuff doing autofocus from where it is, focus error fields
// commit stuff
function forgotpasschange_submit(Pieform $form, $values) {
global $SESSION, $USER;
unset($_SESSION['forgotpasskey']);
try {
$user = new User();
$user->find_by_id($values['user']);
} catch (AuthUnknownUserException $e) {
throw new UserException('Request to change the password for a user who does not exist');
}
$authobj = AuthFactory::create($user->authinstance);
if ($password = $authobj->change_password($user, $values['password1'])) {
// Remove the password request(s) for the user
delete_records('usr_password_request', 'usr', $values['user']);
ensure_user_account_is_active($user);
$USER->reanimate($user->id, $user->authinstance);
// Destroy other sessions of the user
remove_user_sessions($USER->get('id'));
$SESSION->add_ok_msg(get_string('passwordchangedok'));
redirect();
exit;
}
throw new SystemException('User "' . $user->username
. ' tried to change their password, but the attempt failed');
}
$smarty = smarty();
$smarty->assign('forgotpass_form', pieform($form));
$smarty->assign('heading', get_string('forgotusernamepassword'));
$smarty->display('forgotpass.tpl');