1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\System\Modules\Auth |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Modules\Auth\Controller { |
11
|
|
|
|
12
|
|
|
use Modules\Auth, Modules\Entitizer, Utils\Security, Utils\Validate, Str; |
13
|
|
|
|
14
|
|
|
class Reset { |
15
|
|
|
|
16
|
|
|
private $user = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
public function __construct() { |
23
|
|
|
|
24
|
|
|
$this->user = Entitizer::get(TABLE_USERS); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Invoker |
29
|
|
|
* |
30
|
|
|
* @return true|string|array : true on success, otherwise an error code, or an array of type [$param_name, $error_code], |
31
|
|
|
* where $param_name is a name of param that has triggered the error, |
32
|
|
|
* and $error_code is a language phrase related to the error |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
public function __invoke(array $post) { |
36
|
|
|
|
37
|
|
|
# Declare variables |
38
|
|
|
|
39
|
|
|
$name_email = ''; $captcha = ''; |
40
|
|
|
|
41
|
|
|
# Extract post array |
42
|
|
|
|
43
|
|
|
extract($post); |
44
|
|
|
|
45
|
|
|
# Validate values |
46
|
|
|
|
47
|
|
View Code Duplication |
if ((false === ($name = Validate::userName($name_email))) && |
|
|
|
|
48
|
|
|
|
49
|
|
|
(false === ($email = Validate::userEmail($name_email)))) return ['name_email', 'USER_ERROR_NAME_INVALID']; |
50
|
|
|
|
51
|
|
|
if (false === Security::checkCaptcha($captcha)) return ['captcha', 'USER_ERROR_CAPTCHA_INCORRECT']; |
52
|
|
|
|
53
|
|
|
# Init user |
54
|
|
|
|
55
|
|
|
$init_by = ((false !== $name) ? 'name' : 'email'); |
56
|
|
|
|
57
|
|
View Code Duplication |
if ((!$this->user->init($$init_by, $init_by)) || (Auth::isAdmin() && ($this->user->rank < RANK_ADMINISTRATOR))) { |
|
|
|
|
58
|
|
|
|
59
|
|
|
return ['name_email', ('USER_ERROR_' . strtoupper($init_by) .'_INCORRECT')]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
# Check access |
63
|
|
|
|
64
|
|
|
if (!Auth::isAdmin() && ($this->user->rank === RANK_GUEST)) return 'USER_ERROR_ACCESS'; |
|
|
|
|
65
|
|
|
|
66
|
|
|
# Create secret |
67
|
|
|
|
68
|
|
|
$secret = Entitizer::get(TABLE_USERS_SECRETS, $this->user->id); $secret->remove(); |
|
|
|
|
69
|
|
|
|
70
|
|
|
$code = Str::random(40); $ip = REQUEST_CLIENT_IP; $time = REQUEST_TIME; |
71
|
|
|
|
72
|
|
|
$data = ['id' => $this->user->id, 'code' => $code, 'ip' => $ip, 'time' => $time]; |
|
|
|
|
73
|
|
|
|
74
|
|
|
if (!$secret->create($data)) return 'USER_ERROR_AUTH_RESET'; |
75
|
|
|
|
76
|
|
|
# Send mail |
77
|
|
|
|
78
|
|
|
Auth\Utils\Mail::sendPasswordMessage($this->user, $code); |
|
|
|
|
79
|
|
|
|
80
|
|
|
# ------------------------ |
81
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.