b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (Version 1.9) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _usercache.class.php

Documentation is available at _usercache.class.php

  1. <?php
  2. /**
  3.  * This file implements the UserCache class.
  4.  *
  5.  * This file is part of the evoCore framework - {@link http://evocore.net/}
  6.  * See also {@link http://sourceforge.net/projects/evocms/}.
  7.  *
  8.  * @copyright (c)2003-2006 by Francois PLANQUE - {@link http://fplanque.net/}
  9.  *  Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.
  10.  *
  11.  *  {@internal License choice
  12.  *  - If you have received this file as part of a package, please find the license.txt file in
  13.  *    the same folder or the closest folder above for complete license terms.
  14.  *  - If you have received this file individually (e-g: from http://cvs.sourceforge.net/viewcvs.py/evocms/)
  15.  *    then you must choose one of the following licenses before using the file:
  16.  *    - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17.  *    - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18.  *  }}}
  19.  *
  20.  *  {@internal Open Source relicensing agreement:
  21.  *  Daniel HAHLER grants Francois PLANQUE the right to license
  22.  *  Daniel HAHLER's contributions to this file and the b2evolution project
  23.  *  under any OSI approved OSS license (http://www.opensource.org/licenses/).
  24.  *  }}}
  25.  *
  26.  * @package evocore
  27.  *
  28.  *  {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  29.  * @author fplanque: Francois PLANQUE
  30.  * @author blueyed: Daniel HAHLER
  31.  *
  32.  * @version $Id: _usercache.class.php,v 1.3.4.4 2006/11/04 19:54:55 fplanque Exp $
  33.  */
  34. if!defined('EVO_MAIN_INIT') ) die'Please, do not access this page directly.' );
  35.  
  36. /**
  37.  * Includes:
  38.  */
  39. require_once dirname(__FILE__).'/../dataobjects/_dataobjectcache.class.php';
  40.  
  41. /**
  42.  * Blog Cache Class
  43.  *
  44.  * @package evocore
  45.  */
  46. class UserCache extends DataObjectCache
  47. {
  48.     /**
  49.      * Cache for login -> User object reference. "login" is transformed to lowercase.
  50.      * @access private
  51.      * @var array 
  52.      */
  53.     var $cache_login array();
  54.  
  55.  
  56.     /**
  57.      * Remember special cache loads.
  58.      * @access protected
  59.      */
  60.     var $alreadyCached = array();
  61.  
  62.  
  63.     /**
  64.      * Constructor
  65.      */
  66.     function UserCache()
  67.     {
  68.         parent::DataObjectCache'User'false'T_users''user_''user_ID' );
  69.     }
  70.  
  71.     /* this is for debugging only:
  72.     function & get_by_ID( $req_ID, $halt_on_error = true )
  73.     {
  74.         $obj = parent::get_by_ID( $req_ID, $halt_on_error );
  75.             pre_dump($obj);
  76.         return $obj;
  77.     }
  78.     */
  79.  
  80.  
  81.     /**
  82.      * Get a user object by login.
  83.      *
  84.      * Does not halt on error.
  85.      *
  86.      * @return false|UserReference to the user object or false if not found
  87.      */
  88.     function get_by_login$login )
  89.     {
  90.         // Make sure we have a lowercase login:
  91.         // We want all logins to be lowercase to guarantee uniqueness regardless of the database case handling for UNIQUE indexes.
  92.         $login strtolower$login );
  93.  
  94.         if!isset$this->cache_login[$login) )
  95.         {
  96.             global $DB;
  97.             if$row $DB->get_row'
  98.                     SELECT *
  99.                       FROM T_users
  100.                      WHERE user_login = "'.$DB->escape($login).'"'00'Get User login' ) )
  101.             {
  102.                 $this->addnew User$row ) );
  103.             }
  104.             else
  105.             {
  106.                 $this->cache_login[$loginfalse;
  107.             }
  108.         }
  109.  
  110.         return $this->cache_login[$login];
  111.     }
  112.  
  113.  
  114.     /**
  115.      * Get a user object by login, only if password matches.
  116.      *
  117.      * @param string Login
  118.      * @param string Password
  119.      * @param boolean Password is MD5()'ed
  120.      * @return false|User
  121.      */
  122.     function get_by_loginAndPwd$login$pass$pass_is_md5 true )
  123.     {
  124.         if!($User =$this->get_by_login$login )) )
  125.         {
  126.             return false;
  127.         }
  128.  
  129.         if!$pass_is_md5 )
  130.         {
  131.             $pass md5($pass);
  132.         }
  133.  
  134.         if$User->pass != $pass )
  135.         {
  136.             return false;
  137.         }
  138.  
  139.         return $User;
  140.     }
  141.  
  142.  
  143.     /**
  144.      * Overload parent's function to also maintain the login cache.
  145.      *
  146.      * @param User 
  147.      * @return boolean 
  148.      */
  149.     function add$Obj )
  150.     {
  151.         ifparent::add$Obj ) )
  152.         {
  153.             $this->cache_loginstrtolower($Obj->login$Obj;
  154.  
  155.             return true;
  156.         }
  157.  
  158.         return false;
  159.     }
  160.  
  161.  
  162.     /**
  163.      * Load members of a given blog
  164.      *
  165.      * @todo make a UNION query when we upgrade to MySQL 4
  166.      * @param integer blog ID to load members for
  167.      */
  168.     function load_blogmembers$blog_ID )
  169.     {
  170.         global $DB$Debuglog;
  171.  
  172.         ifisset$this->alreadyCached['blogmembers'&& isset$this->alreadyCached['blogmembers'][$blog_ID) )
  173.         {
  174.             $Debuglog->add"Already loaded <strong>$this->objtype(Blog #$blog_ID members)</strong> into cache"'dataobjects' );
  175.             return false;
  176.         }
  177.  
  178.         // Remember this special load:
  179.         $this->alreadyCached['blogmembers'][$blog_IDtrue;
  180.  
  181.         $Debuglog->add"Loading <strong>$this->objtype(Blog #$blog_ID members)</strong> into cache"'dataobjects' );
  182.  
  183.         // User perms:
  184.         $sql 'SELECT T_users.*
  185.                         FROM T_users INNER JOIN T_coll_user_perms ON user_ID = bloguser_user_ID
  186.                      WHERE bloguser_blog_ID = '.$blog_ID.'
  187.                          AND bloguser_ismember <> 0';
  188.         foreach$DB->get_results$sql as $row )
  189.         {
  190.             if!isset($this->cache[$row->user_ID]) )
  191.             {    // Save reinstatiating User if it's already been added
  192.                 $this->addnew User$row ) );
  193.             }
  194.         }
  195.  
  196.         // Group perms:
  197.         $sql 'SELECT T_users.*
  198.                         FROM T_users LEFT JOIN T_coll_group_perms ON user_grp_ID = bloggroup_group_ID
  199.                        WHERE bloggroup_blog_ID = '.$blog_ID.'
  200.                          AND bloggroup_ismember  <> 0';
  201.         foreach$DB->get_results$sql as $row )
  202.         {
  203.             if!isset($this->cache[$row->user_ID]) )
  204.             {    // Save reinstatiating User if it's already been added
  205.                 $this->addnew User$row ) );
  206.             }
  207.         }
  208.  
  209.         return true;
  210.     }
  211.  
  212.  
  213.     /**
  214.      * Loads cache with blog memeber, then display form option list with cache contents
  215.      *
  216.      * Optionally, also adds default choice to the cache.
  217.      *
  218.      * @param integer blog ID
  219.      * @param integer selected ID
  220.      * @param boolean provide a choice for "none" with ID 0
  221.      */
  222.     function blog_member_list$blog_ID$default 0$allow_none false$always_load_default false$disp true )
  223.     {
  224.         if$blog_ID )
  225.         // Load requested blog members:
  226.             $this->load_blogmembers$blog_ID );
  227.  
  228.             // Make sure current user is in list:
  229.             if$default && $always_load_default )
  230.             {
  231.                 // echo '<option>getting default';
  232.                 $this->get_by_ID$default );
  233.             }
  234.         }
  235.         else
  236.         // No blog specified: load ALL members:
  237.             $this->load_all();
  238.         }
  239.  
  240.         if$disp )
  241.         {
  242.             parent::option_list$default$allow_none'preferred_name' );
  243.         }
  244.         else
  245.         {
  246.             return parent::option_list_return$default$allow_none'preferred_name_return' );
  247.         }
  248.     }
  249.  
  250.  
  251.     /**
  252.      * Display form option list with cache contents
  253.      *
  254.      * Load the cache if necessary
  255.      *
  256.      * @param integer selected ID
  257.      * @param boolean provide a choice for "none" with ID 0
  258.      */
  259.     function option_list$default 0$allow_none false )
  260.     {
  261.         parent::option_list$default$allow_none'preferred_name' );
  262.     }
  263.  
  264.  
  265.     /**
  266.      * Clear our caches.
  267.      */
  268.     function clear$keep_shadow false )
  269.     {
  270.         $this->alreadyCached = array();
  271.         $this->cache_login array();
  272.  
  273.         return parent::clear($keep_shadow);
  274.     }
  275.  
  276.     /**
  277.      * Handle our login cache.
  278.      */
  279.     function remove_by_ID$reg_ID )
  280.     {
  281.         ifisset($this->cache[$req_ID]) )
  282.         {
  283.             unset$this->cache_login$this->cache[$req_ID] ] );
  284.         }
  285.         parent::remove_by_ID($req_ID);
  286.     }
  287.  
  288. }
  289.  
  290.  
  291. /*
  292.  * $Log: _usercache.class.php,v $
  293.  * Revision 1.3.4.4  2006/11/04 19:54:55  fplanque
  294.  * Reinjected old Log blocks. Removing them from CVS was a bad idea -- especially since Daniel has decided branch 1.9 was his HEAD...
  295.  *
  296.  * Revision 1.3  2006/04/19 20:13:50  fplanque
  297.  * do not restrict to :// (does not catch subdomains, not even www.)
  298.  *
  299.  * Revision 1.2  2006/03/12 23:09:00  fplanque
  300.  * doc cleanup
  301.  *
  302.  * Revision 1.1  2006/02/23 21:11:58  fplanque
  303.  * File reorganization to MVC (Model View Controller) architecture.
  304.  * See index.hml files in folders.
  305.  * (Sorry for all the remaining bugs induced by the reorg... :/)
  306.  *
  307.  * Revision 1.25  2006/02/10 22:33:19  fplanque
  308.  * logins should be lowercase
  309.  *
  310.  * Revision 1.24  2006/02/09 00:53:10  blueyed
  311.  * add(): Cache logins lowercase!
  312.  *
  313.  * Revision 1.23  2005/12/12 19:21:23  fplanque
  314.  * big merge; lots of small mods; hope I didn't make to many mistakes :]
  315.  *
  316.  * Revision 1.22  2005/11/23 17:28:00  fplanque
  317.  * also check group permissions
  318.  *
  319.  * Revision 1.21  2005/10/03 17:26:44  fplanque
  320.  * synched upgrade with fresh DB;
  321.  * renamed user_ID field
  322.  *
  323.  * Revision 1.20  2005/09/29 15:07:30  fplanque
  324.  * spelling
  325.  *
  326.  * Revision 1.19  2005/09/06 17:13:55  fplanque
  327.  * stop processing early if referer spam has been detected
  328.  *
  329.  * Revision 1.18  2005/05/25 17:13:34  fplanque
  330.  * implemented email notifications on new comments/trackbacks
  331.  *
  332.  * Revision 1.17  2005/05/16 15:17:13  fplanque
  333.  * minor
  334.  *
  335.  * Revision 1.16  2005/03/14 20:22:20  fplanque
  336.  * refactoring, some cacheing optimization
  337.  *
  338.  * Revision 1.15  2005/02/28 09:06:34  blueyed
  339.  * removed constants for DB config (allows to override it from _config_TEST.php), introduced EVO_CONFIG_LOADED
  340.  *
  341.  * Revision 1.14  2005/02/20 23:08:41  blueyed
  342.  * get_by_loginAndPwd() added
  343.  *
  344.  * Revision 1.13  2005/02/18 00:36:08  blueyed
  345.  * $alreadyCached class member
  346.  *
  347.  * Revision 1.12  2005/02/16 15:48:06  fplanque
  348.  * merged with work app :p
  349.  *
  350.  * Revision 1.11  2005/02/15 20:05:52  fplanque
  351.  * no message
  352.  *
  353.  * Revision 1.10  2005/02/14 21:17:54  blueyed
  354.  * optimized cache handling
  355.  *
  356.  * Revision 1.9  2005/02/14 14:33:35  fplanque
  357.  * todo..
  358.  *
  359.  * Revision 1.8  2005/02/09 00:27:13  blueyed
  360.  * Removed deprecated globals / userdata handling
  361.  *
  362.  * Revision 1.7  2005/02/08 04:45:02  blueyed
  363.  * improved $DB get_results() handling
  364.  *
  365.  * Revision 1.6  2005/02/08 04:07:47  blueyed
  366.  * fixed results from DB::get_var()
  367.  *
  368.  * Revision 1.5  2005/01/20 20:38:58  fplanque
  369.  * refactoring
  370.  *
  371.  * Revision 1.4  2004/12/30 16:45:40  fplanque
  372.  * minor changes on file manager user interface
  373.  *
  374.  * Revision 1.3  2004/12/29 03:15:38  blueyed
  375.  * added get_by_login()
  376.  *
  377.  * Revision 1.2  2004/12/23 21:19:41  fplanque
  378.  * no message
  379.  *
  380.  * Revision 1.1  2004/12/21 21:18:38  fplanque
  381.  * Finished handling of assigning posts/items to users
  382.  *
  383.  */
  384. ?>

Documentation generated on Tue, 18 Dec 2007 22:53:52 +0100 by phpDocumentor 1.4.0