home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / member.php < prev    next >
Encoding:
PHP Script  |  2007-09-09  |  16.3 KB  |  508 lines

  1. <?php
  2. // $Id: member.php 1029 2007-09-09 03:49:25Z 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.     * 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.         $inst =& $this->_gHandler->create();
  92.         return $inst;
  93.     }
  94.  
  95.     /**
  96.      * create a new user
  97.      *
  98.      * @return object XoopsUser reference to the new user
  99.      */
  100.     function &createUser()
  101.     {
  102.         $inst =& $this->_uHandler->create();
  103.         return $inst;
  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.         return $this->_gHandler->get($id);
  115.     }
  116.  
  117.     /**
  118.      * retrieve a user
  119.      *
  120.      * @param int $id ID for the user
  121.      * @return object XoopsUser reference to the user
  122.      */
  123.     function &getUser($id)
  124.     {
  125.         if (!isset($this->_members[$id])) {
  126.             $this->_members[$id] =& $this->_uHandler->get($id);
  127.         }
  128.         return $this->_members[$id];
  129.     }
  130.  
  131.     /**
  132.      * delete a group
  133.      *
  134.      * @param object $group reference to the group to delete
  135.      * @return bool FALSE if failed
  136.      */
  137.     function deleteGroup(&$group)
  138.     {
  139.         $this->_gHandler->delete($group);
  140.         $this->_mHandler->deleteAll(new Criteria('groupid', $group->getVar('groupid')));
  141.         return true;
  142.     }
  143.  
  144.     /**
  145.      * delete a user
  146.      *
  147.      * @param object $user reference to the user to delete
  148.      * @return bool FALSE if failed
  149.      */
  150.     function deleteUser(&$user)
  151.     {
  152.         $this->_uHandler->delete($user);
  153.         $this->_mHandler->deleteAll(new Criteria('uid', $user->getVar('uid')));
  154.         return true;
  155.     }
  156.  
  157.     /**
  158.      * insert a group into the database
  159.      *
  160.      * @param object $group reference to the group to insert
  161.      * @return bool TRUE if already in database and unchanged
  162.      * FALSE on failure
  163.      */
  164.     function insertGroup(&$group)
  165.     {
  166.         return $this->_gHandler->insert($group);
  167.     }
  168.  
  169.     /**
  170.      * insert a user into the database
  171.      *
  172.      * @param object $user reference to the user to insert
  173.      * @return bool TRUE if already in database and unchanged
  174.      * FALSE on failure
  175.      */
  176.     function insertUser(&$user, $force = false)
  177.     {
  178.         return $this->_uHandler->insert($user, $force);
  179.     }
  180.  
  181.     /**
  182.      * retrieve groups from the database
  183.      *
  184.      * @param object $criteria {@link CriteriaElement}
  185.      * @param bool $id_as_key use the group's ID as key for the array?
  186.      * @return array array of {@link XoopsGroup} objects
  187.      */
  188.     function getGroups($criteria = null, $id_as_key = false)
  189.     {
  190.         return $this->_gHandler->getObjects($criteria, $id_as_key);
  191.     }
  192.  
  193.     /**
  194.      * retrieve users from the database
  195.      *
  196.      * @param object $criteria {@link CriteriaElement}
  197.      * @param bool $id_as_key use the group's ID as key for the array?
  198.      * @return array array of {@link XoopsUser} objects
  199.      */
  200.     function getUsers($criteria = null, $id_as_key = false)
  201.     {
  202.         return $this->_uHandler->getObjects($criteria, $id_as_key);
  203.     }
  204.  
  205.     /**
  206.      * get a list of groupnames and their IDs
  207.      *
  208.      * @param object $criteria {@link CriteriaElement} object
  209.      * @return array associative array of group-IDs and names
  210.      */
  211.     function getGroupList($criteria = null)
  212.     {
  213.         $groups = $this->_gHandler->getObjects($criteria, true);
  214.         $ret = array();
  215.         foreach (array_keys($groups) as $i) {
  216.             $ret[$i] = $groups[$i]->getVar('name');
  217.         }
  218.         return $ret;
  219.     }
  220.  
  221.     /**
  222.      * get a list of usernames and their IDs
  223.      *
  224.      * @param object $criteria {@link CriteriaElement} object
  225.      * @return array associative array of user-IDs and names
  226.      */
  227.     function getUserList($criteria = null)
  228.     {
  229.         $users = $this->_uHandler->getObjects($criteria, true);
  230.         $ret = array();
  231.         foreach (array_keys($users) as $i) {
  232.             $ret[$i] = $users[$i]->getVar('uname');
  233.         }
  234.         return $ret;
  235.     }
  236.  
  237.     /**
  238.      * add a user to a group
  239.      *
  240.      * @param int $group_id ID of the group
  241.      * @param int $user_id ID of the user
  242.      * @return object XoopsMembership
  243.      */
  244.     function addUserToGroup($group_id, $user_id)
  245.     {
  246.         $mship =& $this->_mHandler->create();
  247.         $mship->setVar('groupid', $group_id);
  248.         $mship->setVar('uid', $user_id);
  249.         return $this->_mHandler->insert($mship);
  250.     }
  251.  
  252.     /**
  253.      * remove a list of users from a group
  254.      *
  255.      * @param int $group_id ID of the group
  256.      * @param array $user_ids array of user-IDs
  257.      * @return bool success?
  258.      */
  259.     function removeUsersFromGroup($group_id, $user_ids = array())
  260.     {
  261.         $criteria = new CriteriaCompo();
  262.         $criteria->add(new Criteria('groupid', $group_id));
  263.         $criteria2 = new CriteriaCompo();
  264.         foreach ($user_ids as $uid) {
  265.             $criteria2->add(new Criteria('uid', $uid), 'OR');
  266.         }
  267.         $criteria->add($criteria2);
  268.         return $this->_mHandler->deleteAll($criteria);
  269.     }
  270.  
  271.     /**
  272.      * get a list of users belonging to a group
  273.      *
  274.      * @param int $group_id ID of the group
  275.      * @param bool $asobject return the users as objects?
  276.      * @param int $limit number of users to return
  277.      * @param int $start index of the first user to return
  278.      * @return array Array of {@link XoopsUser} objects (if $asobject is TRUE)
  279.      * or of associative arrays matching the record structure in the database.
  280.      */
  281.     function getUsersByGroup($group_id, $asobject = false, $limit = 0, $start = 0)
  282.     {
  283.         $user_ids = $this->_mHandler->getUsersByGroup($group_id, $limit, $start);
  284.         if (!$asobject) {
  285.            return $user_ids;
  286.         } else {
  287.            $ret = array();
  288.            foreach ($user_ids as $u_id) {
  289.                $user =& $this->getUser($u_id);
  290.                 if (is_object($user)) {
  291.                     $ret[] =& $user;
  292.                 }
  293.                 unset($user);
  294.            }
  295.            return $ret;
  296.         }
  297.     }
  298.  
  299.     /**
  300.      * get a list of groups that a user is member of
  301.      *
  302.      * @param int $user_id ID of the user
  303.      * @param bool $asobject return groups as {@link XoopsGroup} objects or arrays?
  304.      * @return array array of objects or arrays
  305.      */
  306.     function getGroupsByUser($user_id, $asobject = false)
  307.     {
  308.         $group_ids = $this->_mHandler->getGroupsByUser($user_id);
  309.         if (!$asobject) {
  310.            return $group_ids;
  311.         } else {
  312.            foreach ($group_ids as $g_id) {
  313.                $ret[] =& $this->getGroup($g_id);
  314.            }
  315.            return $ret;
  316.         }
  317.     }
  318.  
  319.     /**
  320.      * log in a user
  321.      *
  322.      * @param string $uname username as entered in the login form
  323.      * @param string $pwd password entered in the login form
  324.      * @return object XoopsUser reference to the logged in user. FALSE if failed to log in
  325.      */
  326.     function &loginUser($uname, $pwd)
  327.     {
  328.         $criteria = new CriteriaCompo(new Criteria('uname', $uname));
  329.         $criteria->add(new Criteria('pass', md5($pwd)));
  330.         $user = $this->_uHandler->getObjects($criteria, false);
  331.         if (!$user || count($user) != 1) {
  332.             $user = false;
  333.             return $user;
  334.         }
  335.         return $user[0];
  336.     }
  337.  
  338.     /**
  339.      * logs in a user with an nd5 encrypted password
  340.      *
  341.      * @param string $uname username
  342.      * @param string $md5pwd password encrypted with md5
  343.      * @return object XoopsUser reference to the logged in user. FALSE if failed to log in
  344.      */
  345.     function &loginUserMd5($uname, $md5pwd)
  346.     {
  347.         $criteria = new CriteriaCompo(new Criteria('uname', $uname));
  348.         $criteria->add(new Criteria('pass', $md5pwd));
  349.         $user = $this->_uHandler->getObjects($criteria, false);
  350.         if (!$user || count($user) != 1) {
  351.             $user = false;
  352.             return $user;
  353.         }
  354.         return $user[0];
  355.     }
  356.  
  357.     /**
  358.      * count users matching certain conditions
  359.      *
  360.      * @param object $criteria {@link CriteriaElement} object
  361.      * @return int
  362.      */
  363.     function getUserCount($criteria = null)
  364.     {
  365.         return $this->_uHandler->getCount($criteria);
  366.     }
  367.  
  368.     /**
  369.      * count users belonging to a group
  370.      *
  371.      * @param int $group_id ID of the group
  372.      * @return int
  373.      */
  374.     function getUserCountByGroup($group_id)
  375.     {
  376.         return $this->_mHandler->getCount(new Criteria('groupid', $group_id));
  377.     }
  378.  
  379.     /**
  380.      * updates a single field in a users record
  381.      *
  382.      * @param object $user reference to the {@link XoopsUser} object
  383.      * @param string $fieldName name of the field to update
  384.      * @param string $fieldValue updated value for the field
  385.      * @return bool TRUE if success or unchanged, FALSE on failure
  386.      */
  387.     function updateUserByField(&$user, $fieldName, $fieldValue)
  388.     {
  389.         $user->setVar($fieldName, $fieldValue);
  390.         return $this->insertUser($user);
  391.     }
  392.  
  393.     /**
  394.      * updates a single field in a users record
  395.      *
  396.      * @param string $fieldName name of the field to update
  397.      * @param string $fieldValue updated value for the field
  398.      * @param object $criteria {@link CriteriaElement} object
  399.      * @return bool TRUE if success or unchanged, FALSE on failure
  400.      */
  401.     function updateUsersByField($fieldName, $fieldValue, $criteria = null)
  402.     {
  403.         return $this->_uHandler->updateAll($fieldName, $fieldValue, $criteria);
  404.     }
  405.  
  406.     /**
  407.      * activate a user
  408.      *
  409.      * @param object $user reference to the {@link XoopsUser} object
  410.      * @return bool successful?
  411.      */
  412.     function activateUser(&$user)
  413.     {
  414.         if ($user->getVar('level') != 0) {
  415.             return true;
  416.         }
  417.         $user->setVar('level', 1);
  418.         return $this->_uHandler->insert($user, true);
  419.     }
  420.  
  421.     /**
  422.      * Get a list of users belonging to certain groups and matching criteria
  423.      * Temporary solution
  424.      *
  425.      * @param int $groups IDs of groups
  426.      * @param object $criteria {@link CriteriaElement} object
  427.      * @param bool $asobject return the users as objects?
  428.      * @param bool $id_as_key use the UID as key for the array if $asobject is TRUE
  429.      * @return array Array of {@link XoopsUser} objects (if $asobject is TRUE)
  430.      * or of associative arrays matching the record structure in the database.
  431.      */
  432.     function getUsersByGroupLink($groups, $criteria = null, $asobject = false, $id_as_key = false)
  433.     {
  434.         $ret = array();
  435.  
  436.         $select = $asobject ? "u.*" : "u.uid";
  437.         $sql[] = "    SELECT DISTINCT {$select} ".
  438.                 "    FROM " . $this->_uHandler->db->prefix("users") . " AS u".
  439.                 "        LEFT JOIN ". $this->_mHandler->db->prefix("groups_users_link") . " AS m ON m.uid = u.uid".
  440.                 "    WHERE 1 = 1";
  441.         if (!empty($groups)) {
  442.             $sql[] = "m.groupid IN (".implode(", ", $groups).")";
  443.         }
  444.         $limit = $start = 0;
  445.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  446.             $sql_criteria = $criteria->render();
  447.             if ($criteria->getSort() != '') {
  448.                 $sql_criteria .= ' ORDER BY '.$criteria->getSort().' '.$criteria->getOrder();
  449.             }
  450.             $limit = $criteria->getLimit();
  451.             $start = $criteria->getStart();
  452.             if ($sql_criteria) {
  453.                 $sql[] = $sql_criteria;
  454.             }
  455.         }
  456.         $sql_string = implode(" AND ", array_filter($sql));
  457.         if (!$result = $this->_uHandler->db->query($sql_string, $limit, $start)) {
  458.             return $ret;
  459.         }
  460.         while ($myrow = $this->_uHandler->db->fetchArray($result)) {
  461.             if ($asobject) {
  462.                 $user = new XoopsUser();
  463.                 $user->assignVars($myrow);
  464.                 if (!$id_as_key) {
  465.                     $ret[] =& $user;
  466.                 } else {
  467.                     $ret[$myrow['uid']] =& $user;
  468.                 }
  469.                 unset($user);
  470.             } else {
  471.                 $ret[] = $myrow['uid'];
  472.             }
  473.         }
  474.         return $ret;
  475.     }
  476.  
  477.     /**
  478.      * Get count of users belonging to certain groups and matching criteria
  479.      * Temporary solution
  480.      *
  481.      * @param int $groups IDs of groups
  482.      * @return int count of users
  483.      */
  484.     function getUserCountByGroupLink($groups, $criteria = null)
  485.     {
  486.         $ret = 0;
  487.  
  488.         $sql[] = "    SELECT COUNT(DISTINCT u.uid) ".
  489.                 "    FROM " . $this->_uHandler->db->prefix("users") . " AS u".
  490.                 "        LEFT JOIN ". $this->_mHandler->db->prefix("groups_users_link") . " AS m ON m.uid = u.uid".
  491.                 "    WHERE 1 = 1";
  492.         if (!empty($groups)) {
  493.             $sql[] = "m.groupid IN (".implode(", ", $groups).")";
  494.         }
  495.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  496.             $sql[] = $criteria->render();
  497.         }
  498.         $sql_string = implode(" AND ", array_filter($sql));
  499.         if (!$result = $this->_uHandler->db->query($sql_string)) {
  500.             return $ret;
  501.         }
  502.         list($ret) = $this->_uHandler->db->fetchRow($result);
  503.         return $ret;
  504.     }
  505.  
  506. }
  507. ?>
  508.