5 * Arbitrary configuration storage
7 * Please do not store booleans - convert to 0/1 integer values
8 * The get_?config() functions return boolean false for keys that are unset,
9 * and this could lead to subtle bugs.
11 * There are a few places in the code (such as the admin panel) where boolean
12 * configurations need to be fixed as of 10/08/2011.
16 // retrieve a "family" of config variables from database to cached storage
18 if(! function_exists('load_config')) {
19 function load_config($family) {
22 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
26 if ($family === 'config') {
27 $a->config[$k] = $rr['v'];
29 $a->config[$family][$k] = $rr['v'];
32 } else if ($family != 'config') {
34 $a->config[$family] = "!<unset>!";
38 // get a particular config variable given the family name
39 // and key. Returns false if not set.
40 // $instore is only used by the set_config function
41 // to determine if the key already exists in the DB
42 // If a key is found in the DB but doesn't exist in
43 // local config cache, pull it into the cache so we don't have
44 // to hit the DB again for this item.
46 if(! function_exists('get_config')) {
47 function get_config($family, $key, $instore = false) {
52 // Looking if the whole family isn't set
53 if(isset($a->config[$family])) {
54 if($a->config[$family] === '!<unset>!') {
59 if(isset($a->config[$family][$key])) {
60 if($a->config[$family][$key] === '!<unset>!') {
63 return $a->config[$family][$key];
67 // If APC is enabled then fetch the data from there, else try XCache
68 /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
69 if (apc_exists($family."|".$key)) {
70 $val = apc_fetch($family."|".$key);
71 $a->config[$family][$key] = $val;
73 if ($val === '!<unset>!')
78 elseif (function_exists("xcache_fetch") AND function_exists("xcache_isset"))
79 if (xcache_isset($family."|".$key)) {
80 $val = xcache_fetch($family."|".$key);
81 $a->config[$family][$key] = $val;
83 if ($val === '!<unset>!')
90 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
96 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
97 $a->config[$family][$key] = $val;
99 // If APC is enabled then store the data there, else try XCache
100 /*if (function_exists("apc_store"))
101 apc_store($family."|".$key, $val, 600);
102 elseif (function_exists("xcache_set"))
103 xcache_set($family."|".$key, $val, 600);*/
108 $a->config[$family][$key] = '!<unset>!';
110 // If APC is enabled then store the data there, else try XCache
111 /*if (function_exists("apc_store"))
112 apc_store($family."|".$key, '!<unset>!', 600);
113 elseif (function_exists("xcache_set"))
114 xcache_set($family."|".$key, '!<unset>!', 600);*/
119 // Store a config value ($value) in the category ($family)
120 // under the key ($key)
121 // Return the value, or false if the database update failed
123 if(! function_exists('set_config')) {
124 function set_config($family,$key,$value) {
127 // If $a->config[$family] has been previously set to '!<unset>!', then
128 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
129 // $a->config[$family][$key] = $value will be equivalent to
130 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
131 // so unset the family before assigning a value to a family's key
132 if($a->config[$family] === '!<unset>!')
133 unset($a->config[$family]);
135 // manage array value
136 $dbvalue = (is_array($value)?serialize($value):$value);
137 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
138 if(get_config($family,$key,true) === false) {
139 $a->config[$family][$key] = $value;
140 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
150 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
156 $a->config[$family][$key] = $value;
158 // If APC is enabled then store the data there, else try XCache
159 /*if (function_exists("apc_store"))
160 apc_store($family."|".$key, $value, 600);
161 elseif (function_exists("xcache_set"))
162 xcache_set($family."|".$key, $value, 600);*/
170 if(! function_exists('load_pconfig')) {
171 function load_pconfig($uid,$family) {
173 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
180 $a->config[$uid][$family][$k] = $rr['v'];
182 } else if ($family != 'config') {
184 $a->config[$uid][$family] = "!<unset>!";
190 if(! function_exists('get_pconfig')) {
191 function get_pconfig($uid,$family, $key, $instore = false) {
196 // Looking if the whole family isn't set
197 if(isset($a->config[$uid][$family])) {
198 if($a->config[$uid][$family] === '!<unset>!') {
203 if(isset($a->config[$uid][$family][$key])) {
204 if($a->config[$uid][$family][$key] === '!<unset>!') {
207 return $a->config[$uid][$family][$key];
211 // If APC is enabled then fetch the data from there, else try XCache
212 /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
213 if (apc_exists($uid."|".$family."|".$key)) {
214 $val = apc_fetch($uid."|".$family."|".$key);
215 $a->config[$uid][$family][$key] = $val;
217 if ($val === '!<unset>!')
222 elseif (function_exists("xcache_get") AND function_exists("xcache_isset"))
223 if (xcache_isset($uid."|".$family."|".$key)) {
224 $val = xcache_get($uid."|".$family."|".$key);
225 $a->config[$uid][$family][$key] = $val;
227 if ($val === '!<unset>!')
234 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
241 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
242 $a->config[$uid][$family][$key] = $val;
244 // If APC is enabled then store the data there, else try XCache
245 /*if (function_exists("apc_store"))
246 apc_store($uid."|".$family."|".$key, $val, 600);
247 elseif (function_exists("xcache_set"))
248 xcache_set($uid."|".$family."|".$key, $val, 600);*/
253 $a->config[$uid][$family][$key] = '!<unset>!';
255 // If APC is enabled then store the data there, else try XCache
256 /*if (function_exists("apc_store"))
257 apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
258 elseif (function_exists("xcache_set"))
259 xcache_set($uid."|".$family."|".$key, '!<unset>!', 600);*/
264 if(! function_exists('del_config')) {
265 function del_config($family,$key) {
268 if(x($a->config[$family],$key))
269 unset($a->config[$family][$key]);
270 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
274 // If APC is enabled then delete the data from there, else try XCache
275 /*if (function_exists("apc_delete"))
276 apc_delete($family."|".$key);
277 elseif (function_exists("xcache_unset"))
278 xcache_unset($family."|".$key);*/
285 // Same as above functions except these are for personal config storage and take an
286 // additional $uid argument.
288 if(! function_exists('set_pconfig')) {
289 function set_pconfig($uid,$family,$key,$value) {
293 // manage array value
294 $dbvalue = (is_array($value)?serialize($value):$value);
296 if(get_pconfig($uid,$family,$key,true) === false) {
297 $a->config[$uid][$family][$key] = $value;
298 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
308 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
315 $a->config[$uid][$family][$key] = $value;
317 // If APC is enabled then store the data there, else try XCache
318 /*if (function_exists("apc_store"))
319 apc_store($uid."|".$family."|".$key, $value, 600);
320 elseif (function_exists("xcache_set"))
321 xcache_set($uid."|".$family."|".$key, $value, 600);*/
329 if(! function_exists('del_pconfig')) {
330 function del_pconfig($uid,$family,$key) {
333 if(x($a->config[$uid][$family],$key))
334 unset($a->config[$uid][$family][$key]);
335 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",