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'];
33 } else if ($rr['cat'] != 'config') {
35 $a->config[$family] = "!<unset>!";
39 // get a particular config variable given the family name
40 // and key. Returns false if not set.
41 // $instore is only used by the set_config function
42 // to determine if the key already exists in the DB
43 // If a key is found in the DB but doesn't exist in
44 // local config cache, pull it into the cache so we don't have
45 // to hit the DB again for this item.
47 if(! function_exists('get_config')) {
48 function get_config($family, $key, $instore = false) {
53 // Looking if the whole family isn't set
54 if(isset($a->config[$family])) {
55 if($a->config[$family] === '!<unset>!') {
60 if(isset($a->config[$family][$key])) {
61 if($a->config[$family][$key] === '!<unset>!') {
64 return $a->config[$family][$key];
67 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
73 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
74 $a->config[$family][$key] = $val;
78 $a->config[$family][$key] = '!<unset>!';
83 // Store a config value ($value) in the category ($family)
84 // under the key ($key)
85 // Return the value, or false if the database update failed
87 if(! function_exists('set_config')) {
88 function set_config($family,$key,$value) {
91 $dbvalue = (is_array($value)?serialize($value):$value);
92 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
93 if(get_config($family,$key,true) === false) {
94 $a->config[$family][$key] = $value;
95 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
105 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
111 $a->config[$family][$key] = $value;
119 if(! function_exists('load_pconfig')) {
120 function load_pconfig($uid,$family) {
122 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
129 $a->config[$uid][$family][$k] = $rr['v'];
131 } else if ($rr['cat'] != 'config') {
133 $a->config[$uid][$family] = "!<unset>!";
139 if(! function_exists('get_pconfig')) {
140 function get_pconfig($uid,$family, $key, $instore = false) {
145 // Looking if the whole family isn't set
146 if(isset($a->config[$uid][$family])) {
147 if($a->config[$uid][$family] === '!<unset>!') {
152 if(isset($a->config[$uid][$family][$key])) {
153 if($a->config[$uid][$family][$key] === '!<unset>!') {
156 return $a->config[$uid][$family][$key];
160 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
167 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
168 $a->config[$uid][$family][$key] = $val;
172 $a->config[$uid][$family][$key] = '!<unset>!';
177 if(! function_exists('del_config')) {
178 function del_config($family,$key) {
181 if(x($a->config[$family],$key))
182 unset($a->config[$family][$key]);
183 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
192 // Same as above functions except these are for personal config storage and take an
193 // additional $uid argument.
195 if(! function_exists('set_pconfig')) {
196 function set_pconfig($uid,$family,$key,$value) {
200 // manage array value
201 $dbvalue = (is_array($value)?serialize($value):$value);
203 if(get_pconfig($uid,$family,$key,true) === false) {
204 $a->config[$uid][$family][$key] = $value;
205 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
215 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
222 $a->config[$uid][$family][$key] = $value;
229 if(! function_exists('del_pconfig')) {
230 function del_pconfig($uid,$family,$key) {
233 if(x($a->config[$uid][$family],$key))
234 unset($a->config[$uid][$family][$key]);
235 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",