1: <?php
2: /**
3: * XOOPS user handler
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18:
19: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20:
21: require_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
22:
23: /**
24: * Class for users
25: * @author Kazumi Ono <onokazu@xoops.org>
26: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
27: * @package kernel
28: */
29: class XoopsUser extends XoopsObject
30: {
31: /**
32: * Array of groups that user belongs to
33: * @var array
34: * @access private
35: */
36: public $_groups = array();
37: /**
38: * @var bool is the user admin?
39: * @access private
40: */
41: public $_isAdmin;
42: /**
43: * @var string user's rank
44: * @access private
45: */
46: public $_rank;
47: /**
48: * @var bool is the user online?
49: * @access private
50: */
51: public $_isOnline;
52:
53: //PHP 8.2 Dynamic properties deprecated
54: public $uid;
55: public $name;
56: public $uname;
57: public $email;
58: public $url;
59: public $user_avatar;
60: public $user_regdate;
61: public $user_icq;
62: public $user_from;
63: public $user_sig;
64: public $user_viewemail;
65: public $actkey;
66: public $user_aim;
67: public $user_yim;
68: public $user_msnm;
69: public $pass;
70: public $posts;
71: public $attachsig;
72: public $rank;
73: public $level;
74: public $theme;
75: public $timezone_offset;
76: public $last_login;
77: public $umode;
78: public $uorder;
79: // RMV-NOTIFY
80: public $notify_method;
81: public $notify_mode;
82: public $user_occ;
83: public $bio;
84: public $user_intrest;
85: public $user_mailok;
86:
87: /**
88: * constructor
89: * @param array|null $id ID of the user to be loaded from the database.
90: */
91: public function __construct($id = null)
92: {
93: $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
94: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 60);
95: $this->initVar('uname', XOBJ_DTYPE_TXTBOX, null, true, 25);
96: $this->initVar('email', XOBJ_DTYPE_TXTBOX, null, true, 60);
97: $this->initVar('url', XOBJ_DTYPE_TXTBOX, null, false, 100);
98: $this->initVar('user_avatar', XOBJ_DTYPE_TXTBOX, null, false, 30);
99: $this->initVar('user_regdate', XOBJ_DTYPE_INT, null, false);
100: $this->initVar('user_icq', XOBJ_DTYPE_TXTBOX, null, false, 15);
101: $this->initVar('user_from', XOBJ_DTYPE_TXTBOX, null, false, 100);
102: $this->initVar('user_sig', XOBJ_DTYPE_TXTAREA, null, false, null);
103: $this->initVar('user_viewemail', XOBJ_DTYPE_INT, 0, false);
104: $this->initVar('actkey', XOBJ_DTYPE_OTHER, null, false);
105: $this->initVar('user_aim', XOBJ_DTYPE_TXTBOX, null, false, 18);
106: $this->initVar('user_yim', XOBJ_DTYPE_TXTBOX, null, false, 25);
107: $this->initVar('user_msnm', XOBJ_DTYPE_TXTBOX, null, false, 100);
108: $this->initVar('pass', XOBJ_DTYPE_TXTBOX, null, false, 255);
109: $this->initVar('posts', XOBJ_DTYPE_INT, null, false);
110: $this->initVar('attachsig', XOBJ_DTYPE_INT, 0, false);
111: $this->initVar('rank', XOBJ_DTYPE_INT, 0, false);
112: $this->initVar('level', XOBJ_DTYPE_INT, 0, false);
113: $this->initVar('theme', XOBJ_DTYPE_OTHER, null, false);
114: $this->initVar('timezone_offset', XOBJ_DTYPE_OTHER, '0.0', false);
115: $this->initVar('last_login', XOBJ_DTYPE_INT, 0, false);
116: $this->initVar('umode', XOBJ_DTYPE_OTHER, null, false);
117: $this->initVar('uorder', XOBJ_DTYPE_INT, 1, false);
118: // RMV-NOTIFY
119: $this->initVar('notify_method', XOBJ_DTYPE_OTHER, XOOPS_NOTIFICATION_METHOD_PM, false);
120: $this->initVar('notify_mode', XOBJ_DTYPE_OTHER, XOOPS_NOTIFICATION_MODE_SENDALWAYS, false);
121: $this->initVar('user_occ', XOBJ_DTYPE_TXTBOX, null, false, 100);
122: $this->initVar('bio', XOBJ_DTYPE_TXTAREA, null, false, null);
123: $this->initVar('user_intrest', XOBJ_DTYPE_TXTBOX, null, false, 150);
124: $this->initVar('user_mailok', XOBJ_DTYPE_INT, 1, false);
125: // for backward compatibility
126: if (isset($id)) {
127: if (is_array($id)) {
128: $this->assignVars($id);
129: } else {
130: /** @var XoopsMemberHandler $member_handler */
131: $member_handler = xoops_getHandler('member');
132: $user = $member_handler->getUser($id);
133: foreach ($user->vars as $k => $v) {
134: $this->assignVar($k, $v['value']);
135: }
136: }
137: }
138: }
139:
140: /**
141: * check if the user is a guest user
142: *
143: * @return bool returns false
144: *
145: */
146: public function isGuest()
147: {
148: return false;
149: }
150:
151: /**
152: * Updated by Catzwolf 11 Jan 2004
153: * find the username for a given ID
154: *
155: * @param int $userid ID of the user to find
156: * @param int $usereal switch for usename or realname
157: * @param bool $linked add a link
158: * @return string name of the user. name for 'anonymous' if not found.
159: */
160: public static function getUnameFromId($userid, $usereal = 0, $linked = false)
161: {
162: $userid = (int)$userid;
163: $usereal = (int)$usereal;
164: if ($userid > 0) {
165: /** @var XoopsMemberHandler $member_handler */
166: $member_handler = xoops_getHandler('member');
167: $user = $member_handler->getUser($userid);
168: if (is_object($user)) {
169: $myts = \MyTextSanitizer::getInstance();
170: if ($usereal && $user->getVar('name')) {
171: $username = $myts->htmlSpecialChars($user->getVar('name'));
172: } else {
173: $username = $myts->htmlSpecialChars($user->getVar('uname'));
174: }
175: if (!empty($linked)) {
176: $username = '<a href="' . XOOPS_URL . '/userinfo.php?uid=' . $userid . '" title="' . $username . '">' . $username . '</a>';
177: }
178: return $username;
179: }
180: }
181:
182: return $GLOBALS['xoopsConfig']['anonymous'];
183: }
184:
185: /**
186: * increase the number of posts for the user
187: *
188: * @deprecated
189: */
190: public function incrementPost()
191: {
192: /** @var XoopsMemberHandler $member_handler */
193: $member_handler = xoops_getHandler('member');
194:
195: return $member_handler->updateUserByField($this, 'posts', $this->getVar('posts') + 1);
196: }
197:
198: /**
199: * set the groups for the user
200: *
201: * @param array $groupsArr Array of groups that user belongs to
202: */
203: public function setGroups($groupsArr)
204: {
205: if (is_array($groupsArr)) {
206: $this->_groups =& $groupsArr;
207: }
208: }
209:
210: /**
211: * get the groups that the user belongs to
212: *
213: * @return array array of groups
214: */
215: public function &getGroups()
216: {
217: if (empty($this->_groups)) {
218: /** @var XoopsMemberHandler $member_handler */
219: $member_handler = xoops_getHandler('member');
220: $this->_groups = $member_handler->getGroupsByUser($this->getVar('uid'));
221: }
222:
223: return $this->_groups;
224: }
225:
226: /**
227: * alias for {@link getGroups()}
228: * @see getGroups()
229: * @return array array of groups
230: * @deprecated
231: */
232: public function &groups()
233: {
234: $groups =& $this->getGroups();
235:
236: return $groups;
237: }
238:
239: /**
240: * Is the user admin ?
241: *
242: * This method will return true if this user has admin rights for the specified module.<br>
243: * - If you don't specify any module ID, the current module will be checked.<br>
244: * - If you set the module_id to -1, it will return true if the user has admin rights for at least one module
245: *
246: * @param int $module_id check if user is admin of this module
247: * @return bool is the user admin of that module?
248: */
249: public function isAdmin($module_id = null)
250: {
251: if (null === $module_id) {
252: $module_id = (isset($GLOBALS['xoopsModule']) && is_object($GLOBALS['xoopsModule'])) ? $GLOBALS['xoopsModule']->getVar('mid', 'n') : 1;
253: } elseif ((int)$module_id < 1) {
254: $module_id = 0;
255: }
256: /** @var XoopsGroupPermHandler $moduleperm_handler */
257: $moduleperm_handler = xoops_getHandler('groupperm');
258:
259: return $moduleperm_handler->checkRight('module_admin', $module_id, $this->getGroups());
260: }
261:
262: /**
263: * get the user's rank
264: * @return array array of rank ID and title
265: */
266: public function rank()
267: {
268: if (!isset($this->_rank)) {
269: $this->_rank = xoops_getrank($this->getVar('rank'), $this->getVar('posts'));
270: }
271:
272: return $this->_rank;
273: }
274:
275: /**
276: * is the user activated?
277: * @return bool
278: */
279: public function isActive()
280: {
281: return !($this->getVar('level') == 0);
282: }
283:
284: /**
285: * is the user currently logged in?
286: * @return bool
287: */
288: public function isOnline()
289: {
290: if (!isset($this->_isOnline)) {
291: /** @var XoopsOnlineHandler $onlinehandler */
292: $onlinehandler = xoops_getHandler('online');
293: $this->_isOnline = ($onlinehandler->getCount(new Criteria('online_uid', $this->getVar('uid'))) > 0);// ? true : false;
294: }
295:
296: return $this->_isOnline;
297: }
298:
299: /**
300: * get the users UID
301: * @param string $format
302: * @return int
303: */
304: public function uid($format = '')
305: {
306: return $this->getVar('uid', $format);
307: }
308:
309: /**
310: * get the users UID
311: * @param string $format
312: * @return int
313: */
314: public function id($format = 'N')
315: {
316: return $this->getVar('uid', $format);
317: }
318:
319: /**
320: * get the users name
321: * @param string $format format for the output, see {@link XoopsObject::getVar($format = '')}
322: * @return string
323: */
324: public function name($format = 'S')
325: {
326: return $this->getVar('name', $format);
327: }
328:
329: /**
330: * get the user's uname
331: * @param string $format format for the output, see {@link XoopsObject::getVar($format = '')}
332: * @return string
333: */
334: public function uname($format = 'S')
335: {
336: return $this->getVar('uname', $format);
337: }
338:
339: /**
340: * get the user's email
341: *
342: * @param string $format format for the output, see {@link XoopsObject::getVar($format = '')}
343: * @return string
344: */
345: public function email($format = 'S')
346: {
347: return $this->getVar('email', $format);
348: }
349:
350: /**
351: * @param string $format
352: *
353: * @return mixed
354: */
355: public function url($format = 'S')
356: {
357: return $this->getVar('url', $format);
358: }
359:
360: /**
361: * @param string $format
362: *
363: * @return mixed
364: */
365: public function user_avatar($format = 'S')
366: {
367: return $this->getVar('user_avatar', $format);
368: }
369:
370: /**
371: * @param string $format
372: *
373: * @return mixed
374: */
375: public function user_regdate($format = '')
376: {
377: return $this->getVar('user_regdate', $format);
378: }
379:
380: /**
381: * @param string $format
382: *
383: * @return mixed
384: */
385: public function user_icq($format = 'S')
386: {
387: return $this->getVar('user_icq', $format);
388: }
389:
390: /**
391: * @param string $format
392: *
393: * @return mixed
394: */
395: public function user_from($format = 'S')
396: {
397: return $this->getVar('user_from', $format);
398: }
399:
400: /**
401: * @param string $format
402: *
403: * @return mixed
404: */
405: public function user_sig($format = 'S')
406: {
407: return $this->getVar('user_sig', $format);
408: }
409:
410: /**
411: * @param string $format
412: *
413: * @return mixed
414: */
415: public function user_viewemail($format = '')
416: {
417: return $this->getVar('user_viewemail', $format);
418: }
419:
420: /**
421: * @param string $format
422: *
423: * @return mixed
424: */
425: public function actkey($format = '')
426: {
427: return $this->getVar('actkey', $format);
428: }
429:
430: /**
431: * @param string $format
432: *
433: * @return mixed
434: */
435: public function user_aim($format = 'S')
436: {
437: return $this->getVar('user_aim', $format);
438: }
439:
440: /**
441: * @param string $format
442: *
443: * @return mixed
444: */
445: public function user_yim($format = 'S')
446: {
447: return $this->getVar('user_yim', $format);
448: }
449:
450: /**
451: * @param string $format
452: *
453: * @return mixed
454: */
455: public function user_msnm($format = 'S')
456: {
457: return $this->getVar('user_msnm', $format);
458: }
459:
460: /**
461: * @param string $format
462: *
463: * @return mixed
464: */
465: public function pass($format = '')
466: {
467: return $this->getVar('pass', $format);
468: }
469:
470: /**
471: * @param string $format
472: *
473: * @return mixed
474: */
475: public function posts($format = '')
476: {
477: return $this->getVar('posts', $format);
478: }
479:
480: /**
481: * @param string $format
482: *
483: * @return mixed
484: */
485: public function attachsig($format = '')
486: {
487: return $this->getVar('attachsig', $format);
488: }
489:
490: /**
491: * @param string $format
492: *
493: * @return mixed
494: */
495: public function level($format = '')
496: {
497: return $this->getVar('level', $format);
498: }
499:
500: /**
501: * @param string $format
502: *
503: * @return mixed
504: */
505: public function theme($format = '')
506: {
507: return $this->getVar('theme', $format);
508: }
509:
510: /**
511: * @param string $format
512: *
513: * @return mixed
514: */
515: public function timezone($format = '')
516: {
517: return $this->getVar('timezone_offset', $format);
518: }
519:
520: /**
521: * @param string $format
522: *
523: * @return mixed
524: */
525: public function umode($format = '')
526: {
527: return $this->getVar('umode', $format);
528: }
529:
530: /**
531: * @param string $format
532: *
533: * @return mixed
534: */
535: public function uorder($format = '')
536: {
537: return $this->getVar('uorder', $format);
538: }
539:
540: // RMV-NOTIFY
541: /**
542: * @param string $format
543: *
544: * @return mixed
545: */
546: public function notify_method($format = '')
547: {
548: return $this->getVar('notify_method', $format);
549: }
550:
551: /**
552: * @param string $format
553: *
554: * @return mixed
555: */
556: public function notify_mode($format = '')
557: {
558: return $this->getVar('notify_mode', $format);
559: }
560:
561: /**
562: * @param string $format
563: *
564: * @return mixed
565: */
566: public function user_occ($format = 'S')
567: {
568: return $this->getVar('user_occ', $format);
569: }
570:
571: /**
572: * @param string $format
573: *
574: * @return mixed
575: */
576: public function bio($format = 'S')
577: {
578: return $this->getVar('bio', $format);
579: }
580:
581: /**
582: * @param string $format
583: *
584: * @return mixed
585: */
586: public function user_intrest($format = 'S')
587: {
588: return $this->getVar('user_intrest', $format);
589: }
590: /**#@-*/
591:
592: /**#@+
593: * @deprecated
594: */
595: public function getProfile()
596: {
597: $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
598:
599: return false;
600: }
601: /**#@-*/
602: }
603:
604: /**
605: * Class that represents a guest user
606: * @author Kazumi Ono <onokazu@xoops.org>
607: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
608: * @package kernel
609: */
610: class XoopsGuestUser extends XoopsUser
611: {
612: /**
613: * check if the user is a guest user
614: *
615: * @return bool returns true
616: *
617: */
618: public function isGuest()
619: {
620: return true;
621: }
622: }
623:
624: /**
625: * XOOPS user handler class.
626: * This class is responsible for providing data access mechanisms to the data source
627: * of XOOPS user class objects.
628: *
629: * @author Kazumi Ono <onokazu@xoops.org>
630: * @author Taiwen Jiang <phppp@users.sourceforge.net>
631: * @package kernel
632: */
633: class XoopsUserHandler extends XoopsPersistableObjectHandler
634: {
635: /**
636: * @param XoopsDatabase|null| $db
637: */
638: public function __construct(XoopsDatabase $db)
639: {
640: parent::__construct($db, 'users', 'XoopsUser', 'uid', 'uname');
641: }
642:
643: /**#@+
644: * @deprecated
645: * @param bool $uname
646: * @param $pwd
647: * @param bool $md5
648: * @return bool|object
649: */
650: public function loginUser($uname, $pwd, $md5 = false)
651: {
652: $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
653:
654: return false;
655: }
656:
657: /**
658: * @param $fieldName
659: * @param $fieldValue
660: * @param $uid
661: *
662: * @return bool
663: */
664: public function updateUserByField($fieldName, $fieldValue, $uid)
665: {
666: $GLOBALS['xoopsLogger']->addDeprecated(__METHOD__ . ' is deprecated');
667:
668: return false;
669: }
670: /**#@-*/
671: }
672: