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]+:{.*}$|", $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 $dbvalue = (is_array($value)?serialize($value):$value);
90 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
91 if(get_config($family,$key,true) === false) {
92 $a->config[$family][$key] = $value;
93 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
103 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
109 $a->config[$family][$key] = $value;
117 if(! function_exists('load_pconfig')) {
118 function load_pconfig($uid,$family) {
120 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
127 $a->config[$uid][$family][$k] = $rr['v'];
129 } else if ($family != 'config') {
131 $a->config[$uid][$family] = "!<unset>!";
137 if(! function_exists('get_pconfig')) {
138 function get_pconfig($uid,$family, $key, $instore = false) {
143 // Looking if the whole family isn't set
144 if(isset($a->config[$uid][$family])) {
145 if($a->config[$uid][$family] === '!<unset>!') {
150 if(isset($a->config[$uid][$family][$key])) {
151 if($a->config[$uid][$family][$key] === '!<unset>!') {
154 return $a->config[$uid][$family][$key];
158 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
165 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
166 $a->config[$uid][$family][$key] = $val;
170 $a->config[$uid][$family][$key] = '!<unset>!';
175 if(! function_exists('del_config')) {
176 function del_config($family,$key) {
179 if(x($a->config[$family],$key))
180 unset($a->config[$family][$key]);
181 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
190 // Same as above functions except these are for personal config storage and take an
191 // additional $uid argument.
193 if(! function_exists('set_pconfig')) {
194 function set_pconfig($uid,$family,$key,$value) {
198 // manage array value
199 $dbvalue = (is_array($value)?serialize($value):$value);
201 if(get_pconfig($uid,$family,$key,true) === false) {
202 $a->config[$uid][$family][$key] = $value;
203 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
213 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
220 $a->config[$uid][$family][$key] = $value;
227 if(! function_exists('del_pconfig')) {
228 function del_pconfig($uid,$family,$key) {
231 if(x($a->config[$uid][$family],$key))
232 unset($a->config[$uid][$family][$key]);
233 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",