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'",
27 if ($rr['cat'] === 'config') {
28 $a->config[$k] = $rr['v'];
30 $a->config[$family][$k] = $rr['v'];
36 // get a particular config variable given the family name
37 // and key. Returns false if not set.
38 // $instore is only used by the set_config function
39 // to determine if the key already exists in the DB
40 // If a key is found in the DB but doesn't exist in
41 // local config cache, pull it into the cache so we don't have
42 // to hit the DB again for this item.
44 if(! function_exists('get_config')) {
45 function get_config($family, $key, $instore = false) {
50 if(isset($a->config[$family][$key])) {
51 if($a->config[$family][$key] === '!<unset>!') {
54 return $a->config[$family][$key];
57 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
63 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
64 $a->config[$family][$key] = $val;
68 $a->config[$family][$key] = '!<unset>!';
73 // Store a config value ($value) in the category ($family)
74 // under the key ($key)
75 // Return the value, or false if the database update failed
77 if(! function_exists('set_config')) {
78 function set_config($family,$key,$value) {
82 $dbvalue = (is_array($value)?serialize($value):$value);
83 $dbvalue = (is_bool($value) ? intval($value) : $value);
85 if(get_config($family,$key,true) === false) {
86 $a->config[$family][$key] = $value;
87 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
97 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
103 $a->config[$family][$key] = $value;
111 if(! function_exists('load_pconfig')) {
112 function load_pconfig($uid,$family) {
114 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
121 $a->config[$uid][$family][$k] = $rr['v'];
128 if(! function_exists('get_pconfig')) {
129 function get_pconfig($uid,$family, $key, $instore = false) {
134 if(isset($a->config[$uid][$family][$key])) {
135 if($a->config[$uid][$family][$key] === '!<unset>!') {
138 return $a->config[$uid][$family][$key];
142 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
149 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
150 $a->config[$uid][$family][$key] = $val;
154 $a->config[$uid][$family][$key] = '!<unset>!';
159 if(! function_exists('del_config')) {
160 function del_config($family,$key) {
163 if(x($a->config[$family],$key))
164 unset($a->config[$family][$key]);
165 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
174 // Same as above functions except these are for personal config storage and take an
175 // additional $uid argument.
177 if(! function_exists('set_pconfig')) {
178 function set_pconfig($uid,$family,$key,$value) {
182 // manage array value
183 $dbvalue = (is_array($value)?serialize($value):$value);
185 if(get_pconfig($uid,$family,$key,true) === false) {
186 $a->config[$uid][$family][$key] = $value;
187 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
197 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
204 $a->config[$uid][$family][$key] = $value;
211 if(! function_exists('del_pconfig')) {
212 function del_pconfig($uid,$family,$key) {
215 if(x($a->config[$uid][$family],$key))
216 unset($a->config[$uid][$family][$key]);
217 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",