|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle CMS |
|
4
|
|
|
* @subpackage System module |
|
5
|
|
|
* @category modules |
|
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2015, Nazar Mokrynskyi |
|
8
|
|
|
* @license MIT License, see license.txt |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace cs\modules\System\api\Controller; |
|
11
|
|
|
use |
|
12
|
|
|
cs\Config, |
|
13
|
|
|
cs\ExitException, |
|
14
|
|
|
cs\Language, |
|
15
|
|
|
cs\Mail, |
|
16
|
|
|
cs\Page, |
|
17
|
|
|
cs\Session, |
|
18
|
|
|
cs\User; |
|
19
|
|
|
trait user_ { |
|
20
|
|
|
static function user_change_password () { |
|
21
|
|
|
$L = Language::instance(); |
|
22
|
|
|
$Page = Page::instance(); |
|
23
|
|
|
$User = User::instance(); |
|
24
|
|
|
if (!isset($_POST['current_password'], $_POST['new_password'])) { |
|
25
|
|
|
throw new ExitException(400); |
|
26
|
|
|
} |
|
27
|
|
|
if (!$User->user()) { |
|
28
|
|
|
throw new ExitException(403); |
|
29
|
|
|
} elseif (!$_POST['new_password']) { |
|
30
|
|
|
throw new ExitException($L->please_type_new_password, 400); |
|
31
|
|
|
} elseif (!$User->validate_password($_POST['current_password'], $User->id, true)) { |
|
32
|
|
|
throw new ExitException($L->wrong_current_password, 400); |
|
33
|
|
|
} |
|
34
|
|
|
$id = $User->id; |
|
35
|
|
|
if ($User->set_password($_POST['new_password'], $id, true)) { |
|
36
|
|
|
Session::instance()->add($id); |
|
37
|
|
|
$Page->json('OK'); |
|
38
|
|
|
} else { |
|
39
|
|
|
throw new ExitException($L->change_password_server_error, 400); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
static function user_registration () { |
|
43
|
|
|
$Config = Config::instance(); |
|
44
|
|
|
$L = Language::instance(); |
|
45
|
|
|
$Page = Page::instance(); |
|
46
|
|
|
$User = User::instance(); |
|
47
|
|
|
if (!isset($_POST['email'])) { |
|
48
|
|
|
throw new ExitException(400); |
|
49
|
|
|
} elseif (!$User->guest()) { |
|
50
|
|
|
$Page->json('reload'); |
|
51
|
|
|
return; |
|
52
|
|
|
} elseif (!$Config->core['allow_user_registration']) { |
|
53
|
|
|
throw new ExitException($L->registration_prohibited, 403); |
|
54
|
|
|
} elseif (empty($_POST['email'])) { |
|
55
|
|
|
throw new ExitException($L->please_type_your_email, 400); |
|
56
|
|
|
} |
|
57
|
|
|
$_POST['email'] = mb_strtolower($_POST['email']); |
|
58
|
|
|
$result = $User->registration($_POST['email']); |
|
59
|
|
|
if ($result === false) { |
|
60
|
|
|
throw new ExitException($L->please_type_correct_email, 400); |
|
61
|
|
|
} elseif ($result == 'error') { |
|
62
|
|
|
throw new ExitException($L->reg_server_error, 500); |
|
63
|
|
|
} elseif ($result == 'exists') { |
|
64
|
|
|
throw new ExitException($L->reg_error_exists, 400); |
|
65
|
|
|
} |
|
66
|
|
|
$confirm = $result['reg_key'] !== true; |
|
67
|
|
|
if ($confirm) { |
|
68
|
|
|
$body = $L->reg_need_confirmation_mail_body( |
|
|
|
|
|
|
69
|
|
|
strstr($_POST['email'], '@', true), |
|
70
|
|
|
get_core_ml_text('name'), |
|
71
|
|
|
$Config->core_url()."/profile/registration_confirmation/$result[reg_key]", |
|
72
|
|
|
$L->time($Config->core['registration_confirmation_time'], 'd') |
|
73
|
|
|
); |
|
74
|
|
|
} else { |
|
75
|
|
|
$body = $L->reg_success_mail_body( |
|
|
|
|
|
|
76
|
|
|
strstr($_POST['email'], '@', true), |
|
77
|
|
|
get_core_ml_text('name'), |
|
78
|
|
|
$Config->core_url().'/profile/settings', |
|
79
|
|
|
$User->get('login', $result['id']), |
|
80
|
|
|
$result['password'] |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
if (Mail::instance()->send_to( |
|
84
|
|
|
$_POST['email'], |
|
85
|
|
|
$L->{$confirm ? 'reg_need_confirmation_mail' : 'reg_success_mail'}(get_core_ml_text('name')), |
|
86
|
|
|
$body |
|
87
|
|
|
) |
|
88
|
|
|
) { |
|
89
|
|
|
$Page->json($confirm ? 'reg_confirmation' : 'reg_success'); |
|
90
|
|
|
} else { |
|
91
|
|
|
$User->registration_cancel(); |
|
92
|
|
|
throw new ExitException($L->sending_reg_mail_error, 500); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
static function user_restore_password () { |
|
96
|
|
|
$Config = Config::instance(); |
|
97
|
|
|
$L = Language::instance(); |
|
98
|
|
|
$Page = Page::instance(); |
|
99
|
|
|
$User = User::instance(); |
|
100
|
|
|
if (!isset($_POST['email'])) { |
|
101
|
|
|
throw new ExitException(400); |
|
102
|
|
|
} elseif (!$User->guest()) { |
|
103
|
|
|
throw new ExitException(403); |
|
104
|
|
|
} elseif (!$_POST['email']) { |
|
105
|
|
|
throw new ExitException($L->please_type_your_email, 400); |
|
106
|
|
|
} |
|
107
|
|
|
$id = $User->get_id(mb_strtolower($_POST['email'])); |
|
108
|
|
|
if (!$id) { |
|
109
|
|
|
throw new ExitException($L->user_with_such_login_email_not_found, 400); |
|
110
|
|
|
} |
|
111
|
|
|
if ( |
|
112
|
|
|
($key = $User->restore_password($id)) && |
|
113
|
|
|
Mail::instance()->send_to( |
|
114
|
|
|
$User->get('email', $id), |
|
115
|
|
|
$L->restore_password_confirmation_mail(get_core_ml_text('name')), |
|
|
|
|
|
|
116
|
|
|
$L->restore_password_confirmation_mail_body( |
|
|
|
|
|
|
117
|
|
|
$User->username($id), |
|
118
|
|
|
get_core_ml_text('name'), |
|
119
|
|
|
$Config->core_url()."/profile/restore_password_confirmation/$key", |
|
120
|
|
|
$L->time($Config->core['registration_confirmation_time'], 'd') |
|
121
|
|
|
) |
|
122
|
|
|
) |
|
123
|
|
|
) { |
|
124
|
|
|
$Page->json('OK'); |
|
125
|
|
|
} else { |
|
126
|
|
|
throw new ExitException($L->restore_password_server_error, 500); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
static function user_sign_in () { |
|
130
|
|
|
$Config = Config::instance(); |
|
131
|
|
|
$L = Language::instance(); |
|
132
|
|
|
$User = User::instance(); |
|
133
|
|
|
if (!$User->guest()) { |
|
134
|
|
|
return; |
|
135
|
|
|
} elseif ( |
|
136
|
|
|
$Config->core['sign_in_attempts_block_count'] && |
|
137
|
|
|
$User->get_sign_in_attempts_count(@$_POST['login']) >= $Config->core['sign_in_attempts_block_count'] |
|
138
|
|
|
) { |
|
139
|
|
|
$User->sign_in_result(false, @$_POST['login']); |
|
140
|
|
|
throw new ExitException("$L->sign_in_attempts_ends_try_after ".format_time($Config->core['sign_in_attempts_block_time']), 403); |
|
141
|
|
|
} |
|
142
|
|
|
$id = $User->get_id(@$_POST['login']); |
|
143
|
|
|
if ( |
|
144
|
|
|
$id && |
|
145
|
|
|
$User->validate_password(@$_POST['password'], $id, true) |
|
146
|
|
|
) { |
|
147
|
|
|
$status = $User->get('status', $id); |
|
148
|
|
|
$block_until = $User->get('block_until', $id); |
|
149
|
|
|
if ($status == User::STATUS_NOT_ACTIVATED) { |
|
150
|
|
|
throw new ExitException($L->your_account_is_not_active, 403); |
|
151
|
|
|
} elseif ($status == User::STATUS_INACTIVE) { |
|
152
|
|
|
throw new ExitException($L->your_account_disabled, 403); |
|
153
|
|
|
} elseif ($block_until > time()) { |
|
154
|
|
|
throw new ExitException($L->your_account_blocked_until.' '.date($L->_datetime, $block_until), 403); |
|
155
|
|
|
} |
|
156
|
|
|
Session::instance()->add($id); |
|
157
|
|
|
$User->sign_in_result(true, $_POST['login']); |
|
158
|
|
|
} else { |
|
159
|
|
|
$User->sign_in_result(false, @$_POST['login']); |
|
160
|
|
|
$content = $L->auth_error_sign_in; |
|
161
|
|
|
if ( |
|
162
|
|
|
$Config->core['sign_in_attempts_block_count'] && |
|
163
|
|
|
$User->get_sign_in_attempts_count(@$_POST['login']) >= $Config->core['sign_in_attempts_block_count'] * 2 / 3 |
|
164
|
|
|
) { |
|
165
|
|
|
$content .= " $L->sign_in_attempts_left ".($Config->core['sign_in_attempts_block_count'] - $User->get_sign_in_attempts_count(@$_POST['login'])); |
|
166
|
|
|
} |
|
167
|
|
|
throw new ExitException($content, 400); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
static function user_sign_out () { |
|
171
|
|
|
$User = User::instance(); |
|
172
|
|
|
if ($User->guest()) { |
|
173
|
|
|
Page::instance()->json(1); |
|
174
|
|
|
return; |
|
175
|
|
|
} |
|
176
|
|
|
if (isset($_POST['sign_out'])) { |
|
177
|
|
|
Session::instance()->del(); |
|
178
|
|
|
/** |
|
179
|
|
|
* Hack for 403 after sign out in administration |
|
180
|
|
|
*/ |
|
181
|
|
|
_setcookie('sign_out', 1, TIME + 5, true); |
|
182
|
|
|
Page::instance()->json(1); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: