3 require_once("include/PConfig.php");
6 * @file include/config.php
8 * @brief Arbitrary configuration storage
10 * Please do not store booleans - convert to 0/1 integer values
11 * The get_?config() functions return boolean false for keys that are unset,
12 * and this could lead to subtle bugs.
14 * There are a few places in the code (such as the admin panel) where boolean
15 * configurations need to be fixed as of 10/08/2011.
19 * @brief Loads all configuration values of family into a cached storage.
21 * All configuration values of the system are stored in global cache
22 * which is available under the global variable $a->config
24 * @param string $family
25 * The category of the configuration value
28 function load_config($family) {
31 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
35 if ($family === 'config') {
36 $a->config[$k] = $rr['v'];
38 $a->config[$family][$k] = $rr['v'];
41 } else if ($family != 'config') {
43 $a->config[$family] = "!<unset>!";
48 * @brief Get a particular user's config variable given the category name
49 * ($family) and a key.
51 * Get a particular config value from the given category ($family)
52 * and the $key from a cached storage in $a->config[$uid].
53 * $instore is only used by the set_config function
54 * to determine if the key already exists in the DB
55 * If a key is found in the DB but doesn't exist in
56 * local config cache, pull it into the cache so we don't have
57 * to hit the DB again for this item.
59 * @param string $family
60 * The category of the configuration value
62 * The configuration key to query
63 * @param boolean $instore Determines if the key already exists in the DB
64 * @return mixed Stored value or false if it does not exist
66 function get_config($family, $key, $instore = false) {
71 // Looking if the whole family isn't set
72 if(isset($a->config[$family])) {
73 if($a->config[$family] === '!<unset>!') {
78 if(isset($a->config[$family][$key])) {
79 if($a->config[$family][$key] === '!<unset>!') {
82 return $a->config[$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($family."|".$key)) {
89 $val = apc_fetch($family."|".$key);
90 $a->config[$family][$key] = $val;
92 if ($val === '!<unset>!')
97 elseif (function_exists("xcache_fetch") AND function_exists("xcache_isset"))
98 if (xcache_isset($family."|".$key)) {
99 $val = xcache_fetch($family."|".$key);
100 $a->config[$family][$key] = $val;
102 if ($val === '!<unset>!')
109 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
114 // manage array value
115 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
116 $a->config[$family][$key] = $val;
118 // If APC is enabled then store the data there, else try XCache
119 /*if (function_exists("apc_store"))
120 apc_store($family."|".$key, $val, 600);
121 elseif (function_exists("xcache_set"))
122 xcache_set($family."|".$key, $val, 600);*/
127 $a->config[$family][$key] = '!<unset>!';
129 // If APC is enabled then store the data there, else try XCache
130 /*if (function_exists("apc_store"))
131 apc_store($family."|".$key, '!<unset>!', 600);
132 elseif (function_exists("xcache_set"))
133 xcache_set($family."|".$key, '!<unset>!', 600);*/
139 * @brief Sets a configuration value for system config
141 * Stores a config value ($value) in the category ($family) under the key ($key)
142 * for the user_id $uid.
144 * Note: Please do not store booleans - convert to 0/1 integer values!
146 * @param string $family
147 * The category of the configuration value
149 * The configuration key to set
150 * @param string $value
152 * @return mixed Stored $value or false if the database update failed
154 function set_config($family,$key,$value) {
157 // If $a->config[$family] has been previously set to '!<unset>!', then
158 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
159 // $a->config[$family][$key] = $value will be equivalent to
160 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
161 // so unset the family before assigning a value to a family's key
162 if($a->config[$family] === '!<unset>!')
163 unset($a->config[$family]);
165 // manage array value
166 $dbvalue = (is_array($value)?serialize($value):$value);
167 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
168 if(get_config($family,$key,true) === false) {
169 $a->config[$family][$key] = $value;
170 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
180 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
186 $a->config[$family][$key] = $value;
188 // If APC is enabled then store the data there, else try XCache
189 /*if (function_exists("apc_store"))
190 apc_store($family."|".$key, $value, 600);
191 elseif (function_exists("xcache_set"))
192 xcache_set($family."|".$key, $value, 600);*/
200 * @brief Deletes the given key from the system configuration.
202 * Removes the configured value from the stored cache in $a->config
203 * and removes it from the database.
205 * @param string $family
206 * The category of the configuration value
208 * The configuration key to delete
211 function del_config($family,$key) {
214 if(x($a->config[$family],$key))
215 unset($a->config[$family][$key]);
216 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
220 // If APC is enabled then delete the data from there, else try XCache
221 /*if (function_exists("apc_delete"))
222 apc_delete($family."|".$key);
223 elseif (function_exists("xcache_unset"))
224 xcache_unset($family."|".$key);*/
230 * @brief (Deprecated) Loads all configuration values of a user's config family into a cached storage.
232 * Note: This function is deprecated. Use PConfig::load() instead.
236 * @param string $family
237 * The category of the configuration value
240 function load_pconfig($uid,$family) {
241 return PConfig::load($uid, $family);
245 * @brief (Deprecated) Get a particular user's config variable given the category name
246 * ($family) and a key.
248 * Note: This function is deprecated. Use PConfig::get() instead.
252 * @param string $family
253 * The category of the configuration value
255 * The configuration key to query
256 * @param boolean $instore
257 * Determines if the key already exists in the DB
258 * @return mixed Stored value or false if it does not exist
260 function get_pconfig($uid,$family, $key, $instore = false) {
261 return PConfig::get($uid, $family, $key, $instore);
265 * @brief (Deprecated) Sets a configuration value for a user
267 * Note: This function is deprecated. Use PConfig::set() instead.
271 * @param string $family
272 * The category of the configuration value
274 * The configuration key to set
275 * @param string $value
277 * @return mixed Stored $value or false
279 function set_pconfig($uid,$family,$key,$value) {
280 return PConfig::set($uid, $family, $key, $value);
284 * @brief (Deprecated) Deletes the given key from the users's configuration.
286 * Note: This function is deprecated. Use PConfig::delete() instead.
288 * @param string $uid The user_id
289 * @param string $family
290 * The category of the configuration value
292 * The configuration key to delete
295 function del_pconfig($uid,$family,$key) {
296 return PConfig::delete($uid, $family, $key);