2 namespace Friendica\Core;
4 * @file include/Core/PConfig.php
5 * @brief contains the class with methods for the management
6 * of the user configuration
10 * @brief Management of user configuration storage
12 * Please do not store booleans - convert to 0/1 integer values
13 * The PConfig::get() functions return boolean false for keys that are unset,
14 * and this could lead to subtle bugs.
19 * @brief Loads all configuration values of a user's config family into a cached storage.
21 * All configuration values of the given user are stored in global cache
22 * which is available under the global variable $a->config[$uid].
26 * @param string $family
27 * The category of the configuration value
30 public static function load($uid,$family) {
32 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
39 $a->config[$uid][$family][$k] = $rr['v'];
41 } else if ($family != 'config') {
43 $a->config[$uid][$family] = "!<unset>!";
48 * @brief Get a particular user's config variable given the category name
49 * ($family) and a key.
51 * Get a particular user's config value from the given category ($family)
52 * and the $key from a cached storage in $a->config[$uid].
56 * @param string $family
57 * The category of the configuration value
59 * The configuration key to query
60 * @param mixed $default_value optional
61 * The value to return if key is not set (default: null)
62 * @param boolean $refresh optional
63 * If true the config is loaded from the db and not from the cache (default: false)
64 * @return mixed Stored value or null if it does not exist
66 public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
71 // Looking if the whole family isn't set
72 if(isset($a->config[$uid][$family])) {
73 if($a->config[$uid][$family] === '!<unset>!') {
74 return $default_value;
78 if(isset($a->config[$uid][$family][$key])) {
79 if($a->config[$uid][$family][$key] === '!<unset>!') {
80 return $default_value;
82 return $a->config[$uid][$family][$key];
86 // If APC is enabled then fetch the data from there, else try XCache
87 /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
88 if (apc_exists($uid."|".$family."|".$key)) {
89 $val = apc_fetch($uid."|".$family."|".$key);
90 $a->config[$uid][$family][$key] = $val;
92 if ($val === '!<unset>!')
97 elseif (function_exists("xcache_get") AND function_exists("xcache_isset"))
98 if (xcache_isset($uid."|".$family."|".$key)) {
99 $val = xcache_get($uid."|".$family."|".$key);
100 $a->config[$uid][$family][$key] = $val;
102 if ($val === '!<unset>!')
109 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
116 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
117 $a->config[$uid][$family][$key] = $val;
119 // If APC is enabled then store the data there, else try XCache
120 /*if (function_exists("apc_store"))
121 apc_store($uid."|".$family."|".$key, $val, 600);
122 elseif (function_exists("xcache_set"))
123 xcache_set($uid."|".$family."|".$key, $val, 600);*/
128 $a->config[$uid][$family][$key] = '!<unset>!';
130 // If APC is enabled then store the data there, else try XCache
131 /*if (function_exists("apc_store"))
132 apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
133 elseif (function_exists("xcache_set"))
134 xcache_set($uid."|".$family."|".$key, '!<unset>!', 600);*/
136 return $default_value;
140 * @brief Sets a configuration value for a user
142 * Stores a config value ($value) in the category ($family) under the key ($key)
143 * for the user_id $uid.
145 * @note Please do not store booleans - convert to 0/1 integer values!
149 * @param string $family
150 * The category of the configuration value
152 * The configuration key to set
153 * @param string $value
155 * @return mixed Stored $value or false
157 public static function set($uid,$family,$key,$value) {
161 // manage array value
162 $dbvalue = (is_array($value)?serialize($value):$value);
164 if(is_null(self::get($uid,$family,$key,null, true))) {
165 $a->config[$uid][$family][$key] = $value;
166 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
176 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
183 $a->config[$uid][$family][$key] = $value;
185 // If APC is enabled then store the data there, else try XCache
186 /*if (function_exists("apc_store"))
187 apc_store($uid."|".$family."|".$key, $value, 600);
188 elseif (function_exists("xcache_set"))
189 xcache_set($uid."|".$family."|".$key, $value, 600);*/
198 * @brief Deletes the given key from the users's configuration.
200 * Removes the configured value from the stored cache in $a->config[$uid]
201 * and removes it from the database.
203 * @param string $uid The user_id
204 * @param string $family
205 * The category of the configuration value
207 * The configuration key to delete
210 public static function delete($uid,$family,$key) {
213 if(x($a->config[$uid][$family],$key))
214 unset($a->config[$uid][$family][$key]);
215 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",