2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
2014-06-13 15:15:21 +02:00
|
|
|
* EGroupware API - Preferences
|
2009-10-06 08:45:28 +02:00
|
|
|
*
|
2008-03-15 16:11:48 +01:00
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Joseph Engo <jengo@phpgroupware.org>
|
|
|
|
* @author Mark Peters <skeeter@phpgroupware.org>
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de> merging prefs on runtime, session prefs and reworked the class
|
|
|
|
* Copyright (C) 2000, 2001 Joseph Engo
|
|
|
|
* @license http://opensource.org/licenses/lgpl-license.php LGPL - GNU Lesser General Public License
|
|
|
|
* @package api
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* preferences class used for setting application preferences
|
|
|
|
*
|
2014-01-18 18:43:15 +01:00
|
|
|
* preferences are read into following arrays:
|
|
|
|
* - $data effective prefs used everywhere in EGroupware
|
|
|
|
* Effective prefs are merged together in following precedence from:
|
|
|
|
* - $forced forced preferences set by the admin, they take precedence over user or default prefs
|
|
|
|
* - $session temporary prefs eg. language set on login just for session
|
|
|
|
* - $user the stored user prefs, only used for manipulating and storeing the user prefs
|
|
|
|
* - $group the stored prefs of all group-memberships of current user, can NOT be deleted or stored directly!
|
|
|
|
* - $default the default preferences, always used when the user has no own preference set
|
2010-05-12 11:23:50 +02:00
|
|
|
*
|
2010-01-27 06:44:31 +01:00
|
|
|
* To update the prefs of a certain group, not just the primary group of the user, you have to
|
|
|
|
* create a new instance of preferences class, with the given id of the group. This takes into
|
|
|
|
* account the offset of DEFAULT_ID, we are using currently for groups (as -1, and -2) are already
|
|
|
|
* taken!
|
2014-06-13 15:15:21 +02:00
|
|
|
*
|
|
|
|
* Preferences get now json-encoded and no longer PHP serialized and addslashed,
|
|
|
|
* thought they only change when they get updated.
|
2008-03-15 16:11:48 +01:00
|
|
|
*/
|
|
|
|
class preferences
|
|
|
|
{
|
2009-10-18 14:45:09 +02:00
|
|
|
/**
|
|
|
|
* account_id for default prefs
|
|
|
|
*/
|
|
|
|
const DEFAULT_ID = -2;
|
|
|
|
/**
|
|
|
|
* account_id for forced prefs
|
|
|
|
*/
|
|
|
|
const FORCED_ID = -1;
|
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* account the class is instanciated for
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
var $account_id;
|
|
|
|
/**
|
|
|
|
* account-type u or g
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
var $account_type;
|
|
|
|
/**
|
2009-10-06 08:45:28 +02:00
|
|
|
* effectiv user prefs, used by all apps
|
2008-03-15 16:11:48 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $data = array();
|
|
|
|
/**
|
2009-10-06 08:45:28 +02:00
|
|
|
* set user prefs for saveing (no defaults/forced prefs merged)
|
2008-03-15 16:11:48 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $user = array();
|
2010-01-27 06:44:31 +01:00
|
|
|
/**
|
|
|
|
* primary group prefs
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $group = array();
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
2009-10-06 08:45:28 +02:00
|
|
|
* default prefs
|
2008-03-15 16:11:48 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $default = array();
|
|
|
|
/**
|
|
|
|
* forced prefs
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $forced = array();
|
|
|
|
/**
|
|
|
|
* session / tempory prefs
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
var $session = array();
|
|
|
|
/**
|
2009-10-06 08:45:28 +02:00
|
|
|
* @var egw_db
|
2008-03-15 16:11:48 +01:00
|
|
|
*/
|
|
|
|
var $db;
|
|
|
|
/**
|
|
|
|
* table-name
|
|
|
|
*/
|
2014-06-13 15:15:21 +02:00
|
|
|
const TABLE = 'egw_preferences';
|
|
|
|
var $table = self::TABLE;
|
2008-03-15 16:11:48 +01:00
|
|
|
|
|
|
|
var $values,$vars; // standard notify substitues, will be set by standard_substitues()
|
2009-10-18 14:45:09 +02:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
2009-10-18 14:45:09 +02:00
|
|
|
* Contstructor
|
|
|
|
*
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param int|string $account_id =''
|
2009-10-18 14:45:09 +02:00
|
|
|
* @return preferences
|
2008-03-15 16:11:48 +01:00
|
|
|
*/
|
2009-10-18 14:45:09 +02:00
|
|
|
function __construct($account_id = '')
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
2010-01-27 06:44:31 +01:00
|
|
|
if (isset($GLOBALS['egw']->db))
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
|
|
|
$this->db = $GLOBALS['egw']->db;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db = $GLOBALS['egw_setup']->db;
|
|
|
|
$this->table = $GLOBALS['egw_setup']->prefs_table;
|
|
|
|
}
|
2013-05-10 19:20:37 +02:00
|
|
|
$this->set_account_id($account_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set account_id for class
|
|
|
|
*
|
|
|
|
* Takes care of offset for groups.
|
|
|
|
*
|
2013-11-14 16:33:12 +01:00
|
|
|
* @param int|string $account_id numeric account_id, "default", "forced" to load default or forced preferences
|
|
|
|
* or account_lid (only if !== "default" or "forced"!)
|
2013-05-10 19:20:37 +02:00
|
|
|
*/
|
|
|
|
function set_account_id($account_id)
|
|
|
|
{
|
2013-11-14 16:33:12 +01:00
|
|
|
if ($account_id === 'default')
|
|
|
|
{
|
|
|
|
$this->account_id = self::DEFAULT_ID;
|
|
|
|
}
|
|
|
|
elseif ($account_id === 'forced')
|
|
|
|
{
|
|
|
|
$this->account_id = self::FORCED_ID;
|
|
|
|
}
|
2010-01-27 06:44:31 +01:00
|
|
|
// if we got instancated for a group, need to set offset of DEFAULT_ID!
|
2013-11-14 16:33:12 +01:00
|
|
|
elseif ($account_id < 0 || !is_numeric($account_id) && ($account_id = get_account_id($account_id)) < 0)
|
2010-01-27 06:44:31 +01:00
|
|
|
{
|
|
|
|
$this->account_id = $account_id + self::DEFAULT_ID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-14 16:33:12 +01:00
|
|
|
$this->account_id = $account_id;
|
2010-01-27 06:44:31 +01:00
|
|
|
}
|
2013-11-14 16:33:12 +01:00
|
|
|
//error_log(__METHOD__."($account_id) setting this->account_id to $this->account_id");
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
|
|
|
|
2013-05-10 19:20:37 +02:00
|
|
|
/**
|
|
|
|
* Return account_id class is instanciated for or "default" or "forced"
|
|
|
|
*
|
|
|
|
* Takes care of offset for groups.
|
|
|
|
*
|
|
|
|
* @return string|int
|
|
|
|
*/
|
|
|
|
function get_account_id()
|
|
|
|
{
|
|
|
|
switch ($this->account_id)
|
|
|
|
{
|
|
|
|
case self::DEFAULT_ID:
|
|
|
|
return 'default';
|
|
|
|
case self::FORCED_ID:
|
|
|
|
return 'forced';
|
|
|
|
}
|
|
|
|
return $this->account_id < self::DEFAULT_ID ? $this->account_id-self::DEFAULT_ID : $this->account_id;
|
|
|
|
}
|
|
|
|
|
2009-10-18 14:45:09 +02:00
|
|
|
/**
|
|
|
|
* Old PHP4 contstructor
|
|
|
|
*
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param int|string $account_id =''
|
2009-10-18 14:45:09 +02:00
|
|
|
* @return preferences
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
function preferences($account_id = '')
|
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
self::__construct($account_id);
|
2009-10-18 14:45:09 +02:00
|
|
|
}
|
|
|
|
|
2012-08-07 15:57:56 +02:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2012-10-30 11:22:05 +01:00
|
|
|
$prefs = egw_cache::getInstance(__CLASS__, $ids);
|
|
|
|
$db_read = array();
|
2012-08-07 15:57:56 +02:00
|
|
|
foreach((array)$ids as $id)
|
|
|
|
{
|
2012-10-30 11:22:05 +01:00
|
|
|
// if prefs are not returned, null or not an array, read them from db
|
2012-09-26 12:18:18 +02:00
|
|
|
if (!isset($prefs[$id]) && !is_array($prefs[$id])) $db_read[] = $id;
|
2012-08-07 15:57:56 +02:00
|
|
|
}
|
|
|
|
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']);
|
|
|
|
|
2014-06-13 15:15:21 +02:00
|
|
|
$prefs[$row['preference_owner']][$app] = self::unserialize($row['preference_value']);
|
2012-08-07 15:57:56 +02:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-07-22 11:05:01 +02:00
|
|
|
/**
|
2008-03-15 16:11:48 +01:00
|
|
|
* parses a notify and replaces the substitutes
|
2005-07-22 11:05:01 +02:00
|
|
|
*
|
2008-03-15 16:11:48 +01:00
|
|
|
* @param string $msg message to parse / substitute
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param array $values =array() extra vars to replace in addition to $this->values, vars are in an array with \
|
2008-03-15 16:11:48 +01:00
|
|
|
* $key => $value pairs, $key does not include the $'s and is the *untranslated* name
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param boolean $use_standard_values =true should the standard values are used
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return string with parsed notify-msg
|
|
|
|
*/
|
|
|
|
function parse_notify($msg,$values=array(),$use_standard_values=True)
|
|
|
|
{
|
|
|
|
$vals = $values ? $values : array();
|
|
|
|
|
|
|
|
if ($use_standard_values && is_array($this->values))
|
|
|
|
{
|
|
|
|
$vals += $this->values;
|
|
|
|
}
|
2011-12-20 16:57:03 +01:00
|
|
|
$replace = $with = array();
|
2008-03-15 16:11:48 +01:00
|
|
|
foreach($vals as $key => $val)
|
|
|
|
{
|
2008-12-09 13:59:03 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__." replacing \$\$$key\$\$ with $val ");
|
2008-03-15 16:11:48 +01:00
|
|
|
$replace[] = '$$'.$key.'$$';
|
|
|
|
$with[] = $val;
|
|
|
|
}
|
|
|
|
return str_replace($replace,$with,$msg);
|
|
|
|
}
|
2009-10-06 08:45:28 +02:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* replaces the english key's with translated ones, or if $un_lang the opposite
|
2005-11-13 08:49:56 +01:00
|
|
|
*
|
2008-03-15 16:11:48 +01:00
|
|
|
* @param string $msg message to translate
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param array $vals =array() extra vars to replace in addition to $this->values, vars are in an array with \
|
2008-03-15 16:11:48 +01:00
|
|
|
* $key => $value pairs, $key does not include the $'s and is the *untranslated* name
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param boolean $un_lang =false if true translate back
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return string
|
2005-07-22 11:05:01 +02:00
|
|
|
*/
|
2008-03-15 16:11:48 +01:00
|
|
|
function lang_notify($msg,$vals=array(),$un_lang=False)
|
2001-07-26 13:13:15 +02:00
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
foreach(array_keys($vals) as $key)
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$lname = ($lname = lang($key)) == $key.'*' ? $key : $lname;
|
|
|
|
if ($un_lang)
|
2005-07-22 11:05:01 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$langs[$lname] = '$$'.$key.'$$';
|
2005-07-22 11:05:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$langs[$key] = '$$'.$lname.'$$';
|
2005-07-22 11:05:01 +02:00
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
return $this->parse_notify($msg,$langs,False);
|
|
|
|
}
|
2001-02-03 11:48:41 +01:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* define some standard substitues-values and use them on the prefs, if needed
|
|
|
|
*/
|
|
|
|
function standard_substitutes()
|
|
|
|
{
|
2008-12-09 13:59:03 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__." is called ");
|
2008-03-15 16:11:48 +01:00
|
|
|
if (!is_array(@$GLOBALS['egw_info']['user']['preferences']))
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$GLOBALS['egw_info']['user']['preferences'] = $this->data; // else no lang()
|
2003-05-02 03:33:32 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
// we cant use phpgw_info/user/fullname, as it's not set when we run
|
2014-06-13 15:15:21 +02:00
|
|
|
$lid = $fname = $lname = null;
|
2008-03-15 16:11:48 +01:00
|
|
|
$GLOBALS['egw']->accounts->get_account_name($this->account_id,$lid,$fname,$lname);
|
|
|
|
|
|
|
|
$this->values = array( // standard notify replacements
|
2012-08-07 15:57:56 +02:00
|
|
|
'fullname' => common::display_fullname('',$fname,$lname),
|
2008-03-15 16:11:48 +01:00
|
|
|
'firstname' => $fname,
|
|
|
|
'lastname' => $lname,
|
|
|
|
'domain' => $GLOBALS['egw_info']['server']['mail_suffix'],
|
|
|
|
'email' => $this->email_address($this->account_id),
|
2012-08-07 15:57:56 +02:00
|
|
|
'date' => common::show_date('',$GLOBALS['egw_info']['user']['preferences']['common']['dateformat']),
|
2008-03-15 16:11:48 +01:00
|
|
|
);
|
|
|
|
// do this first, as it might be already contain some substitues
|
|
|
|
//
|
|
|
|
$this->values['email'] = $this->parse_notify($this->values['email']);
|
|
|
|
|
|
|
|
$this->vars = array( // langs have to be in common !!!
|
|
|
|
'fullname' => lang('name of the user, eg. "%1"',$this->values['fullname']),
|
|
|
|
'firstname' => lang('first name of the user, eg. "%1"',$this->values['firstname']),
|
|
|
|
'lastname' => lang('last name of the user, eg. "%1"',$this->values['lastname']),
|
|
|
|
'domain' => lang('domain name for mail-address, eg. "%1"',$this->values['domain']),
|
|
|
|
'email' => lang('email-address of the user, eg. "%1"',$this->values['email']),
|
|
|
|
'date' => lang('todays date, eg. "%1"',$this->values['date']),
|
|
|
|
);
|
2008-12-09 14:07:29 +01:00
|
|
|
if ($this->debug) error_log(__METHOD__.print_r($this->vars,true));
|
2008-03-15 16:11:48 +01:00
|
|
|
// do the substituetion in the effective prefs (data)
|
|
|
|
//
|
|
|
|
foreach($this->data as $app => $data)
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2013-06-12 00:43:42 +02:00
|
|
|
if(!is_array($data)) continue;
|
2008-03-15 16:11:48 +01:00
|
|
|
foreach($data as $key => $val)
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
if (!is_array($val) && strpos($val,'$$') !== False)
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->data[$app][$key] = $this->parse_notify($val);
|
2003-05-02 03:33:32 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
elseif (is_array($val))
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
foreach($val as $k => $v)
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
if (!is_array($v) && strpos($v,'$$') !== False)
|
2003-05-02 03:33:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->data[$app][$key][$k] = $this->parse_notify($v);
|
2003-05-02 03:33:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2003-05-02 03:33:32 +02:00
|
|
|
|
2014-06-13 15:15:21 +02:00
|
|
|
/**
|
|
|
|
* Unserialize data from either json_encode or PHP serialize and addslashes
|
|
|
|
*
|
|
|
|
* @param string $str serialized prefs
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function unserialize($str)
|
|
|
|
{
|
|
|
|
// handling of new json-encoded prefs
|
2014-06-26 19:38:29 +02:00
|
|
|
if ($str[0] != 'a' && $str[1] != ':')
|
2014-06-13 15:15:21 +02:00
|
|
|
{
|
|
|
|
return json_decode($str, true);
|
|
|
|
}
|
|
|
|
// handling of old PHP serialized and addslashed prefs
|
2014-06-26 19:38:29 +02:00
|
|
|
$data = php_safe_unserialize($str);
|
2014-06-13 15:15:21 +02:00
|
|
|
if($data === false)
|
|
|
|
{
|
|
|
|
// manually retrieve the string lengths of the serialized array if unserialize failed
|
2014-06-26 19:38:29 +02:00
|
|
|
$data = php_safe_unserialize(preg_replace_callback('!s:(\d+):"(.*?)";!s', function($matches)
|
2014-06-13 15:15:21 +02:00
|
|
|
{
|
|
|
|
return 's:'.mb_strlen($matches[2],'8bit').':"'.$matches[2].'";';
|
|
|
|
}, $str));
|
|
|
|
}
|
|
|
|
self::unquote($data);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* unquote (stripslashes) recursivly the whole array
|
|
|
|
*
|
|
|
|
* @param array &$arr array to unquote (var-param!)
|
|
|
|
*/
|
2014-06-13 15:15:21 +02:00
|
|
|
protected static function unquote(&$arr)
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
|
|
|
if (!is_array($arr))
|
2003-05-01 11:19:50 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$arr = stripslashes($arr);
|
|
|
|
return;
|
2003-05-01 11:19:50 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
foreach($arr as $key => $value)
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
if (is_array($value))
|
2001-07-26 13:13:15 +02:00
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
self::unquote($arr[$key]);
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
else
|
2003-12-20 19:32:53 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$arr[$key] = stripslashes($value);
|
2003-05-01 11:19:50 +02:00
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2001-07-26 13:13:15 +02:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* read preferences from the repository
|
|
|
|
*
|
|
|
|
* the function ready all 3 prefs user/default/forced and merges them to the effective ones
|
|
|
|
*
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param boolean $use_session =true should the session prefs get used (default true) or not (false)
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return array with effective prefs ($this->data)
|
|
|
|
*/
|
2010-05-20 21:32:52 +02:00
|
|
|
function read_repository($use_session=true)
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
2012-08-07 15:57:56 +02:00
|
|
|
$this->session = $use_session ? egw_cache::getSession('preferences','preferences') : array();
|
2008-03-15 16:11:48 +01:00
|
|
|
if (!is_array($this->session))
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->session = array();
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2010-01-27 06:44:31 +01:00
|
|
|
$this->forced = $this->default = $this->user = $this->group = array();
|
2012-10-30 11:22:05 +01:00
|
|
|
$to_read = array(self::DEFAULT_ID,self::FORCED_ID,$this->account_id);
|
|
|
|
if ($this->account_id > 0)
|
|
|
|
{
|
|
|
|
$primary_group = accounts::id2name($this->account_id, 'account_primary_group');
|
2014-07-18 13:14:33 +02:00
|
|
|
foreach((array)$GLOBALS['egw']->accounts->memberships($this->account_id, true) as $gid)
|
2012-10-30 11:22:05 +01:00
|
|
|
{
|
|
|
|
if ($gid != $primary_group) $to_read[] = $gid + self::DEFAULT_ID; // need to offset it with DEFAULT_ID = -2!
|
|
|
|
}
|
|
|
|
$to_read[] = $primary_group + self::DEFAULT_ID;
|
|
|
|
}
|
|
|
|
foreach($this->cache_read($to_read) as $id => $values)
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2012-08-07 15:57:56 +02:00
|
|
|
switch($id)
|
2003-05-20 01:05:32 +02:00
|
|
|
{
|
2009-10-18 14:45:09 +02:00
|
|
|
case self::FORCED_ID:
|
2012-08-07 15:57:56 +02:00
|
|
|
$this->forced = $values;
|
2004-03-21 22:17:34 +01:00
|
|
|
break;
|
2009-10-18 14:45:09 +02:00
|
|
|
case self::DEFAULT_ID:
|
2012-08-07 15:57:56 +02:00
|
|
|
$this->default = $values;
|
2003-05-20 01:05:32 +02:00
|
|
|
break;
|
2010-01-27 06:44:31 +01:00
|
|
|
case $this->account_id: // user
|
2012-08-07 15:57:56 +02:00
|
|
|
$this->user = $values;
|
2003-05-20 01:05:32 +02:00
|
|
|
break;
|
2010-01-27 06:44:31 +01:00
|
|
|
default:
|
2012-10-30 11:22:05 +01:00
|
|
|
foreach($values as $app => $vals)
|
|
|
|
{
|
|
|
|
$this->group[$app] = $vals + (array)$this->group[$app];
|
|
|
|
}
|
2010-01-27 06:44:31 +01:00
|
|
|
break;
|
2003-05-20 01:05:32 +02:00
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->data = $this->user;
|
2001-07-26 13:13:15 +02:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
// let the (temp.) session prefs. override the user prefs.
|
|
|
|
//
|
|
|
|
foreach($this->session as $app => $values)
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
foreach($values as $var => $value)
|
2001-07-26 13:13:15 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->data[$app][$var] = $value;
|
2003-05-20 01:05:32 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2003-09-06 05:23:37 +02:00
|
|
|
|
2010-01-27 06:44:31 +01:00
|
|
|
// now use (primary) group defaults if needed (user-value unset or empty)
|
|
|
|
//
|
2012-08-22 10:00:36 +02:00
|
|
|
foreach((array)$this->group as $app => $values)
|
2010-01-27 06:44:31 +01:00
|
|
|
{
|
2012-08-22 10:00:36 +02:00
|
|
|
foreach((array)$values as $var => $value)
|
2010-01-27 06:44:31 +01:00
|
|
|
{
|
|
|
|
if (!isset($this->data[$app][$var]) || $this->data[$app][$var] === '')
|
|
|
|
{
|
|
|
|
$this->data[$app][$var] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
// now use defaults if needed (user-value unset or empty)
|
|
|
|
//
|
2012-08-22 10:00:36 +02:00
|
|
|
foreach((array)$this->default as $app => $values)
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
2012-08-22 10:00:36 +02:00
|
|
|
foreach((array)$values as $var => $value)
|
2003-05-20 01:05:32 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
if (!isset($this->data[$app][$var]) || $this->data[$app][$var] === '')
|
2003-05-20 01:05:32 +02:00
|
|
|
{
|
2011-09-26 11:52:43 +02:00
|
|
|
//if ($var=='remote_application_url') error_log(__METHOD__.__LINE__.' default for '.$var.' with '.$value);
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->data[$app][$var] = $value;
|
2003-05-20 01:05:32 +02:00
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
// now set/force forced values
|
|
|
|
//
|
2012-08-22 10:00:36 +02:00
|
|
|
foreach((array)$this->forced as $app => $values)
|
2005-11-20 08:32:51 +01:00
|
|
|
{
|
2012-08-22 10:00:36 +02:00
|
|
|
foreach((array)$values as $var => $value)
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
|
|
|
$this->data[$app][$var] = $value;
|
|
|
|
}
|
2005-11-20 08:32:51 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
// setup the standard substitutes and substitutes the data in $this->data
|
|
|
|
//
|
2013-02-07 16:32:18 +01:00
|
|
|
if ($GLOBALS['egw_info']['flags']['load_translations'] !== false)
|
|
|
|
{
|
|
|
|
$this->standard_substitutes();
|
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
// This is to supress warnings during login
|
|
|
|
if (is_array($this->data))
|
2002-01-10 11:09:10 +01:00
|
|
|
{
|
|
|
|
reset($this->data);
|
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
if (isset($this->debug) && substr($GLOBALS['egw_info']['flags']['currentapp'],0,3) != 'log')
|
|
|
|
{
|
|
|
|
echo 'user<pre>'; print_r($this->user); echo "</pre>\n";
|
|
|
|
echo 'forced<pre>'; print_r($this->forced); echo "</pre>\n";
|
|
|
|
echo 'default<pre>'; print_r($this->default); echo "</pre>\n";
|
2010-01-27 06:44:31 +01:00
|
|
|
echo 'group<pre>'; print_r($this->group); echo "</pre>\n";
|
2009-10-06 08:45:28 +02:00
|
|
|
echo 'effectiv<pre>'; print_r($this->data); echo "</pre>\n";
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2009-10-06 08:45:28 +02:00
|
|
|
$this->check_set_tz_offset();
|
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
return $this->data;
|
|
|
|
}
|
2002-01-10 11:09:10 +01:00
|
|
|
|
2012-03-01 19:03:13 +01:00
|
|
|
/**
|
|
|
|
* Get default preferences (also taking forced preferences into account!)
|
|
|
|
*
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param string $app =null
|
|
|
|
* @param string $name =null
|
2012-03-01 19:03:13 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function default_prefs($app=null,$name=null)
|
|
|
|
{
|
|
|
|
// boetemplate::complete_array_merge() is identical to PHP >= 5.3 array_replace_recursive()
|
|
|
|
$default = boetemplate::complete_array_merge($this->default, $this->forced);
|
|
|
|
|
|
|
|
if ($app) $default = $default[$app];
|
|
|
|
|
|
|
|
if ($name && is_array($default)) $default = $default[$name];
|
|
|
|
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2009-10-06 08:45:28 +02:00
|
|
|
/**
|
2009-10-07 10:05:10 +02:00
|
|
|
* Checking new timezone ('tz') pref and setting old tz_offset pref from it
|
2009-10-06 08:45:28 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function check_set_tz_offset()
|
|
|
|
{
|
2012-08-07 15:57:56 +02:00
|
|
|
$prefs =& $GLOBALS['egw_info']['user']['preferences']['common'];
|
2009-10-06 08:45:28 +02:00
|
|
|
|
2009-10-08 18:14:18 +02:00
|
|
|
if (!empty($prefs['tz']))
|
2009-10-06 08:45:28 +02:00
|
|
|
{
|
2012-03-02 10:53:41 +01:00
|
|
|
egw_time::setUserPrefs($prefs['tz'],$prefs['dateformat'],$prefs['timeformat']);
|
2009-10-08 18:14:18 +02:00
|
|
|
// set the old preference for compatibilty with old code
|
2012-08-07 15:57:56 +02:00
|
|
|
$prefs['tz_offset'] = egw_time::tz_offset_s()/3600;
|
|
|
|
//echo "<p>".__METHOD__."() tz=$prefs[tz] --> tz_offset=$prefs[tz_offset]</p>\n";
|
2009-10-06 08:45:28 +02:00
|
|
|
|
2009-10-08 18:14:18 +02:00
|
|
|
// ToDo: get rid of that
|
2011-09-26 11:52:43 +02:00
|
|
|
if (isset($GLOBALS['egw']) && ($GLOBALS['egw'] instanceof egw))
|
2009-10-08 18:14:18 +02:00
|
|
|
{
|
|
|
|
$GLOBALS['egw']->unset_datetime(); // to force an update
|
|
|
|
}
|
2009-10-06 08:45:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-08 18:14:18 +02:00
|
|
|
/**
|
|
|
|
* Set user timezone, if we get restored from session
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function __wakeup()
|
|
|
|
{
|
|
|
|
$this->check_set_tz_offset();
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* read preferences from repository and stores in an array
|
|
|
|
*
|
|
|
|
* @return array containing the effective user preferences
|
|
|
|
*/
|
|
|
|
function read()
|
|
|
|
{
|
|
|
|
if (count($this->data) == 0)
|
2002-01-10 11:09:10 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->read_repository();
|
2002-01-10 11:09:10 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
reset ($this->data);
|
|
|
|
return $this->data;
|
|
|
|
}
|
2002-01-10 11:09:10 +01:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* add preference to $app_name a particular app
|
|
|
|
*
|
|
|
|
* the effective prefs ($this->data) are updated to reflect the change
|
|
|
|
*
|
|
|
|
* @param string $app_name name of the app
|
|
|
|
* @param string $var name of preference to be stored
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param mixed $value ='##undef##' value of the preference, if not given $GLOBALS[$var] is used
|
|
|
|
* @param string $type ='user' of preference to set: forced, default, user
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return array with new effective prefs (even when forced or default prefs are set !)
|
|
|
|
*/
|
|
|
|
function add($app_name,$var,$value = '##undef##',$type='user')
|
|
|
|
{
|
|
|
|
//echo "<p>add('$app_name','$var','$value')</p>\n";
|
|
|
|
if ($value === '##undef##')
|
2003-05-01 11:19:50 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$value = $GLOBALS[$var];
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($type)
|
|
|
|
{
|
|
|
|
case 'session':
|
|
|
|
if (!isset($this->forced[$app_name][$var]) || $this->forced[$app_name][$var] === '')
|
2003-05-01 11:19:50 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->session[$app_name][$var] = $this->data[$app_name][$var] = $value;
|
|
|
|
$GLOBALS['egw']->session->appsession('preferences','preferences',$this->session);
|
|
|
|
if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->invalidate_session_cache(); // in case with cache the egw_info array in the session
|
|
|
|
}
|
2003-05-01 11:19:50 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'forced':
|
|
|
|
$this->data[$app_name][$var] = $this->forced[$app_name][$var] = $value;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'default':
|
|
|
|
$this->default[$app_name][$var] = $value;
|
|
|
|
if ((!isset($this->forced[$app_name][$var]) || $this->forced[$app_name][$var] === '') &&
|
|
|
|
(!isset($this->user[$app_name][$var]) || $this->user[$app_name][$var] === ''))
|
2003-05-01 11:19:50 +02:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$this->data[$app_name][$var] = $value;
|
2003-05-01 11:19:50 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
break;
|
|
|
|
|
2010-04-21 13:09:38 +02:00
|
|
|
case 'user':
|
2008-03-15 16:11:48 +01:00
|
|
|
default:
|
|
|
|
$this->user[$app_name][$var] = $value;
|
|
|
|
if (!isset($this->forced[$app_name][$var]) || $this->forced[$app_name][$var] === '')
|
|
|
|
{
|
|
|
|
$this->data[$app_name][$var] = $value;
|
|
|
|
}
|
|
|
|
break;
|
2003-05-01 11:19:50 +02:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
reset($this->data);
|
|
|
|
return $this->data;
|
|
|
|
}
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* delete preference from $app_name
|
|
|
|
*
|
|
|
|
* the effektive prefs ($this->data) are updated to reflect the change
|
|
|
|
*
|
|
|
|
* @param string $app_name name of app
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param string $var =false variable to be deleted
|
|
|
|
* @param string $type ='user' of preference to set: forced, default, user
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return array with new effective prefs (even when forced or default prefs are deleted!)
|
|
|
|
*/
|
|
|
|
function delete($app_name, $var = False,$type = 'user')
|
|
|
|
{
|
|
|
|
//echo "<p>delete('$app_name','$var','$type')</p>\n";
|
|
|
|
$set_via = array(
|
|
|
|
'forced' => array('user','default'),
|
|
|
|
'default' => array('forced','user'),
|
2010-01-27 06:44:31 +01:00
|
|
|
'user' => array('forced','group','default'),
|
|
|
|
'group' => array('forced'),
|
2008-03-15 16:11:48 +01:00
|
|
|
);
|
|
|
|
if (!isset($set_via[$type]))
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
$type = 'user';
|
|
|
|
}
|
|
|
|
$pref = &$this->$type;
|
2003-05-01 11:19:50 +02:00
|
|
|
|
2010-04-21 13:09:38 +02:00
|
|
|
if (($all = empty($var))) // to check if $var is regarded as empty (false, 0, '', null, array() should do the trick
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
2010-05-12 11:23:50 +02:00
|
|
|
unset($pref[$app_name]);
|
2008-03-15 16:11:48 +01:00
|
|
|
unset($this->data[$app_name]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-12 11:23:50 +02:00
|
|
|
unset($pref[$app_name][$var]);
|
2008-03-15 16:11:48 +01:00
|
|
|
unset($this->data[$app_name][$var]);
|
|
|
|
}
|
|
|
|
// set the effectiv pref again if needed
|
|
|
|
//
|
|
|
|
foreach ($set_via[$type] as $set_from)
|
|
|
|
{
|
2010-04-21 13:09:38 +02:00
|
|
|
$arr = &$this->$set_from;
|
2008-03-15 16:11:48 +01:00
|
|
|
if ($all)
|
2001-04-04 06:54:12 +02:00
|
|
|
{
|
2010-04-21 13:09:38 +02:00
|
|
|
if (isset($arr[$app_name]))
|
2001-04-04 06:54:12 +02:00
|
|
|
{
|
2010-04-21 13:09:38 +02:00
|
|
|
$this->data[$app_name] = $arr[$app_name];
|
2008-03-15 16:11:48 +01:00
|
|
|
break;
|
2001-04-04 06:54:12 +02:00
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2001-04-04 06:54:12 +02:00
|
|
|
else
|
|
|
|
{
|
2008-03-15 16:11:48 +01:00
|
|
|
if($var && @isset($arr[$app_name][$var]) && $arr[$app_name][$var] !== '')
|
|
|
|
{
|
2010-04-21 13:09:38 +02:00
|
|
|
$this->data[$app_name][$var] = $arr[$app_name][$var];
|
2008-03-15 16:11:48 +01:00
|
|
|
break;
|
|
|
|
}
|
2005-11-24 21:08:09 +01:00
|
|
|
}
|
2010-04-21 13:09:38 +02:00
|
|
|
unset($arr);
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
reset ($this->data);
|
|
|
|
return $this->data;
|
|
|
|
}
|
2009-10-06 08:45:28 +02:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* delete all prefs of a given user
|
|
|
|
*
|
|
|
|
* @param int $accountid
|
|
|
|
*/
|
|
|
|
function delete_user($accountid)
|
|
|
|
{
|
2014-01-14 12:53:53 +01:00
|
|
|
if ($accountid > 0)
|
2010-01-27 06:44:31 +01:00
|
|
|
{
|
2014-01-23 13:33:25 +01:00
|
|
|
$this->db->delete($this->table,array('preference_owner' => $accountid),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
egw_cache::unsetInstance(__CLASS__, $accountid);
|
2010-01-27 06:44:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete all prefs of a given group
|
|
|
|
*
|
|
|
|
* @param int $accountid
|
|
|
|
*/
|
|
|
|
function delete_group($accountid)
|
|
|
|
{
|
2014-01-14 12:53:53 +01:00
|
|
|
if ($accountid < 0)
|
2010-01-27 06:44:31 +01:00
|
|
|
{
|
2014-01-23 13:33:25 +01:00
|
|
|
$this->db->delete($this->table,array('preference_owner' => $accountid+self::DEFAULT_ID),__LINE__,__FILE__);
|
|
|
|
|
|
|
|
egw_cache::unsetInstance(__CLASS__, $accountid+self::DEFAULT_ID);
|
2010-01-27 06:44:31 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2001-07-26 13:13:15 +02:00
|
|
|
|
2013-04-23 00:32:40 +02:00
|
|
|
/**
|
2014-06-13 15:15:21 +02:00
|
|
|
* Change single value in preferences of all users (incl. groups, default and forced)
|
2013-04-23 00:32:40 +02:00
|
|
|
*
|
2015-08-24 18:02:07 +02:00
|
|
|
* @param string $app app-name or null for all apps
|
|
|
|
* @param string $name attribute name or regular expression (enclosed in /) to match attribute-name eg. '/^favorite_/'
|
|
|
|
* @param string|callable $value new value to set, or null or '' to delete it or callable returning new value: function($attr, $old_value)
|
2014-06-13 15:15:21 +02:00
|
|
|
* @param string $old_value if given, only change if that's current value
|
|
|
|
* @param string $type if given limit to "user", "forced", "default", "group"
|
2013-04-23 00:32:40 +02:00
|
|
|
*/
|
2014-06-13 15:15:21 +02:00
|
|
|
public static function change_preference($app, $name, $value, $old_value=null, $type=null)
|
2013-04-23 00:32:40 +02:00
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
$db = isset($GLOBALS['egw_setup']->db) ? $GLOBALS['egw_setup']->db : $GLOBALS['egw']->db;
|
|
|
|
|
2015-08-24 18:02:07 +02:00
|
|
|
$where = array();
|
|
|
|
if ($app) $where['preference_app'] = $app;
|
|
|
|
|
2013-04-23 00:32:40 +02:00
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case 'forced':
|
|
|
|
$where['preference_owner'] = self::FORCED_ID;
|
|
|
|
break;
|
|
|
|
case 'default':
|
|
|
|
$where['preference_owner'] = self::DEFAULT_ID;
|
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
$where[] = 'preference_owner > 0';
|
|
|
|
break;
|
2014-06-13 15:15:21 +02:00
|
|
|
case 'group':
|
|
|
|
$where[] = 'preference_owner < '.self::DEFAULT_ID;
|
|
|
|
break;
|
2013-04-23 00:32:40 +02:00
|
|
|
}
|
2014-06-13 15:15:21 +02:00
|
|
|
foreach($db->select(self::TABLE, '*', $where, __LINE__, __FILE__) as $row)
|
2013-04-23 00:32:40 +02:00
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
$prefs = self::unserialize($row['preference_value']);
|
|
|
|
|
2015-08-24 18:02:07 +02:00
|
|
|
if ($name[0] == '/' && substr($name, -1) == '/')
|
2013-04-23 00:32:40 +02:00
|
|
|
{
|
2015-08-24 18:02:07 +02:00
|
|
|
$attrs = array_filter(array_keys($prefs), function($n) use ($name)
|
|
|
|
{
|
|
|
|
return preg_match($name, $n);
|
|
|
|
});
|
2014-06-13 15:15:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-24 18:02:07 +02:00
|
|
|
$attrs = array($name);
|
2013-04-23 00:32:40 +02:00
|
|
|
}
|
|
|
|
|
2015-08-24 18:02:07 +02:00
|
|
|
$updated = false;
|
|
|
|
foreach($attrs as $attr)
|
|
|
|
{
|
|
|
|
if (isset($old_value) && $prefs[$attr] != $old_value) continue;
|
|
|
|
|
|
|
|
$val = is_callable($value) ? call_user_func($value, $attr, $prefs[$attr]) : $value;
|
|
|
|
if ($val === $prefs[$attr]) continue;
|
2001-10-05 05:15:45 +02:00
|
|
|
|
2015-08-24 18:02:07 +02:00
|
|
|
$updated = true;
|
|
|
|
if ((string)$val !== '')
|
|
|
|
{
|
|
|
|
$prefs[$attr] = $val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($prefs[$attr]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if somethings changed or old row was php-serialized --> store it again json-encoded
|
|
|
|
if ($updated || $row['preference_value'][0] == 'a' && $row['preference_value'][1] == ':')
|
2014-06-13 15:15:21 +02:00
|
|
|
{
|
2015-08-24 18:02:07 +02:00
|
|
|
$db->update(self::TABLE, array(
|
|
|
|
'preference_value' => json_encode($prefs),
|
|
|
|
), array(
|
|
|
|
'preference_owner' => $row['preference_owner'],
|
|
|
|
'preference_app' => $row['preference_app'],
|
|
|
|
), __LINE__, __FILE__);
|
|
|
|
|
|
|
|
// update instance-wide cache
|
|
|
|
$cached = egw_cache::getInstance(__CLASS__, $row['preference_owner']);
|
|
|
|
if($cached && $cached[$row['preference_app']])
|
|
|
|
{
|
|
|
|
$cached[$row['preference_app']] = $prefs;
|
|
|
|
egw_cache::setInstance(__CLASS__, $row['preference_owner'], $cached);
|
|
|
|
}
|
2014-06-13 15:15:21 +02:00
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
2014-06-13 15:15:21 +02:00
|
|
|
* Completely delete the specified preference name from all users
|
2008-03-15 16:11:48 +01:00
|
|
|
*
|
2014-06-13 15:15:21 +02:00
|
|
|
* @param string $app Application name
|
|
|
|
* @param string $name Preference name
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param string $type ='user' of preference to set: forced, default, user
|
2008-03-15 16:11:48 +01:00
|
|
|
*/
|
2014-06-13 15:15:21 +02:00
|
|
|
public static function delete_preference($app, $name, $type='user')
|
2008-03-15 16:11:48 +01:00
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
self::change_preference($app, $name, null, null, $type);
|
2008-03-15 16:11:48 +01:00
|
|
|
}
|
2001-02-05 23:30:21 +01:00
|
|
|
|
2014-07-22 16:57:23 +02:00
|
|
|
/**
|
|
|
|
* Copy preferences from one app to an other
|
|
|
|
*
|
|
|
|
* @param string $from_app
|
|
|
|
* @param string $to_app
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param array $names =null array of names to copy or null for all
|
2014-07-22 16:57:23 +02:00
|
|
|
*/
|
|
|
|
public static function copy_preferences($from_app, $to_app, array $names=null)
|
|
|
|
{
|
2014-07-22 18:52:22 +02:00
|
|
|
//error_log(__METHOD__."('$from_app', '$to_app', ".array2string($names).')');
|
2014-07-22 16:57:23 +02:00
|
|
|
$db = isset($GLOBALS['egw_setup']->db) ? $GLOBALS['egw_setup']->db : $GLOBALS['egw']->db;
|
|
|
|
|
|
|
|
foreach($db->select(self::TABLE, '*', array('preference_app' => $from_app), __LINE__, __FILE__) as $row)
|
|
|
|
{
|
|
|
|
$prefs = self::unserialize($row['preference_value']);
|
|
|
|
|
|
|
|
if ($names)
|
|
|
|
{
|
|
|
|
$prefs = array_intersect_key($prefs, array_flip($names));
|
|
|
|
}
|
|
|
|
if (!$prefs) continue; // nothing to change, as nothing set
|
|
|
|
|
|
|
|
$row['preference_app'] = $to_app;
|
|
|
|
unset($row['preference_value']);
|
|
|
|
|
2014-07-22 18:52:22 +02:00
|
|
|
if (($values = $db->select(self::TABLE, 'preference_value', $row, __LINE__, __FILE__)->fetchColumn()))
|
2014-07-22 16:57:23 +02:00
|
|
|
{
|
2014-07-22 18:52:22 +02:00
|
|
|
$prefs = array_merge(self::unserialize($values), $prefs);
|
2014-07-22 16:57:23 +02:00
|
|
|
}
|
2015-08-20 15:13:49 +02:00
|
|
|
unset($row['preference_id']);
|
2014-07-22 18:52:22 +02:00
|
|
|
//error_log(__LINE__.': '.__METHOD__."() inserting app=$row[preference_app], owner=$row[preference_owner]: ".array2string($prefs));
|
|
|
|
$db->insert(self::TABLE, array(
|
2014-07-22 16:57:23 +02:00
|
|
|
'preference_value' => json_encode($prefs)
|
|
|
|
), $row, __LINE__, __FILE__);
|
|
|
|
|
|
|
|
// update instance-wide cache
|
|
|
|
if (($cached = egw_cache::getInstance(__CLASS__, $row['prefences_owner'])))
|
|
|
|
{
|
|
|
|
$cached[$from_app] = $prefs;
|
|
|
|
egw_cache::setInstance(__CLASS__, $row['preference_owner'], $cached);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
2010-11-16 10:34:47 +01:00
|
|
|
* Save the the preferences to the repository
|
2008-03-15 16:11:48 +01:00
|
|
|
*
|
|
|
|
* User prefs for saveing are in $this->user not in $this->data, which are the effectiv prefs only!
|
|
|
|
*
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param boolean $update_session_info =false old param, seems not to be used (not used anymore)
|
|
|
|
* @param string $type ='user' which prefs to update: user/default/forced
|
|
|
|
* @param boolean $invalid_cache =true should we invalidate the cache, default true (not used anymore)
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return array with new effective prefs (even when forced or default prefs are deleted!)
|
|
|
|
*/
|
|
|
|
function save_repository($update_session_info = False,$type='user',$invalid_cache=true)
|
|
|
|
{
|
2014-06-13 15:15:21 +02:00
|
|
|
unset($update_session_info, $invalid_cache); // no longer used
|
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
switch($type)
|
|
|
|
{
|
|
|
|
case 'forced':
|
2009-10-18 14:45:09 +02:00
|
|
|
$account_id = self::FORCED_ID;
|
2008-03-15 16:11:48 +01:00
|
|
|
$prefs = &$this->forced;
|
|
|
|
break;
|
|
|
|
case 'default':
|
2009-10-18 14:45:09 +02:00
|
|
|
$account_id = self::DEFAULT_ID;
|
2008-03-15 16:11:48 +01:00
|
|
|
$prefs = &$this->default;
|
|
|
|
break;
|
2010-01-27 06:44:31 +01:00
|
|
|
case 'group':
|
2012-10-30 11:22:05 +01:00
|
|
|
throw new egw_exception_wrong_parameter("Can NOT save group preferences, as they are from multiple groups!");
|
2014-06-13 15:15:21 +02:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
default:
|
|
|
|
$account_id = (int)$this->account_id;
|
|
|
|
$prefs = &$this->user; // we use the user-array as data contains default values too
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//echo "<p>preferences::save_repository(,$type): account_id=$account_id, prefs="; print_r($prefs); echo "</p>\n";
|
2001-07-26 13:13:15 +02:00
|
|
|
|
2012-08-07 15:57:56 +02:00
|
|
|
if (isset($GLOBALS['egw_setup']) || !$GLOBALS['egw']->acl->check('session_only_preferences',1,'preferences') &&
|
2015-01-15 21:12:42 +01:00
|
|
|
(!($old_prefs = $this->cache_read($account_id)) || $old_prefs != $prefs))
|
2001-02-08 01:49:34 +01:00
|
|
|
{
|
2014-01-23 13:33:25 +01:00
|
|
|
//error_log(__METHOD__."(type=$type) saved, because old_prefs[$account_id] != prefs=".array2string($prefs));
|
2014-06-13 18:08:42 +02:00
|
|
|
$changed = 0;
|
2008-03-15 16:11:48 +01:00
|
|
|
foreach($prefs as $app => $value)
|
2001-07-26 13:13:15 +02:00
|
|
|
{
|
2014-06-13 18:08:42 +02:00
|
|
|
// check if app preferences have changed, if not no need to save them
|
|
|
|
if ($old_prefs && $old_prefs[$app] == $value) continue;
|
|
|
|
|
|
|
|
if (!$changed++) $this->db->transaction_begin();
|
|
|
|
|
2012-08-07 15:57:56 +02:00
|
|
|
if (!is_array($value) || !$value)
|
2001-05-14 05:54:40 +02:00
|
|
|
{
|
2014-01-23 13:33:25 +01:00
|
|
|
$this->db->delete($this->table, array(
|
|
|
|
'preference_owner' => $account_id,
|
|
|
|
'preference_app' => $app,
|
|
|
|
), __LINE__, __FILE__);
|
|
|
|
unset($prefs[$app]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->db->insert($this->table,array(
|
2014-06-13 15:15:21 +02:00
|
|
|
'preference_value' => json_encode($value),
|
2014-01-23 13:33:25 +01:00
|
|
|
),array(
|
|
|
|
'preference_owner' => $account_id,
|
|
|
|
'preference_app' => $app,
|
|
|
|
),__LINE__,__FILE__);
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-13 18:08:42 +02:00
|
|
|
if ($changed)
|
|
|
|
{
|
|
|
|
$this->db->transaction_commit();
|
2009-10-07 20:59:01 +02:00
|
|
|
|
2014-06-13 18:08:42 +02:00
|
|
|
// update instance-wide cache
|
|
|
|
egw_cache::setInstance(__CLASS__, $account_id, $prefs);
|
|
|
|
}
|
2001-02-08 01:49:34 +01:00
|
|
|
}
|
2014-01-23 13:33:25 +01:00
|
|
|
//else error_log(__METHOD__."(type=$type) NOT saved because old_prefs[$account_id] == prefs=".array2string($prefs));
|
2008-03-15 16:11:48 +01:00
|
|
|
return $this->data;
|
|
|
|
}
|
2001-12-24 05:53:07 +01:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
2014-06-13 15:15:21 +02:00
|
|
|
* @deprecated use add
|
2008-03-15 16:11:48 +01:00
|
|
|
*/
|
|
|
|
function change($app_name,$var,$value = "")
|
|
|
|
{
|
|
|
|
return $this->add($app_name,$var,$value);
|
|
|
|
}
|
2003-12-13 16:03:23 +01:00
|
|
|
|
2008-03-15 16:11:48 +01:00
|
|
|
/**
|
|
|
|
* returns the custom email-address (if set) or generates a default one
|
|
|
|
*
|
2009-10-06 08:45:28 +02:00
|
|
|
* This will generate the appropriate email address used as the "From:"
|
|
|
|
* email address when the user sends email, the localpert * part. The "personal"
|
2008-03-15 16:11:48 +01:00
|
|
|
* part is generated elsewhere.
|
|
|
|
* In the absence of a custom ['email']['address'], this function should be used to set it.
|
|
|
|
*
|
|
|
|
* @access public
|
2015-01-15 21:12:42 +01:00
|
|
|
* @param int $account_id as determined in and/or passed to "create_email_preferences"
|
2008-03-15 16:11:48 +01:00
|
|
|
* @return string with email-address
|
|
|
|
*/
|
|
|
|
function email_address($account_id='')
|
|
|
|
{
|
|
|
|
if (isset($this->data['email']['address']))
|
|
|
|
{
|
|
|
|
return $this->data['email']['address'];
|
|
|
|
}
|
|
|
|
// if email-address is set in the account, return it
|
|
|
|
if (($email = $GLOBALS['egw']->accounts->id2name($account_id,'account_email')))
|
|
|
|
{
|
|
|
|
return $email;
|
|
|
|
}
|
|
|
|
$prefs_email_address = $GLOBALS['egw']->accounts->id2name($account_id);
|
|
|
|
if (strpos($prefs_email_address,'@') === False)
|
|
|
|
{
|
|
|
|
$prefs_email_address .= '@' . $GLOBALS['egw_info']['server']['mail_suffix'];
|
|
|
|
}
|
|
|
|
return $prefs_email_address;
|
|
|
|
}
|
|
|
|
}
|