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 // To-Do: How to integrate APC here?
24 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'", dbesc($family));
28 if ($family === 'config') {
29 $a->config[$k] = $rr['v'];
31 $a->config[$family][$k] = $rr['v'];
34 } else if ($family != 'config') {
36 $a->config[$family] = "!<unset>!";
40 // get a particular config variable given the family name
41 // and key. Returns false if not set.
42 // $instore is only used by the set_config function
43 // to determine if the key already exists in the DB
44 // If a key is found in the DB but doesn't exist in
45 // local config cache, pull it into the cache so we don't have
46 // to hit the DB again for this item.
48 if(! function_exists('get_config')) {
49 function get_config($family, $key, $instore = false) {
54 // Looking if the whole family isn't set
55 if(isset($a->config[$family])) {
56 if($a->config[$family] === '!<unset>!') {
61 if(isset($a->config[$family][$key])) {
62 if($a->config[$family][$key] === '!<unset>!') {
65 return $a->config[$family][$key];
69 // If APC is enabled then fetch the data from there
70 if (function_exists("apc_fetch") AND function_exists("apc_exists"))
71 if (apc_exists($family."|".$key)) {
72 $val = apc_fetch($family."|".$key);
73 $a->config[$family][$key] = $val;
75 if ($val === '!<unset>!')
81 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
87 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
88 $a->config[$family][$key] = $val;
90 // If APC is enabled then store the data there
91 if (function_exists("apc_store"))
92 apc_store($family."|".$key, $val, 600);
97 $a->config[$family][$key] = '!<unset>!';
99 // If APC is enabled then store the data there
100 if (function_exists("apc_store"))
101 apc_store($family."|".$key, '!<unset>!', 600);
106 // Store a config value ($value) in the category ($family)
107 // under the key ($key)
108 // Return the value, or false if the database update failed
110 if(! function_exists('set_config')) {
111 function set_config($family,$key,$value) {
114 // If $a->config[$family] has been previously set to '!<unset>!', then
115 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
116 // $a->config[$family][$key] = $value will be equivalent to
117 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
118 // so unset the family before assigning a value to a family's key
119 if($a->config[$family] === '!<unset>!')
120 unset($a->config[$family]);
122 // manage array value
123 $dbvalue = (is_array($value)?serialize($value):$value);
124 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
125 if(get_config($family,$key,true) === false) {
126 $a->config[$family][$key] = $value;
127 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
137 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
143 $a->config[$family][$key] = $value;
145 // If APC is enabled then store the data there
146 if (function_exists("apc_store"))
147 apc_store($family."|".$key, $value, 600);
155 if(! function_exists('load_pconfig')) {
156 function load_pconfig($uid,$family) {
158 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
165 $a->config[$uid][$family][$k] = $rr['v'];
167 } else if ($family != 'config') {
169 $a->config[$uid][$family] = "!<unset>!";
175 if(! function_exists('get_pconfig')) {
176 function get_pconfig($uid,$family, $key, $instore = false) {
181 // Looking if the whole family isn't set
182 if(isset($a->config[$uid][$family])) {
183 if($a->config[$uid][$family] === '!<unset>!') {
188 if(isset($a->config[$uid][$family][$key])) {
189 if($a->config[$uid][$family][$key] === '!<unset>!') {
192 return $a->config[$uid][$family][$key];
196 // If APC is enabled then fetch the data from there
197 if (function_exists("apc_fetch") AND function_exists("apc_exists"))
198 if (apc_exists($uid."|".$family."|".$key)) {
199 $val = apc_fetch($uid."|".$family."|".$key);
200 $a->config[$uid][$family][$key] = $val;
202 if ($val === '!<unset>!')
209 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
216 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
217 $a->config[$uid][$family][$key] = $val;
219 // If APC is enabled then store the data there
220 if (function_exists("apc_store"))
221 apc_store($uid."|".$family."|".$key, $val, 600);
226 $a->config[$uid][$family][$key] = '!<unset>!';
228 // If APC is enabled then store the data there
229 if (function_exists("apc_store"))
230 apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
235 if(! function_exists('del_config')) {
236 function del_config($family,$key) {
239 if(x($a->config[$family],$key))
240 unset($a->config[$family][$key]);
241 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
245 // If APC is enabled then store the data there
246 if (function_exists("apc_delete"))
247 apc_delete($family."|".$key);
254 // Same as above functions except these are for personal config storage and take an
255 // additional $uid argument.
257 if(! function_exists('set_pconfig')) {
258 function set_pconfig($uid,$family,$key,$value) {
262 // manage array value
263 $dbvalue = (is_array($value)?serialize($value):$value);
265 if(get_pconfig($uid,$family,$key,true) === false) {
266 $a->config[$uid][$family][$key] = $value;
267 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
277 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
284 $a->config[$uid][$family][$key] = $value;
286 // If APC is enabled then store the data there
287 if (function_exists("apc_store"))
288 apc_store($uid."|".$family."|".$key, $value, 600);
296 if(! function_exists('del_pconfig')) {
297 function del_pconfig($uid,$family,$key) {
300 if(x($a->config[$uid][$family],$key))
301 unset($a->config[$uid][$family][$key]);
302 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",