|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle Framework |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs; |
|
9
|
|
|
use |
|
10
|
|
|
cs\DB\Accessor, |
|
11
|
|
|
cs\User\Data as User_data, |
|
12
|
|
|
cs\User\Group as User_group, |
|
13
|
|
|
cs\User\Management as User_management, |
|
14
|
|
|
cs\User\Permission as User_permission, |
|
15
|
|
|
cs\User\Profile as User_profile; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class for users manipulating |
|
19
|
|
|
* |
|
20
|
|
|
* Provides next events: |
|
21
|
|
|
* System/User/construct/before |
|
22
|
|
|
* |
|
23
|
|
|
* System/User/construct/after |
|
24
|
|
|
* |
|
25
|
|
|
* System/User/registration/before |
|
26
|
|
|
* ['email' => <i>email</i>] |
|
27
|
|
|
* |
|
28
|
|
|
* System/User/registration/after |
|
29
|
|
|
* ['id' => <i>user_id</i>] |
|
30
|
|
|
* |
|
31
|
|
|
* System/User/registration/confirmation/before |
|
32
|
|
|
* ['reg_key' => <i>reg_key</i>] |
|
33
|
|
|
* |
|
34
|
|
|
* System/User/registration/confirmation/after |
|
35
|
|
|
* ['id' => <i>user_id</i>] |
|
36
|
|
|
* |
|
37
|
|
|
* System/User/del/before |
|
38
|
|
|
* ['id' => <i>user_id</i>] |
|
39
|
|
|
* |
|
40
|
|
|
* System/User/del/after |
|
41
|
|
|
* ['id' => <i>user_id</i>] |
|
42
|
|
|
* |
|
43
|
|
|
* @property int $id |
|
44
|
|
|
* @property string $login |
|
45
|
|
|
* @property string $login_hash sha224 hash |
|
46
|
|
|
* @property string $username |
|
47
|
|
|
* @property string $password_hash sha512 hash |
|
48
|
|
|
* @property string $email |
|
49
|
|
|
* @property string $email_hash sha224 hash |
|
50
|
|
|
* @property string $language |
|
51
|
|
|
* @property string $timezone |
|
52
|
|
|
* @property int $reg_date unix timestamp |
|
53
|
|
|
* @property string $reg_ip hex value, obtained by function ip2hex() |
|
54
|
|
|
* @property string $reg_key random md5 hash, generated during registration |
|
55
|
|
|
* @property int $status '-1' - not activated (for example after registration), 0 - inactive, 1 - active |
|
56
|
|
|
* @property int $block_until unix timestamp |
|
57
|
|
|
* @property string $avatar |
|
58
|
|
|
* |
|
59
|
|
|
* @method static $this instance($check = false) |
|
60
|
|
|
*/ |
|
61
|
|
|
class User { |
|
62
|
|
|
use |
|
63
|
|
|
Accessor, |
|
64
|
|
|
Singleton, |
|
65
|
|
|
User_data, |
|
66
|
|
|
User_group, |
|
67
|
|
|
User_management, |
|
68
|
|
|
User_permission, |
|
69
|
|
|
User_profile; |
|
70
|
|
|
/** |
|
71
|
|
|
* Id of system guest user |
|
72
|
|
|
*/ |
|
73
|
|
|
const GUEST_ID = 1; |
|
74
|
|
|
/** |
|
75
|
|
|
* Id of first, primary system administrator |
|
76
|
|
|
*/ |
|
77
|
|
|
const ROOT_ID = 2; |
|
78
|
|
|
/** |
|
79
|
|
|
* Id of system group for administrators |
|
80
|
|
|
*/ |
|
81
|
|
|
const ADMIN_GROUP_ID = 1; |
|
82
|
|
|
/** |
|
83
|
|
|
* Id of system group for users |
|
84
|
|
|
*/ |
|
85
|
|
|
const USER_GROUP_ID = 2; |
|
86
|
|
|
/** |
|
87
|
|
|
* Status of active user |
|
88
|
|
|
*/ |
|
89
|
|
|
const STATUS_ACTIVE = 1; |
|
90
|
|
|
/** |
|
91
|
|
|
* Status of inactive user |
|
92
|
|
|
*/ |
|
93
|
|
|
const STATUS_INACTIVE = 0; |
|
94
|
|
|
/** |
|
95
|
|
|
* Status of not activated user |
|
96
|
|
|
*/ |
|
97
|
|
|
const STATUS_NOT_ACTIVATED = -1; |
|
98
|
|
|
/** |
|
99
|
|
|
* @var Cache\Prefix |
|
100
|
|
|
*/ |
|
101
|
|
|
protected $cache; |
|
102
|
|
|
/** |
|
103
|
|
|
* Whether to use memory cache (locally, inside object, may require a lot of memory if working with many users together) |
|
104
|
|
|
* @var bool |
|
105
|
|
|
*/ |
|
106
|
|
|
protected $memory_cache = true; |
|
107
|
|
|
/** |
|
108
|
|
|
* Returns database index |
|
109
|
|
|
* |
|
110
|
|
|
* @return int |
|
111
|
|
|
*/ |
|
112
|
32 |
|
protected function cdb () { |
|
113
|
32 |
|
return Config::instance()->module('System')->db('users'); |
|
114
|
|
|
} |
|
115
|
34 |
|
protected function construct () { |
|
116
|
34 |
|
$this->cache = Cache::prefix('users'); |
|
117
|
34 |
|
Event::instance()->fire('System/User/construct/before'); |
|
118
|
34 |
|
$this->initialize_data(); |
|
119
|
|
|
/** |
|
120
|
|
|
* Initialize session |
|
121
|
|
|
*/ |
|
122
|
34 |
|
Session::instance(); |
|
123
|
34 |
|
Event::instance()->fire('System/User/construct/after'); |
|
124
|
34 |
|
} |
|
125
|
|
|
/** |
|
126
|
|
|
* Check number of sign in attempts (is used by system) |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $login_hash Hash (sha224) from login (hash from lowercase string) |
|
129
|
|
|
* |
|
130
|
|
|
* @return int Number of attempts |
|
131
|
|
|
*/ |
|
132
|
|
|
public function get_sign_in_attempts_count ($login_hash) { |
|
133
|
|
|
if (!preg_match('/^[0-9a-z]{56}$/', $login_hash)) { |
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
$time = time(); |
|
137
|
|
|
return $this->db()->qfs( |
|
138
|
|
|
"SELECT COUNT(`expire`) |
|
139
|
|
|
FROM `[prefix]sign_ins` |
|
140
|
|
|
WHERE |
|
141
|
|
|
`expire` > $time AND |
|
142
|
|
|
( |
|
143
|
|
|
`login_hash` = '%s' OR |
|
144
|
|
|
`ip` = '%s' |
|
145
|
|
|
)", |
|
146
|
|
|
$login_hash, |
|
147
|
|
|
ip2hex(Request::instance()->ip) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
/** |
|
151
|
|
|
* Process sign in result (is used by system) |
|
152
|
|
|
* |
|
153
|
|
|
* @param bool $success |
|
154
|
|
|
* @param string $login_hash Hash (sha224) from login (hash from lowercase string) |
|
155
|
|
|
*/ |
|
156
|
2 |
|
public function sign_in_result ($success, $login_hash) { |
|
157
|
2 |
|
if (!preg_match('/^[0-9a-z]{56}$/', $login_hash)) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
2 |
|
$ip = ip2hex(Request::instance()->ip); |
|
161
|
2 |
|
$time = time(); |
|
162
|
2 |
|
if ($success) { |
|
163
|
2 |
|
$this->db_prime()->q( |
|
164
|
|
|
"DELETE FROM `[prefix]sign_ins` |
|
165
|
|
|
WHERE |
|
166
|
2 |
|
`expire` > $time AND |
|
167
|
|
|
( |
|
168
|
|
|
`login_hash` = '%s' OR `ip` = '%s' |
|
169
|
2 |
|
)", |
|
170
|
|
|
$login_hash, |
|
171
|
|
|
$ip |
|
172
|
|
|
); |
|
173
|
|
|
} else { |
|
174
|
|
|
$Config = Config::instance(); |
|
175
|
|
|
$this->db_prime()->q( |
|
176
|
|
|
"INSERT INTO `[prefix]sign_ins` |
|
177
|
|
|
( |
|
178
|
|
|
`expire`, |
|
179
|
|
|
`login_hash`, |
|
180
|
|
|
`ip` |
|
181
|
|
|
) VALUES ( |
|
182
|
|
|
'%s', |
|
183
|
|
|
'%s', |
|
184
|
|
|
'%s' |
|
185
|
|
|
)", |
|
186
|
|
|
$time + $Config->core['sign_in_attempts_block_time'], |
|
187
|
|
|
$login_hash, |
|
188
|
|
|
$ip |
|
189
|
|
|
); |
|
190
|
|
|
if ($this->db_prime()->id() % $Config->core['inserts_limit'] == 0) { |
|
191
|
|
|
$this->db_prime()->q("DELETE FROM `[prefix]sign_ins` WHERE `expire` < $time"); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
2 |
|
} |
|
195
|
|
|
/** |
|
196
|
|
|
* Get data item of current user |
|
197
|
|
|
* |
|
198
|
|
|
* @param string|string[] $item |
|
199
|
|
|
* |
|
200
|
|
|
* @return false|int|mixed[]|string|User\Properties If <i>$item</i> is integer - cs\User\Properties object will be returned |
|
201
|
|
|
*/ |
|
202
|
24 |
|
public function __get ($item) { |
|
203
|
24 |
|
if ($item == 'id') { |
|
204
|
24 |
|
return Session::instance()->get_user(); |
|
205
|
|
|
} |
|
206
|
8 |
|
return $this->get($item); |
|
207
|
|
|
} |
|
208
|
|
|
/** |
|
209
|
|
|
* Set data item of current user |
|
210
|
|
|
* |
|
211
|
|
|
* @param array|int|string $item Item-value array may be specified for setting several items at once |
|
212
|
|
|
* @param mixed|null $value |
|
213
|
|
|
*/ |
|
214
|
2 |
|
public function __set ($item, $value = null) { |
|
215
|
2 |
|
$this->set($item, $value); |
|
216
|
2 |
|
} |
|
217
|
|
|
/** |
|
218
|
|
|
* Is admin |
|
219
|
|
|
* |
|
220
|
|
|
* Proxy to \cs\Session::instance()->admin() for convenience |
|
221
|
|
|
* |
|
222
|
|
|
* @return bool |
|
223
|
|
|
*/ |
|
224
|
8 |
|
public function admin () { |
|
225
|
8 |
|
return Session::instance()->admin(); |
|
226
|
|
|
} |
|
227
|
|
|
/** |
|
228
|
|
|
* Is user |
|
229
|
|
|
* |
|
230
|
|
|
* Proxy to \cs\Session::instance()->user() for convenience |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
2 |
|
public function user () { |
|
235
|
2 |
|
return Session::instance()->user(); |
|
236
|
|
|
} |
|
237
|
|
|
/** |
|
238
|
|
|
* Is guest |
|
239
|
|
|
* |
|
240
|
|
|
* Proxy to \cs\Session::instance()->guest() for convenience |
|
241
|
|
|
* |
|
242
|
|
|
* @return bool |
|
243
|
|
|
*/ |
|
244
|
8 |
|
public function guest () { |
|
245
|
8 |
|
return Session::instance()->guest(); |
|
246
|
|
|
} |
|
247
|
|
|
/** |
|
248
|
|
|
* Disable memory cache |
|
249
|
|
|
* |
|
250
|
|
|
* Memory cache stores users data inside User class in order to get data faster next time. |
|
251
|
|
|
* But in case of working with large amount of users this cache can be too large. Disabling will cause some performance drop, but save a lot of RAM. |
|
252
|
|
|
*/ |
|
253
|
10 |
|
public function disable_memory_cache () { |
|
254
|
10 |
|
$this->memory_cache = false; |
|
255
|
10 |
|
$this->data = []; |
|
256
|
10 |
|
$this->permissions = []; |
|
257
|
10 |
|
} |
|
258
|
|
|
} |
|
259
|
|
|
|