forked from Piwigo/Piwigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
password.php
380 lines (313 loc) · 10 KB
/
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
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
<?php
// +-----------------------------------------------------------------------+
// | This file is part of Piwigo. |
// | |
// | For copyright and license information, please view the COPYING.txt |
// | file that was distributed with this source code. |
// +-----------------------------------------------------------------------+
// +-----------------------------------------------------------------------+
// | initialization |
// +-----------------------------------------------------------------------+
define('PHPWG_ROOT_PATH','./');
include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );
include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
// +-----------------------------------------------------------------------+
check_status(ACCESS_FREE);
trigger_notify('loc_begin_password');
check_input_parameter('action', $_GET, false, '/^(lost|reset|none)$/');
// +-----------------------------------------------------------------------+
// | Functions |
// +-----------------------------------------------------------------------+
/**
* checks the validity of input parameters, fills $page['errors'] and
* $page['infos'] and send an email with confirmation link
*
* @return bool (true if email was sent, false otherwise)
*/
function process_password_request()
{
global $page, $conf;
if (empty($_POST['username_or_email']))
{
$page['errors'][] = l10n('Invalid username or email');
return false;
}
$user_id = get_userid_by_email($_POST['username_or_email']);
if (!is_numeric($user_id))
{
$user_id = get_userid($_POST['username_or_email']);
}
if (!is_numeric($user_id))
{
$page['errors'][] = l10n('Invalid username or email');
return false;
}
$userdata = getuserdata($user_id, false);
// password request is not possible for guest/generic users
$status = $userdata['status'];
if (is_a_guest($status) or is_generic($status))
{
$page['errors'][] = l10n('Password reset is not allowed for this user');
return false;
}
if (empty($userdata['email']))
{
$page['errors'][] = l10n(
'User "%s" has no email address, password reset is not possible',
$userdata['username']
);
return false;
}
$activation_key = generate_key(20);
list($expire) = pwg_db_fetch_row(pwg_query('SELECT ADDDATE(NOW(), INTERVAL 1 HOUR)'));
single_update(
USER_INFOS_TABLE,
array(
'activation_key' => pwg_password_hash($activation_key),
'activation_key_expire' => $expire,
),
array('user_id' => $user_id)
);
$userdata['activation_key'] = $activation_key;
set_make_full_url();
$message = l10n('Someone requested that the password be reset for the following user account:') . "\r\n\r\n";
$message.= l10n(
'Username "%s" on gallery %s',
$userdata['username'],
get_gallery_home_url()
);
$message.= "\r\n\r\n";
$message.= l10n('To reset your password, visit the following address:') . "\r\n";
$message.= get_root_url().'password.php?key='.$activation_key.'-'.urlencode($userdata['email']);
$message.= "\r\n\r\n";
$message.= l10n('If this was a mistake, just ignore this email and nothing will happen.')."\r\n";
unset_make_full_url();
$message = trigger_change('render_lost_password_mail_content', $message);
$email_params = array(
'subject' => '['.$conf['gallery_title'].'] '.l10n('Password Reset'),
'content' => $message,
'email_format' => 'text/plain',
);
if (pwg_mail($userdata['email'], $email_params))
{
$page['infos'][] = l10n('Check your email for the confirmation link');
return true;
}
else
{
$page['errors'][] = l10n('Error sending email');
return false;
}
}
/**
* checks the activation key: does it match the expected pattern? is it
* linked to a user? is this user allowed to reset his password?
*
* @return mixed (user_id if OK, false otherwise)
*/
function check_password_reset_key($reset_key)
{
global $page, $conf;
list($key, $email) = explode('-', $reset_key, 2);
if (!preg_match('/^[a-z0-9]{20}$/i', $key))
{
$page['errors'][] = l10n('Invalid key');
return false;
}
$user_ids = array();
$query = '
SELECT
'.$conf['user_fields']['id'].' AS id
FROM '.USERS_TABLE.'
WHERE '.$conf['user_fields']['email'].' = \''.pwg_db_real_escape_string($email).'\'
;';
$user_ids = query2array($query, null, 'id');
if (count($user_ids) == 0)
{
$page['errors'][] = l10n('Invalid username or email');
return false;
}
$user_id = null;
$query = '
SELECT
user_id,
status,
activation_key,
activation_key_expire,
NOW() AS dbnow
FROM '.USER_INFOS_TABLE.'
WHERE user_id IN ('.implode(',', $user_ids).')
;';
$result = pwg_query($query);
while ($row = pwg_db_fetch_assoc($result))
{
if (pwg_password_verify($key, $row['activation_key']))
{
if (strtotime($row['dbnow']) > strtotime($row['activation_key_expire']))
{
// key has expired
$page['errors'][] = l10n('Invalid key');
return false;
}
if (is_a_guest($row['status']) or is_generic($row['status']))
{
$page['errors'][] = l10n('Password reset is not allowed for this user');
return false;
}
$user_id = $row['user_id'];
}
}
if (empty($user_id))
{
$page['errors'][] = l10n('Invalid key');
return false;
}
return $user_id;
}
/**
* checks the passwords, checks that user is allowed to reset his password,
* update password, fills $page['errors'] and $page['infos'].
*
* @return bool (true if password was reset, false otherwise)
*/
function reset_password()
{
global $page, $conf;
if ($_POST['use_new_pwd'] != $_POST['passwordConf'])
{
$page['errors'][] = l10n('The passwords do not match');
return false;
}
if (!isset($_GET['key']))
{
$page['errors'][] = l10n('Invalid key');
}
$user_id = check_password_reset_key($_GET['key']);
if (!is_numeric($user_id))
{
return false;
}
single_update(
USERS_TABLE,
array($conf['user_fields']['password'] => $conf['password_hash']($_POST['use_new_pwd'])),
array($conf['user_fields']['id'] => $user_id)
);
single_update(
USER_INFOS_TABLE,
array(
'activation_key' => null,
'activation_key_expire' => null,
),
array('user_id' => $user_id)
);
deactivate_user_auth_keys($user_id);
$page['infos'][] = l10n('Your password has been reset');
$page['infos'][] = '<a href="'.get_root_url().'identification.php">'.l10n('Login').'</a>';
return true;
}
// +-----------------------------------------------------------------------+
// | Process form |
// +-----------------------------------------------------------------------+
if (isset($_POST['submit']))
{
check_pwg_token();
if ('lost' == $_GET['action'])
{
if (process_password_request())
{
$page['action'] = 'none';
}
}
if ('reset' == $_GET['action'])
{
if (reset_password())
{
$page['action'] = 'none';
}
}
}
// +-----------------------------------------------------------------------+
// | key and action |
// +-----------------------------------------------------------------------+
// a connected user can't reset the password from a mail
if (isset($_GET['key']) and !is_a_guest())
{
unset($_GET['key']);
}
if (isset($_GET['key']) and !isset($_POST['submit']))
{
$user_id = check_password_reset_key($_GET['key']);
if (is_numeric($user_id))
{
$userdata = getuserdata($user_id, false);
$page['username'] = $userdata['username'];
$template->assign('key', $_GET['key']);
if (!isset($page['action']))
{
$page['action'] = 'reset';
}
}
else
{
$page['action'] = 'none';
}
}
if (!isset($page['action']))
{
if (!isset($_GET['action']))
{
$page['action'] = 'lost';
}
elseif (in_array($_GET['action'], array('lost', 'reset', 'none')))
{
$page['action'] = $_GET['action'];
}
}
if ('reset' == $page['action'] and !isset($_GET['key']) and (is_a_guest() or is_generic()))
{
redirect(get_gallery_home_url());
}
if ('lost' == $page['action'] and !is_a_guest())
{
redirect(get_gallery_home_url());
}
// +-----------------------------------------------------------------------+
// | template initialization |
// +-----------------------------------------------------------------------+
$title = l10n('Password Reset');
if ('lost' == $page['action'])
{
$title = l10n('Forgot your password?');
if (isset($_POST['username_or_email']))
{
$template->assign('username_or_email', htmlspecialchars(stripslashes($_POST['username_or_email'])));
}
}
$page['body_id'] = 'thePasswordPage';
$template->set_filenames(array('password'=>'password.tpl'));
$template->assign(
array(
'title' => $title,
'form_action'=> get_root_url().'password.php',
'action' => $page['action'],
'username' => isset($page['username']) ? $page['username'] : $user['username'],
'PWG_TOKEN' => get_pwg_token(),
)
);
// include menubar
$themeconf = $template->get_template_vars('themeconf');
if (!isset($themeconf['hide_menu_on']) OR !in_array('thePasswordPage', $themeconf['hide_menu_on']))
{
include( PHPWG_ROOT_PATH.'include/menubar.inc.php');
}
// +-----------------------------------------------------------------------+
// | html code display |
// +-----------------------------------------------------------------------+
include(PHPWG_ROOT_PATH.'include/page_header.php');
trigger_notify('loc_end_password');
flush_page_messages();
$template->pparse('password');
include(PHPWG_ROOT_PATH.'include/page_tail.php');
?>