forked from AlfnRU/roundcube-password_recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_recovery.php
533 lines (456 loc) · 20.9 KB
/
password_recovery.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php
/**
* Password recovery
*
* Plugin to reset an account password
*
* @version 1.0
* @original_author Alexander Alferov
*
* @url https://github.com/AlfnRU/roundcube-password_recovery
*/
class password_recovery extends rcube_plugin {
public $task = 'login|logout|settings|mail';
public $rc;
public $db;
public $user;
public $use_password;
public $pwd_conf;
private $pwd;
private $fields;
private $send;
function init() {
$this->rc = rcmail::get_instance();
$this->load_config();
if (!$this->rc->config->get('pr_use_confirm_code') && !$this->rc->config->get('pr_use_question'))
return;
$this->init_ui();
$this->add_texts('localization/');
if ($this->rc->task == 'login' || $this->rc->task == 'logout') {
$this->add_hook('render_page', [$this, 'add_labels_to_login_page']);
$this->add_hook('startup', [$this, 'startup']);
$this->register_action('plugin.get_confirm_code_count', [$this, 'get_confirm_code_count']);
} else if ($this->rc->task == 'mail') {
$this->add_hook('render_page', [$this, 'add_labels_to_mail_page']);
$this->add_hook('messages_list', [$this, 'check_identities']);
} else if ($this->rc->task == 'settings') {
$this->add_hook('identity_form', [$this, 'identity_form']);
$this->add_hook('identity_update', [$this, 'identity_update']);
}
$this->include_script('password_recovery.js');
}
/*******************
* STARTUP
*******************/
function init_ui() {
if (!$this->fields) $this->fields = $this->rc->config->get('pr_fields');
if (!$this->db) $this->get_dbh();
if (!$this->user) $this->get_user_props();
$this->use_password = ($this->rc->config->get('pr_use_password_plugin') && $this->rc->plugins->load_plugin('password', true));
$new_fields = [
'token' => ['type' => 'VARCHAR(255)', 'default' => '\'\''],
'token_validity' => ['type' => 'DATETIME' , 'default' => '\'2000-01-01 00:00:00\'']
];
foreach($this->fields as $field => $field_name){
$new_fields[$field_name] = ['type' => ($field == 'phone' ? 'VARCHAR(30)' : 'VARCHAR(255)'), 'default' => '\'\''];
}
foreach($new_fields as $field_name => $field_props){
$query = "SELECT " . $field_name . " FROM " . $this->rc->config->get('pr_users_table');
$result = $this->db->query($query);
if (!$result) {
$query = "ALTER TABLE " . $this->rc->config->get('pr_users_table') . " ADD " . $field_name . " " . $field_props['type'] . " DEFAULT " . $field_props['default'];
$result = $this->db->query($query);
}
}
require_once $this->home . '/lib/send.php';
$this->send = new password_recovery_send($this);
require_once $this->home . '/lib/password.php';
$this->pwd = new password_recovery_pwd($this);
}
function startup($p) {
if ($this->rc->action != 'plugin.password_recovery' || !isset($_SESSION['temp']))
return $p;
switch ($this->get_action()) {
case 'init':
$this->recovery_password_form();
break;
case 'renew':
$this->renew_confirm_code();
break;
case 'new':
$this->new_password_form();
break;
case 'reset':
$this->reset_password();
break;
case 'save':
$this->save_password();
break;
case 'cancel':
$this->rc->kill_session();
$this->rc->output->command('redirect', './');
break;
}
return $p;
}
function add_labels_to_login_page($p) {
if ($p['template'] == 'login') {
$this->rc->output->add_label('password_recovery.forgot_password');
}
return $p;
}
function add_labels_to_mail_page($p) {
$this->rc->output->add_label('password_recovery.no_identities');
$this->rc->output->add_script('rcmail.message_time = 10000;');
return $p;
}
/*******************
* PASSWORD
*******************/
// Creating form for reset password
private function recovery_password_form() {
$this->rc->output->add_label(
'password_recovery.recovery_password',
'password_recovery.no_username'
);
$this->rc->output->set_pagetitle($this->gettext('recovery_password'));
$this->rc->output->add_gui_object('recoverypasswordform', 'recovery-password-form');
$this->rc->output->send('password_recovery.recovery_password_form');
}
// Creating form for new password
private function new_password_form() {
$this->rc->output->add_label(
'password_recovery.newpassword',
'password_recovery.newpassword_confirm',
'password_recovery.question',
'password_recovery.answer',
'password_recovery.code',
'password_recovery.recovery_password',
'password_recovery.renew_code',
'password_recovery.count_send_code_maximum',
'password_recovery.no_code',
'password_recovery.no_answer',
'password_recovery.no_password',
'password_recovery.no_password_confirm',
'password_recovery.password_inconsistency',
'password_recovery.password_too_short'
);
$this->rc->output->set_pagetitle($this->gettext('recovery_password'));
$this->rc->output->add_gui_object('newpasswordform', 'new-password-form');
$password_minimum_length = ($this->use_password ? $this->rc->config->get('password_minimum_length',8) : $this->rc->config->get('pr_password_minimum_length',8));
$this->rc->output->set_env('pr_username', $this->user['username']);
$this->rc->output->set_env('pr_question', $this->user['question']);
$this->rc->output->set_env('pr_use_question', (bool) ($this->rc->config->get('pr_use_question') && $this->user['have_answer']));
$this->rc->output->set_env('pr_use_confirm_code', (bool) ($this->rc->config->get('pr_use_confirm_code') && $this->user['have_code']));
$this->rc->output->set_env('pr_password_minimum_length', (int) $password_minimum_length);
$this->rc->output->send('password_recovery.new_password_form');
}
// Renew and send confirmation code to user (to alternative email and phone)
private function renew_confirm_code() {
if ($this->get_confirm_code_count() < $this->rc->config->get('pr_confirm_code_count_max')) {
$result = $this->send->send_confirm_code_to_user();
if ($result['send']) {
$this->update_confirm_code_count(1);
$message = $result['message'];
$type = 'confirmation';
} else {
$message = $this->gettext('send_failed') . "\n" . $result['message'];
$type = 'error';
}
} else {
$message = $this->gettext('count_send_code_maximum');
$type = 'error';
}
$this->rc->output->command('display_message', $message, $type);
$this->rc->output->send('plugin');
}
// Creating and send confirmation code to user (to alternative email and phone) or send message to administrator
private function reset_password() {
// kill remember_me cookies
setcookie ('rememberme_user', '', time()-3600);
setcookie ('rememberme_pass', '', time()-3600);
$allow_answer = ($this->rc->config->get('pr_use_question') && $this->user['have_answer']);
$allow_code = ($this->rc->config->get('pr_use_confirm_code') && $this->user['have_code']);
if (!$this->user['username']) {
$message = $this->gettext('user_not_found');
$type = 'error';
} else if (!$allow_answer && !$allow_code) {
$this->send->send_alert_to_admin($this->user['username']);
$message = $this->gettext('sent_to_admin');
$type = 'error';
} else if ($allow_code) {
$result['send'] = false;
if ($this->user['token_validity'] && !$this->user['token_expired']) {
$this->update_confirm_code_count(1);
$result['send'] = true;
$message = $this->gettext('check_account_notice');
$type = 'notice';
} else {
$result = $this->send->send_confirm_code_to_user();
if ($result['send']) {
$this->update_confirm_code_count(1);
$message = $result['message'];
$type = 'confirmation';
} else {
$message = $this->gettext('send_failed') . "\n" . $result['message'];
$type = 'error';
}
}
}
$this->logging("Password recovery request for '" . $this->user['username'] . "' (IP: " . rcube_utils::remote_addr() . ")");
if ($message) {
$this->rc->output->command('display_message', $message, $type);
}
if ($type == 'error') {
$this->recovery_password_form();
} else {
$this->new_password_form();
}
}
// Save new password to DB
private function save_password() {
$params = rcube_utils::request2param(rcube_utils::INPUT_POST);
$this->debug("Save new password: " . print_r($params, true));
if ($this->rc->config->get('pr_use_question') && $this->user['have_answer'] && $this->user['answer'] != $params['answer']) {
$message = $this->gettext('answer_failed');
$type = 'error';
} else if ($this->rc->config->get('pr_use_confirm_code') && $this->user['token_expired']) {
$message = $this->gettext('code_expired');
$type = 'error';
} else if ($this->rc->config->get('pr_use_confirm_code') && $this->user['token'] != $params['code']) {
$message = $this->gettext('code_failed');
$type = 'error';
} else {
// props to save
$save = ['token'=>'', 'token_validity'=>''];
// check allowed characters according to the configured 'password_charset' option
// by converting the password entered by the user to this charset and back to UTF-8
$rc_charset = strtoupper($this->rc->output->get_charset());
$orig_pwd = $params['newpassword'];
$chk_pwd = rcube_charset::convert($orig_pwd, $rc_charset, 'UTF-8');
$chk_pwd = rcube_charset::convert($chk_pwd, 'UTF-8', $rc_charset);
// We're doing this for consistence with Roundcube core
$newpassword = rcube_charset::convert($params['newpassword'], $rc_charset, 'UTF-8');
if ($chk_pwd != $orig_pwd || preg_match('/[\x00-\x1F\x7F]/', $newpassword)) {
$message = $this->gettext('password_forbidden');
$type = 'error';
} else if (!$this->use_password && ($chk_strength = $this->pwd->_check_strength($newpassword))) {
$message = $chk_strength;
$type = 'error';
} else {
if ($this->use_password) {
$result = $this->pwd->_save($newpassword, $this->user['username']);
if ($result != 0) {
$message = $this->gettext('write_failed') . ": " . $result;
$type = 'error';
$this->debug($message);
}
} else {
$save['password'] = crypt($newpassword, '$1$' . rcube_utils::random_bytes(9));
//$save['password'] = crypt($newpassword, '$6$' . rcube_utils::random_bytes(16));
}
if ($type != 'error' && $this->set_user_props($save)) {
$this->logging("Save new password for '" . $this->user['username'] . "' (IP: " . rcube_utils::remote_addr() . ")");
$message = $this->gettext('password_changed');
$type = 'confirmation';
} else if (!$this->use_password) {
$message = $this->gettext('password_not_changed');
$type = 'error';
}
}
}
$this->rc->output->command('display_message', $message, $type);
if ($type != 'error') {
$this->rc->kill_session();
$this->rc->output->command('redirect', './', 2);
// $this->rc->output->send('login');
}
}
/*******************
* IDENTITIES
*******************/
// Verifying the user identities needed to recover the password
function check_identities() {
if (!isset($_SESSION['show_notice_identities']) && !$this->user['have_altemail'] && !$this->user['have_phone']) {
$link = "<a href='./?_task=settings&_action=identities'>". $this->gettext('click_here') ."</a>";
$this->rc->output->command('display_message', sprintf($this->gettext('no_identities'), $link), 'notice');
$_SESSION['show_notice_identities'] = true;
}
}
// Handler for 'identity_form' hook (executed on identities form create)
function identity_form($p) {
if (isset($p['form']['addressing']) && !empty($p['record']['identity_id'])) {
$new_fields = [];
foreach ($p['form']['addressing']['content'] as $col => $colprop) {
$new_fields[$col] = $colprop;
if ($col == 'email') {
// add ext fields after 'email'
foreach ($this->fields as $field => $field_name){
$new_fields[$field] = ['type' => 'text', 'size' => 40, 'label' => $this->gettext($field)];
}
}
}
if (!$this->rc->config->get('pr_use_question')) {
unset($new_fields['question']);
unset($new_fields['answer']);
}
$p['form']['addressing']['content'] = $new_fields;
if($this->user['username']){
foreach ($this->fields as $field => $field_name){
$p['record'][$field] = $this->user[$field];
}
}
}
return $p;
}
// Handler for identity_update hook (executed on identities form submit)
function identity_update($p) {
$save = [];
foreach ($this->fields as $field => $field_name) {
$save[$field] = rcube_utils::get_input_value("_".$field,rcube_utils::INPUT_POST);
}
foreach ($save as $par => $val) {
if ((!$this->user['username'] && empty($val)) || ($this->user['username'] && $val == $this->user[$par])) {
unset($save[$par]);
}
}
if ($save['altemail']) {
$save['altemail'] = rcube_utils::idn_to_ascii($save['altemail']);
if (empty($save['altemail'])) {
$this->rc->output->command('display_message', $this->gettext('altemail_cleared'), 'confirmation');
} else if($save['altemail'] == $p['record']['email']) {
unset($save['altemail']);
$p['abort'] = true;
$p['message'] = $this->gettext('altemail_match_primary');
return $p;
} else if(!rcube_utils::check_email($save['altemail'])) {
unset($save['altemail']);
$p['abort'] = true;
$p['message'] = $this->gettext('altemail_invalid');
return $p;
}
}
if (count($save)) {
$this->debug("Save user identities: " . print_r($save, true));
$this->set_user_props($save);
}
return $p;
}
// Return array - user props (username must be [email protected])
function get_user_props($username = null, $with_alias = true) {
if (!$username) {
$username = ($this->rc->get_user_name() ? $this->rc->get_user_name() : rcube_utils::get_input_value("_username", rcube_utils::INPUT_GPC));
}
$ret = [];
$user = trim(urldecode($username));
if ($user) {
// get user row
$query = "SELECT u.user_id, u.username, i.email" .
" FROM users u" .
" INNER JOIN identities i ON i.user_id = u.user_id" .
" WHERE username=?";
$result = $this->rc->db->query($query, $user);
if ($result && ($arr = $this->rc->db->fetch_assoc($result))) {
$fields = [];
foreach ($this->fields as $field => $field_name) {
$fields[] = $field_name . " as " . $field;
}
$query = "SELECT " . implode(",",$fields) . ", token, token_validity, token='' OR token_validity < NOW() as token_expired" .
" FROM " . $this->rc->config->get('pr_users_table') .
" WHERE username=?";
$result = $this->db->query($query, $arr['email']);
$ret = array_merge($arr, $this->db->fetch_assoc($result));
} else {
// for alias (with users_alias plugin)
if ($with_alias && $this->rc->plugins->load_plugin('users_alias', true)) {
$users_alias = new users_alias($this->api);
$result = $users_alias->alias2user(['user' => $user]);
if ($result['user']) {
$ret = $this->get_user_props($result['user'], false);
}
}
}
$_SESSION['username'] = $ret['username']; //for password plugin
$ret['have_altemail'] = ($ret['altemail'] && !empty($ret['altemail']));
$ret['have_phone'] = ($ret['phone'] && !empty($ret['phone']));
$ret['have_code'] = ($ret['have_altemail'] || $ret['have_phone']);
$ret['have_answer'] = ($ret['question'] && !empty($ret['question']) && $ret['answer'] && !empty($ret['answer']));
}
$this->user = $ret;
return $ret;
}
// Save user props to DB
function set_user_props($props) {
$fields = [];
foreach ($this->fields as $field => $field_name) {
if (isset($props[$field])) {
$fields[] = $field_name . " = '" . $props[$field] . "'";
}
}
if (isset($props['token'])) {
if (empty($props['token'])) {
$code_validity_time = 0;
} else {
$code_validity_time = (int) $this->rc->config->get('pr_confirm_code_validity_time', 30);
}
$fields[] = "token = '" . $props['token'] . "', token_validity = NOW() + INTERVAL " . $code_validity_time . " MINUTE";
//$fields[] = "token = '" . $props['token'] . "', token_validity = NOW() + '" . $code_validity_time . " MINUTE'";
}
if ($props['password']) {
$fields[] = "password = '" . $props['password'] . "'";
$fields[] = "mdp = '{SHA512-CRYPT}" . $props['password'] . "'";
}
if (count($fields)) {
$query = "UPDATE " . $this->rc->config->get('pr_users_table') . " SET " . implode(",",$fields) . " WHERE username=?";
$this->db->query($query, $this->user['username']);
$this->get_user_props(); //update user props
$this->debug("Update user '" . $this->user['username'] . "' props: " . print_r($fields, true));
return $this->db->affected_rows() == 1;
}
return false;
}
function update_confirm_code_count($plus = 0) {
$count = $this->get_confirm_code_count() + $plus;
$_SESSION['pr_confirm_code_count'] = $count;
}
function get_confirm_code_count() {
if (!isset($_SESSION['pr_confirm_code_count'])) {
$_SESSION['pr_confirm_code_count'] = 0;
}
return $_SESSION['pr_confirm_code_count'];
}
/*******************
* service functions
*******************/
function get_dbh() {
if (!$this->db) {
if ($dsn = $this->rc->config->get('pr_db_dsn')) {
$this->db = rcube_db::factory($dsn);
$this->db->set_debug((bool)$this->rc->config->get('sql_debug'));
}
else {
$this->db = $this->rc->get_dbh();
}
}
return $this->db;
}
function get_action() {
$action = rcube_utils::get_input_value('_a', rcube_utils::INPUT_GPC);
if (!$action || empty($action)) {
$action = 'init';
}
return $action;
}
function logging($text) {
if ($this->rc->config->get('pr_password_log') == true) {
rcube::write_log('password', $text);
}
}
function debug($text) {
if ($this->rc->config->get('pr_debug') == true) {
$msg = (is_array($text) ? print_r($text, true) : $text);
rcube::write_log('console', $msg);
}
}
}
?>