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 //logger("APC: fetched stored value ".$family."|".$key, LOGGER_DEBUG);
74 $a->config[$family][$key] = $val;
76 if ($val === '!<unset>!')
81 //logger("APC: cache miss for value ".$family."|".$key, LOGGER_DEBUG);
84 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
90 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
91 $a->config[$family][$key] = $val;
93 // If APC is enabled then store the data there
94 if (function_exists("apc_store"))
95 apc_store($family."|".$key, $val, 600);
100 $a->config[$family][$key] = '!<unset>!';
102 // If APC is enabled then store the data there
103 if (function_exists("apc_store"))
104 apc_store($family."|".$key, '!<unset>!', 600);
109 // Store a config value ($value) in the category ($family)
110 // under the key ($key)
111 // Return the value, or false if the database update failed
113 if(! function_exists('set_config')) {
114 function set_config($family,$key,$value) {
117 // If $a->config[$family] has been previously set to '!<unset>!', then
118 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
119 // $a->config[$family][$key] = $value will be equivalent to
120 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
121 // so unset the family before assigning a value to a family's key
122 if($a->config[$family] === '!<unset>!')
123 unset($a->config[$family]);
125 // manage array value
126 $dbvalue = (is_array($value)?serialize($value):$value);
127 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
128 if(get_config($family,$key,true) === false) {
129 $a->config[$family][$key] = $value;
130 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
140 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
146 $a->config[$family][$key] = $value;
148 // If APC is enabled then store the data there
149 if (function_exists("apc_store"))
150 apc_store($family."|".$key, $value, 600);
158 if(! function_exists('load_pconfig')) {
159 function load_pconfig($uid,$family) {
161 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
168 $a->config[$uid][$family][$k] = $rr['v'];
170 } else if ($family != 'config') {
172 $a->config[$uid][$family] = "!<unset>!";
178 if(! function_exists('get_pconfig')) {
179 function get_pconfig($uid,$family, $key, $instore = false) {
184 // Looking if the whole family isn't set
185 if(isset($a->config[$uid][$family])) {
186 if($a->config[$uid][$family] === '!<unset>!') {
191 if(isset($a->config[$uid][$family][$key])) {
192 if($a->config[$uid][$family][$key] === '!<unset>!') {
195 return $a->config[$uid][$family][$key];
199 // If APC is enabled then fetch the data from there
200 if (function_exists("apc_fetch") AND function_exists("apc_exists"))
201 if (apc_exists($uid."|".$family."|".$key)) {
202 $val = apc_fetch($uid."|".$family."|".$key);
203 //logger("APC: fetched stored value ".$uid."|".$family."|".$key, LOGGER_DEBUG);
204 $a->config[$uid][$family][$key] = $val;
206 if ($val === '!<unset>!')
211 //logger("APC: cache miss for value ".$family."|".$key, LOGGER_DEBUG);
214 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
221 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
222 $a->config[$uid][$family][$key] = $val;
224 // If APC is enabled then store the data there
225 if (function_exists("apc_store"))
226 apc_store($uid."|".$family."|".$key, $val, 600);
231 $a->config[$uid][$family][$key] = '!<unset>!';
233 // If APC is enabled then store the data there
234 if (function_exists("apc_store"))
235 apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
240 if(! function_exists('del_config')) {
241 function del_config($family,$key) {
244 if(x($a->config[$family],$key))
245 unset($a->config[$family][$key]);
246 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
250 // If APC is enabled then store the data there
251 if (function_exists("apc_delete"))
252 apc_delete($family."|".$key);
259 // Same as above functions except these are for personal config storage and take an
260 // additional $uid argument.
262 if(! function_exists('set_pconfig')) {
263 function set_pconfig($uid,$family,$key,$value) {
267 // manage array value
268 $dbvalue = (is_array($value)?serialize($value):$value);
270 if(get_pconfig($uid,$family,$key,true) === false) {
271 $a->config[$uid][$family][$key] = $value;
272 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
282 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
289 $a->config[$uid][$family][$key] = $value;
291 // If APC is enabled then store the data there
292 if (function_exists("apc_store"))
293 apc_store($uid."|".$family."|".$key, $value, 600);
301 if(! function_exists('del_pconfig')) {
302 function del_pconfig($uid,$family,$key) {
305 if(x($a->config[$uid][$family],$key))
306 unset($a->config[$uid][$family][$key]);
307 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",