1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31: if (!defined('XOOPS_ROOT_PATH')) {
32: exit();
33: }
34: 35: 36: 37: 38: 39:
40: class XoopsUser extends XoopsObject
41: {
42:
43: 44: 45: 46: 47:
48: var $_groups = array();
49: 50: 51: 52:
53: var $_isAdmin = null;
54: 55: 56: 57:
58: var $_rank = null;
59: 60: 61: 62:
63: var $_isOnline = null;
64:
65: 66: 67: 68: 69:
70: function XoopsUser($id = null)
71: {
72: static $initVars;
73: if (isset($initVars)) {
74: $this->vars = $initVars;
75: }
76: else{
77: $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
78: $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, false, 60);
79: $this->initVar('uname', XOBJ_DTYPE_TXTBOX, null, true, 25);
80: $this->initVar('email', XOBJ_DTYPE_TXTBOX, null, true, 256);
81: $this->initVar('url', XOBJ_DTYPE_TXTBOX, null, false, 100);
82: $this->initVar('user_avatar', XOBJ_DTYPE_TXTBOX, null, false, 30);
83: $this->initVar('user_regdate', XOBJ_DTYPE_INT, null, false);
84: $this->initVar('user_icq', XOBJ_DTYPE_TXTBOX, null, false, 15);
85: $this->initVar('user_from', XOBJ_DTYPE_TXTBOX, null, false, 100);
86: $this->initVar('user_sig', XOBJ_DTYPE_TXTAREA, null, false, null);
87: $this->initVar('user_viewemail', XOBJ_DTYPE_INT, 0, false);
88: $this->initVar('actkey', XOBJ_DTYPE_OTHER, null, false);
89: $this->initVar('user_aim', XOBJ_DTYPE_TXTBOX, null, false, 18);
90: $this->initVar('user_yim', XOBJ_DTYPE_TXTBOX, null, false, 25);
91: $this->initVar('user_msnm', XOBJ_DTYPE_TXTBOX, null, false, 100);
92: $this->initVar('pass', XOBJ_DTYPE_TXTBOX, null, false, 32);
93: $this->initVar('posts', XOBJ_DTYPE_INT, null, false);
94: $this->initVar('attachsig', XOBJ_DTYPE_INT, 0, false);
95: $this->initVar('rank', XOBJ_DTYPE_INT, 0, false);
96: $this->initVar('level', XOBJ_DTYPE_INT, 0, false);
97: $this->initVar('theme', XOBJ_DTYPE_OTHER, null, false);
98: $this->initVar('timezone_offset', XOBJ_DTYPE_OTHER, null, false);
99: $this->initVar('last_login', XOBJ_DTYPE_INT, 0, false);
100: $this->initVar('umode', XOBJ_DTYPE_OTHER, null, false);
101: $this->initVar('uorder', XOBJ_DTYPE_INT, 1, false);
102:
103: $this->initVar('notify_method', XOBJ_DTYPE_OTHER, 1, false);
104: $this->initVar('notify_mode', XOBJ_DTYPE_OTHER, 0, false);
105: $this->initVar('user_occ', XOBJ_DTYPE_TXTBOX, null, false, 100);
106: $this->initVar('bio', XOBJ_DTYPE_TXTAREA, null, false, null);
107: $this->initVar('user_intrest', XOBJ_DTYPE_TXTBOX, null, false, 150);
108: $this->initVar('user_mailok', XOBJ_DTYPE_INT, 1, false);
109: $initVars = $this->vars;
110: }
111:
112:
113: if (isset($id)) {
114: if (is_array($id)) {
115: $this->assignVars($id);
116: } else {
117: $member_handler = xoops_gethandler('member');
118: $user =& $member_handler->getUser($id);
119: foreach ($user->vars as $k => $v) {
120: $this->assignVar($k, $v['value']);
121: }
122: }
123: }
124: }
125:
126: 127: 128: 129: 130: 131:
132: function isGuest()
133: {
134: return false;
135: }
136:
137:
138: 139: 140: 141: 142: 143: 144: 145:
146: static function getUnameFromId( $userid, $usereal = 0 )
147: {
148: $userid = (int)$userid;
149: $usereal = (int)$usereal;
150: if ($userid > 0) {
151: static $nameCache;
152: $field = $usereal?'name':'uname';
153: if (isset($nameCache[$field][$userid])) return $nameCache[$field][$userid];
154: $member_handler = xoops_gethandler('member');
155: $user =& $member_handler->getUser($userid);
156: if (is_object($user)) {
157: return ($nameCache[$field][$userid] = $user->getVar($field));
158: }
159: }
160: return $GLOBALS['xoopsConfig']['anonymous'];
161: }
162: 163: 164: 165: 166:
167: function incrementPost(){
168: $member_handler = xoops_gethandler('member');
169: return $member_handler->updateUserByField($this, 'posts', $this->getVar('posts') + 1);
170: }
171: 172: 173: 174: 175:
176: function setGroups($groupsArr)
177: {
178: if (is_array($groupsArr)) {
179: $this->_groups =& $groupsArr;
180: }
181: }
182: 183: 184: 185: 186: 187: 188: 189:
190: function getGroups($bReget = false)
191: {
192: if ($bReget) {
193: unset($this->_groups);
194: }
195:
196: if (empty($this->_groups)) {
197: $member_handler = xoops_gethandler('member');
198: $this->_groups = $member_handler->getGroupsByUser($this->getVar('uid'));
199: }
200: return $this->_groups;
201: }
202:
203: function getNumGroups()
204: {
205: if (empty($this->_groups)) {
206: $this->getGroups();
207: }
208: return count($this->_groups);
209: }
210:
211:
212: 213: 214: 215: 216: 217:
218: function groups()
219: {
220: return $this->getGroups();
221: }
222: 223: 224: 225: 226: 227: 228: 229: 230: 231:
232: function isAdmin( $module_id = null ) {
233: if ( $module_id === null ) {
234: global $xoopsModule;
235: $module_id = isset($xoopsModule) ? $xoopsModule->getVar( 'mid', 'n' ) : 1;
236: } elseif ( (int)$module_id < 1 ) {
237: $module_id = 0;
238: }
239: static $moduleperm_handler;
240: isset($moduleperm_handler) || $moduleperm_handler = xoops_gethandler('groupperm');
241: return $moduleperm_handler->checkRight('module_admin', $module_id, $this->getGroups());
242: }
243: 244: 245: 246:
247: function rank()
248: {
249: if (!isset($this->_rank)) {
250: $this->_rank = xoops_getrank($this->getVar('rank'), $this->getVar('posts'));
251: }
252: return $this->_rank;
253: }
254: 255: 256: 257:
258: function isActive()
259: {
260: if ($this->getVar('level') == 0) {
261: return false;
262: }
263: return true;
264: }
265: 266: 267: 268:
269: function isOnline()
270: {
271: if (!isset($this->_isOnline)) {
272: $onlinehandler = xoops_gethandler('online');
273: $this->_isOnline = ($onlinehandler->getCount(new Criteria('online_uid', $this->getVar('uid', 'N'))) > 0) ? true : false;
274: }
275: return $this->_isOnline;
276: }
277: 278: 279: 280: 281: 282: 283: 284:
285: 286: 287: 288:
289: function uid()
290: {
291: return $this->getVar('uid');
292: }
293:
294: 295: 296: 297: 298:
299: function name($format="S")
300: {
301: return $this->getVar("name", $format);
302: }
303:
304: 305: 306: 307: 308:
309: function uname($format="S")
310: {
311: return $this->getVar("uname", $format);
312: }
313:
314: 315: 316: 317: 318: 319:
320: function email($format="S")
321: {
322: return $this->getVar("email", $format);
323: }
324:
325: function url($format="S")
326: {
327: return $this->getVar("url", $format);
328: }
329:
330: function user_avatar($format="S")
331: {
332: return $this->getVar("user_avatar");
333: }
334:
335: function user_regdate()
336: {
337: return $this->getVar("user_regdate");
338: }
339:
340: function user_icq($format="S")
341: {
342: return $this->getVar("user_icq", $format);
343: }
344:
345: function user_from($format="S")
346: {
347: return $this->getVar("user_from", $format);
348: }
349: function user_sig($format="S")
350: {
351: return $this->getVar("user_sig", $format);
352: }
353:
354: function user_viewemail()
355: {
356: return $this->getVar("user_viewemail");
357: }
358:
359: function actkey()
360: {
361: return $this->getVar("actkey");
362: }
363:
364: function user_aim($format="S")
365: {
366: return $this->getVar("user_aim", $format);
367: }
368:
369: function user_yim($format="S")
370: {
371: return $this->getVar("user_yim", $format);
372: }
373:
374: function user_msnm($format="S")
375: {
376: return $this->getVar("user_msnm", $format);
377: }
378:
379: function pass()
380: {
381: return $this->getVar("pass");
382: }
383:
384: function posts()
385: {
386: return $this->getVar("posts");
387: }
388:
389: function attachsig()
390: {
391: return $this->getVar("attachsig");
392: }
393:
394: function level()
395: {
396: return $this->getVar("level");
397: }
398:
399: function theme()
400: {
401: return $this->getVar("theme");
402: }
403:
404: function timezone()
405: {
406: return $this->getVar("timezone_offset");
407: }
408:
409: function umode()
410: {
411: return $this->getVar("umode");
412: }
413:
414: function uorder()
415: {
416: return $this->getVar("uorder");
417: }
418:
419:
420: function notify_method()
421: {
422: return $this->getVar("notify_method");
423: }
424:
425: function notify_mode()
426: {
427: return $this->getVar("notify_mode");
428: }
429:
430: function user_occ($format="S")
431: {
432: return $this->getVar("user_occ", $format);
433: }
434:
435: function bio($format="S")
436: {
437: return $this->getVar("bio", $format);
438: }
439:
440: function user_intrest($format="S")
441: {
442: return $this->getVar("user_intrest", $format);
443: }
444:
445: function last_login()
446: {
447: return $this->getVar("last_login");
448: }
449:
450: 451: 452: 453:
454: function hasAvatar()
455: {
456: $avatar=$this->getVar('user_avatar');
457: if(!$avatar || $avatar=="blank.gif")
458: return false;
459:
460: $file=XOOPS_UPLOAD_PATH."/".$avatar;
461: return file_exists($file);
462: }
463:
464: 465: 466: 467: 468: 469:
470: function getAvatarUrl()
471: {
472: if($this->hasAvatar())
473: return XOOPS_UPLOAD_URL."/".$this->getVar('user_avatar');
474:
475: return null;
476: }
477:
478:
479:
480: }
481:
482: 483: 484: 485: 486: 487:
488: class XoopsGuestUser extends XoopsUser
489: {
490: 491: 492: 493: 494: 495:
496: function isGuest()
497: {
498: return true;
499: }
500:
501: function getGroups()
502: {
503: return XOOPS_GROUP_ANONYMOUS;
504: }
505: }
506:
507:
508: 509: 510: 511: 512: 513: 514: 515: 516:
517: class XoopsUserHandler extends XoopsObjectHandler
518: {
519:
520: 521: 522: 523: 524: 525:
526: function &create($isNew = true)
527: {
528: $user =new XoopsUser();
529: if ($isNew) {
530: $user->setNew();
531: }
532: return $user;
533: }
534:
535: 536: 537: 538: 539: 540:
541: function &get($id)
542: {
543: $ret = false;
544: if ((int)$id > 0) {
545: $sql = 'SELECT * FROM '.$this->db->prefix('users').' WHERE uid='.$id;
546: if ($result = $this->db->query($sql)) {
547: $numrows = $this->db->getRowsNum($result);
548: if ($numrows == 1) {
549: $user =new XoopsUser();
550: $user->assignVars($this->db->fetchArray($result));
551: $ret =& $user;
552: }
553: }
554: }
555: return $ret;
556: }
557:
558: 559: 560: 561: 562: 563: 564:
565: function insert(&$user, $force = false)
566: {
567: if (strtolower(get_class($user)) != 'xoopsuser') {
568: return false;
569: }
570: if (!$user->isDirty()) {
571: return true;
572: }
573: if (!$user->cleanVars()) {
574: return false;
575: }
576: foreach ($user->cleanVars as $k => $v) {
577: ${$k} = $v;
578: }
579:
580:
581: if ($user->isNew()) {
582: $config = xoops_gethandler('config');
583: $options = $config->getConfigs(new Criteria('conf_name', 'notify_method'));
584: if (isset($options) and (count($options) == 1)) {
585: $notify_method = $options[0]->getvar('conf_value');
586: }
587: $uid = $this->db->genId('users_uid_seq');
588: $sql = sprintf("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);
589: } else {
590: $sql = sprintf("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);
591: }
592: if (false != $force) {
593: $result = $this->db->queryF($sql);
594: } else {
595: $result = $this->db->query($sql);
596: }
597: if (!$result) {
598: return false;
599: }
600: if (empty($uid)) {
601: $uid = $this->db->getInsertId();
602: }
603: $user->assignVar('uid', $uid);
604: return true;
605: }
606:
607: 608: 609: 610: 611: 612: 613:
614: function delete(&$user, $force = false)
615: {
616: if (strtolower(get_class($user)) != 'xoopsuser') {
617: return false;
618: }
619: $sql = sprintf("DELETE FROM %s WHERE uid = %u", $this->db->prefix("users"), $user->getVar('uid'));
620: if (false != $force) {
621: $result = $this->db->queryF($sql);
622: } else {
623: $result = $this->db->query($sql);
624: }
625: if (!$result) {
626: return false;
627: }
628: return true;
629: }
630:
631: 632: 633: 634: 635: 636: 637:
638: function &getObjects($criteria = null, $id_as_key = false)
639: {
640: $ret = array();
641: $limit = $start = 0;
642: $sql = 'SELECT * FROM '.$this->db->prefix('users');
643: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
644: $sql .= ' '.$criteria->renderWhere();
645: if ($criteria->getSort() != '') {
646: $sql .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
647: }
648: $limit = $criteria->getLimit();
649: $start = $criteria->getStart();
650: }
651: $result = $this->db->query($sql, $limit, $start);
652: if (!$result) {
653: return $ret;
654: }
655: while ($myrow = $this->db->fetchArray($result)) {
656: $user =new XoopsUser();
657: $user->assignVars($myrow);
658: if (!$id_as_key) {
659: $ret[] =& $user;
660: } else {
661: $ret[$myrow['uid']] =& $user;
662: }
663: unset($user);
664: }
665: return $ret;
666: }
667:
668: 669: 670: 671: 672:
673: function &getObjectsByLevel($level=0)
674: {
675: $ret=array();
676: $level=(int)$level;
677: $result = $this->db->query("SELECT * FROM ".$this->db->prefix("users")." WHERE level > $level ORDER BY uname");
678: if(!$result)
679: return $ret;
680:
681: while($myrow=$this->db->fetchArray($result)) {
682: $user=new XoopsUser();
683: $user->assignVars($myrow);
684: $ret[]=&$user;
685: unset($user);
686: }
687:
688: return $ret;
689: }
690:
691: 692: 693: 694: 695: 696:
697: function getCount($criteria = null)
698: {
699: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('users');
700: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
701: $sql .= ' '.$criteria->renderWhere();
702: }
703: $result = $this->db->query($sql);
704: if (!$result) {
705: return 0;
706: }
707: list($count) = $this->db->fetchRow($result);
708: return $count;
709: }
710:
711: 712: 713: 714: 715: 716:
717: function deleteAll($criteria = null)
718: {
719: $sql = 'DELETE FROM '.$this->db->prefix('users');
720: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
721: $sql .= ' '.$criteria->renderWhere();
722: }
723: if (!$result = $this->db->query($sql)) {
724: return false;
725: }
726: return true;
727: }
728:
729: 730: 731: 732: 733: 734: 735: 736: 737:
738: function updateAll($fieldname, $fieldvalue, $criteria = null)
739: {
740: $set_clause = is_numeric($fieldvalue) ? $fieldname.' = '.$fieldvalue : $fieldname.' = '.$this->db->quoteString($fieldvalue);
741: $sql = 'UPDATE '.$this->db->prefix('users').' SET '.$set_clause;
742: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
743: $sql .= ' '.$criteria->renderWhere();
744: }
745: if (!$result = $this->db->query($sql)) {
746: return false;
747: }
748: return true;
749: }
750: }
751: ?>
752: