4 * @file include/PConfig.php
5 * @brief contains the class with methods for the management
6 * of the user configuration
10 * @brief Management of user configuration
15 * @brief Loads all configuration values of a user's config family into a cached storage.
17 * All configuration values of the given user are stored in global cache
18 * which is available under the global variable $a->config[$uid].
22 * @param string $family
23 * The category of the configuration value
26 public static function load($uid,$family) {
28 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
35 $a->config[$uid][$family][$k] = $rr['v'];
37 } else if ($family != 'config') {
39 $a->config[$uid][$family] = "!<unset>!";
44 * @brief Get a particular user's config variable given the category name
45 * ($family) and a key.
47 * Get a particular user's config value from the given category ($family)
48 * and the $key from a cached storage in $a->config[$uid].
52 * @param string $family
53 * The category of the configuration value
55 * The configuration key to query
56 * @param boolean $instore
57 * Determines if the key already exists in the DB
58 * @return mixed Stored value or false if it does not exist
60 public static function get($uid,$family, $key, $instore = false) {
65 // Looking if the whole family isn't set
66 if(isset($a->config[$uid][$family])) {
67 if($a->config[$uid][$family] === '!<unset>!') {
72 if(isset($a->config[$uid][$family][$key])) {
73 if($a->config[$uid][$family][$key] === '!<unset>!') {
76 return $a->config[$uid][$family][$key];
80 // If APC is enabled then fetch the data from there, else try XCache
81 /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
82 if (apc_exists($uid."|".$family."|".$key)) {
83 $val = apc_fetch($uid."|".$family."|".$key);
84 $a->config[$uid][$family][$key] = $val;
86 if ($val === '!<unset>!')
91 elseif (function_exists("xcache_get") AND function_exists("xcache_isset"))
92 if (xcache_isset($uid."|".$family."|".$key)) {
93 $val = xcache_get($uid."|".$family."|".$key);
94 $a->config[$uid][$family][$key] = $val;
96 if ($val === '!<unset>!')
103 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
110 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
111 $a->config[$uid][$family][$key] = $val;
113 // If APC is enabled then store the data there, else try XCache
114 /*if (function_exists("apc_store"))
115 apc_store($uid."|".$family."|".$key, $val, 600);
116 elseif (function_exists("xcache_set"))
117 xcache_set($uid."|".$family."|".$key, $val, 600);*/
122 $a->config[$uid][$family][$key] = '!<unset>!';
124 // If APC is enabled then store the data there, else try XCache
125 /*if (function_exists("apc_store"))
126 apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
127 elseif (function_exists("xcache_set"))
128 xcache_set($uid."|".$family."|".$key, '!<unset>!', 600);*/
134 * @brief Sets a configuration value for a user
136 * Stores a config value ($value) in the category ($family) under the key ($key)
137 * for the user_id $uid.
139 * @note Please do not store booleans - convert to 0/1 integer values!
143 * @param string $family
144 * The category of the configuration value
146 * The configuration key to set
147 * @param string $value
149 * @return mixed Stored $value or false
151 public static function set($uid,$family,$key,$value) {
155 // manage array value
156 $dbvalue = (is_array($value)?serialize($value):$value);
158 if(get_pconfig($uid,$family,$key,true) === false) {
159 $a->config[$uid][$family][$key] = $value;
160 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
170 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
177 $a->config[$uid][$family][$key] = $value;
179 // If APC is enabled then store the data there, else try XCache
180 /*if (function_exists("apc_store"))
181 apc_store($uid."|".$family."|".$key, $value, 600);
182 elseif (function_exists("xcache_set"))
183 xcache_set($uid."|".$family."|".$key, $value, 600);*/
192 * @brief Deletes the given key from the users's configuration.
194 * Removes the configured value from the stored cache in $a->config[$uid]
195 * and removes it from the database.
197 * @param string $uid The user_id
198 * @param string $family
199 * The category of the configuration value
201 * The configuration key to delete
204 public static function delete($uid,$family,$key) {
207 if(x($a->config[$uid][$family],$key))
208 unset($a->config[$uid][$family][$key]);
209 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",