source: temp/test-xoops.ec-cube.net/html/kernel/user.php @ 405

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