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>!')
82 //logger("APC: cache miss for value ".$family."|".$key, LOGGER_DEBUG);
85 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
91 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
92 $a->config[$family][$key] = $val;
94 // If APC is enabled then store the data there
95 if (function_exists("apc_store"))
96 apc_store($family."|".$key, $val, 600);
101 $a->config[$family][$key] = '!<unset>!';
103 // If APC is enabled then store the data there
104 if (function_exists("apc_store"))
105 apc_store($family."|".$key, '!<unset>!', 600);
110 // Store a config value ($value) in the category ($family)
111 // under the key ($key)
112 // Return the value, or false if the database update failed
114 if(! function_exists('set_config')) {
115 function set_config($family,$key,$value) {
118 // If $a->config[$family] has been previously set to '!<unset>!', then
119 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
120 // $a->config[$family][$key] = $value will be equivalent to
121 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
122 // so unset the family before assigning a value to a family's key
123 if($a->config[$family] === '!<unset>!')
124 unset($a->config[$family]);
126 // manage array value
127 $dbvalue = (is_array($value)?serialize($value):$value);
128 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
129 if(get_config($family,$key,true) === false) {
130 $a->config[$family][$key] = $value;
131 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
141 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
147 $a->config[$family][$key] = $value;
149 // If APC is enabled then store the data there
150 if (function_exists("apc_store"))
151 apc_store($family."|".$key, $value, 600);
159 if(! function_exists('load_pconfig')) {
160 function load_pconfig($uid,$family) {
162 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
169 $a->config[$uid][$family][$k] = $rr['v'];
171 } else if ($family != 'config') {
173 $a->config[$uid][$family] = "!<unset>!";
179 if(! function_exists('get_pconfig')) {
180 function get_pconfig($uid,$family, $key, $instore = false) {
185 // Looking if the whole family isn't set
186 if(isset($a->config[$uid][$family])) {
187 if($a->config[$uid][$family] === '!<unset>!') {
192 if(isset($a->config[$uid][$family][$key])) {
193 if($a->config[$uid][$family][$key] === '!<unset>!') {
196 return $a->config[$uid][$family][$key];
200 // If APC is enabled then fetch the data from there
201 if (function_exists("apc_fetch") AND function_exists("apc_exists"))
202 if (apc_exists($uid."|".$family."|".$key)) {
203 $val = apc_fetch($uid."|".$family."|".$key);
204 //logger("APC: fetched stored value ".$uid."|".$family."|".$key, LOGGER_DEBUG);
205 $a->config[$uid][$family][$key] = $val;
207 if ($val === '!<unset>!')
212 //logger("APC: cache miss for value ".$family."|".$key, LOGGER_DEBUG);
215 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
222 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
223 $a->config[$uid][$family][$key] = $val;
225 // If APC is enabled then store the data there
226 if (function_exists("apc_store"))
227 apc_store($uid."|".$family."|".$key, $val, 600);
232 $a->config[$uid][$family][$key] = '!<unset>!';
234 // If APC is enabled then store the data there
235 if (function_exists("apc_store"))
236 apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
241 if(! function_exists('del_config')) {
242 function del_config($family,$key) {
245 if(x($a->config[$family],$key))
246 unset($a->config[$family][$key]);
247 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
251 // If APC is enabled then store the data there
252 if (function_exists("apc_delete"))
253 apc_delete($family."|".$key);
260 // Same as above functions except these are for personal config storage and take an
261 // additional $uid argument.
263 if(! function_exists('set_pconfig')) {
264 function set_pconfig($uid,$family,$key,$value) {
268 // manage array value
269 $dbvalue = (is_array($value)?serialize($value):$value);
271 if(get_pconfig($uid,$family,$key,true) === false) {
272 $a->config[$uid][$family][$key] = $value;
273 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
283 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
290 $a->config[$uid][$family][$key] = $value;
292 // If APC is enabled then store the data there
293 if (function_exists("apc_store"))
294 apc_store($uid."|".$family."|".$key, $value, 600);
302 if(! function_exists('del_pconfig')) {
303 function del_pconfig($uid,$family,$key) {
306 if(x($a->config[$uid][$family],$key))
307 unset($a->config[$uid][$family][$key]);
308 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",