using a static var as cache, which is a reference to the session:

a) automatic shared between all instances of accounts class
b) no need to have a shutdown function storing it to the session
--> cleaner code
This commit is contained in:
Ralf Becker 2009-12-07 17:56:31 +00:00
parent a3a3faab22
commit 6aa95316ba
2 changed files with 113 additions and 140 deletions

View File

@ -1,19 +1,19 @@
<?php
/**
* API - accounts
*
*
* This class extends a backend class (at them moment SQL or LDAP) and implements some
* caching on to top of the backend functions. The cache is share for all instances of
* the accounts class and for LDAP it is persistent through the whole session, for SQL
* the accounts class and for LDAP it is persistent through the whole session, for SQL
* it's only on a per request basis.
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> complete rewrite in 6/2006 and earlier modifications
*
* Implements the (now depricated) interfaces on the former accounts class written by
*
* Implements the (now depricated) interfaces on the former accounts class written by
* Joseph Engo <jengo@phpgroupware.org> and Bettina Gille <ceb@phpgroupware.org>
* Copyright (C) 2000 - 2002 Joseph Engo, Copyright (C) 2003 Joseph Engo, Bettina Gille
*
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage accounts
@ -23,22 +23,21 @@
/**
* API - accounts
*
*
* This class uses a backend class (at them moment SQL or LDAP) and implements some
* caching on to top of the backend functions. The cache is share for all instances of
* the accounts class and for LDAP it is persistent through the whole session, for SQL
* it's only on a per request basis.
*
* The backend only implements the read, save, delete, name2id and the {set_}members{hips} methods.
* caching on to top of the backend functions. The cache is static and therefore shared
* between all instances of accounts class.
*
* The backend only implements the read, save, delete, name2id and the {set_}members{hips} methods.
* The account class implements all other (eg. name2id, id2name) functions on top of these.
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> complete rewrite in 6/2006
*
* Implements the (now depricated) interfaces on the former accounts class written by
*
* Implements the (now depricated) interfaces on the former accounts class written by
* Joseph Engo <jengo@phpgroupware.org> and Bettina Gille <ceb@phpgroupware.org>
* Copyright (C) 2000 - 2002 Joseph Engo, Copyright (C) 2003 Joseph Engo, Bettina Gille
*
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package api
* @subpackage accounts
@ -66,12 +65,19 @@ class accounts
),
);
/**
* enables the session-cache, done in the constructor for the LDAP backend only
*
* Enables the session-cache, currently switched on independent of the backend
*
* @var boolean $use_session_cache
*/
var $use_session_cache = true;
static $use_session_cache = true;
/**
* Cache, stored in sesssion
*
* @var array
*/
static $cache;
/**
* Depricated: Account this class was instanciated for
*
@ -106,39 +112,39 @@ class accounts
'firstname' => 'firstname',
'lastname' => 'lastname',
'lid' => 'LoginID',
'email' => 'email',
'email' => 'email',
'start' => 'start with',
'exact' => 'exact',
);
/**
* Backend to use
*
* @var accounts_sql|accounts_ldap
*/
var $backend;
/**
* total number of found entries
*
* @var int
*/
var $total;
/**
* Current configuration
*
* @var array
*/
var $config;
/**
* hold an instance of the accounts class
*
* @var accounts the instance of the accounts class
*/
private static $_instance = NULL;
/**
* the singleton passtern
*
@ -146,16 +152,16 @@ class accounts
*/
public static function getInstance()
{
if (self::$_instance === NULL) {
if (self::$_instance === NULL)
{
self::$_instance = new accounts;
}
return self::$_instance;
}
}
/**
* Constructor
*
*
* @param string|array $backend=null string with backend 'sql'|'ldap', or whole config array, default read from global egw_info
*/
public function __construct($backend=null)
@ -190,10 +196,10 @@ class accounts
$backend = $this->config['account_repository'];
}
$backend_class = 'accounts_'.$backend;
$this->backend = new $backend_class($this);
}
/**
* Old constructor name
*
@ -206,19 +212,21 @@ class accounts
$this->__construct();
}
/**
* set the accountId
*
* @param int $accountId
* @deprecated
*/
function setAccountId($accountId)
{
if($accountId && is_numeric($accountId)) {
if($accountId && is_numeric($accountId))
{
$this->account_id = (int)$accountId;
}
}
/**
* Searches / lists accounts: users and/or groups
*
@ -242,9 +250,9 @@ class accounts
function search($param)
{
//echo "<p>accounts::search(".print_r($param,True).") start: ".microtime()."</p>\n";
$this->setup_cache();
$account_search = &$this->cache['account_search'];
self::setup_cache();
$account_search = &self::$cache['account_search'];
$serial = serialize($param);
if (isset($account_search[$serial]))
@ -333,14 +341,14 @@ class accounts
//echo "<p>accounts::search() end: ".microtime()."</p>\n";
return $account_search[$serial]['data'];
}
/**
* Reads the data of one account
*
* It's depricated to use read with out parameter to read the internal data of this class!!!
* All key of the returned array use the 'account_' prefix.
* All key of the returned array use the 'account_' prefix.
* For backward compatibility some values are additionaly availible without the prefix, using them is depricated!
*
*
* @param int/string $id numeric account_id or string with account_lid (use of default value of 0 is depricated!!!)
* @param boolean $set_depricated_names=false set _additionaly_ the depricated keys without 'account_' prefix
* @return array/boolean array with account data (keys: account_id, account_lid, ...) or false if account not found
@ -356,15 +364,15 @@ class accounts
$id = $this->name2id($id);
}
if (!$id) return false;
$this->setup_cache();
$account_data = &$this->cache['account_data'];
self::setup_cache();
$account_data = &self::$cache['account_data'];
if (!isset($account_data[$id]))
{
$account_data[$id] = $this->backend->read($id);
}
if (!$account_data[$id] || !$set_depricated_names)
if (!$account_data[$id] || !$set_depricated_names)
{
return $account_data[$id];
}
@ -375,10 +383,10 @@ class accounts
}
return $data;
}
/**
* Saves / adds the data of one account
*
*
* If no account_id is set in data the account is added and the new id is set in $data.
*
* @param array $data array with account-data
@ -398,7 +406,7 @@ class accounts
}
}
if (($id = $this->backend->save($data)) && $data['account_type'] != 'g')
{
{
// if we are not on a pure LDAP system, we have to write the account-date via the contacts class now
if (($this->config['account_repository'] != 'ldap' ||
$this->config['contact_repository'] == 'sql-ldap') &&
@ -420,19 +428,19 @@ class accounts
$GLOBALS['egw']->contacts->save($contact,true); // true = ignore addressbook acl
}
// save primary group if necessary
if ($data['account_primary_group'] && (!($memberships = $this->memberships($id,true)) ||
if ($data['account_primary_group'] && (!($memberships = $this->memberships($id,true)) ||
!in_array($data['account_primary_group'],$memberships)))
{
$this->cache_invalidate($data['account_id']);
self::cache_invalidate($data['account_id']);
$memberships[] = $data['account_primary_group'];
$this->set_memberships($memberships,$id);
}
}
$this->cache_invalidate($data['account_id']);
self::cache_invalidate($data['account_id']);
return $id;
}
/**
* Delete one account, deletes also all acl-entries for that account
*
@ -447,12 +455,12 @@ class accounts
}
if (!$id) return false;
$this->cache_invalidate($id);
self::cache_invalidate($id);
$this->backend->delete($id);
// delete all acl_entries belonging to that user or group
$GLOBALS['egw']->acl->delete_account($id);
return true;
}
@ -477,7 +485,7 @@ class accounts
* Please note:
* - if a group and an user have the same account_lid the group will be returned (LDAP only)
* - if multiple user have the same email address, the returned user is undefined
*
*
* @param string $name value to convert
* @param string $which='account_lid' type of $name: account_lid (default), account_email, person_id, account_fullname
* @param string $account_type=null u = user or g = group, or default null = try both
@ -485,8 +493,8 @@ class accounts
*/
function name2id($name,$which='account_lid',$account_type=null)
{
$this->setup_cache();
$name_list = &$this->cache['name_list'];
self::setup_cache();
$name_list = &self::$cache['name_list'];
if(@isset($name_list[$which][$name]) && $name_list[$which][$name])
{
@ -503,7 +511,7 @@ class accounts
/**
* Convert an numeric account_id to any other value of that account (account_lid, account_email, ...)
*
*
* Uses the read method to fetch all data.
*
* @param int $account_id numerica account_id
@ -513,7 +521,7 @@ class accounts
function id2name($account_id,$which='account_lid')
{
if (!($data = $this->read($account_id))) return false;
//echo "<p>accounts::id2name($account_id,$which)='{$data[$which]}'";
return $data[$which];
}
@ -521,7 +529,7 @@ class accounts
/**
* get the type of an account: 'u' = user, 'g' = group
*
* @param int/string $accountid numeric account-id or alphanum. account-lid,
* @param int/string $accountid numeric account-id or alphanum. account-lid,
* if !$accountid account of the user of this session
* @return string/false 'u' = user, 'g' = group or false on error ($accountid not found)
*/
@ -533,12 +541,12 @@ class accounts
}
return $account_id > 0 ? 'u' : ($account_id < 0 ? 'g' : false);
}
/**
* check if an account exists and if it is an user or group
*
* @param int/string $account_id numeric account_id or account_lid
* @return int 0 = acount does not exist, 1 = user, 2 = group
* @return int 0 = acount does not exist, 1 = user, 2 = group
*/
function exists($account_id)
{
@ -558,8 +566,8 @@ class accounts
*/
function memberships($account_id,$just_id=false)
{
$this->setup_cache();
$memberships_list = &$this->cache['memberships_list'];
self::setup_cache();
$memberships_list = &self::$cache['memberships_list'];
if (!is_int($account_id) && !is_numeric($account_id))
{
@ -588,21 +596,21 @@ class accounts
}
$this->backend->set_memberships($groups,$account_id);
$this->cache_invalidate($account_id);
self::cache_invalidate($account_id);
}
/**
* Get all members of the group $account_id
*
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* default account of the user of this session
* @param boolean $just_id=false return just an array of id's and not id => lid pairs, default false
* @return array with account_id ($just_id) or account_id => account_lid pairs (!$just_id)
*/
function members($account_id,$just_id=false)
{
$this->setup_cache();
$members_list = &$this->cache['members_list'];
self::setup_cache();
$members_list = &self::$cache['members_list'];
if (!is_int($account_id) && !is_numeric($account_id))
{
@ -618,7 +626,7 @@ class accounts
/**
* Set the members of a group
*
*
* @param array $members array with uidnumber or uid's
* @param int $gid gidnumber of group to set
*/
@ -627,7 +635,7 @@ class accounts
//echo "<p>accounts::set_members(".print_r($members,true).",$gid)</p>\n";
$this->backend->set_members($members,$gid);
$this->cache_invalidate(0);
self::cache_invalidate(0);
}
/**
@ -643,8 +651,8 @@ class accounts
{
if (!is_array($app_users))
{
$this->setup_cache();
$cache = &$this->cache['account_split'][$app_user];
self::setup_cache();
$cache = &self::$cache['account_split'][$app_user];
if (is_array($cache))
{
@ -704,7 +712,7 @@ class accounts
* Add an account for an authenticated user
*
* Expiration date and primary group are read from the system configuration.
*
*
* @param string $account_lid
* @param string $passwd
* @param array $GLOBALS['auto_create_acct'] values for 'firstname', 'lastname', 'email' and 'primary_group'
@ -716,7 +724,7 @@ class accounts
$this->config['auto_create_expire'] == 'never' ? -1 :
time() + $this->config['auto_create_expire'] + 2;
if (!($default_group_id = $this->name2id($this->config['default_group_lid'])))
{
$default_group_id = $this->name2id('Default');
@ -737,8 +745,8 @@ class accounts
'account_primary_group' => $primary_group,
);
// use given account_id, if it's not already used
if (isset($GLOBALS['auto_create_acct']['account_id']) &&
is_numeric($GLOBALS['auto_create_acct']['account_id']) &&
if (isset($GLOBALS['auto_create_acct']['account_id']) &&
is_numeric($GLOBALS['auto_create_acct']['account_id']) &&
!$this->id2name($GLOBALS['auto_create_acct']['account_id']))
{
$data['account_id'] = $GLOBALS['auto_create_acct']['account_id'];
@ -757,7 +765,7 @@ class accounts
return $data['account_id'];
}
/**
* Update the last login timestamps and the IP
*
@ -807,90 +815,59 @@ class accounts
* Invalidate the cache (or parts of it) after change in $account_id
*
* Atm simplest approach - delete it all ;-)
*
*
* @param int $account_id for which account_id should the cache be invalid, default 0 = all
*/
function cache_invalidate($account_id=0)
static function cache_invalidate($account_id=0)
{
//echo "<p>accounts::cache_invalidate($account_id)</p>\n";
if (is_object($GLOBALS['egw']->accounts))
{
$GLOBALS['egw']->accounts->cache = array();
}
if ($this->cache) $this->cache = array();
if (self::$cache) self::$cache = array();
if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited
{
$GLOBALS['egw']->invalidate_session_cache(); // invalidates whole egw-enviroment if stored in the session
}
}
/**
* Internal functions not meant to use outside this class!!!
*/
/**
* Sets up the account-data cache
*
* The cache is shared between all instances of the account-class and it can be save in the session,
* if use_session_cache is set to True
*
* @internal
*
* @internal
*/
function setup_cache()
static function setup_cache()
{
//echo "<p>accounts::setup_cache() use_session_cache=$this->use_session_cache, is_array(this->cache)=".(int)is_array($this->cache)."</p>\n";
if (is_array($this->cache)) return; // cache is already setup
//echo "<p>accounts::setup_cache() use_session_cache={self::$use_session_cache}, is_array(self::\$cache)=".(int)is_array(self::$cache)."</p>\n";
if (is_array(self::$cache)) return; // cache is already setup
if ($this->use_session_cache && isset($GLOBALS['egw']->accounts) && !is_array($GLOBALS['egw']->accounts->cache) && is_object($GLOBALS['egw']->session))
if (self::$use_session_cache && is_object($GLOBALS['egw']->session))
{
//echo "<p>restoring the session-cache for \$GLOBALS['egw']->accounts</p>\n";
$GLOBALS['egw']->accounts->cache = $GLOBALS['egw']->session->appsession('accounts_cache','phpgwapi');
self::$cache =& egw_cache::getSession('accounts_cache','phpgwapi');
//echo "<p>restoring cache from session, ".count(call_user_func_array('array_merge',(array)self::$cache))." items</p>\n";
}
if (!isset($this->cache) && isset($GLOBALS['egw']->accounts))
{
$this->cache =& $GLOBALS['egw']->accounts->cache;
}
if (!is_array($this->cache))
if (!is_array(self::$cache))
{
//echo "<p>initialising this->cache to array()</p>\n";
$this->cache = array();
self::$cache = array();
}
}
/**
* Magic function called if the object get's unserialized
*
* We unset our cache, which is a reference to the cache of the global account object in $GLOBALS['egw']->accounts,
* as it will be no reference after wakeup (causes cache_invalidate to no longer work).
* For the global accounts object the cache get unset too, as the object was stored in a early state of the framework
* initialisation, and it the cache stored in the session by save_session_cache is a lot more up to date.
*
* @internal
*/
function __wakeup()
{
unset($this->cache);
}
/**
* Saves the account-data cache in the session
*
* Gets called from common::egw_final()
*
* @internal
* @deprecated not used any more, as static cache is a reference to the session
*/
function save_session_cache()
{
if (is_object($GLOBALS['egw']->session) && is_array($GLOBALS['egw']->accounts->cache))
{
//echo "<p>save_session_cache() saving</p>\n";
$GLOBALS['egw']->session->appsession('accounts_cache','phpgwapi',$GLOBALS['egw']->accounts->cache);
}
}
/**
* Depricated functions of the old accounts class.
* Depricated functions of the old accounts class.
*
* Do NOT use them in new code, they will be removed after the next major release!!!
*/
@ -908,7 +885,7 @@ class accounts
/**
* saves the account-data in the internal data-structure of this class to the repository
*
*
* @deprecated use save of $GLOBALS['egw']->accounts and not own instances of the accounts class
*/
function save_repository()
@ -940,7 +917,7 @@ class accounts
/**
* Create a new account with the given $account_info
*
*
* @deprecated use save
* @param array $data account data for the new account
* @param booelan $default_prefs has no meaning any more, as we use "real" default prefs since 1.0
@ -967,7 +944,7 @@ class accounts
* Get all memberships of an account $accountid / groups the account is a member off
*
* @deprecated use memberships() which account_id => account_lid pairs
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* default account of the user of this session
* @return array or arrays with keys 'account_id' and 'account_name' for the groups $accountid is a member of
*/
@ -987,12 +964,12 @@ class accounts
//echo "<p>accounts::membership($accountid)="; _debug_array($old);
return $old;
}
/**
* Get all members of the group $accountid
*
* @deprecated use members which returns acount_id => account_lid pairs
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* default account of the user of this session
* @return array of arrays with keys 'account_id' and 'account_name'
*/
@ -1030,21 +1007,21 @@ class accounts
* Gets account-name (lid), firstname and lastname of an account $accountid
*
* @deprecated use read to read account data
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* @param int/string $accountid='' numeric account-id or alphanum. account-lid,
* if !$accountid account of the user of this session
* @param string &$lid on return: alphanumeric account-name (lid)
* @param string &$fname on return: first name
* @param string &$lname on return: last name
* @return boolean true if $accountid was found, false otherwise
*/
*/
function get_account_name($accountid,&$lid,&$fname,&$lname)
{
if (!($data = $this->read($accountid))) return false;
$lid = $data['account_lid'];
$fname = $data['account_firstname'];
$lname = $data['account_lastname'];
if (empty($fname)) $fname = $lid;
if (empty($lname)) $lname = $this->get_type($accountid) == 'g' ? lang('Group') : lang('user');
@ -1057,7 +1034,7 @@ class accounts
* Same effect as instanciating the class with that account, dont do it with $GLOBALS['egw']->account !!!
*
* @deprecated use read to read account data and store it in your own code
* @param int $accountid numeric account-id
* @param int $accountid numeric account-id
* @return array with keys lid, firstname, lastname, fullname, type
*/
function get_account_data($account_id)

View File

@ -498,10 +498,6 @@ class egw extends egw_minimal
{
define('EGW_SHUTDOWN',True);
if (isset($this->accounts))
{
$this->accounts->save_session_cache();
}
if (class_exists('egw_link',false)) // false = no autoload!
{
egw_link::save_session_cache();