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) {
21 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'", dbesc($family));
25 if ($family === 'config') {
26 $a->config[$k] = $rr['v'];
28 $a->config[$family][$k] = $rr['v'];
31 } else if ($family != 'config') {
33 $a->config[$family] = "!<unset>!";
37 // get a particular config variable given the family name
38 // and key. Returns false if not set.
39 // $instore is only used by the set_config function
40 // to determine if the key already exists in the DB
41 // If a key is found in the DB but doesn't exist in
42 // local config cache, pull it into the cache so we don't have
43 // to hit the DB again for this item.
45 if(! function_exists('get_config')) {
46 function get_config($family, $key, $instore = false) {
51 // Looking if the whole family isn't set
52 if(isset($a->config[$family])) {
53 if($a->config[$family] === '!<unset>!') {
58 if(isset($a->config[$family][$key])) {
59 if($a->config[$family][$key] === '!<unset>!') {
62 return $a->config[$family][$key];
66 // If APC is enabled then fetch the data from there
67 if (function_exists("apc_fetch") AND function_exists("apc_exists"))
68 if (apc_exists($family."|".$key)) {
69 $val = apc_fetch($family."|".$key);
70 $a->config[$family][$key] = $val;
74 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
80 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
81 $a->config[$family][$key] = $val;
85 $a->config[$family][$key] = '!<unset>!';
90 // Store a config value ($value) in the category ($family)
91 // under the key ($key)
92 // Return the value, or false if the database update failed
94 if(! function_exists('set_config')) {
95 function set_config($family,$key,$value) {
98 // If $a->config[$family] has been previously set to '!<unset>!', then
99 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
100 // $a->config[$family][$key] = $value will be equivalent to
101 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
102 // so unset the family before assigning a value to a family's key
103 if($a->config[$family] === '!<unset>!')
104 unset($a->config[$family]);
106 // manage array value
107 $dbvalue = (is_array($value)?serialize($value):$value);
108 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
109 if(get_config($family,$key,true) === false) {
110 $a->config[$family][$key] = $value;
111 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
121 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
127 $a->config[$family][$key] = $value;
129 // If APC is enabled then store the data there
130 if (function_exists("apc_store"))
131 apc_store($family."|".$key, $value, 600);
139 if(! function_exists('load_pconfig')) {
140 function load_pconfig($uid,$family) {
142 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
149 $a->config[$uid][$family][$k] = $rr['v'];
151 } else if ($family != 'config') {
153 $a->config[$uid][$family] = "!<unset>!";
159 if(! function_exists('get_pconfig')) {
160 function get_pconfig($uid,$family, $key, $instore = false) {
165 // Looking if the whole family isn't set
166 if(isset($a->config[$uid][$family])) {
167 if($a->config[$uid][$family] === '!<unset>!') {
172 if(isset($a->config[$uid][$family][$key])) {
173 if($a->config[$uid][$family][$key] === '!<unset>!') {
176 return $a->config[$uid][$family][$key];
180 // If APC is enabled then fetch the data from there
181 if (function_exists("apc_fetch") AND function_exists("apc_exists"))
182 if (apc_exists($uid."|".$family."|".$key)) {
183 $val = apc_fetch($uid."|".$family."|".$key);
184 $a->config[$uid][$family][$key] = $val;
188 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
195 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
196 $a->config[$uid][$family][$key] = $val;
200 $a->config[$uid][$family][$key] = '!<unset>!';
205 if(! function_exists('del_config')) {
206 function del_config($family,$key) {
209 if(x($a->config[$family],$key))
210 unset($a->config[$family][$key]);
211 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
215 // If APC is enabled then store the data there
216 if (function_exists("apc_delete"))
217 apc_delete($family."|".$key);
224 // Same as above functions except these are for personal config storage and take an
225 // additional $uid argument.
227 if(! function_exists('set_pconfig')) {
228 function set_pconfig($uid,$family,$key,$value) {
232 // manage array value
233 $dbvalue = (is_array($value)?serialize($value):$value);
235 if(get_pconfig($uid,$family,$key,true) === false) {
236 $a->config[$uid][$family][$key] = $value;
237 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
247 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
254 $a->config[$uid][$family][$key] = $value;
256 // If APC is enabled then store the data there
257 if (function_exists("apc_store"))
258 apc_store($uid."|".$family."|".$key, $value, 600);
266 if(! function_exists('del_pconfig')) {
267 function del_pconfig($uid,$family,$key) {
270 if(x($a->config[$uid][$family],$key))
271 unset($a->config[$uid][$family][$key]);
272 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",