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

Revision 405, 12.6 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: member.php,v 1.5 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// ------------------------------------------------------------------------- //
31
32if (!defined('XOOPS_ROOT_PATH')) {
33    exit();
34}
35require_once XOOPS_ROOT_PATH.'/kernel/user.php';
36require_once XOOPS_ROOT_PATH.'/kernel/group.php';
37
38/**
39* XOOPS member handler class.
40* This class provides simple interface (a facade class) for handling groups/users/
41* membership data.
42*
43*
44* @author  Kazumi Ono <[email protected]>
45* @copyright copyright (c) 2000-2003 XOOPS.org
46* @package kernel
47*/
48
49class XoopsMemberHandler{
50
51    /**#@+
52    * holds reference to group handler(DAO) class
53    * @access private
54    */
55    var $_gHandler;
56
57    /**
58    * holds reference to user handler(DAO) class
59    */
60    var $_uHandler;
61
62    /**
63    * holds reference to membership handler(DAO) class
64    */
65    var $_mHandler;
66
67    /**
68    * holds temporary user objects
69    */
70    var $_members = array();
71    /**#@-*/
72
73    /**
74     * constructor
75     *
76     */
77    function XoopsMemberHandler(&$db)
78    {
79        $this->_gHandler =& new XoopsGroupHandler($db);
80        $this->_uHandler =& new XoopsUserHandler($db);
81        $this->_mHandler =& new XoopsMembershipHandler($db);
82    }
83
84    /**
85     * create a new group
86     *
87     * @return object XoopsGroup reference to the new group
88     */
89    function &createGroup()
90    {
91        $ret =& $this->_gHandler->create();
92        return $ret;
93    }
94
95    /**
96     * create a new user
97     *
98     * @return object XoopsUser reference to the new user
99     */
100    function &createUser()
101    {
102        $ret =& $this->_uHandler->create();
103        return $ret;
104    }
105
106    /**
107     * retrieve a group
108     *
109     * @param int $id ID for the group
110     * @return object XoopsGroup reference to the group
111     */
112    function &getGroup($id)
113    {
114        $ret =& $this->_gHandler->get($id);
115        return $ret;
116    }
117
118    /**
119     * retrieve a user
120     *
121     * @param int $id ID for the user
122     * @return object XoopsUser reference to the user
123     */
124    function &getUser($id)
125    {
126        if (!isset($this->_members[$id])) {
127            $this->_members[$id] =& $this->_uHandler->get($id);
128        }
129        return $this->_members[$id];
130    }
131
132    /**
133     * delete a group
134     *
135     * @param object $group reference to the group to delete
136     * @return bool FALSE if failed
137     */
138    function deleteGroup(&$group)
139    {
140        $this->_gHandler->delete($group);
141        $this->_mHandler->deleteAll(new Criteria('groupid', $group->getVar('groupid')));
142        return true;
143    }
144
145    /**
146     * delete a user
147     *
148     * @param object $user reference to the user to delete
149     * @return bool FALSE if failed
150     */
151    function deleteUser(&$user)
152    {
153        $this->_uHandler->delete($user);
154        $this->_mHandler->deleteAll(new Criteria('uid', $user->getVar('uid')));
155        return true;
156    }
157
158    /**
159     * insert a group into the database
160     *
161     * @param object $group reference to the group to insert
162     * @return bool TRUE if already in database and unchanged
163     * FALSE on failure
164     */
165    function insertGroup(&$group)
166    {
167        return $this->_gHandler->insert($group);
168    }
169
170    /**
171     * insert a user into the database
172     *
173     * @param object $user reference to the user to insert
174     * @return bool TRUE if already in database and unchanged
175     * FALSE on failure
176     */
177    function insertUser(&$user, $force = false)
178    {
179        return $this->_uHandler->insert($user, $force);
180    }
181
182    /**
183     * retrieve groups from the database
184     *
185     * @param object $criteria {@link CriteriaElement}
186     * @param bool $id_as_key use the group's ID as key for the array?
187     * @return array array of {@link XoopsGroup} objects
188     */
189    function getGroups($criteria = null, $id_as_key = false)
190    {
191        return $this->_gHandler->getObjects($criteria, $id_as_key);
192    }
193
194    /**
195     * retrieve users from the database
196     *
197     * @param object $criteria {@link CriteriaElement}
198     * @param bool $id_as_key use the group's ID as key for the array?
199     * @return array array of {@link XoopsUser} objects
200     */
201    function getUsers($criteria = null, $id_as_key = false)
202    {
203        return $this->_uHandler->getObjects($criteria, $id_as_key);
204    }
205
206    /**
207     * get a list of groupnames and their IDs
208     *
209     * @param object $criteria {@link CriteriaElement} object
210     * @return array associative array of group-IDs and names
211     */
212    function &getGroupList($criteria = null)
213    {
214        $groups =& $this->_gHandler->getObjects($criteria, true);
215        $ret = array();
216        foreach (array_keys($groups) as $i) {
217            $ret[$i] = $groups[$i]->getVar('name');
218        }
219        return $ret;
220    }
221
222    /**
223     * get a list of usernames and their IDs
224     *
225     * @param object $criteria {@link CriteriaElement} object
226     * @return array associative array of user-IDs and names
227     */
228    function getUserList($criteria = null)
229    {
230        $users =& $this->_uHandler->getObjects($criteria, true);
231        $ret = array();
232        foreach (array_keys($users) as $i) {
233            $ret[$i] = $users[$i]->getVar('uname');
234        }
235        return $ret;
236    }
237
238    /**
239     * add a user to a group
240     *
241     * @param int $group_id ID of the group
242     * @param int $user_id ID of the user
243     * @return object XoopsMembership
244     */
245    function addUserToGroup($group_id, $user_id)
246    {
247        $mship =& $this->_mHandler->create();
248        $mship->setVar('groupid', $group_id);
249        $mship->setVar('uid', $user_id);
250        return $this->_mHandler->insert($mship);
251    }
252
253    /**
254     * remove a list of users from a group
255     *
256     * @param int $group_id ID of the group
257     * @param array $user_ids array of user-IDs
258     * @return bool success?
259     */
260    function removeUsersFromGroup($group_id, $user_ids = array())
261    {
262        $criteria = new CriteriaCompo();
263        $criteria->add(new Criteria('groupid', $group_id));
264        $criteria2 = new CriteriaCompo();
265        foreach ($user_ids as $uid) {
266            $criteria2->add(new Criteria('uid', $uid), 'OR');
267        }
268        $criteria->add($criteria2);
269        return $this->_mHandler->deleteAll($criteria);
270    }
271
272    /**
273     * get a list of users belonging to a group
274     *
275     * @param int $group_id ID of the group
276     * @param bool $asobject return the users as objects?
277     * @param int $limit number of users to return
278     * @param int $start index of the first user to return
279     * @return array Array of {@link XoopsUser} objects (if $asobject is TRUE)
280     * or of associative arrays matching the record structure in the database.
281     */
282    function getUsersByGroup($group_id, $asobject = false, $limit = 0, $start = 0)
283    {
284        $user_ids = $this->_mHandler->getUsersByGroup($group_id, $limit, $start);
285        if (!$asobject) {
286           return $user_ids;
287        } else {
288           $ret = array();
289           foreach ($user_ids as $u_id) {
290               $user =& $this->getUser($u_id);
291                if (is_object($user)) {
292                    $ret[] =& $user;
293                }
294                unset($user);
295           }
296           return $ret;
297        }
298    }
299
300    /**
301     * get a list of groups that a user is member of
302     *
303     * @param int $user_id ID of the user
304     * @param bool $asobject return groups as {@link XoopsGroup} objects or arrays?
305     * @return array array of objects or arrays
306     */
307    function getGroupsByUser($user_id, $asobject = false)
308    {
309        $group_ids = $this->_mHandler->getGroupsByUser($user_id);
310        if (!$asobject) {
311           return $group_ids;
312        } else {
313           foreach ($group_ids as $g_id) {
314               $ret[] =& $this->getGroup($g_id);
315           }
316           return $ret;
317        }
318    }
319
320    /**
321     * log in a user
322     *
323     * @param string $uname username as entered in the login form
324     * @param string $pwd password entered in the login form
325     * @return mixed XoopsUser reference to the logged in user. FALSE if failed to log in
326     */
327    function &loginUser($uname, $pwd)
328    {
329        $user =& $this->loginUserMd5($uname, md5($pwd));
330        return $user;
331    }
332
333    /**
334     * logs in a user with an md5 encrypted password
335     *
336     * @param string $uname username
337     * @param string $md5pwd password encrypted with md5
338     * @return mixed XoopsUser reference to the logged in user. FALSE if failed to log in
339     */
340    function &loginUserMd5($uname, $md5pwd)
341    {
342        $criteria = new CriteriaCompo(new Criteria('uname', $uname));
343        $criteria->add(new Criteria('pass', $md5pwd));
344        $user =& $this->_uHandler->getObjects($criteria, false);
345        if (!$user || count($user) != 1) {
346            $ret = false;
347            return $ret;
348        }
349        return $user[0];
350    }
351
352    /**
353     * count users matching certain conditions
354     *
355     * @param object $criteria {@link CriteriaElement} object
356     * @return int
357     */
358    function getUserCount($criteria = null)
359    {
360        return $this->_uHandler->getCount($criteria);
361    }
362
363    /**
364     * count users belonging to a group
365     *
366     * @param int $group_id ID of the group
367     * @return int
368     */
369    function getUserCountByGroup($group_id)
370    {
371        return $this->_mHandler->getCount(new Criteria('groupid', $group_id));
372    }
373
374    /**
375     * updates a single field in a users record
376     *
377     * @param object $user reference to the {@link XoopsUser} object
378     * @param string $fieldName name of the field to update
379     * @param string $fieldValue updated value for the field
380     * @return bool TRUE if success or unchanged, FALSE on failure
381     */
382    function updateUserByField(&$user, $fieldName, $fieldValue)
383    {
384        $user->setVar($fieldName, $fieldValue);
385        return $this->insertUser($user);
386    }
387
388    /**
389     * updates a single field in a users record
390     *
391     * @param string $fieldName name of the field to update
392     * @param string $fieldValue updated value for the field
393     * @param object $criteria {@link CriteriaElement} object
394     * @return bool TRUE if success or unchanged, FALSE on failure
395     */
396    function updateUsersByField($fieldName, $fieldValue, $criteria = null)
397    {
398        return $this->_uHandler->updateAll($fieldName, $fieldValue, $criteria);
399    }
400
401    /**
402     * activate a user
403     *
404     * @param object $user reference to the {@link XoopsUser} object
405     * @return bool successful?
406     */
407    function activateUser(&$user)
408    {
409        if ($user->getVar('level') != 0) {
410            return true;
411        }
412        $user->setVar('level', 1);
413        return $this->_uHandler->insert($user, true);
414    }
415
416}
417?>
Note: See TracBrowser for help on using the repository browser.