* API/accounts/preferences: instance-wide cache of account and preference data, so changes from other sessions have immediate effect

r40051: * API/accounts: instance-wide cache for account-data incl. members and memberships, so change take imediate effect (compared to previous session based cache)
r40052: not storing $GLOBALS[egw_info][user] twice in session (was also stored as $GLOBALS[egw]->session->user), also removing not used $GLOBALS[egw_info][user][acl], but re-reading preferences in session::verify() so long running sessions get preferences set by an other session, removing nowhere used creditspoint class from api (calls not public available creditspoint app)
r40053: * API/preferences: caching preferences in instance cache instead of session, to get immediate update in long running sessions (eg. sync) and get smaller sessions
This commit is contained in:
Ralf Becker 2012-08-07 14:26:52 +00:00
parent 63da01c5e1
commit 02c464b94f
5 changed files with 238 additions and 243 deletions

View File

@ -20,8 +20,13 @@
* API - accounts * API - accounts
* *
* This class uses a backend class (at them moment SQL or LDAP) and implements some * 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 static and therefore shared * caching on to top of the backend functions:
* between all instances of accounts class. *
* a) instance-wide account-data cache queried by account_id including also members(hips)
* implemented by self::cache_read($account_id) and self::cache_invalidate($account_ids)
*
* b) session based cache for search, split_accounts and name2id
* implemented by self::setup_cache() and self::cache_invalidate()
* *
* The backend only implements the read, save, delete, name2id and the {set_}members{hips} methods. * 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. * The account class implements all other (eg. name2id, id2name) functions on top of these.
@ -399,21 +404,14 @@ class accounts
} }
if (!$id) return false; if (!$id) return false;
self::setup_cache(); $data = self::cache_read($id);
$account_data = &self::$cache['account_data'];
if (!isset($account_data[$id])) if ($set_depricated_names && $data)
{ {
$account_data[$id] = $this->backend->read($id); foreach($this->depricated_names as $name)
} {
if (!$account_data[$id] || !$set_depricated_names) $data[$name] =& $data['account_'.$name];
{ }
return $account_data[$id];
}
$data = $account_data[$id];
foreach($this->depricated_names as $name)
{
$data[$name] =& $data['account_'.$name];
} }
return $data; return $data;
} }
@ -465,9 +463,8 @@ class accounts
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))) !in_array($data['account_primary_group'],$memberships)))
{ {
self::cache_invalidate($data['account_id']);
$memberships[] = $data['account_primary_group']; $memberships[] = $data['account_primary_group'];
$this->set_memberships($memberships,$id); $this->set_memberships($memberships, $id); // invalidates cache for account_id and primary group
} }
} }
self::cache_invalidate($data['account_id']); self::cache_invalidate($data['account_id']);
@ -489,12 +486,23 @@ class accounts
} }
if (!$id) return false; if (!$id) return false;
self::cache_invalidate($id); if ($this->get_type($id) == 'u')
{
$invalidate = $this->memberships($id, true);
}
else
{
$invalidate = $this->members($id, true);
}
$invalidate[] = $id;
$this->backend->delete($id); $this->backend->delete($id);
// delete all acl_entries belonging to that user or group // delete all acl_entries belonging to that user or group
$GLOBALS['egw']->acl->delete_account($id); $GLOBALS['egw']->acl->delete_account($id);
self::cache_invalidate($invalidate);
return true; return true;
} }
@ -553,9 +561,9 @@ class accounts
* @param string $which='account_lid' type to convert to: account_lid (default), account_email, ... * @param string $which='account_lid' type to convert to: account_lid (default), account_email, ...
* @return string/false converted value or false on error ($account_id not found) * @return string/false converted value or false on error ($account_id not found)
*/ */
function id2name($account_id,$which='account_lid') static function id2name($account_id, $which='account_lid')
{ {
if (!($data = $this->read($account_id))) return false; if (!($data = self::cache_read($account_id))) return false;
//echo "<p>accounts::id2name($account_id,$which)='{$data[$which]}'"; //echo "<p>accounts::id2name($account_id,$which)='{$data[$which]}'";
return $data[$which]; return $data[$which];
@ -633,21 +641,17 @@ class accounts
* @param boolean $just_id=false return just account_id's or account_id => account_lid pairs * @param boolean $just_id=false return just account_id's or account_id => account_lid pairs
* @return array with account_id's ($just_id) or account_id => account_lid pairs (!$just_id) * @return array with account_id's ($just_id) or account_id => account_lid pairs (!$just_id)
*/ */
function memberships($account_id,$just_id=false) function memberships($account_id, $just_id=false)
{ {
self::setup_cache();
$memberships_list = &self::$cache['memberships_list'];
if (!is_int($account_id) && !is_numeric($account_id)) if (!is_int($account_id) && !is_numeric($account_id))
{ {
$account_id = $this->name2id($account_id,'account_lid','u'); $account_id = $this->name2id($account_id,'account_lid','u');
} }
if (!isset($memberships_list[$account_id])) if ($account_id && ($data = self::cache_read($account_id)))
{ {
$memberships_list[$account_id] = $this->backend->memberships($account_id); return $just_id && $data['memberships'] ? array_keys($data['memberships']) : $data['memberships'];
} }
//echo "accounts::memberships($account_id)"; _debug_array($memberships_list[$account_id]); return null;
return $just_id && $memberships_list[$account_id] ? array_keys($memberships_list[$account_id]) : $memberships_list[$account_id];
} }
/** /**
@ -663,9 +667,16 @@ class accounts
{ {
$account_id = $this->name2id($account_id); $account_id = $this->name2id($account_id);
} }
$this->backend->set_memberships($groups,$account_id); if (($old_memberships = $this->memberships($account_id, true)) != $groups)
{
$this->backend->set_memberships($groups, $account_id);
self::cache_invalidate($account_id); self::cache_invalidate(array_unique(array_merge(
array($account_id),
array_diff($old_memberships, $groups),
array_diff($groups, $old_memberships)
)));
}
} }
/** /**
@ -678,19 +689,15 @@ class accounts
*/ */
function members($account_id,$just_id=false) function members($account_id,$just_id=false)
{ {
self::setup_cache();
$members_list = &self::$cache['members_list'];
if (!is_int($account_id) && !is_numeric($account_id)) if (!is_int($account_id) && !is_numeric($account_id))
{ {
$account_id = $this->name2id($account_id); $account_id = $this->name2id($account_id);
} }
if (!isset($members_list[$account_id])) if ($account_id && ($data = self::cache_read($account_id)))
{ {
$members_list[$account_id] = $this->backend->members($account_id); return $just_id && $data['members'] ? array_keys($data['members']) : $data['members'];
} }
//echo "accounts::members($account_id)"; _debug_array($members_list[$account_id]); return null;
return $just_id && $members_list[$account_id] ? array_keys($members_list[$account_id]) : $members_list[$account_id];
} }
/** /**
@ -702,9 +709,16 @@ class accounts
function set_members($members,$gid) function set_members($members,$gid)
{ {
//echo "<p>accounts::set_members(".print_r($members,true).",$gid)</p>\n"; //echo "<p>accounts::set_members(".print_r($members,true).",$gid)</p>\n";
$this->backend->set_members($members,$gid); if (($old_members = $this->members($gid, true)) != $members)
{
$this->backend->set_members($members, $gid);
self::cache_invalidate(0); self::cache_invalidate(array_unique(array_merge(
array($gid),
array_diff($old_members, $members),
array_diff($members, $old_members)
)));
}
} }
/** /**
@ -904,15 +918,28 @@ class accounts
} }
/** /**
* Invalidate the cache (or parts of it) after change in $account_id * Invalidate cache (or parts of it) after change in $account_ids
* *
* Atm simplest approach - delete it all ;-) * We use now an instance-wide read-cache storing account-data and members(hips).
* *
* @param int $account_id for which account_id should the cache be invalid, default 0 = all * @param int|array $account_ids user- or group-id(s) for which cache should be invalidated, default 0 = only search/name2id cache
*/ */
static function cache_invalidate($account_id=0) static function cache_invalidate($account_ids=0)
{ {
//echo "<p>accounts::cache_invalidate($account_id)</p>\n"; //error_log(__METHOD__.'('.array2string($account_ids).')');
// instance-wide cache
if ($account_ids)
{
foreach((array)$account_ids as $account_id)
{
egw_cache::unsetInstance(__CLASS__, 'account-'.$account_id);
unset(self::$request_cache[$account_id]);
}
}
// session-cache
if (self::$cache) self::$cache = array(); if (self::$cache) self::$cache = array();
egw_cache::unsetSession('accounts_cache','phpgwapi'); egw_cache::unsetSession('accounts_cache','phpgwapi');
@ -922,21 +949,73 @@ class accounts
} }
} }
/**
* Timeout of instance wide cache for reading account-data and members(hips)
*/
const READ_CACHE_TIMEOUT = 43200;
/**
* Local per request cache, to minimize calls to instance cache
*
* @var array
*/
static $request_cache = array();
/**
* Read account incl. members/memberships from cache (or backend and cache it)
*
* @param int $account_id
* @return array
*/
static function cache_read($account_id)
{
if (!is_numeric($account_id)) throw new egw_exception_wrong_parameter('Not an integer!');
$account =& self::$request_cache[$account_id];
if (!isset($account)) // not in request cache --> try intance cache
{
$account = egw_cache::getInstance(__CLASS__, 'account-'.$account_id);
if (!isset($account)) // not in instance cache --> read from backend
{
$instance = self::getInstance();
if (($account = $instance->backend->read($account_id)))
{
if ($instance->get_type($account_id) == 'u')
{
$account['memberships'] = $instance->backend->memberships($account_id);
}
else
{
$account['members'] = $instance->backend->members($account_id);
}
egw_cache::setInstance(__CLASS__, 'account-'.$account_id, $account, self::READ_CACHE_TIMEOUT);
}
//error_log(__METHOD__."($account_id) read from backend ".array2string($account));
}
//else error_log(__METHOD__."($account_id) read from instance cache ".array2string($account));
}
return $account;
}
/** /**
* Internal functions not meant to use outside this class!!! * Internal functions not meant to use outside this class!!!
*/ */
/** /**
* Sets up the account-data cache * Sets up session cache, now only used for search and name2id list
*
* Other account-data is cached on instance-level
* *
* The cache is shared between all instances of the account-class and it can be save in the session, * 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 * if use_session_cache is set to True
* *
* @internal * @internal
*/ */
static function setup_cache() private static function setup_cache()
{ {
//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 (is_array(self::$cache)) return; // cache is already setup
if (self::$use_session_cache && is_object($GLOBALS['egw']->session)) if (self::$use_session_cache && is_object($GLOBALS['egw']->session))
@ -944,6 +1023,8 @@ class accounts
self::$cache =& egw_cache::getSession('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"; //echo "<p>restoring cache from session, ".count(call_user_func_array('array_merge',(array)self::$cache))." items</p>\n";
} }
//error_log(__METHOD__."() use_session_cache=".array2string(self::$use_session_cache).", is_array(self::\$cache)=".array2string(is_array(self::$cache)));
if (!is_array(self::$cache)) if (!is_array(self::$cache))
{ {
//echo "<p>initialising this->cache to array()</p>\n"; //echo "<p>initialising this->cache to array()</p>\n";

View File

@ -414,8 +414,7 @@ class asyncservice
{ {
$GLOBALS['egw']->session->account_lid = $GLOBALS['egw']->accounts->id2name($job['account_id']); $GLOBALS['egw']->session->account_lid = $GLOBALS['egw']->accounts->id2name($job['account_id']);
$GLOBALS['egw']->session->account_domain = $domain; $GLOBALS['egw']->session->account_domain = $domain;
$GLOBALS['egw']->session->read_repositories(); $GLOBALS['egw_info']['user'] = $GLOBALS['egw']->session->read_repositories();
$GLOBALS['egw_info']['user'] = $GLOBALS['egw']->session->user;
if ($lang != $GLOBALS['egw_info']['user']['preferences']['common']['lang']) if ($lang != $GLOBALS['egw_info']['user']['preferences']['common']['lang'])
{ {

View File

@ -1,119 +0,0 @@
<?php
/**************************************************************************\
* eGroupWare API - Wrapper for the creditspoint credits check *
* Written by Rob van Kraanen<rob@lingewoud.nl> *
* *
* Wrapper for the savant2 template engine www.phpsavant.com *
* Copyright (C) 2005 Lingewoud BV and Rob van Kraanen *
* -------------------------------------------------------------------------*
* This library is part of the eGroupWare API *
* http://www.egroupware.org *
* ------------------------------------------------------------------------ *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, *
* or any later version. *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
\**************************************************************************/
class creditspoint
{
var $cpapi;
var $useCP = false;
function creditspoint()
{
$found = false;
foreach($GLOBALS['phpgw_info']['user']['acl'] as $acl)
{
if($acl['appname'] == 'creditspoint')
{
$found =true;
}
}
if( is_array($GLOBALS['phpgw_info']['apps']['creditspoint']) and $found)
{
$this->cpapi = CreateObject('creditspoint.api');
$this->useCP = true;
}
}
function exec_service_plain($appname, $service, $link, $uniqid)
{
if($this->useCP)
{
return $this->cpapi->exec_service_plain($appname, $service, $link, $uniqid);
}
else
{
return $link;
}
}
function exec_service_link($appname, $service, $link, $linkname, $uniqid)
{
if($this->useCP)
{
return $this->cpapi->exec_service_link($appname, $service, $link, $linkname, $uniqid);
}
else
{
return $link;
}
}
function exec_service_button($appname, $service, $link, $buttonlabel, $uniqid)
{
if($this->useCP)
{
return $this->cpapi->exec_service_button($appname, $service, $link, $buttonlabel, $uniqid);
}
else
{
return $link;
}
}
function exec_service_img($appname, $service, $link, $imgsrc, $uniqid)
{
if($this->useCP)
{
return $this->cpapi->exec_service_img($appname, $service, $link, $imgsrc, $uniqid);
}
else
{
return $link;
}
}
function confirm($uniqid)
{
if($this->useCP)
{
return $this->cpapi->confirm($uniqid);
}
else
{
return $link;
}
}
function refund($uniqid)
{
if($this->useCP)
{
return $this->cpapi->refund($uniqid);
}
else
{
return $link;
}
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
/** /**
* eGroupWare API: eGW session handling * EGroupware API: session handling
* *
* This class is based on the old phpgwapi/inc/class.sessions(_php4).inc.php: * This class is based on the old phpgwapi/inc/class.sessions(_php4).inc.php:
* (c) 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp * (c) 1998-2000 NetUSE AG Boris Erdmann, Kristian Koehntopp
@ -21,9 +21,9 @@
*/ */
/** /**
* eGW session handling * session handling
* *
* Create, verifies or destroys an eGroupWare session * Create, verifies or destroys an EGroupware session
* *
* There are separate session-handler classes: egw_session_(files|memcache), * There are separate session-handler classes: egw_session_(files|memcache),
* which implement custom session handler or certain extra functionality, like eg. listing sessions, * which implement custom session handler or certain extra functionality, like eg. listing sessions,
@ -528,8 +528,8 @@ class egw_session
} }
$this->kp3 = common::randomstring(24); $this->kp3 = common::randomstring(24);
$this->read_repositories(); $GLOBALS['egw_info']['user'] = $this->read_repositories();
if ($GLOBALS['egw']->accounts->is_expired($this->user)) if ($GLOBALS['egw']->accounts->is_expired($GLOBALS['egw_info']['user']))
{ {
if(is_object($GLOBALS['egw']->log)) if(is_object($GLOBALS['egw']->log))
{ {
@ -547,8 +547,6 @@ class egw_session
return false; return false;
} }
$GLOBALS['egw_info']['user'] = $this->user;
$this->appsession('password','phpgwapi',base64_encode($this->passwd)); $this->appsession('password','phpgwapi',base64_encode($this->passwd));
if ($GLOBALS['egw']->acl->check('anonymous',1,'phpgwapi')) if ($GLOBALS['egw']->acl->check('anonymous',1,'phpgwapi'))
@ -926,10 +924,15 @@ class egw_session
if ($fill_egw_info_and_repositories) if ($fill_egw_info_and_repositories)
{ {
$this->read_repositories(); $GLOBALS['egw_info']['user'] = $this->read_repositories();
}
else
{
// update prefs, which might be changed by an other session
$GLOBALS['egw_info']['user']['preferences'] = $GLOBALS['egw']->preferences->read_repository();
} }
if ($this->user['expires'] != -1 && $this->user['expires'] < time()) if ($GLOBALS['egw']->accounts->is_expired($GLOBALS['egw_info']['user']))
{ {
if (self::ERROR_LOG_DEBUG) error_log("*** session::verify($sessionid) accounts is expired"); if (self::ERROR_LOG_DEBUG) error_log("*** session::verify($sessionid) accounts is expired");
if(is_object($GLOBALS['egw']->log)) if(is_object($GLOBALS['egw']->log))
@ -946,8 +949,6 @@ class egw_session
} }
if ($fill_egw_info_and_repositories) if ($fill_egw_info_and_repositories)
{ {
$GLOBALS['egw_info']['user'] = $this->user;
$GLOBALS['egw_info']['user']['session_ip'] = $session['session_ip']; $GLOBALS['egw_info']['user']['session_ip'] = $session['session_ip'];
$GLOBALS['egw_info']['user']['passwd'] = base64_decode($this->appsession('password','phpgwapi')); $GLOBALS['egw_info']['user']['passwd'] = base64_decode($this->appsession('password','phpgwapi'));
} }
@ -993,7 +994,6 @@ class egw_session
if ($fill_egw_info_and_repositories) if ($fill_egw_info_and_repositories)
{ {
$GLOBALS['egw']->acl->acl($this->account_id); $GLOBALS['egw']->acl->acl($this->account_id);
accounts::getInstance()->setAccountId($this->account_id);
$GLOBALS['egw']->preferences->preferences($this->account_id); $GLOBALS['egw']->preferences->preferences($this->account_id);
$GLOBALS['egw']->applications->applications($this->account_id); $GLOBALS['egw']->applications->applications($this->account_id);
} }
@ -1206,6 +1206,7 @@ class egw_session
* @param string $location free lable to store the data * @param string $location free lable to store the data
* @param string $appname='' default current application (egw_info[flags][currentapp]) * @param string $appname='' default current application (egw_info[flags][currentapp])
* @param mixed $data='##NOTHING##' if given, data to store, if not specified * @param mixed $data='##NOTHING##' if given, data to store, if not specified
* @deprecated use egw_cache::setSession($appname, $location, $data) or egw_cache::getSession($appname, $location)
* @return mixed session data or false if no data stored for $appname/$location * @return mixed session data or false if no data stored for $appname/$location
*/ */
public static function &appsession($location = 'default', $appname = '', $data = '##NOTHING##') public static function &appsession($location = 'default', $appname = '', $data = '##NOTHING##')
@ -1462,46 +1463,47 @@ class egw_session
/** /**
* Read the diverse repositories / init classes with data from the just loged in user * Read the diverse repositories / init classes with data from the just loged in user
* *
* @return array used to assign to $GLOBALS['egw_info']['user']
*/ */
public function read_repositories() public function read_repositories()
{ {
$GLOBALS['egw']->acl->acl($this->account_id); $GLOBALS['egw']->acl->acl($this->account_id);
accounts::getInstance()->setAccountId($this->account_id);
$GLOBALS['egw']->preferences->preferences($this->account_id); $GLOBALS['egw']->preferences->preferences($this->account_id);
$GLOBALS['egw']->applications->applications($this->account_id); $GLOBALS['egw']->applications->applications($this->account_id);
$this->user = $GLOBALS['egw']->accounts->read_repository(); $user = $GLOBALS['egw']->accounts->read($this->account_id);
// set homedirectory from auth_ldap or auth_ads, to be able to use it in vfs // set homedirectory from auth_ldap or auth_ads, to be able to use it in vfs
if (!isset($this->user['homedirectory'])) if (!isset($user['homedirectory']))
{ {
// authentication happens in login.php, which does NOT yet create egw-object in session // authentication happens in login.php, which does NOT yet create egw-object in session
// --> need to store homedirectory in session // --> need to store homedirectory in session
if(isset($GLOBALS['auto_create_acct']['homedirectory'])) if(isset($GLOBALS['auto_create_acct']['homedirectory']))
{ {
egw_cache::setSession(__CLASS__, 'homedirectory', egw_cache::setSession(__CLASS__, 'homedirectory',
$this->user['homedirectory'] = $GLOBALS['auto_create_acct']['homedirectory']); $user['homedirectory'] = $GLOBALS['auto_create_acct']['homedirectory']);
} }
else else
{ {
$this->user['homedirectory'] = egw_cache::getSession(__CLASS__, 'homedirectory'); $user['homedirectory'] = egw_cache::getSession(__CLASS__, 'homedirectory');
} }
} }
$this->user['acl'] = $GLOBALS['egw']->acl->read_repository(); $user['preferences'] = $GLOBALS['egw']->preferences->read_repository();
$this->user['preferences'] = $GLOBALS['egw']->preferences->read_repository();
if (is_object($GLOBALS['egw']->datetime)) if (is_object($GLOBALS['egw']->datetime))
{ {
$GLOBALS['egw']->datetime->datetime(); // to set tz_offset from the now read prefs $GLOBALS['egw']->datetime->datetime(); // to set tz_offset from the now read prefs
} }
$this->user['apps'] = $GLOBALS['egw']->applications->read_repository(); $user['apps'] = $GLOBALS['egw']->applications->read_repository();
$this->user['domain'] = $this->account_domain; $user['domain'] = $this->account_domain;
$this->user['sessionid'] = $this->sessionid; $user['sessionid'] = $this->sessionid;
$this->user['kp3'] = $this->kp3; $user['kp3'] = $this->kp3;
$this->user['session_ip'] = $this->getuser_ip(); $user['session_ip'] = $this->getuser_ip();
$this->user['session_lid'] = $this->account_lid.'@'.$this->account_domain; $user['session_lid'] = $this->account_lid.'@'.$this->account_domain;
$this->user['account_id'] = $this->account_id; $user['account_id'] = $this->account_id;
$this->user['account_lid'] = $this->account_lid; $user['account_lid'] = $this->account_lid;
$this->user['userid'] = $this->account_lid; $user['userid'] = $this->account_lid;
$this->user['passwd'] = @$this->passwd; $user['passwd'] = $this->passwd;
return $user;
} }
/** /**

View File

@ -130,6 +130,64 @@ class preferences
self::__construct(); self::__construct();
} }
/**
* Magic function to avoid storing perferences in session, as they get re-read on each request by egw_session::verify()
*
* @return array with class vars to store
*/
function __sleep()
{
$vars = array_keys(get_object_vars($this));
return array_diff($vars, array('data', 'user', 'group', 'default', 'forced', 'session'));
}
/**
* Lifetime in seconds of cached items 1d
*/
const CACHE_LIFETIME = 86400;
/**
* Read preferences of requested id(s)
*
* @param int|array $ids
* @return array id => app => preference data
*/
function cache_read($ids)
{
$prefs = $db_read = array();
foreach((array)$ids as $id)
{
$prefs[$id] = egw_cache::getInstance(__CLASS__, $id);
if (!isset($prefs[$id])) $db_read[] = $id;
}
if ($db_read)
{
foreach($this->db->select($this->table,'*',array('preference_owner' => $db_read),__LINE__,__FILE__) as $row)
{
// The following replacement is required for PostgreSQL to work
$app = trim($row['preference_app']);
$value = unserialize($row['preference_value']);
if($value === false)
{
// manually retrieve the string lengths of the serialized array if unserialize failed
$value = unserialize(preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.mb_strlen('$2','8bit').':\"$2\";'", $row['preference_value']));
}
$this->unquote($value);
$prefs[$row['preference_owner']][$app] = $value;
}
foreach($db_read as $id)
{
if (!isset($prefs[$id])) $prefs[$id] = array();
egw_cache::setInstance(__CLASS__, $id, $prefs[$id]);
}
}
//error_log(__METHOD__.'('.array2string($ids).') read-from-db='.array2string($db_read));
return $prefs;
}
/**************************************************************************\ /**************************************************************************\
* These are the standard $this->account_id specific functions * * These are the standard $this->account_id specific functions *
\**************************************************************************/ \**************************************************************************/
@ -201,12 +259,12 @@ class preferences
$GLOBALS['egw']->accounts->get_account_name($this->account_id,$lid,$fname,$lname); $GLOBALS['egw']->accounts->get_account_name($this->account_id,$lid,$fname,$lname);
$this->values = array( // standard notify replacements $this->values = array( // standard notify replacements
'fullname' => $GLOBALS['egw']->common->display_fullname('',$fname,$lname), 'fullname' => common::display_fullname('',$fname,$lname),
'firstname' => $fname, 'firstname' => $fname,
'lastname' => $lname, 'lastname' => $lname,
'domain' => $GLOBALS['egw_info']['server']['mail_suffix'], 'domain' => $GLOBALS['egw_info']['server']['mail_suffix'],
'email' => $this->email_address($this->account_id), 'email' => $this->email_address($this->account_id),
'date' => $GLOBALS['egw']->common->show_date('',$GLOBALS['egw_info']['user']['preferences']['common']['dateformat']), 'date' => common::show_date('',$GLOBALS['egw_info']['user']['preferences']['common']['dateformat']),
); );
// do this first, as it might be already contain some substitues // do this first, as it might be already contain some substitues
// //
@ -280,47 +338,33 @@ class preferences
*/ */
function read_repository($use_session=true) function read_repository($use_session=true)
{ {
$this->session = $use_session ? $GLOBALS['egw']->session->appsession('preferences','preferences') : array(); $this->session = $use_session ? egw_cache::getSession('preferences','preferences') : array();
if (!is_array($this->session)) if (!is_array($this->session))
{ {
$this->session = array(); $this->session = array();
} }
$this->forced = $this->default = $this->user = $this->group = array(); $this->forced = $this->default = $this->user = $this->group = array();
$primary_group = $GLOBALS['egw']->accounts->id2name($this->account_id,'account_primary_group'); $primary_group = accounts::id2name($this->account_id, 'account_primary_group');
foreach($this->db->select($this->table,'*',array('preference_owner' => array( foreach($this->cache_read(array(
self::DEFAULT_ID, self::DEFAULT_ID,
self::FORCED_ID, self::FORCED_ID,
$this->account_id, $this->account_id,
$primary_group+self::DEFAULT_ID, // need to offset it with DEFAULT_ID = -2! $primary_group+self::DEFAULT_ID, // need to offset it with DEFAULT_ID = -2!
)),__LINE__,__FILE__) as $row) )) as $id => $values)
{ {
// The following replacement is required for PostgreSQL to work switch($id)
$app = trim($row['preference_app']);
$value = unserialize($row['preference_value']);
if($value === false)
{
// manually retrieve the string lengths of the serialized array if unserialize failed
$value = unserialize(preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.mb_strlen('$2','8bit').':\"$2\";'", $row['preference_value']));
}
$this->unquote($value);
if (!is_array($value))
{
continue;
}
switch($row['preference_owner'])
{ {
case self::FORCED_ID: case self::FORCED_ID:
$this->forced[$app] = $value; $this->forced = $values;
break; break;
case self::DEFAULT_ID: case self::DEFAULT_ID:
//if ($app=='common') error_log(__METHOD__.__LINE__.array2string($value)); $this->default = $values;
$this->default[$app] = $value;
break; break;
case $this->account_id: // user case $this->account_id: // user
$this->user[$app] = $value; $this->user = $values;
break; break;
default: default:
$this->group[$app] = $value; $this->group = $values;
break; break;
} }
} }
@ -387,10 +431,6 @@ class preferences
echo 'group<pre>'; print_r($this->group); echo "</pre>\n"; echo 'group<pre>'; print_r($this->group); echo "</pre>\n";
echo 'effectiv<pre>'; print_r($this->data); echo "</pre>\n"; echo 'effectiv<pre>'; print_r($this->data); echo "</pre>\n";
} }
//error_log(__METHOD__.__LINE__.'->user: remote_application_url:'.array2string($this->user['common']['remote_application_url']));
//error_log(__METHOD__.__LINE__.'->default: remote_application_url:'.array2string($this->default['common']['remote_application_url']));
//error_log(__METHOD__.__LINE__.'->forced: remote_application_url:'.array2string($this->forced['common']['remote_application_url']));
//error_log(__METHOD__.__LINE__.'->effective: remote_application_url:'.array2string($this->data['common']['remote_application_url']));
$this->check_set_tz_offset(); $this->check_set_tz_offset();
return $this->data; return $this->data;
@ -421,14 +461,14 @@ class preferences
*/ */
function check_set_tz_offset() function check_set_tz_offset()
{ {
$prefs =& $this->data['common']; $prefs =& $GLOBALS['egw_info']['user']['preferences']['common'];
if (!empty($prefs['tz'])) if (!empty($prefs['tz']))
{ {
egw_time::setUserPrefs($prefs['tz'],$prefs['dateformat'],$prefs['timeformat']); egw_time::setUserPrefs($prefs['tz'],$prefs['dateformat'],$prefs['timeformat']);
// set the old preference for compatibilty with old code // set the old preference for compatibilty with old code
$GLOBALS['egw_info']['user']['preferences']['common']['tz_offset'] = egw_time::tz_offset_s()/3600; $prefs['tz_offset'] = egw_time::tz_offset_s()/3600;
//echo "<p>".__METHOD__."() tz=$prefs[tz] --> tz_offset={$GLOBALS['egw_info']['user']['preferences']['common']['tz_offset']}</p>\n"; //echo "<p>".__METHOD__."() tz=$prefs[tz] --> tz_offset=$prefs[tz_offset]</p>\n";
// ToDo: get rid of that // ToDo: get rid of that
if (isset($GLOBALS['egw']) && ($GLOBALS['egw'] instanceof egw)) if (isset($GLOBALS['egw']) && ($GLOBALS['egw'] instanceof egw))
@ -719,14 +759,15 @@ class preferences
} }
//echo "<p>preferences::save_repository(,$type): account_id=$account_id, prefs="; print_r($prefs); echo "</p>\n"; //echo "<p>preferences::save_repository(,$type): account_id=$account_id, prefs="; print_r($prefs); echo "</p>\n";
if (isset($GLOBALS['egw_setup']) || !$GLOBALS['egw']->acl->check('session_only_preferences',1,'preferences')) if (isset($GLOBALS['egw_setup']) || !$GLOBALS['egw']->acl->check('session_only_preferences',1,'preferences') &&
(!($old_prefs = $this->cache_read($account_id)) || $old_prefs[$account_id] != $prefs))
{ {
$this->db->transaction_begin(); $this->db->transaction_begin();
$this->db->delete($this->table,array('preference_owner' => $account_id),__LINE__,__FILE__); $this->db->delete($this->table,array('preference_owner' => $account_id),__LINE__,__FILE__);
foreach($prefs as $app => $value) foreach($prefs as $app => $value)
{ {
if (!is_array($value)) if (!is_array($value) || !$value)
{ {
continue; continue;
} }
@ -741,17 +782,8 @@ class preferences
} }
$this->db->transaction_commit(); $this->db->transaction_commit();
if (!isset($GLOBALS['egw_setup'])) // update instance-wide cache
{ egw_cache::setInstance(__CLASS__, $account_id, $prefs);
// no need to invalidate session cache, if we write the prefs to the session too
$egw = unserialize($_SESSION[egw_session::EGW_OBJECT_CACHE]);
$egw->preferences = $this;
$_SESSION[egw_session::EGW_OBJECT_CACHE] = serialize($egw);
}
}
if (!isset($GLOBALS['egw_setup']))
{
$_SESSION[egw_session::EGW_INFO_CACHE]['user']['preferences'] = $GLOBALS['egw_info']['user']['preferences'] = $this->data;
} }
return $this->data; return $this->data;
} }
@ -843,7 +875,7 @@ class preferences
if (!isset($GLOBALS['egw_info']['user']['preferences']['common']['lang']) || if (!isset($GLOBALS['egw_info']['user']['preferences']['common']['lang']) ||
!$GLOBALS['egw_info']['user']['preferences']['common']['lang']) !$GLOBALS['egw_info']['user']['preferences']['common']['lang'])
{ {
$this->add('common','lang',$GLOBALS['egw']->common->getPreferredLanguage()); $this->add('common','lang',common::getPreferredLanguage());
$preferences_update = True; $preferences_update = True;
} }
if ($preferences_update) if ($preferences_update)