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];
65 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
71 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
72 $a->config[$family][$key] = $val;
76 $a->config[$family][$key] = '!<unset>!';
81 // Store a config value ($value) in the category ($family)
82 // under the key ($key)
83 // Return the value, or false if the database update failed
85 if(! function_exists('set_config')) {
86 function set_config($family,$key,$value) {
89 // If $a->config[$family] has been previously set to '!<unset>!', then
90 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
91 // $a->config[$family][$key] = $value will be equivalent to
92 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
93 // so unset the family before assigning a value to a family's key
94 if($a->config[$family] === '!<unset>!')
95 unset($a->config[$family]);
98 $dbvalue = (is_array($value)?serialize($value):$value);
99 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
100 if(get_config($family,$key,true) === false) {
101 $a->config[$family][$key] = $value;
102 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
112 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
118 $a->config[$family][$key] = $value;
126 if(! function_exists('load_pconfig')) {
127 function load_pconfig($uid,$family) {
129 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
136 $a->config[$uid][$family][$k] = $rr['v'];
138 } else if ($family != 'config') {
140 $a->config[$uid][$family] = "!<unset>!";
146 if(! function_exists('get_pconfig')) {
147 function get_pconfig($uid,$family, $key, $instore = false) {
152 // Looking if the whole family isn't set
153 if(isset($a->config[$uid][$family])) {
154 if($a->config[$uid][$family] === '!<unset>!') {
159 if(isset($a->config[$uid][$family][$key])) {
160 if($a->config[$uid][$family][$key] === '!<unset>!') {
163 return $a->config[$uid][$family][$key];
167 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
174 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
175 $a->config[$uid][$family][$key] = $val;
179 $a->config[$uid][$family][$key] = '!<unset>!';
184 if(! function_exists('del_config')) {
185 function del_config($family,$key) {
188 if(x($a->config[$family],$key))
189 unset($a->config[$family][$key]);
190 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
199 // Same as above functions except these are for personal config storage and take an
200 // additional $uid argument.
202 if(! function_exists('set_pconfig')) {
203 function set_pconfig($uid,$family,$key,$value) {
207 // manage array value
208 $dbvalue = (is_array($value)?serialize($value):$value);
210 if(get_pconfig($uid,$family,$key,true) === false) {
211 $a->config[$uid][$family][$key] = $value;
212 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
222 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
229 $a->config[$uid][$family][$key] = $value;
236 if(! function_exists('del_pconfig')) {
237 function del_pconfig($uid,$family,$key) {
240 if(x($a->config[$uid][$family],$key))
241 unset($a->config[$uid][$family][$key]);
242 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",