[ Index ]

PHP Cross Reference of Xoops v2.3.1

[Global whois Lookup]    [Ranchi, Jharkhand, India website]     [Hindi Magazine]     [Desi Community website in tristate area]    [B 4 Bollywood]    [Internet nation of India]

title

Body

[close]

/kernel/ -> member.php (source)

   1  <?php
   2  // $Id: member.php 1513 2008-04-28 00:51:02Z phppp $

   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  
  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      /**#@+

  53      * holds reference to group handler(DAO) class

  54      * @access private

  55      */
  56      var $_gHandler;
  57  
  58      /**

  59      * holds reference to user handler(DAO) class

  60      */
  61      var $_uHandler;
  62  
  63      /**

  64      * holds reference to membership handler(DAO) class

  65      */
  66      var $_mHandler;
  67  
  68      /**

  69      * holds temporary user objects

  70      */
  71      var $_members = array();
  72      /**#@-*/

  73  
  74      /**

  75       * constructor

  76       *

  77       */
  78      function XoopsMemberHandler(&$db)
  79      {
  80          $this->_gHandler = new XoopsGroupHandler($db);
  81          $this->_uHandler = new XoopsUserHandler($db);
  82          $this->_mHandler = new XoopsMembershipHandler($db);
  83      }
  84  
  85      /**

  86       * create a new group

  87       *

  88       * @return object XoopsGroup reference to the new group

  89       */
  90      function &createGroup()
  91      {
  92          $inst =& $this->_gHandler->create();
  93          return $inst;
  94      }
  95  
  96      /**

  97       * create a new user

  98       *

  99       * @return object XoopsUser reference to the new user

 100       */
 101      function &createUser()
 102      {
 103          $inst =& $this->_uHandler->create();
 104          return $inst;
 105      }
 106  
 107      /**

 108       * retrieve a group

 109       *

 110       * @param int $id ID for the group

 111       * @return object XoopsGroup reference to the group

 112       */
 113      function getGroup($id)
 114      {
 115          return $this->_gHandler->get($id);
 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 object XoopsUser reference to the logged in user. FALSE if failed to log in

 326       */
 327      function &loginUser($uname, $pwd)
 328      {
 329          $criteria = new CriteriaCompo(new Criteria('uname', $uname));
 330          $criteria->add(new Criteria('pass', md5($pwd)));
 331          $user = $this->_uHandler->getObjects($criteria, false);
 332          if (!$user || count($user) != 1) {
 333              $user = false;
 334              return $user;
 335          }
 336          return $user[0];
 337      }
 338  
 339      /**

 340       * logs in a user with an nd5 encrypted password

 341       *

 342       * @param string $uname username

 343       * @param string $md5pwd password encrypted with md5

 344       * @return object XoopsUser reference to the logged in user. FALSE if failed to log in

 345       */
 346      function &loginUserMd5($uname, $md5pwd)
 347      {
 348          $criteria = new CriteriaCompo(new Criteria('uname', $uname));
 349          $criteria->add(new Criteria('pass', $md5pwd));
 350          $user = $this->_uHandler->getObjects($criteria, false);
 351          if (!$user || count($user) != 1) {
 352              $user = false;
 353              return $user;
 354          }
 355          return $user[0];
 356      }
 357  
 358      /**

 359       * count users matching certain conditions

 360       *

 361       * @param object $criteria {@link CriteriaElement} object

 362       * @return int

 363       */
 364      function getUserCount($criteria = null)
 365      {
 366          return $this->_uHandler->getCount($criteria);
 367      }
 368  
 369      /**

 370       * count users belonging to a group

 371       *

 372       * @param int $group_id ID of the group

 373       * @return int

 374       */
 375      function getUserCountByGroup($group_id)
 376      {
 377          return $this->_mHandler->getCount(new Criteria('groupid', $group_id));
 378      }
 379  
 380      /**

 381       * updates a single field in a users record

 382       *

 383       * @param object $user reference to the {@link XoopsUser} object

 384       * @param string $fieldName name of the field to update

 385       * @param string $fieldValue updated value for the field

 386       * @return bool TRUE if success or unchanged, FALSE on failure

 387       */
 388      function updateUserByField(&$user, $fieldName, $fieldValue)
 389      {
 390          $user->setVar($fieldName, $fieldValue);
 391          return $this->insertUser($user);
 392      }
 393  
 394      /**

 395       * updates a single field in a users record

 396       *

 397       * @param string $fieldName name of the field to update

 398       * @param string $fieldValue updated value for the field

 399       * @param object $criteria {@link CriteriaElement} object

 400       * @return bool TRUE if success or unchanged, FALSE on failure

 401       */
 402      function updateUsersByField($fieldName, $fieldValue, $criteria = null)
 403      {
 404          return $this->_uHandler->updateAll($fieldName, $fieldValue, $criteria);
 405      }
 406  
 407      /**

 408       * activate a user

 409       *

 410       * @param object $user reference to the {@link XoopsUser} object

 411       * @return bool successful?

 412       */
 413      function activateUser(&$user)
 414      {
 415          if ($user->getVar('level') != 0) {
 416              return true;
 417          }
 418          $user->setVar('level', 1);
 419          return $this->_uHandler->insert($user, true);
 420      }
 421  
 422      /**

 423       * Get a list of users belonging to certain groups and matching criteria

 424       * Temporary solution

 425       *

 426       * @param int $groups IDs of groups

 427       * @param object $criteria {@link CriteriaElement} object

 428       * @param bool $asobject return the users as objects?

 429       * @param bool $id_as_key use the UID as key for the array if $asobject is TRUE

 430       * @return array Array of {@link XoopsUser} objects (if $asobject is TRUE)

 431       * or of associative arrays matching the record structure in the database.

 432       */
 433      function getUsersByGroupLink($groups, $criteria = null, $asobject = false, $id_as_key = false)
 434      {
 435          $ret = array();
 436  
 437          $select = $asobject ? "u.*" : "u.uid";
 438          $sql[] = "    SELECT DISTINCT {$select} ".
 439                  "    FROM " . $this->_uHandler->db->prefix("users") . " AS u".
 440                  "        LEFT JOIN ". $this->_mHandler->db->prefix("groups_users_link") . " AS m ON m.uid = u.uid".
 441                  "    WHERE 1 = 1";
 442          if (!empty($groups)) {
 443              $sql[] = "m.groupid IN (".implode(", ", $groups).")";
 444          }
 445          $limit = $start = 0;
 446          if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
 447              $sql_criteria = $criteria->render();
 448              if ($criteria->getSort() != '') {
 449                  $sql_criteria .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
 450              }
 451              $limit = $criteria->getLimit();
 452              $start = $criteria->getStart();
 453              if ($sql_criteria) {
 454                  $sql[] = $sql_criteria;
 455              }
 456          }
 457          $sql_string = implode(" AND ", array_filter($sql));
 458          if (!$result = $this->_uHandler->db->query($sql_string, $limit, $start)) {
 459              return $ret;
 460          }
 461          while ($myrow = $this->_uHandler->db->fetchArray($result)) {
 462              if ($asobject) {
 463                  $user = new XoopsUser();
 464                  $user->assignVars($myrow);
 465                  if (!$id_as_key) {
 466                      $ret[] =& $user;
 467                  } else {
 468                      $ret[$myrow['uid']] =& $user;
 469                  }
 470                  unset($user);
 471              } else {
 472                  $ret[] = $myrow['uid'];
 473              }
 474          }
 475          return $ret;
 476      }
 477  
 478      /**

 479       * Get count of users belonging to certain groups and matching criteria

 480       * Temporary solution

 481       *

 482       * @param int $groups IDs of groups

 483       * @return int count of users

 484       */
 485      function getUserCountByGroupLink($groups, $criteria = null)
 486      {
 487          $ret = 0;
 488  
 489          $sql[] = "    SELECT COUNT(DISTINCT u.uid) ".
 490                  "    FROM " . $this->_uHandler->db->prefix("users") . " AS u".
 491                  "        LEFT JOIN ". $this->_mHandler->db->prefix("groups_users_link") . " AS m ON m.uid = u.uid".
 492                  "    WHERE 1 = 1";
 493          if (!empty($groups)) {
 494              $sql[] = "m.groupid IN (".implode(", ", $groups).")";
 495          }
 496          if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
 497              $sql[] = $criteria->render();
 498          }
 499          $sql_string = implode(" AND ", array_filter($sql));
 500          if (!$result = $this->_uHandler->db->query($sql_string)) {
 501              return $ret;
 502          }
 503          list($ret) = $this->_uHandler->db->fetchRow($result);
 504          return $ret;
 505      }
 506  
 507  }
 508  ?>


[ Xoops]     [PhpNuke]     [PostNuke]     [Joomla]    [Drupal]    [E107]    [NucleusCms]
[Php-Fusion]     [PhpBB]     [WordPress]     [Typo3]
Generated: Mon Oct 27 11:51:45 2008
Open Source related documentation for developers.