1
|
|
|
<?php |
2
|
1 |
|
require_once('MODx.php'); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class modUsers |
6
|
|
|
*/ |
7
|
|
|
class modUsers extends MODxAPI |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $default_field = array( |
13
|
|
|
'user' => array( |
14
|
|
|
'username' => '', |
15
|
|
|
'password' => '', |
16
|
|
|
'cachepwd' => '' |
17
|
|
|
), |
18
|
|
|
'attribute' => array( |
19
|
|
|
'fullname' => '', |
20
|
|
|
'role' => 0, |
21
|
|
|
'email' => '', |
22
|
|
|
'phone' => '', |
23
|
|
|
'mobilephone' => '', |
24
|
|
|
'blocked' => 0, |
25
|
|
|
'blockeduntil' => 0, |
26
|
|
|
'blockedafter' => 0, |
27
|
|
|
'logincount' => 0, |
28
|
|
|
'lastlogin' => 0, |
29
|
|
|
'thislogin' => 0, |
30
|
|
|
'failedlogincount' => 0, |
31
|
|
|
'sessionid' => '', |
32
|
|
|
'dob' => 0, |
33
|
|
|
'gender' => 0, |
34
|
|
|
'country' => '', |
35
|
|
|
'state' => '', |
36
|
|
|
'city' => '', |
37
|
|
|
'street' => '', |
38
|
|
|
'zip' => '', |
39
|
|
|
'fax' => '', |
40
|
|
|
'photo' => '', |
41
|
|
|
'comment' => '', |
42
|
|
|
'createdon' => 0, |
43
|
|
|
'editedon' => 0 |
44
|
|
|
), |
45
|
|
|
'hidden' => array( |
46
|
|
|
'internalKey' |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $givenPassword = ''; |
54
|
|
|
protected $groupIds = array(); |
55
|
|
|
protected $userIdCache = array( |
56
|
|
|
'attribute.internalKey' => '', |
57
|
|
|
'attribute.email' => '', |
58
|
|
|
'user.username' => '' |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var integer |
63
|
|
|
*/ |
64
|
|
|
private $rememberTime; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* MODxAPI constructor. |
68
|
|
|
* @param DocumentParser $modx |
69
|
|
|
* @param bool $debug |
70
|
|
|
* @throws Exception |
71
|
|
|
*/ |
72
|
10 |
|
public function __construct(DocumentParser $modx, $debug = false) |
73
|
|
|
{ |
74
|
10 |
|
$this->setRememberTime(60 * 60 * 24 * 365 * 5); |
75
|
10 |
|
parent::__construct($modx, $debug); |
76
|
10 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $val |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
10 |
|
protected function setRememberTime($val) |
83
|
|
|
{ |
84
|
10 |
|
$this->rememberTime = (int)$val; |
85
|
10 |
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return integer |
90
|
|
|
*/ |
91
|
|
|
public function getRememberTime() |
92
|
|
|
{ |
93
|
|
|
return $this->rememberTime; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $key |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function issetField($key) |
101
|
|
|
{ |
102
|
|
|
return (array_key_exists($key, $this->default_field['user']) || array_key_exists( |
103
|
|
|
$key, |
104
|
|
|
$this->default_field['attribute'] |
105
|
|
|
) || in_array($key, $this->default_field['hidden'])); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $data |
110
|
|
|
* @return string|false |
111
|
|
|
*/ |
112
|
|
|
protected function findUser($data) |
113
|
|
|
{ |
114
|
|
|
switch (true) { |
115
|
|
|
case (is_int($data) || ((int)$data > 0 && (string)intval($data) === $data)): |
116
|
|
|
$find = 'attribute.internalKey'; |
117
|
|
|
break; |
118
|
|
|
case filter_var($data, FILTER_VALIDATE_EMAIL): |
119
|
|
|
$find = 'attribute.email'; |
120
|
|
|
break; |
121
|
|
|
case is_scalar($data): |
122
|
|
|
$find = 'user.username'; |
123
|
|
|
break; |
124
|
|
|
default: |
125
|
|
|
$find = false; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $find; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $data |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function create($data = array()) |
136
|
|
|
{ |
137
|
|
|
parent::create($data); |
138
|
|
|
$this->set('createdon', time()); |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* |
145
|
|
|
*/ |
146
|
|
|
public function close() |
147
|
|
|
{ |
148
|
|
|
parent::close(); |
149
|
|
|
$this->userIdCache = array( |
150
|
|
|
'attribute.internalKey' => '', |
151
|
|
|
'attribute.email' => '', |
152
|
|
|
'user.username' => '' |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $id |
158
|
|
|
* @return mixed |
159
|
|
|
*/ |
160
|
|
|
protected function getUserId($id) { |
161
|
|
|
$find = $this->findUser($id); |
162
|
|
|
if ($find && ! empty($this->userIdCache[$find])) { |
163
|
|
|
$id = $this->userIdCache[$find]; |
164
|
|
|
} else { |
165
|
|
|
$id = null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $id; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param $id |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function edit($id) |
176
|
|
|
{ |
177
|
|
|
$id = is_scalar($id) ? trim($id) : ''; |
178
|
|
|
if ($this->getUserId($id) != $id) { |
179
|
|
|
$this->close(); |
180
|
|
|
$this->newDoc = false; |
181
|
|
|
|
182
|
|
|
if (! $find = $this->findUser($id)) { |
183
|
|
|
$this->id = null; |
184
|
|
|
} else { |
185
|
|
|
$this->set('editedon', time()); |
186
|
|
|
$this->editQuery($find, $id); |
187
|
|
|
$this->id = empty($this->field['internalKey']) ? null : $this->get('internalKey'); |
188
|
|
|
$this->userIdCache['attribute.internalKey'] = $this->getID(); |
189
|
|
|
$this->userIdCache['attribute.email'] = $this->get('email'); |
190
|
|
|
$this->userIdCache['user.username'] = $this->get('username'); |
191
|
|
|
$this->loadUserSettings(); |
192
|
|
|
$this->store($this->toArray()); |
193
|
|
|
unset($this->field['id']); |
194
|
|
|
unset($this->field['internalKey']); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
protected function loadUserSettings() |
202
|
|
|
{ |
203
|
|
|
$webUser = $this->getID(); |
204
|
|
|
|
205
|
|
|
if (!empty($webUser)) { |
|
|
|
|
206
|
|
|
$settings = $this->modx->db->makeArray($this->modx->db->select('*', $this->makeTable('web_user_settings'), "webuser = {$webUser}")); |
207
|
|
|
$this->fromArray(array_column($settings,'setting_value','setting_name')); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $find |
213
|
|
|
* @param string $id |
214
|
|
|
*/ |
215
|
|
|
protected function editQuery($find, $id) |
216
|
|
|
{ |
217
|
|
|
$result = $this->query(" |
218
|
|
|
SELECT * from {$this->makeTable('web_user_attributes')} as attribute |
219
|
10 |
|
LEFT JOIN {$this->makeTable('web_users')} as user ON user.id=attribute.internalKey |
220
|
|
|
WHERE {$find}='{$this->escape($id)}' |
221
|
10 |
|
"); |
222
|
|
|
$this->field = $this->modx->db->getRow($result); |
223
|
10 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $key |
227
|
10 |
|
* @param $value |
228
|
|
|
* @return $this |
229
|
|
|
*/ |
230
|
|
|
public function set($key, $value) |
|
|
|
|
231
|
|
|
{ |
232
|
|
|
if (is_scalar($value) && is_scalar($key) && ! empty($key)) { |
233
|
|
|
switch ($key) { |
234
|
|
|
case 'password': |
235
|
|
|
$this->givenPassword = $value; |
236
|
10 |
|
$value = $this->getPassword($value); |
237
|
10 |
|
break; |
238
|
|
|
case 'sessionid': |
239
|
|
|
//short bug fix when authoring a web user if the manager is logged in |
240
|
|
|
$oldSessionId = session_id(); |
241
|
10 |
|
session_regenerate_id(false); |
242
|
10 |
|
$value = session_id(); |
243
|
|
|
if ($mid = $this->modx->getLoginUserID('mgr')) { |
|
|
|
|
244
|
10 |
|
//short bug fix when authoring a web user if the manager is logged in |
245
|
|
|
$this->modx->db->delete($this->makeTable('active_users'),"`internalKey`={$mid} and `sid` != '{$oldSessionId}' " ); |
246
|
|
|
$this->modx->db->delete($this->makeTable('active_user_sessions'),"`internalKey`={$mid} and `sid` != '{$oldSessionId}' " ); |
247
|
|
|
|
248
|
|
|
$this->modx->db->query("UPDATE {$this->makeTable('active_user_locks')} SET `sid`='{$value}' WHERE `internalKey`={$mid}"); |
249
|
|
|
$this->modx->db->query("UPDATE {$this->makeTable('active_user_sessions')} SET `sid`='{$value}' WHERE `internalKey`={$mid}"); |
250
|
|
|
$this->modx->db->query("UPDATE {$this->makeTable('active_users')} SET `sid`='{$value}' WHERE `internalKey`={$mid}"); |
251
|
|
|
} |
252
|
|
|
break; |
253
|
|
|
case 'editedon': |
254
|
|
|
case 'createdon': |
255
|
|
|
$value = $this->getTime($value); |
256
|
|
|
break; |
257
|
|
|
} |
258
|
|
|
$this->field[$key] = $value; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $this; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param $pass |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getPassword($pass) |
269
|
|
|
{ |
270
|
|
|
return md5($pass); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param bool $fire_events |
275
|
|
|
* @param bool $clearCache |
276
|
|
|
* @return bool|int|null|void |
277
|
|
|
*/ |
278
|
|
|
public function save($fire_events = false, $clearCache = false) |
279
|
|
|
{ |
280
|
|
|
if ($this->get('email') == '' || $this->get('username') == '' || $this->get('password') == '') { |
281
|
|
|
$this->log['EmptyPKField'] = 'Email, username or password is empty <pre>' . print_r( |
282
|
|
|
$this->toArray(), |
283
|
|
|
true |
284
|
|
|
) . '</pre>'; |
285
|
|
|
|
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
if ($this->isChanged('username') && ! $this->checkUnique('web_users', 'username')) { |
290
|
|
|
$this->log['UniqueUsername'] = 'username not unique <pre>' . print_r( |
291
|
|
|
$this->get('username'), |
292
|
|
|
true |
293
|
|
|
) . '</pre>'; |
294
|
|
|
|
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if ($this->isChanged('username') && ! $this->checkUnique('web_user_attributes', 'email', 'internalKey')) { |
299
|
|
|
$this->log['UniqueEmail'] = 'Email not unique <pre>' . print_r($this->get('email'), true) . '</pre>'; |
300
|
|
|
|
301
|
|
|
return false; |
302
|
|
|
} |
303
|
|
|
$this->set('sessionid', ''); |
304
|
|
|
$fld = $this->toArray(); |
305
|
|
|
foreach ($this->default_field['user'] as $key => $value) { |
306
|
|
|
$tmp = $this->get($key); |
307
|
|
|
if ($this->newDoc && (!is_int($tmp) && $tmp == '')) { |
|
|
|
|
308
|
|
|
$this->field[$key] = $value; |
309
|
|
|
} |
310
|
|
|
$this->Uset($key, 'user'); |
311
|
|
|
unset($fld[$key]); |
312
|
|
|
} |
313
|
|
|
if (! empty($this->set['user'])) { |
314
|
|
|
if ($this->newDoc) { |
315
|
|
|
$SQL = "INSERT into {$this->makeTable('web_users')} SET " . implode(', ', $this->set['user']); |
316
|
|
|
} else { |
317
|
|
|
$SQL = "UPDATE {$this->makeTable('web_users')} SET " . implode( |
318
|
|
|
', ', |
319
|
|
|
$this->set['user'] |
320
|
|
|
) . " WHERE id = " . $this->id; |
321
|
|
|
} |
322
|
|
|
$this->query($SQL); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
if ($this->newDoc) { |
326
|
|
|
$this->id = $this->modx->db->getInsertId(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
$this->saveQuery($fld); |
330
|
|
|
unset($fld['id']); |
331
|
|
|
|
332
|
|
|
foreach ($fld as $key => $value) { |
333
|
|
|
if ($value == '' || ! $this->isChanged($key)) { |
334
|
|
|
continue; |
335
|
|
|
} |
336
|
|
|
$result = $this->query("SELECT `setting_value` FROM {$this->makeTable('web_user_settings')} WHERE `webuser` = '{$this->id}' AND `setting_name` = '{$key}'"); |
337
|
|
|
if ($this->modx->db->getRecordCount($result) > 0) { |
338
|
|
|
$this->query("UPDATE {$this->makeTable('web_user_settings')} SET `setting_value` = '{$value}' WHERE `webuser` = '{$this->id}' AND `setting_name` = '{$key}';"); |
339
|
|
|
} else { |
340
|
|
|
$this->query("INSERT into {$this->makeTable('web_user_settings')} SET `webuser` = {$this->id},`setting_name` = '{$key}',`setting_value` = '{$value}';"); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
if (! $this->newDoc && $this->givenPassword) { |
344
|
|
|
$this->invokeEvent('OnWebChangePassword', array( |
345
|
|
|
'userObj' => $this, |
346
|
|
|
'userid' => $this->id, |
347
|
|
|
'user' => $this->toArray(), |
348
|
|
|
'userpassword' => $this->givenPassword, |
349
|
|
|
'internalKey' => $this->id, |
350
|
|
|
'username' => $this->get('username') |
351
|
|
|
), $fire_events); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
if (! empty($this->groupIds)) { |
355
|
|
|
$this->setUserGroups($this->id, $this->groupIds); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
$this->invokeEvent('OnWebSaveUser', array( |
359
|
|
|
'userObj' => $this, |
360
|
|
|
'mode' => $this->newDoc ? "new" : "upd", |
361
|
|
|
'id' => $this->id, |
362
|
|
|
'user' => $this->toArray() |
363
|
|
|
), $fire_events); |
364
|
|
|
|
365
|
|
|
if ($clearCache) { |
366
|
|
|
$this->clearCache($fire_events); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
return $this->id; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param array $fld |
374
|
|
|
*/ |
375
|
|
|
protected function saveQuery(array &$fld) |
376
|
|
|
{ |
377
|
|
|
foreach ($this->default_field['attribute'] as $key => $value) { |
378
|
|
|
$tmp = $this->get($key); |
379
|
|
|
if ($this->newDoc && (!is_int($tmp) && $tmp == '')) { |
|
|
|
|
380
|
|
|
$this->field[$key] = $value; |
381
|
|
|
} |
382
|
|
|
$this->Uset($key, 'attribute'); |
383
|
|
|
unset($fld[$key]); |
384
|
|
|
} |
385
|
|
|
if (! empty($this->set['attribute'])) { |
386
|
|
|
if ($this->newDoc) { |
387
|
|
|
$this->set('internalKey', $this->id)->Uset('internalKey', 'attribute'); |
388
|
|
|
$SQL = "INSERT into {$this->makeTable('web_user_attributes')} SET " . implode( |
389
|
|
|
', ', |
390
|
|
|
$this->set['attribute'] |
391
|
|
|
); |
392
|
|
|
} else { |
393
|
|
|
$SQL = "UPDATE {$this->makeTable('web_user_attributes')} SET " . implode( |
394
|
|
|
', ', |
395
|
|
|
$this->set['attribute'] |
396
|
|
|
) . " WHERE internalKey = " . $this->getID(); |
397
|
|
|
} |
398
|
|
|
$this->query($SQL); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @param $ids |
404
|
|
|
* @param bool $fire_events |
405
|
|
|
* @return bool|null|void |
406
|
|
|
*/ |
407
|
|
|
public function delete($ids, $fire_events = false) |
408
|
|
|
{ |
409
|
|
|
if ($this->edit($ids)) { |
410
|
|
|
$flag = $this->deleteQuery(); |
411
|
|
|
$this->query("DELETE FROM {$this->makeTable('web_user_settings')} WHERE webuser='{$this->getID()}'"); |
412
|
|
|
$this->query("DELETE FROM {$this->makeTable('web_groups')} WHERE webuser='{$this->getID()}'"); |
413
|
|
|
$this->invokeEvent('OnWebDeleteUser', array( |
414
|
|
|
'userObj' => $this, |
415
|
|
|
'userid' => $this->getID(), |
416
|
|
|
'internalKey' => $this->getID(), |
417
|
|
|
'username' => $this->get('username'), |
418
|
|
|
'timestamp' => time() |
419
|
|
|
), $fire_events); |
420
|
|
|
} else { |
421
|
|
|
$flag = false; |
422
|
|
|
} |
423
|
|
|
$this->close(); |
424
|
|
|
|
425
|
|
|
return $flag; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @return mixed |
430
|
|
|
*/ |
431
|
|
|
protected function deleteQuery() |
432
|
|
|
{ |
433
|
|
|
return $this->query(" |
434
|
|
|
DELETE user,attribute FROM {$this->makeTable('web_user_attributes')} as attribute |
435
|
|
|
LEFT JOIN {$this->makeTable('web_users')} as user ON user.id=attribute.internalKey |
436
|
|
|
WHERE attribute.internalKey='{$this->escape($this->getID())}'"); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @param int $id |
441
|
|
|
* @param bool|integer $fulltime |
442
|
|
|
* @param string $cookieName |
443
|
|
|
* @param bool $fire_events |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
|
|
public function authUser($id = 0, $fulltime = true, $cookieName = 'WebLoginPE', $fire_events = false) |
447
|
|
|
{ |
448
|
|
|
$flag = false; |
449
|
|
|
if (null === $this->getID() && $id) { |
450
|
|
|
$this->edit($id); |
451
|
|
|
} |
452
|
|
|
if (null !== $this->getID()) { |
453
|
|
|
$flag = true; |
454
|
|
|
$this->save(false); |
455
|
10 |
|
$this->SessionHandler('start', $cookieName, $fulltime); |
456
|
|
|
$this->invokeEvent("OnWebLogin", array( |
457
|
10 |
|
'userObj' => $this, |
458
|
10 |
|
'userid' => $this->getID(), |
459
|
10 |
|
'username' => $this->get('username'), |
460
|
|
|
'userpassword' => $this->givenPassword, |
461
|
|
|
'rememberme' => $fulltime |
462
|
10 |
|
), $fire_events); |
463
|
|
|
} |
464
|
|
|
|
465
|
10 |
|
return $flag; |
466
|
|
|
} |
467
|
10 |
|
|
468
|
10 |
|
/** |
469
|
10 |
|
* @param int $id |
470
|
10 |
|
* @return bool |
471
|
10 |
|
*/ |
472
|
|
|
public function checkBlock($id = 0) |
473
|
10 |
|
{ |
474
|
|
|
if ($this->getID()) { |
|
|
|
|
475
|
|
|
$tmp = clone $this; |
476
|
|
|
} else { |
477
|
|
|
$tmp = $this; |
478
|
|
|
} |
479
|
|
|
if ($id && $tmp->getUserId($id) != $id) { |
480
|
|
|
$tmp->edit($id); |
481
|
|
|
} |
482
|
|
|
$now = time(); |
483
|
|
|
|
484
|
|
|
$b = $tmp->get('blocked'); |
485
|
|
|
$bu = $tmp->get('blockeduntil'); |
486
|
|
|
$ba = $tmp->get('blockedafter'); |
487
|
|
|
$flag = (($b && ! $bu && ! $ba) || ($bu && $now < $bu) || ($ba && $now > $ba)); |
488
|
|
|
unset($tmp); |
489
|
|
|
|
490
|
|
|
return $flag; |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* @param $id |
495
|
|
|
* @param $password |
496
|
|
|
* @param $blocker |
497
|
|
|
* @param bool $fire_events |
498
|
|
|
* @return bool |
499
|
|
|
*/ |
500
|
|
|
public function testAuth($id, $password, $blocker, $fire_events = false) |
501
|
|
|
{ |
502
|
|
|
if ($this->getID()) { |
|
|
|
|
503
|
|
|
$tmp = clone $this; |
504
|
|
|
} else { |
505
|
|
|
$tmp = $this; |
506
|
|
|
} |
507
|
|
|
if ($id && $tmp->getUserId($id) != $id) { |
508
|
|
|
$tmp->edit($id); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
$flag = $pluginFlag = false; |
512
|
|
|
if ((null !== $tmp->getID()) && (! $blocker || ($blocker && ! $tmp->checkBlock($id))) |
513
|
|
|
) { |
514
|
|
|
$eventResult = $this->getInvokeEventResult('OnWebAuthentication', array( |
515
|
|
|
'userObj' => $this, |
516
|
|
|
'userid' => $tmp->getID(), |
517
|
|
|
'username' => $tmp->get('username'), |
518
|
|
|
'userpassword' => $password, |
519
|
|
|
'savedpassword' => $tmp->get('password') |
520
|
|
|
), $fire_events); |
521
|
|
|
if (is_array($eventResult)) { |
522
|
|
|
foreach ($eventResult as $result) { |
523
|
|
|
$pluginFlag = (bool)$result; |
524
|
|
|
} |
525
|
|
|
} else { |
526
|
|
|
$pluginFlag = (bool)$eventResult; |
527
|
|
|
} |
528
|
|
|
if (! $pluginFlag) { |
529
|
|
|
$flag = ($tmp->get('password') == $tmp->getPassword($password)); |
530
|
|
|
} |
531
|
|
|
} |
532
|
|
|
unset($tmp); |
533
|
|
|
|
534
|
|
|
return $flag || $pluginFlag; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
/** |
538
|
|
|
* @param bool|integer $fulltime |
539
|
|
|
* @param string $cookieName |
540
|
|
|
* @return bool |
541
|
|
|
*/ |
542
|
|
|
public function AutoLogin($fulltime = true, $cookieName = 'WebLoginPE', $fire_events = null) |
|
|
|
|
543
|
|
|
{ |
544
|
|
|
$flag = false; |
545
|
|
|
if (isset($_COOKIE[$cookieName])) { |
546
|
|
|
$cookie = explode('|', $_COOKIE[$cookieName], 4); |
547
|
|
|
if (isset($cookie[0], $cookie[1], $cookie[2]) && strlen($cookie[0]) == 32 && strlen($cookie[1]) == 32) { |
548
|
|
|
if (! $fulltime && isset($cookie[4])) { |
549
|
|
|
$fulltime = (int)$cookie[4]; |
550
|
|
|
} |
551
|
|
|
$this->close(); |
552
|
|
|
$q = $this->modx->db->query("SELECT id FROM " . $this->makeTable('web_users') . " WHERE md5(username)='{$this->escape($cookie[0])}'"); |
553
|
|
|
$id = $this->modx->db->getValue($q); |
554
|
|
|
if ($this->edit($id) |
555
|
|
|
&& null !== $this->getID() |
556
|
|
|
&& $this->get('password') == $cookie[1] |
557
|
|
|
&& $this->get('sessionid') == $cookie[2] |
558
|
|
|
&& ! $this->checkBlock($this->getID()) |
559
|
|
|
) { |
560
|
|
|
$flag = $this->authUser($this->getID(), $fulltime, $cookieName, $fire_events); |
561
|
|
|
} |
562
|
|
|
} |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return $flag; |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* @param string $cookieName |
570
|
|
|
* @param bool $fire_events |
571
|
|
|
*/ |
572
|
|
|
public function logOut($cookieName = 'WebLoginPE', $fire_events = false) |
573
|
|
|
{ |
574
|
|
|
if (! $uid = $this->modx->getLoginUserID('web')) { |
|
|
|
|
575
|
|
|
return; |
576
|
|
|
} |
577
|
|
|
$params = array( |
578
|
|
|
'username' => $_SESSION['webShortname'], |
579
|
|
|
'internalKey' => $uid, |
580
|
|
|
'userid' => $uid // Bugfix by TS |
581
|
|
|
); |
582
|
|
|
$this->invokeEvent('OnBeforeWebLogout', $params, $fire_events); |
583
|
|
|
$this->SessionHandler('destroy', $cookieName ? $cookieName : 'WebLoginPE'); |
584
|
|
|
$this->invokeEvent('OnWebLogout', $params, $fire_events); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* SessionHandler |
589
|
|
|
* Starts the user session on login success. Destroys session on error or logout. |
590
|
|
|
* |
591
|
|
|
* @param string $directive ('start' or 'destroy') |
592
|
|
|
* @param string $cookieName |
593
|
|
|
* @param bool|integer $remember |
594
|
|
|
* @return modUsers |
595
|
|
|
* @author Raymond Irving |
596
|
|
|
* @author Scotty Delicious |
597
|
|
|
* |
598
|
|
|
* remeber может быть числом в секундах |
599
|
|
|
*/ |
600
|
|
|
protected function SessionHandler($directive, $cookieName, $remember = true) |
|
|
|
|
601
|
|
|
{ |
602
|
|
|
switch ($directive) { |
603
|
|
|
case 'start': |
604
|
|
|
if ($this->getID() !== null) { |
605
|
|
|
$_SESSION['webShortname'] = $this->get('username'); |
606
|
|
|
$_SESSION['webFullname'] = $this->get('fullname'); |
607
|
|
|
$_SESSION['webEmail'] = $this->get('email'); |
608
|
|
|
$_SESSION['webValidated'] = 1; |
609
|
|
|
$_SESSION['webInternalKey'] = $this->getID(); |
610
|
|
|
$_SESSION['webValid'] = base64_encode($this->get('password')); |
611
|
|
|
$_SESSION['webUser'] = base64_encode($this->get('username')); |
612
|
|
|
$_SESSION['webFailedlogins'] = $this->get('failedlogincount'); |
613
|
|
|
$_SESSION['webLastlogin'] = $this->get('lastlogin'); |
614
|
|
|
$_SESSION['webnrlogins'] = $this->get('logincount'); |
615
|
|
|
$_SESSION['webUsrConfigSet'] = array(); |
616
|
|
|
$_SESSION['webUserGroupNames'] = $this->getUserGroups(); |
617
|
|
|
$_SESSION['webDocgroups'] = $this->getDocumentGroups(); |
618
|
|
|
if (! empty($remember)) { |
619
|
|
|
$this->setAutoLoginCookie($cookieName, $remember); |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
break; |
623
|
|
|
case 'destroy': |
624
|
|
|
if (isset($_SESSION['mgrValidated'])) { |
625
|
|
|
unset($_SESSION['webShortname']); |
626
|
|
|
unset($_SESSION['webFullname']); |
627
|
|
|
unset($_SESSION['webEmail']); |
628
|
|
|
unset($_SESSION['webValidated']); |
629
|
|
|
unset($_SESSION['webInternalKey']); |
630
|
|
|
unset($_SESSION['webValid']); |
631
|
|
|
unset($_SESSION['webUser']); |
632
|
|
|
unset($_SESSION['webFailedlogins']); |
633
|
|
|
unset($_SESSION['webLastlogin']); |
634
|
|
|
unset($_SESSION['webnrlogins']); |
635
|
|
|
unset($_SESSION['webUsrConfigSet']); |
636
|
|
|
unset($_SESSION['webUserGroupNames']); |
637
|
|
|
unset($_SESSION['webDocgroups']); |
638
|
|
|
|
639
|
|
|
setcookie($cookieName, '', time() - 60, MODX_BASE_URL); |
640
|
|
|
} else { |
641
|
|
|
if (isset($_COOKIE[session_name()])) { |
642
|
|
|
setcookie(session_name(), '', time() - 60, MODX_BASE_URL); |
643
|
|
|
} |
644
|
|
|
setcookie($cookieName, '', time() - 60, MODX_BASE_URL); |
645
|
|
|
session_destroy(); |
646
|
|
|
} |
647
|
|
|
break; |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
return $this; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* @return bool |
655
|
|
|
*/ |
656
|
|
|
public function isSecure() |
657
|
|
|
{ |
658
|
|
|
$out = $this->modxConfig('server_protocol') == 'http' ? false : true; |
659
|
|
|
|
660
|
|
|
return $out; |
661
|
|
|
} |
662
|
|
|
|
663
|
|
|
/** |
664
|
|
|
* @param $cookieName |
665
|
|
|
* @param bool|integer $remember |
666
|
|
|
* @return $this |
667
|
|
|
*/ |
668
|
|
|
public function setAutoLoginCookie($cookieName, $remember = true) |
669
|
|
|
{ |
670
|
|
|
if (! empty($cookieName) && $this->getID() !== null) { |
671
|
|
|
$secure = $this->isSecure(); |
672
|
|
|
$remember = is_bool($remember) ? $this->getRememberTime() : (int)$remember; |
673
|
|
|
$cookieValue = array(md5($this->get('username')), $this->get('password'), $this->get('sessionid'), $remember); |
674
|
|
|
$cookieValue = implode('|', $cookieValue); |
675
|
|
|
$cookieExpires = time() + $remember; |
676
|
|
|
setcookie($cookieName, $cookieValue, $cookieExpires, MODX_BASE_URL, '', $secure, true); |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
return $this; |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
/** |
683
|
|
|
* @param int $userID |
684
|
|
|
* @return array |
685
|
|
|
*/ |
686
|
|
|
public function getDocumentGroups($userID = 0) |
687
|
|
|
{ |
688
|
|
|
$out = array(); |
689
|
|
|
$user = $this->switchObject($userID); |
690
|
|
|
if (null !== $user->getID()) { |
691
|
|
|
$web_groups = $this->modx->getFullTableName('web_groups'); |
692
|
|
|
$webgroup_access = $this->modx->getFullTableName('webgroup_access'); |
693
|
|
|
|
694
|
|
|
$sql = "SELECT `uga`.`documentgroup` FROM {$web_groups} as `ug` |
695
|
|
|
INNER JOIN {$webgroup_access} as `uga` ON `uga`.`webgroup`=`ug`.`webgroup` |
696
|
|
|
WHERE `ug`.`webuser` = " . $user->getID(); |
697
|
|
|
$out = $this->modx->db->getColumn('documentgroup', $this->query($sql)); |
698
|
|
|
} |
699
|
|
|
unset($user); |
700
|
|
|
|
701
|
|
|
return $out; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
/** |
705
|
|
|
* @param int $userID |
706
|
|
|
* @return array |
707
|
|
|
*/ |
708
|
|
|
public function getUserGroups($userID = 0) |
709
|
|
|
{ |
710
|
|
|
$out = array(); |
711
|
|
|
$user = $this->switchObject($userID); |
712
|
|
|
if (null !== $user->getID()) { |
713
|
|
|
$web_groups = $this->makeTable('web_groups'); |
714
|
|
|
$webgroup_names = $this->makeTable('webgroup_names'); |
715
|
|
|
|
716
|
|
|
$rs = $this->query("SELECT `ug`.`webgroup`, `ugn`.`name` FROM {$web_groups} as `ug` |
717
|
|
|
INNER JOIN {$webgroup_names} as `ugn` ON `ugn`.`id`=`ug`.`webgroup` |
718
|
|
|
WHERE `ug`.`webuser` = " . $user->getID()); |
719
|
|
|
while ($row = $this->modx->db->getRow($rs)) { |
720
|
|
|
$out[$row['webgroup']] = $row['name']; |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
unset($user); |
724
|
|
|
|
725
|
|
|
return $out; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
/** |
729
|
|
|
* @param int $userID |
730
|
|
|
* @param array $groupIds |
731
|
|
|
* @return $this |
732
|
|
|
*/ |
733
|
|
|
public function setUserGroups($userID = 0, $groupIds = array()) |
734
|
|
|
{ |
735
|
|
|
if (!is_array($groupIds)) { |
|
|
|
|
736
|
|
|
return $this; |
737
|
|
|
} |
738
|
|
|
if ($this->newDoc && $userID == 0) { |
739
|
|
|
$this->groupIds = $groupIds; |
740
|
|
|
} else { |
741
|
|
|
$user = $this->switchObject($userID); |
742
|
|
|
if ($uid = $user->getID()) { |
743
|
|
|
foreach ($groupIds as $gid) { |
744
|
|
|
$this->query("REPLACE INTO {$this->makeTable('web_groups')} (`webgroup`, `webuser`) VALUES ('{$gid}', '{$uid}')"); |
745
|
|
|
} |
746
|
|
|
if (! $this->newDoc) { |
747
|
|
|
$groupIds = empty($groupIds) ? '0' : implode(',', $groupIds); |
748
|
|
|
$this->query("DELETE FROM {$this->makeTable('web_groups')} WHERE `webuser`={$uid} AND `webgroup` NOT IN ({$groupIds})"); |
749
|
|
|
} |
750
|
|
|
} |
751
|
|
|
unset($user); |
752
|
|
|
$this->groupIds = array(); |
753
|
|
|
} |
754
|
|
|
|
755
|
|
|
return $this; |
756
|
|
|
} |
757
|
|
|
} |
758
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.