XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
user.php
1<?php
12
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17
18class XoopsUser extends XoopsObject
19{
20
26 public $_groups = [];
31 public $_isAdmin = null;
36 public $_rank = null;
41 public $_isOnline = null;
42
47 public function __construct($id = null)
48 {
49 static $initVars;
50 if (isset($initVars)) {
51 $this->vars = $initVars;
52 } else {
53 $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
54 $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 60);
55 $this->initVar('uname', XOBJ_DTYPE_TXTBOX, null, true, 25);
56 $this->initVar('email', XOBJ_DTYPE_TXTBOX, null, true, 256);
57 $this->initVar('url', XOBJ_DTYPE_TXTBOX, null, false, 100);
58 $this->initVar('user_avatar', XOBJ_DTYPE_TXTBOX, null, false, 30);
59 $this->initVar('user_regdate', XOBJ_DTYPE_INT, null, false);
60 $this->initVar('user_icq', XOBJ_DTYPE_TXTBOX, null, false, 15);
61 $this->initVar('user_from', XOBJ_DTYPE_TXTBOX, null, false, 100);
62 $this->initVar('user_sig', XOBJ_DTYPE_TXTAREA, null, false, null);
63 $this->initVar('user_viewemail', XOBJ_DTYPE_INT, 0, false);
64 $this->initVar('actkey', XOBJ_DTYPE_OTHER, null, false);
65 $this->initVar('user_aim', XOBJ_DTYPE_TXTBOX, null, false, 18);
66 $this->initVar('user_yim', XOBJ_DTYPE_TXTBOX, null, false, 25);
67 $this->initVar('user_msnm', XOBJ_DTYPE_TXTBOX, null, false, 100);
68 $this->initVar('pass', XOBJ_DTYPE_TXTBOX, null, false, 255);
69 $this->initVar('posts', XOBJ_DTYPE_INT, null, false);
70 $this->initVar('attachsig', XOBJ_DTYPE_INT, 0, false);
71 $this->initVar('rank', XOBJ_DTYPE_INT, 0, false);
72 $this->initVar('level', XOBJ_DTYPE_INT, 0, false);
73 $this->initVar('theme', XOBJ_DTYPE_OTHER, null, false);
74 $this->initVar('timezone_offset', XOBJ_DTYPE_OTHER, null, false);
75 $this->initVar('last_login', XOBJ_DTYPE_INT, 0, false);
76 $this->initVar('umode', XOBJ_DTYPE_OTHER, null, false);
77 $this->initVar('uorder', XOBJ_DTYPE_INT, 1, false);
78 // RMV-NOTIFY
79 $this->initVar('notify_method', XOBJ_DTYPE_OTHER, 1, false);
80 $this->initVar('notify_mode', XOBJ_DTYPE_OTHER, 0, false);
81 $this->initVar('user_occ', XOBJ_DTYPE_TXTBOX, null, false, 100);
82 $this->initVar('bio', XOBJ_DTYPE_TXTAREA, null, false, null);
83 $this->initVar('user_intrest', XOBJ_DTYPE_TXTBOX, null, false, 150);
84 $this->initVar('user_mailok', XOBJ_DTYPE_INT, 1, false);
85 $initVars = $this->vars;
86 }
87
88 // for backward compatibility
89 if (isset($id)) {
90 if (is_array($id)) {
91 $this->assignVars($id);
92 } else {
93 $member_handler = xoops_gethandler('member');
94 $user =& $member_handler->getUser($id);
95 foreach ($user->vars as $k => $v) {
96 $this->assignVar($k, $v['value']);
97 }
98 }
99 }
100 }
101 public function XoopsUser($id = null)
102 {
103 return self::__construct($id);
104 }
105
112 public function isGuest()
113 {
114 return false;
115 }
116
125 public static function getUnameFromId($userid, $usereal = 0)
126 {
127 $userid = (int)$userid;
128 $usereal = (int)$usereal;
129 if ($userid > 0) {
130 static $nameCache;
131 $field = $usereal?'name':'uname';
132 if (isset($nameCache[$field][$userid])) {
133 return $nameCache[$field][$userid];
134 }
135 $member_handler = xoops_gethandler('member');
136 $user =& $member_handler->getUser($userid);
137 if (is_object($user)) {
138 return ($nameCache[$field][$userid] = $user->getVar($field));
139 }
140 }
141 return $GLOBALS['xoopsConfig']['anonymous'];
142 }
143
148 public function incrementPost()
149 {
150 $member_handler = xoops_gethandler('member');
151 return $member_handler->updateUserByField($this, 'posts', $this->getVar('posts') + 1);
152 }
153
158 public function setGroups($groupsArr)
159 {
160 if (is_array($groupsArr)) {
161 $this->_groups =& $groupsArr;
162 }
163 }
164
173 public function getGroups($bReget = false)
174 {
175 if ($bReget) {
176 unset($this->_groups);
177 }
178
179 if (empty($this->_groups)) {
180 $member_handler = xoops_gethandler('member');
181 $this->_groups = $member_handler->getGroupsByUser($this->getVar('uid'));
182 }
183 return $this->_groups;
184 }
185
186 public function getNumGroups()
187 {
188 if (empty($this->_groups)) {
189 $this->getGroups();
190 }
191 return count($this->_groups);
192 }
193
194
201 public function groups()
202 {
203 return $this->getGroups();
204 }
205
215 public function isAdmin($module_id = null)
216 {
217 if (null === $module_id) {
218 global $xoopsModule;
219 $module_id = isset($xoopsModule) ? $xoopsModule->getVar('mid', 'n') : 1;
220 } elseif ((int)$module_id < 1) {
221 $module_id = 0;
222 }
223 static $moduleperm_handler;
224 isset($moduleperm_handler) || $moduleperm_handler = xoops_gethandler('groupperm');
225 return $moduleperm_handler->checkRight('module_admin', $module_id, $this->getGroups());
226 }
227
231 public function rank()
232 {
233 if (!isset($this->_rank)) {
234 $this->_rank = xoops_getrank($this->getVar('rank'), $this->getVar('posts'));
235 }
236 return $this->_rank;
237 }
238
242 public function isActive()
243 {
244 return ! ( 0 == $this->getVar( 'level' ) );
245 }
246
250 public function isOnline()
251 {
252 if (!isset($this->_isOnline)) {
253 $onlinehandler = xoops_gethandler('online');
254 // $this->_isOnline = ($onlinehandler->getCount(new Criteria('online_uid', $this->getVar('uid', 'N'))) > 0) ? true : false;
255 $this->_isOnline = $onlinehandler->getCount(new Criteria('online_uid', $this->getVar('uid', 'N'))) > 0;
256 }
257 return $this->_isOnline;
258 }
259
271 public function uid()
272 {
273 return $this->getVar('uid');
274 }
275
281 public function name($format= 'S')
282 {
283 return $this->getVar('name', $format);
284 }
285
291 public function uname($format= 'S')
292 {
293 return $this->getVar('uname', $format);
294 }
295
302 public function email($format= 'S')
303 {
304 return $this->getVar('email', $format);
305 }
306
307 public function url($format= 'S')
308 {
309 return $this->getVar('url', $format);
310 }
311
312 public function user_avatar($format= 'S')
313 {
314 return $this->getVar('user_avatar');
315 }
316
317 public function user_regdate()
318 {
319 return $this->getVar('user_regdate');
320 }
321
322 public function user_icq($format= 'S')
323 {
324 return $this->getVar('user_icq', $format);
325 }
326
327 public function user_from($format= 'S')
328 {
329 return $this->getVar('user_from', $format);
330 }
331 public function user_sig($format= 'S')
332 {
333 return $this->getVar('user_sig', $format);
334 }
335
336 public function user_viewemail()
337 {
338 return $this->getVar('user_viewemail');
339 }
340
341 public function actkey()
342 {
343 return $this->getVar('actkey');
344 }
345
346 public function user_aim($format= 'S')
347 {
348 return $this->getVar('user_aim', $format);
349 }
350
351 public function user_yim($format= 'S')
352 {
353 return $this->getVar('user_yim', $format);
354 }
355
356 public function user_msnm($format= 'S')
357 {
358 return $this->getVar('user_msnm', $format);
359 }
360
361 public function pass()
362 {
363 return $this->getVar('pass');
364 }
365
366 public function posts()
367 {
368 return $this->getVar('posts');
369 }
370
371 public function attachsig()
372 {
373 return $this->getVar('attachsig');
374 }
375
376 public function level()
377 {
378 return $this->getVar('level');
379 }
380
381 public function theme()
382 {
383 return $this->getVar('theme');
384 }
385
386 public function timezone()
387 {
388 return $this->getVar('timezone_offset');
389 }
390
391 public function umode()
392 {
393 return $this->getVar('umode');
394 }
395
396 public function uorder()
397 {
398 return $this->getVar('uorder');
399 }
400
401 // RMV-NOTIFY
402 public function notify_method()
403 {
404 return $this->getVar('notify_method');
405 }
406
407 public function notify_mode()
408 {
409 return $this->getVar('notify_mode');
410 }
411
412 public function user_occ($format= 'S')
413 {
414 return $this->getVar('user_occ', $format);
415 }
416
417 public function bio($format= 'S')
418 {
419 return $this->getVar('bio', $format);
420 }
421
422 public function user_intrest($format= 'S')
423 {
424 return $this->getVar('user_intrest', $format);
425 }
426
427 public function last_login()
428 {
429 return $this->getVar('last_login');
430 }
431
436 public function hasAvatar()
437 {
438 $avatar=$this->getVar('user_avatar');
439 if (!$avatar || 'blank.gif' == $avatar) {
440 return false;
441 }
442
443 $file= XOOPS_UPLOAD_PATH . '/' . $avatar;
444 return file_exists($file);
445 }
446
453 public function getAvatarUrl()
454 {
455 if ($this->hasAvatar()) {
456 return XOOPS_UPLOAD_URL . '/' . $this->getVar('user_avatar');
457 }
458
459 return null;
460 }
461
463}
464
471class XoopsGuestUser extends XoopsUser
472{
479 public function isGuest()
480 {
481 return true;
482 }
483
484 public function getGroups($bReget = false)
485 {
486 return XOOPS_GROUP_ANONYMOUS;
487 }
488}
489
490
501{
502
509 public function &create($isNew = true)
510 {
511 $user =new XoopsUser();
512 if ($isNew) {
513 $user->setNew();
514 }
515 return $user;
516 }
517
524 public function &get($id)
525 {
526 $ret = false;
527 if ((int)$id > 0) {
528 $sql = 'SELECT * FROM '.$this->db->prefix('users').' WHERE uid='.$id;
529 if ($result = $this->db->query($sql)) {
530 $numrows = $this->db->getRowsNum($result);
531 if (1 == $numrows) {
532 $user =new XoopsUser();
533 $user->assignVars($this->db->fetchArray($result));
534 $ret =& $user;
535 }
536 }
537 }
538 return $ret;
539 }
540
548 public function insert(&$user, $force = false)
549 {
550 $uname = null;
551 $name = null;
552 $email = null;
553 $url = null;
554 $user_avatar = null;
555 $user_icq = null;
556 $user_from = null;
557 $user_sig = null;
558 $user_viewemail = null;
559 $actkey = null;
560 $user_aim = null;
561 $user_yim = null;
562 $user_msnm = null;
563 $pass = null;
564 $posts = null;
565 $attachsig = null;
566 $rank = null;
567 $level = null;
568 $theme = null;
569 $timezone_offset = null;
570 $umode = null;
571 $uorder = null;
572 $notify_method = null;
573 $notify_mode = null;
574 $user_occ = null;
575 $bio = null;
576 $user_intrest = null;
577 $user_mailok = null;
578 $last_login = null;
579 $uid = null;
580 if ('xoopsuser' != strtolower(get_class($user))) {
581 return false;
582 }
583 if (!$user->isDirty()) {
584 return true;
585 }
586 if (!$user->cleanVars()) {
587 return false;
588 }
589 // check pass colmun length of users table
590 if (!defined('XCUBE_CORE_USER_PASS_LEN_FIXED') && is_callable('User_Utils::checkUsersPassColumnLength')) {
592 }
593 foreach ($user->cleanVars as $k => $v) {
594 ${$k} = $v;
595 }
596 // RMV-NOTIFY
597 // Added two fields, notify_method, notify_mode
598 if ($user->isNew()) {
599 $config = xoops_gethandler('config');
600 $options = $config->getConfigs(new Criteria('conf_name', 'notify_method'));
601 if (isset($options) and (1 == (is_countable($options) ? count($options) : 0))) {
602 $notify_method = $options[0]->getvar('conf_value');
603 }
604 $uid = $this->db->genId('users_uid_seq');
605 $sql = sprintf(
606 'INSERT INTO %s (uid, uname, name, email, url, user_avatar, user_regdate, user_icq, user_from, user_sig, user_viewemail, actkey, user_aim, user_yim, user_msnm, pass, posts, attachsig, `rank`, level, theme, timezone_offset, last_login, umode, uorder, notify_method, notify_mode, user_occ, bio, user_intrest, user_mailok) VALUES (%u, %s, %s, %s, %s, %s, %u, %s, %s, %s, %u, %s, %s, %s, %s, %s, %u, %u, %u, %u, %s, %.2f, %u, %s, %u, %u, %u, %s, %s, %s, %u)', $this->db->prefix('users'), $uid, $this->db->quoteString($uname), $this->db->quoteString($name), $this->db->quoteString($email), $this->db->quoteString($url), $this->db->quoteString($user_avatar), time(), $this->db->quoteString($user_icq), $this->db->quoteString($user_from), $this->db->quoteString($user_sig), $user_viewemail, $this->db->quoteString($actkey), $this->db->quoteString($user_aim), $this->db->quoteString($user_yim), $this->db->quoteString($user_msnm), $this->db->quoteString($pass), $posts, $attachsig, $rank, $level, $this->db->quoteString($theme), $timezone_offset, 0, $this->db->quoteString($umode), $uorder, $notify_method, $notify_mode, $this->db->quoteString($user_occ), $this->db->quoteString($bio), $this->db->quoteString($user_intrest), $user_mailok);
607 } else {
608 $sql = sprintf(
609 'UPDATE %s SET uname = %s, name = %s, email = %s, url = %s, user_avatar = %s, user_icq = %s, user_from = %s, user_sig = %s, user_viewemail = %u, user_aim = %s, user_yim = %s, user_msnm = %s, posts = %d, pass = %s, attachsig = %u, `rank` = %u, level= %u, theme = %s, timezone_offset = %.2f, umode = %s, last_login = %u, uorder = %u, notify_method = %u, notify_mode = %u, user_occ = %s, bio = %s, user_intrest = %s, user_mailok = %u WHERE uid = %u', $this->db->prefix('users'), $this->db->quoteString($uname), $this->db->quoteString($name), $this->db->quoteString($email), $this->db->quoteString($url), $this->db->quoteString($user_avatar), $this->db->quoteString($user_icq), $this->db->quoteString($user_from), $this->db->quoteString($user_sig), $user_viewemail, $this->db->quoteString($user_aim), $this->db->quoteString($user_yim), $this->db->quoteString($user_msnm), $posts, $this->db->quoteString($pass), $attachsig, $rank, $level, $this->db->quoteString($theme), $timezone_offset, $this->db->quoteString($umode), $last_login, $uorder, $notify_method, $notify_mode, $this->db->quoteString($user_occ), $this->db->quoteString($bio), $this->db->quoteString($user_intrest), $user_mailok, $uid);
610 }
611 if (false != $force) {
612 $result = $this->db->queryF($sql);
613 } else {
614 $result = $this->db->query($sql);
615 }
616 if (!$result) {
617 return false;
618 }
619 if (empty($uid)) {
620 $uid = $this->db->getInsertId();
621 }
622 $user->assignVar('uid', $uid);
623 return true;
624 }
625
633 public function delete(&$user, $force = false)
634 {
635 if ('xoopsuser' != strtolower(get_class($user))) {
636 return false;
637 }
638 $sql = sprintf('DELETE FROM %s WHERE uid = %u', $this->db->prefix('users'), $user->getVar('uid'));
639 if (false != $force) {
640 $result = $this->db->queryF($sql);
641 } else {
642 $result = $this->db->query($sql);
643 }
644 if (!$result) {
645 return false;
646 }
647 return true;
648 }
649
657 public function getUnames($criteria = null, $id_as_key = false)
658 {
659 $ret = [];
660 $limit = $start = 0;
661 $sql = 'SELECT * FROM '.$this->db->prefix('users');
662 if (isset($criteria) && $criteria instanceof \criteriaelement) {
663 $sql .= ' '.$criteria->renderWhere();
664 if ('' != $criteria->getSort()) {
665 $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
666 }
667 $limit = $criteria->getLimit();
668 $start = $criteria->getStart();
669 }
670 $result = $this->db->query($sql, $limit, $start);
671 if (!$result) {
672 return $ret;
673 }
674 while ($myrow = $this->db->fetchArray($result)) {
675 if (!$id_as_key) {
676 $ret[] = $myrow['uname'];
677 } else {
678 $ret[$myrow['uid']] = $myrow['uname'];
679 }
680 }
681 return $ret;
682 }
683
691 public function &getObjects($criteria = null, $id_as_key = false)
692 {
693 $ret = [];
694 $limit = $start = 0;
695 $sql = 'SELECT * FROM '.$this->db->prefix('users');
696 if (isset($criteria) && $criteria instanceof \criteriaelement) {
697 $sql .= ' '.$criteria->renderWhere();
698 if ('' != $criteria->getSort()) {
699 $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
700 }
701 $limit = $criteria->getLimit();
702 $start = $criteria->getStart();
703 }
704 $result = $this->db->query($sql, $limit, $start);
705 if (!$result) {
706 return $ret;
707 }
708 while ($myrow = $this->db->fetchArray($result)) {
709 $user =new XoopsUser();
710 $user->assignVars($myrow);
711 if (!$id_as_key) {
712 $ret[] =& $user;
713 } else {
714 $ret[$myrow['uid']] =& $user;
715 }
716 unset($user);
717 }
718 return $ret;
719 }
720
728 public function &getObjectsByLevel($level=0)
729 {
730 $ret= [];
731 $level=(int)$level;
732 $result = $this->db->query('SELECT * FROM ' . $this->db->prefix('users') . " WHERE level > $level ORDER BY uname");
733 if (!$result) {
734 return $ret;
735 }
736
737 while ($myrow=$this->db->fetchArray($result)) {
738 $user=new XoopsUser();
739 $user->assignVars($myrow);
740 $ret[]=&$user;
741 unset($user);
742 }
743
744 return $ret;
745 }
746
753 public function getCount($criteria = null)
754 {
755 $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('users');
756 if (isset($criteria) && $criteria instanceof \criteriaelement) {
757 $sql .= ' '.$criteria->renderWhere();
758 }
759 $result = $this->db->query($sql);
760 if (!$result) {
761 return 0;
762 }
763 [$count] = $this->db->fetchRow($result);
764 return $count;
765 }
766
773 public function deleteAll($criteria = null)
774 {
775 $sql = 'DELETE FROM '.$this->db->prefix('users');
776 if (isset($criteria) && $criteria instanceof \criteriaelement) {
777 $sql .= ' '.$criteria->renderWhere();
778 }
779 if (!$result = $this->db->query($sql)) {
780 return false;
781 }
782 return true;
783 }
784
794 public function updateAll($fieldname, $fieldvalue, $criteria = null)
795 {
796 $set_clause = is_numeric($fieldvalue) ? $fieldname.' = '.$fieldvalue : $fieldname.' = '.$this->db->quoteString($fieldvalue);
797 $sql = 'UPDATE '.$this->db->prefix('users').' SET '.$set_clause;
798 if (isset($criteria) && $criteria instanceof \criteriaelement) {
799 $sql .= ' '.$criteria->renderWhere();
800 }
801 if (!$result = $this->db->query($sql)) {
802 return false;
803 }
804 return true;
805 }
806}
static checkUsersPassColumnLength()
getGroups($bReget=false)
Definition user.php:484
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206
& getVar($key, $format='s')
Definition object.php:317
assignVar($key, $value)
Definition object.php:218
assignVars($var_arr)
Definition object.php:232
& getObjects($criteria=null, $id_as_key=false)
Definition user.php:691
updateAll($fieldname, $fieldvalue, $criteria=null)
Definition user.php:794
insert(&$user, $force=false)
Definition user.php:548
deleteAll($criteria=null)
Definition user.php:773
& getObjectsByLevel($level=0)
Definition user.php:728
getUnames($criteria=null, $id_as_key=false)
Definition user.php:657
getCount($criteria=null)
Definition user.php:753
& create($isNew=true)
Definition user.php:509
email($format='S')
Definition user.php:302
setGroups($groupsArr)
Definition user.php:158
isGuest()
Definition user.php:112
isOnline()
Definition user.php:250
getAvatarUrl()
Definition user.php:453
incrementPost()
Definition user.php:148
static getUnameFromId($userid, $usereal=0)
Definition user.php:125
uname($format='S')
Definition user.php:291
name($format='S')
Definition user.php:281
isAdmin($module_id=null)
Definition user.php:215
groups()
Definition user.php:201
getGroups($bReget=false)
Definition user.php:173
isActive()
Definition user.php:242
__construct($id=null)
Definition user.php:47
hasAvatar()
Definition user.php:436