1: <?php
2: // $Id: member.php,v 1.1 2007/05/15 02:34:37 minahito 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://xoopscube.jp/ //
29: // Project: The XOOPS Project //
30: // ------------------------------------------------------------------------- //
31:
32: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35: require_once XOOPS_ROOT_PATH.'/kernel/user.php';
36: require_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 <onokazu@xoops.org>
45: * @copyright copyright (c) 2000-2003 XOOPS.org
46: * @package kernel
47: */
48:
49: class 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: function &getUserByEmail($email)
133: {
134: $returnUser = null;
135:
136: $myts =& MyTextSanitizer::getInstance(); ///< @todo not depends
137: $users =& $this->getUsers(new Criteria('email', $myts->addSlashes($email)));
138:
139: if(!is_array($users)) {
140: $returnUser=null;
141: }
142: else if(count($users) > 0) {
143: $returnUser = is_object($users[0]) ? $users[0] : null;
144: }
145:
146: return $returnUser;
147: }
148:
149: /**
150: * Delete function. This function is virtual overload function.
151: * @param $object XoopsUser or XoopsGroup
152: */
153: function delete(&$object)
154: {
155: if (is_a($object, "XoopsUser")) {
156: return $this->deleteUser($object);
157: }
158: elseif (is_a($object, "XoopsGroup")) {
159: return $this->deleteGroup($object);
160: }
161: }
162:
163: /**
164: * delete a group
165: *
166: * @param object $group reference to the group to delete
167: * @return bool FALSE if failed
168: */
169: function deleteGroup(&$group)
170: {
171: $this->_gHandler->delete($group);
172: $this->_mHandler->deleteAll(new Criteria('groupid', $group->getVar('groupid')));
173: return true;
174: }
175:
176: /**
177: * delete a user
178: *
179: * @param object $user reference to the user to delete
180: * @return bool FALSE if failed
181: */
182: function deleteUser(&$user)
183: {
184: $this->_uHandler->delete($user);
185: $this->_mHandler->deleteAll(new Criteria('uid', $user->getVar('uid')));
186: return true;
187: }
188:
189: /**
190: * insert a group into the database
191: *
192: * @param object $group reference to the group to insert
193: * @return bool TRUE if already in database and unchanged
194: * FALSE on failure
195: */
196: function insertGroup(&$group)
197: {
198: return $this->_gHandler->insert($group);
199: }
200:
201: /**
202: * insert a user into the database
203: *
204: * @param object $user reference to the user to insert
205: * @return bool TRUE if already in database and unchanged
206: * FALSE on failure
207: */
208: function insertUser(&$user, $force = false)
209: {
210: return $this->_uHandler->insert($user, $force);
211: }
212:
213: /**
214: * retrieve groups from the database
215: *
216: * @param object $criteria {@link CriteriaElement}
217: * @param bool $id_as_key use the group's ID as key for the array?
218: * @return array array of {@link XoopsGroup} objects
219: */
220: function &getGroups($criteria = null, $id_as_key = false)
221: {
222: $groups =& $this->_gHandler->getObjects($criteria, $id_as_key);
223: return $groups;
224: }
225:
226: /**
227: * retrieve users from the database
228: *
229: * @param object $criteria {@link CriteriaElement}
230: * @param bool $id_as_key use the group's ID as key for the array?
231: * @return array array of {@link XoopsUser} objects
232: */
233: function &getUsers($criteria = null, $id_as_key = false)
234: {
235: $users =& $this->_uHandler->getObjects($criteria, $id_as_key);
236: return $users;
237: }
238:
239: /**
240: * get a list of groupnames and their IDs
241: *
242: * @param object $criteria {@link CriteriaElement} object
243: * @return array associative array of group-IDs and names
244: */
245: function &getGroupList($criteria = null)
246: {
247: $groups =& $this->_gHandler->getObjects($criteria, true);
248: $ret = array();
249: foreach (array_keys($groups) as $i) {
250: $ret[$i] = $groups[$i]->getVar('name');
251: }
252: return $ret;
253: }
254:
255: /**
256: * get a list of usernames and their IDs
257: *
258: * @param object $criteria {@link CriteriaElement} object
259: * @return array associative array of user-IDs and names
260: */
261: function getUserList($criteria = null)
262: {
263: $users =& $this->_uHandler->getObjects($criteria, true);
264: $ret = array();
265: foreach (array_keys($users) as $i) {
266: $ret[$i] = $users[$i]->getVar('uname');
267: }
268: return $ret;
269: }
270:
271: /**
272: * add a user to a group
273: *
274: * @param int $group_id ID of the group
275: * @param int $user_id ID of the user
276: * @return object XoopsMembership
277: */
278: function addUserToGroup($group_id, $user_id)
279: {
280: $group_ids =& $this->getGroupsByUser($user_id);
281: if (!in_array($group_id, $group_ids)) {
282: $mship =& $this->_mHandler->create();
283: $mship->setVar('groupid', $group_id);
284: $mship->setVar('uid', $user_id);
285: return $this->_mHandler->insert($mship);
286: }
287:
288: return true;
289: }
290:
291:
292: /**
293: * remove a one user from a group.
294: * @param int $group_id ID of the group
295: * @param int $user_id ID of the user
296: * @return bool success?
297: */
298: function removeUserFromGroup($group_id, $user_id)
299: {
300: $user_ids = array($user_id);
301: return $this->removeUsersFromGroup($group_id, $user_ids);
302: }
303:
304: /**
305: * remove a list of users from a group
306: *
307: * @param int $group_id ID of the group
308: * @param array $user_ids array of user-IDs
309: * @return bool success?
310: */
311: function removeUsersFromGroup($group_id, $user_ids = array())
312: {
313: $criteria = new CriteriaCompo();
314: $criteria->add(new Criteria('groupid', $group_id));
315: $criteria2 = new CriteriaCompo();
316: foreach ($user_ids as $uid) {
317: $criteria2->add(new Criteria('uid', $uid), 'OR');
318: }
319: $criteria->add($criteria2);
320: return $this->_mHandler->deleteAll($criteria);
321: }
322:
323: /**
324: * get a list of users belonging to a group
325: *
326: * @param int $group_id ID of the group
327: * @param bool $asobject return the users as objects?
328: * @param int $limit number of users to return
329: * @param int $start index of the first user to return
330: * @return array Array of {@link XoopsUser} objects (if $asobject is TRUE)
331: * or of associative arrays matching the record structure in the database.
332: */
333: function &getUsersByGroup($group_id, $asobject = false, $limit = 0, $start = 0)
334: {
335: $user_ids =& $this->_mHandler->getUsersByGroup($group_id, $limit, $start);
336: if (!$asobject) {
337: return $user_ids;
338: } else {
339: $ret = array();
340: foreach ($user_ids as $u_id) {
341: $user =& $this->getUser($u_id);
342: if (is_object($user)) {
343: $ret[] =& $user;
344: }
345: unset($user);
346: }
347: return $ret;
348: }
349: }
350:
351: /**
352: * @see getUsersByGroup
353: */
354: function &getUsersByNoGroup($group_id, $asobject = false, $limit = 0, $start = 0)
355: {
356: $user_ids =& $this->_mHandler->getUsersByNoGroup($group_id, $limit, $start);
357: if (!$asobject) {
358: return $user_ids;
359: } else {
360: $ret = array();
361: foreach ($user_ids as $u_id) {
362: $user =& $this->getUser($u_id);
363: if (is_object($user)) {
364: $ret[] =& $user;
365: }
366: unset($user);
367: }
368: return $ret;
369: }
370: }
371:
372: /**
373: * get a list of groups that a user is member of
374: *
375: * @param int $user_id ID of the user
376: * @param bool $asobject return groups as {@link XoopsGroup} objects or arrays?
377: * @return array array of objects or arrays
378: */
379: function &getGroupsByUser($user_id, $asobject = false)
380: {
381: $group_ids =& $this->_mHandler->getGroupsByUser($user_id);
382: if (!$asobject) {
383: return $group_ids;
384: } else {
385: $ret = array();
386: foreach ($group_ids as $g_id) {
387: $ret[] =& $this->getGroup($g_id);
388: }
389: return $ret;
390: }
391: }
392:
393: /**
394: * log in a user
395: *
396: * @param string $uname username as entered in the login form
397: * @param string $pwd password entered in the login form
398: * @return object XoopsUser reference to the logged in user. FALSE if failed to log in
399: */
400: function &loginUser($uname, $pwd)
401: {
402: $criteria = new CriteriaCompo(new Criteria('uname', $uname));
403: $criteria->add(new Criteria('pass', md5($pwd)));
404: $user =& $this->_uHandler->getObjects($criteria, false);
405: if (!$user || count($user) != 1) {
406: $ret = false;
407: return $ret;
408: }
409: return $user[0];
410: }
411:
412: /**
413: * logs in a user with an md5 encrypted password
414: *
415: * @param string $uname username
416: * @param string $md5pwd password encrypted with md5
417: * @return object XoopsUser reference to the logged in user. FALSE if failed to log in
418: */
419: function &loginUserMd5($uname, $md5pwd)
420: {
421: $criteria = new CriteriaCompo(new Criteria('uname', $uname));
422: $criteria->add(new Criteria('pass', $md5pwd));
423: $user =& $this->_uHandler->getObjects($criteria, false);
424: if (!$user || count($user) != 1) {
425: $ret = false;
426: return $ret;
427: }
428: return $user[0];
429: }
430:
431: /**
432: * count users matching certain conditions
433: *
434: * @param object $criteria {@link CriteriaElement} object
435: * @return int
436: */
437: function getUserCount($criteria = null)
438: {
439: return $this->_uHandler->getCount($criteria);
440: }
441:
442: /**
443: * count users belonging to a group
444: *
445: * @param int $group_id ID of the group
446: * @return int
447: */
448: function getUserCountByGroup($group_id)
449: {
450: return $this->_mHandler->getCount(new Criteria('groupid', $group_id));
451: }
452:
453: /**
454: * @see getUserCountByGroup
455: * @return int
456: */
457: function getUserCountByNoGroup($group_id)
458: {
459: $groupid = (int)$group_id;
460: $usersTable = $this->_mHandler->db->prefix('users');
461: $linkTable = $this->_mHandler->db->prefix('groups_users_link');
462:
463: $sql = "SELECT count(*) FROM ${usersTable} u LEFT JOIN ${linkTable} g ON u.uid=g.uid," .
464: "${usersTable} u2 LEFT JOIN ${linkTable} g2 ON u2.uid=g2.uid AND g2.groupid=${groupid} " .
465: "WHERE (g.groupid != ${groupid} OR g.groupid IS NULL) " .
466: "AND (g2.groupid = ${groupid} OR g2.groupid IS NULL) " .
467: "AND u.uid = u2.uid AND g2.uid IS NULL GROUP BY u.uid";
468:
469: $result = $this->_mHandler->db->query($sql);
470: if (!$result) {
471: return 0;
472: }
473:
474: $count = $this->_mHandler->db->getRowsNum($result);
475:
476: return $count;
477: }
478:
479: /**
480: * updates a single field in a users record
481: *
482: * @param object $user reference to the {@link XoopsUser} object
483: * @param string $fieldName name of the field to update
484: * @param string $fieldValue updated value for the field
485: * @return bool TRUE if success or unchanged, FALSE on failure
486: */
487: function updateUserByField(&$user, $fieldName, $fieldValue)
488: {
489: $user->setVar($fieldName, $fieldValue);
490: return $this->insertUser($user);
491: }
492:
493: /**
494: * updates a single field in a users record
495: *
496: * @param string $fieldName name of the field to update
497: * @param string $fieldValue updated value for the field
498: * @param object $criteria {@link CriteriaElement} object
499: * @return bool TRUE if success or unchanged, FALSE on failure
500: */
501: function updateUsersByField($fieldName, $fieldValue, $criteria = null)
502: {
503: return $this->_uHandler->updateAll($fieldName, $fieldValue, $criteria);
504: }
505:
506: /**
507: * activate a user
508: *
509: * @param object $user reference to the {@link XoopsUser} object
510: * @return bool successful?
511: */
512: function activateUser(&$user)
513: {
514: if ($user->getVar('level') != 0) {
515: return true;
516: }
517: $user->setVar('level', 1);
518: return $this->_uHandler->insert($user, true);
519: }
520:
521: }
522: ?>
523: