Completed
Push — master ( 257868...d22069 )
by
unknown
05:00
created
assets/lib/MODxAPI/modUsers.php 1 patch
Braces   +102 added lines, -98 removed lines patch added patch discarded remove patch
@@ -4,8 +4,8 @@  discard block
 block discarded – undo
4 4
 /**
5 5
  * Class modUsers
6 6
  */
7
-class modUsers extends MODxAPI
8
-{
7
+class modUsers extends MODxAPI
8
+{
9 9
     /**
10 10
      * @var array
11 11
      */
@@ -57,8 +57,8 @@  discard block
 block discarded – undo
57 57
      * @param $key
58 58
      * @return bool
59 59
      */
60
-    public function issetField($key)
61
-    {
60
+    public function issetField($key)
61
+    {
62 62
         return (array_key_exists($key, $this->default_field['user']) || array_key_exists($key,
63 63
                 $this->default_field['attribute']) || in_array($key, $this->default_field['hidden']));
64 64
     }
@@ -67,9 +67,9 @@  discard block
 block discarded – undo
67 67
      * @param string $data
68 68
      * @return string|false
69 69
      */
70
-    protected function findUser($data)
71
-    {
72
-        switch (true) {
70
+    protected function findUser($data)
71
+    {
72
+        switch (true) {
73 73
             case (is_int($data) || ((int)$data > 0 && (string)intval($data) === $data)):
74 74
                 $find = 'attribute.internalKey';
75 75
                 break;
@@ -90,8 +90,8 @@  discard block
 block discarded – undo
90 90
      * @param array $data
91 91
      * @return $this
92 92
      */
93
-    public function create($data = array())
94
-    {
93
+    public function create($data = array())
94
+    {
95 95
         parent::create($data);
96 96
         $this->set('createdon', time());
97 97
 
@@ -102,16 +102,16 @@  discard block
 block discarded – undo
102 102
      * @param $id
103 103
      * @return $this
104 104
      */
105
-    public function edit($id)
106
-    {
105
+    public function edit($id)
106
+    {
107 107
         $id = is_scalar($id) ? trim($id) : '';
108
-        if ($this->getID() != $id) {
108
+        if ($this->getID() != $id) {
109 109
             $this->close();
110 110
             $this->newDoc = false;
111 111
 
112
-            if (!$find = $this->findUser($id)) {
112
+            if (!$find = $this->findUser($id)) {
113 113
                 $this->id = null;
114
-            } else {
114
+            } else {
115 115
                 $this->set('editedon', time());
116 116
                 $result = $this->query("
117 117
                     SELECT * from {$this->makeTable('web_user_attributes')} as attribute
@@ -135,10 +135,10 @@  discard block
 block discarded – undo
135 135
      * @param $value
136 136
      * @return $this
137 137
      */
138
-    public function set($key, $value)
139
-    {
140
-        if (is_scalar($value) && is_scalar($key) && !empty($key)) {
141
-            switch ($key) {
138
+    public function set($key, $value)
139
+    {
140
+        if (is_scalar($value) && is_scalar($key) && !empty($key)) {
141
+            switch ($key) {
142 142
                 case 'password':
143 143
                     $this->givenPassword = $value;
144 144
                     $value = $this->getPassword($value);
@@ -162,8 +162,8 @@  discard block
 block discarded – undo
162 162
      * @param $pass
163 163
      * @return string
164 164
      */
165
-    public function getPassword($pass)
166
-    {
165
+    public function getPassword($pass)
166
+    {
167 167
         return md5($pass);
168 168
     }
169 169
 
@@ -172,83 +172,83 @@  discard block
 block discarded – undo
172 172
      * @param bool $clearCache
173 173
      * @return bool|int|null|void
174 174
      */
175
-    public function save($fire_events = false, $clearCache = false)
176
-    {
177
-        if ($this->get('email') == '' || $this->get('username') == '' || $this->get('password') == '') {
175
+    public function save($fire_events = false, $clearCache = false)
176
+    {
177
+        if ($this->get('email') == '' || $this->get('username') == '' || $this->get('password') == '') {
178 178
             $this->log['EmptyPKField'] = 'Email, username or password is empty <pre>' . print_r($this->toArray(),
179 179
                     true) . '</pre>';
180 180
 
181 181
             return false;
182 182
         }
183 183
 
184
-        if (!$this->checkUnique('web_users', 'username')) {
184
+        if (!$this->checkUnique('web_users', 'username')) {
185 185
             $this->log['UniqueUsername'] = 'username not unique <pre>' . print_r($this->get('username'),
186 186
                     true) . '</pre>';
187 187
 
188 188
             return false;
189 189
         }
190 190
 
191
-        if (!$this->checkUnique('web_user_attributes', 'email', 'internalKey')) {
191
+        if (!$this->checkUnique('web_user_attributes', 'email', 'internalKey')) {
192 192
             $this->log['UniqueEmail'] = 'Email not unique <pre>' . print_r($this->get('email'), true) . '</pre>';
193 193
 
194 194
             return false;
195 195
         }
196 196
         $this->set('sessionid', '');
197 197
         $fld = $this->toArray();
198
-        foreach ($this->default_field['user'] as $key => $value) {
198
+        foreach ($this->default_field['user'] as $key => $value) {
199 199
             $tmp = $this->get($key);
200
-            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
200
+            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
201 201
                 $this->field[$key] = $value;
202 202
             }
203 203
             $this->Uset($key, 'user');
204 204
             unset($fld[$key]);
205 205
         }
206
-        if (!empty($this->set['user'])) {
207
-            if ($this->newDoc) {
206
+        if (!empty($this->set['user'])) {
207
+            if ($this->newDoc) {
208 208
                 $SQL = "INSERT into {$this->makeTable('web_users')} SET " . implode(', ', $this->set['user']);
209
-            } else {
209
+            } else {
210 210
                 $SQL = "UPDATE {$this->makeTable('web_users')} SET " . implode(', ',
211 211
                         $this->set['user']) . " WHERE id = " . $this->id;
212 212
             }
213 213
             $this->query($SQL);
214 214
         }
215 215
 
216
-        if ($this->newDoc) {
216
+        if ($this->newDoc) {
217 217
             $this->id = $this->modx->db->getInsertId();
218 218
         }
219 219
 
220
-        foreach ($this->default_field['attribute'] as $key => $value) {
220
+        foreach ($this->default_field['attribute'] as $key => $value) {
221 221
             $tmp = $this->get($key);
222
-            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
222
+            if ($this->newDoc && (!is_int($tmp) && $tmp == '')) {
223 223
                 $this->field[$key] = $value;
224 224
             }
225 225
             $this->Uset($key, 'attribute');
226 226
             unset($fld[$key]);
227 227
         }
228
-        if (!empty($this->set['attribute'])) {
229
-            if ($this->newDoc) {
228
+        if (!empty($this->set['attribute'])) {
229
+            if ($this->newDoc) {
230 230
                 $this->set('internalKey', $this->id)->Uset('internalKey', 'attribute');
231 231
                 $SQL = "INSERT into {$this->makeTable('web_user_attributes')} SET " . implode(', ',
232 232
                         $this->set['attribute']);
233
-            } else {
233
+            } else {
234 234
                 $SQL = "UPDATE {$this->makeTable('web_user_attributes')} SET " . implode(', ',
235 235
                         $this->set['attribute']) . " WHERE  internalKey = " . $this->getID();
236 236
             }
237 237
             $this->query($SQL);
238 238
         }
239 239
         unset($fld['id']);
240
-        foreach ($fld as $key => $value) {
241
-            if ($value == '' || !$this->isChanged($key)) {
240
+        foreach ($fld as $key => $value) {
241
+            if ($value == '' || !$this->isChanged($key)) {
242 242
                 continue;
243 243
             }
244 244
             $result = $this->query("SELECT `setting_value` FROM {$this->makeTable('web_user_settings')} WHERE `webuser` = '{$this->id}' AND `setting_name` = '{$key}'");
245
-            if ($this->modx->db->getRecordCount($result) > 0) {
245
+            if ($this->modx->db->getRecordCount($result) > 0) {
246 246
                 $this->query("UPDATE {$this->makeTable('web_user_settings')} SET `setting_value` = '{$value}' WHERE `webuser` = '{$this->id}' AND `setting_name` = '{$key}';");
247
-            } else {
247
+            } else {
248 248
                 $this->query("INSERT into {$this->makeTable('web_user_settings')} SET `webuser` = {$this->id},`setting_name` = '{$key}',`setting_value` = '{$value}';");
249 249
             }
250 250
         }
251
-        if (!$this->newDoc && $this->givenPassword) {
251
+        if (!$this->newDoc && $this->givenPassword) {
252 252
             $this->invokeEvent('OnWebChangePassword', array(
253 253
                 'userObj'      => $this,
254 254
                 'userid'       => $this->id,
@@ -259,7 +259,9 @@  discard block
 block discarded – undo
259 259
             ), $fire_events);
260 260
         }
261 261
         
262
-        if ($this->groupIds) $this->setUserGroups($this->id, $this->groupIds);
262
+        if ($this->groupIds) {
263
+            $this->setUserGroups($this->id, $this->groupIds);
264
+        }
263 265
         
264 266
         $this->invokeEvent('OnWebSaveUser', array(
265 267
             'userObj' => $this,
@@ -268,7 +270,7 @@  discard block
 block discarded – undo
268 270
             'user'    => $this->toArray()
269 271
         ), $fire_events);
270 272
 
271
-        if ($clearCache) {
273
+        if ($clearCache) {
272 274
             $this->clearCache($fire_events);
273 275
         }
274 276
 
@@ -280,9 +282,9 @@  discard block
 block discarded – undo
280 282
      * @param bool $fire_events
281 283
      * @return bool|null|void
282 284
      */
283
-    public function delete($ids, $fire_events = false)
284
-    {
285
-        if ($this->edit($ids)) {
285
+    public function delete($ids, $fire_events = false)
286
+    {
287
+        if ($this->edit($ids)) {
286 288
             $flag = $this->query("
287 289
           DELETE user,attribute FROM {$this->makeTable('web_user_attributes')} as attribute
288 290
             LEFT JOIN {$this->makeTable('web_users')} as user ON user.id=attribute.internalKey
@@ -296,7 +298,7 @@  discard block
 block discarded – undo
296 298
                 'username'    => $this->get('username'),
297 299
                 'timestamp'   => time()
298 300
             ), $fire_events);
299
-        } else {
301
+        } else {
300 302
             $flag = false;
301 303
         }
302 304
         $this->close();
@@ -311,13 +313,13 @@  discard block
 block discarded – undo
311 313
      * @param bool $fire_events
312 314
      * @return bool
313 315
      */
314
-    public function authUser($id = 0, $fulltime = true, $cookieName = 'WebLoginPE', $fire_events = false)
315
-    {
316
+    public function authUser($id = 0, $fulltime = true, $cookieName = 'WebLoginPE', $fire_events = false)
317
+    {
316 318
         $flag = false;
317
-        if (null === $this->getID() && $id) {
319
+        if (null === $this->getID() && $id) {
318 320
             $this->edit($id);
319 321
         }
320
-        if (null !== $this->getID()) {
322
+        if (null !== $this->getID()) {
321 323
             $flag = true;
322 324
             $this->save(false);
323 325
             $this->SessionHandler('start', $cookieName, $fulltime);
@@ -337,10 +339,10 @@  discard block
 block discarded – undo
337 339
      * @param int $id
338 340
      * @return bool
339 341
      */
340
-    public function checkBlock($id = 0)
341
-    {
342
+    public function checkBlock($id = 0)
343
+    {
342 344
         $tmp = clone $this;
343
-        if ($id && $tmp->getID() != $id) {
345
+        if ($id && $tmp->getID() != $id) {
344 346
             $tmp->edit($id);
345 347
         }
346 348
         $now = time();
@@ -361,17 +363,17 @@  discard block
 block discarded – undo
361 363
      * @param bool $fire_events
362 364
      * @return bool
363 365
      */
364
-    public function testAuth($id, $password, $blocker, $fire_events = false)
365
-    {
366
+    public function testAuth($id, $password, $blocker, $fire_events = false)
367
+    {
366 368
         $tmp = clone $this;
367
-        if ($id && $tmp->getID() != $id) {
369
+        if ($id && $tmp->getID() != $id) {
368 370
             $tmp->edit($id);
369 371
         }
370 372
 
371 373
         $flag = $pluginFlag = false;
372 374
         if (
373 375
             (null !== $tmp->getID()) && (!$blocker || ($blocker && !$tmp->checkBlock($id)))
374
-        ) {
376
+        ) {
375 377
             $eventResult = $this->getInvokeEventResult('OnWebAuthentication', array(
376 378
                 'userObj'       => $this,
377 379
                 'userid'        => $tmp->getID(),
@@ -379,14 +381,14 @@  discard block
 block discarded – undo
379 381
                 'userpassword'  => $password,
380 382
                 'savedpassword' => $tmp->get('password')
381 383
             ), $fire_events);
382
-            if (is_array($eventResult)) {
383
-                foreach ($eventResult as $result) {
384
+            if (is_array($eventResult)) {
385
+                foreach ($eventResult as $result) {
384 386
                     $pluginFlag = (bool)$result;
385 387
                 }
386
-            } else {
388
+            } else {
387 389
                 $pluginFlag = (bool)$eventResult;
388 390
             }
389
-            if (!$pluginFlag) {
391
+            if (!$pluginFlag) {
390 392
                 $flag = ($tmp->get('password') == $tmp->getPassword($password));
391 393
             }
392 394
         }
@@ -400,12 +402,12 @@  discard block
 block discarded – undo
400 402
      * @param string $cookieName
401 403
      * @return bool
402 404
      */
403
-    public function AutoLogin($fulltime = true, $cookieName = 'WebLoginPE', $fire_events = null)
404
-    {
405
+    public function AutoLogin($fulltime = true, $cookieName = 'WebLoginPE', $fire_events = null)
406
+    {
405 407
         $flag = false;
406
-        if (isset($_COOKIE[$cookieName])) {
408
+        if (isset($_COOKIE[$cookieName])) {
407 409
             $cookie = explode('|', $_COOKIE[$cookieName], 3);
408
-            if (isset($cookie[0], $cookie[1], $cookie[2]) && strlen($cookie[0]) == 32 && strlen($cookie[1]) == 32) {
410
+            if (isset($cookie[0], $cookie[1], $cookie[2]) && strlen($cookie[0]) == 32 && strlen($cookie[1]) == 32) {
409 411
                 $this->close();
410 412
                 $q = $this->modx->db->query("SELECT id FROM " . $this->makeTable('web_users') . " WHERE md5(username)='{$this->escape($cookie[0])}'");
411 413
                 $id = $this->modx->db->getValue($q);
@@ -415,7 +417,7 @@  discard block
 block discarded – undo
415 417
                     && $this->get('password') == $cookie[1] 
416 418
                     && $this->get('sessionid') == $cookie[2] 
417 419
                     && !$this->checkBlock($this->getID())
418
-                ) {
420
+                ) {
419 421
                     $flag = $this->authUser($this->getID(), $fulltime, $cookieName, $fire_events);
420 422
 
421 423
                 }
@@ -429,9 +431,9 @@  discard block
 block discarded – undo
429 431
      * @param string $cookieName
430 432
      * @param null $fire_events
431 433
      */
432
-    public function logOut($cookieName = 'WebLoginPE', $fire_events = null)
433
-    {
434
-        if (!$uid = $this->modx->getLoginUserID('web')) {
434
+    public function logOut($cookieName = 'WebLoginPE', $fire_events = null)
435
+    {
436
+        if (!$uid = $this->modx->getLoginUserID('web')) {
435 437
             return;
436 438
         }
437 439
         $params = array(
@@ -456,11 +458,11 @@  discard block
 block discarded – undo
456 458
      *
457 459
      * remeber может быть числом в секундах
458 460
      */
459
-    protected function SessionHandler($directive, $cookieName, $remember = true)
460
-    {
461
-        switch ($directive) {
461
+    protected function SessionHandler($directive, $cookieName, $remember = true)
462
+    {
463
+        switch ($directive) {
462 464
             case 'start':
463
-                if ($this->getID()) {
465
+                if ($this->getID()) {
464 466
                     $_SESSION['webShortname'] = $this->get('username');
465 467
                     $_SESSION['webFullname'] = $this->get('fullname');
466 468
                     $_SESSION['webEmail'] = $this->get('email');
@@ -474,13 +476,13 @@  discard block
 block discarded – undo
474 476
                     $_SESSION['webUsrConfigSet'] = array();
475 477
                     $_SESSION['webUserGroupNames'] = $this->getUserGroups();
476 478
                     $_SESSION['webDocgroups'] = $this->getDocumentGroups();
477
-                    if ($remember) {
479
+                    if ($remember) {
478 480
                         $this->setAutoLoginCookie($cookieName, $remember);
479 481
                     }
480 482
                 }
481 483
                 break;
482 484
             case 'destroy':
483
-                if (isset($_SESSION['mgrValidated'])) {
485
+                if (isset($_SESSION['mgrValidated'])) {
484 486
                     unset($_SESSION['webShortname']);
485 487
                     unset($_SESSION['webFullname']);
486 488
                     unset($_SESSION['webEmail']);
@@ -496,8 +498,8 @@  discard block
 block discarded – undo
496 498
                     unset($_SESSION['webDocgroups']);
497 499
 
498 500
                     setcookie($cookieName, '', time() - 60, '/');
499
-                } else {
500
-                    if (isset($_COOKIE[session_name()])) {
501
+                } else {
502
+                    if (isset($_COOKIE[session_name()])) {
501 503
                         setcookie(session_name(), '', time() - 60, '/');
502 504
                     }
503 505
                     setcookie($cookieName, '', time() - 60, '/');
@@ -512,8 +514,8 @@  discard block
 block discarded – undo
512 514
     /**
513 515
      * @return bool
514 516
      */
515
-    public function isSecure()
516
-    {
517
+    public function isSecure()
518
+    {
517 519
         $out = $this->modxConfig('server_protocol') == 'http' ? false : true;
518 520
 
519 521
         return $out;
@@ -524,9 +526,9 @@  discard block
 block discarded – undo
524 526
      * @param bool $remember
525 527
      * @return $this
526 528
      */
527
-    public function setAutoLoginCookie($cookieName, $remember = true)
528
-    {
529
-        if (!empty($cookieName) && $this->getID()) {
529
+    public function setAutoLoginCookie($cookieName, $remember = true)
530
+    {
531
+        if (!empty($cookieName) && $this->getID()) {
530 532
             $secure = $this->isSecure();
531 533
             $remember = is_bool($remember) ? (60 * 60 * 24 * 365 * 5) : (int)$remember;
532 534
             $cookieValue = array(md5($this->get('username')), $this->get('password'), $this->get('sessionid'), $remember);
@@ -542,11 +544,11 @@  discard block
 block discarded – undo
542 544
      * @param int $userID
543 545
      * @return array
544 546
      */
545
-    public function getDocumentGroups($userID = 0)
546
-    {
547
+    public function getDocumentGroups($userID = 0)
548
+    {
547 549
         $out = array();
548 550
         $user = $this->switchObject($userID);
549
-        if (null !== $user->getID()) {
551
+        if (null !== $user->getID()) {
550 552
             $web_groups = $this->modx->getFullTableName('web_groups');
551 553
             $webgroup_access = $this->modx->getFullTableName('webgroup_access');
552 554
 
@@ -565,18 +567,18 @@  discard block
 block discarded – undo
565 567
      * @param int $userID
566 568
      * @return array
567 569
      */
568
-    public function getUserGroups($userID = 0)
569
-    {
570
+    public function getUserGroups($userID = 0)
571
+    {
570 572
         $out = array();
571 573
         $user = $this->switchObject($userID);
572
-        if (null !== $user->getID()) {
574
+        if (null !== $user->getID()) {
573 575
             $web_groups = $this->makeTable('web_groups');
574 576
             $webgroup_names = $this->makeTable('webgroup_names');
575 577
 
576 578
             $rs = $this->query("SELECT `ug`.`webgroup`, `ugn`.`name` FROM {$web_groups} as `ug`
577 579
                 INNER JOIN {$webgroup_names} as `ugn` ON `ugn`.`id`=`ug`.`webgroup`
578 580
                 WHERE `ug`.`webuser` = " . $user->getID());
579
-            while ($row = $this->modx->db->getRow($rs)) {
581
+            while ($row = $this->modx->db->getRow($rs)) {
580 582
                 $out[$row['webgroup']] = $row['name'];
581 583
             }
582 584
         }
@@ -590,18 +592,20 @@  discard block
 block discarded – undo
590 592
      * @param array $groupIds
591 593
      * @return $this
592 594
      */
593
-    public function setUserGroups($userID = 0, $groupIds = array())
594
-    {
595
-        if (!is_array($groupIds)) return $this;
596
-        if ($this->newDoc && $userID == 0) {
595
+    public function setUserGroups($userID = 0, $groupIds = array())
596
+    {
597
+        if (!is_array($groupIds)) {
598
+            return $this;
599
+        }
600
+        if ($this->newDoc && $userID == 0) {
597 601
             $this->groupIds = $groupIds;
598
-        } else {
602
+        } else {
599 603
             $user = $this->switchObject($userID);
600
-            if ($uid = $user->getID()) {
601
-                foreach ($groupIds as $gid) {
604
+            if ($uid = $user->getID()) {
605
+                foreach ($groupIds as $gid) {
602 606
                     $this->query("REPLACE INTO {$this->makeTable('web_groups')} (`webgroup`, `webuser`) VALUES ('{$gid}', '{$uid}')");
603 607
                 }
604
-                if (!$this->newDoc) {
608
+                if (!$this->newDoc) {
605 609
                     $groupIds = empty($groupIds) ? '0' : implode(',', $groupIds);
606 610
                     $this->query("DELETE FROM {$this->makeTable('web_groups')} WHERE `webuser`={$uid} AND `webgroup` NOT IN ({$groupIds})");
607 611
                 }
Please login to merge, or discard this patch.