4 * Arbitrary configuration storage
6 * Please do not store booleans - convert to 0/1 integer values
7 * The get_?config() functions return boolean false for keys that are unset,
8 * and this could lead to subtle bugs.
10 * There are a few places in the code (such as the admin panel) where boolean
11 * configurations need to be fixed as of 10/08/2011.
18 * retrieve a "family" of config variables
19 * from database to cached storage
21 if(! function_exists('load_config')) {
22 function load_config($family) {
24 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
30 if ($rr['cat'] === 'config') {
31 $a->config[$k] = $rr['v'];
33 $a->config[$family][$k] = $rr['v'];
41 * get a particular config variable given the family name
42 * and key. Returns false if not set.
44 * If a key is found in the DB but doesn't exist in
45 * local config cache, pull it into the cache so we don't have
46 *to hit the DB again for this item.
48 if(! function_exists('get_config')) {
49 function get_config($family, $key) {
54 if(isset($a->config[$family][$key])) {
55 if($a->config[$family][$key] === '!<unset>!') {
58 return $a->config[$family][$key];
60 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
66 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
67 $a->config[$family][$key] = $val;
71 $a->config[$family][$key] = '!<unset>!';
78 * Store a config value ($value) in the category ($family)
79 * under the key ($key)
81 * Return the value, or false if the database update failed
83 if(! function_exists('set_config')) {
85 function set_config($family,$key,$value) {
89 $dbvalue = (is_array($value)?serialize($value):$value);
90 $dbvalue = (is_bool($value) ? intval($value) : $value);
92 $a->config[$family][$key] = $value;
93 $ret = q("REPLACE INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
107 if(! function_exists('load_pconfig')) {
108 function load_pconfig($uid,$family) {
110 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
117 $a->config[$uid][$family][$k] = $rr['v'];
125 * get a particular user-specific config variable given the family name,
126 * the user id and key. Returns false if not set.
\r
128 * If a key is found in the DB but doesn't exist in
\r
129 * local config cache, pull it into the cache so we don't have
\r
130 * to hit the DB again for this item.
\r
132 if(! function_exists('get_pconfig')) {
133 function get_pconfig($uid,$family, $key) {
138 if(isset($a->config[$uid][$family][$key])) {
139 if($a->config[$uid][$family][$key] === '!<unset>!') {
142 return $a->config[$uid][$family][$key];
146 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
153 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
154 $a->config[$uid][$family][$key] = $val;
158 $a->config[$uid][$family][$key] = '!<unset>!';
165 * Delete a value from config. This function
166 * deletes both: db value and cache entry.
168 if(! function_exists('del_config')) {
169 function del_config($family,$key) {
172 if(x($a->config[$family],$key))
173 unset($a->config[$family][$key]);
174 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
184 * Store a user-specific config value ($value) for user $uid in the category ($family)
\r
185 * under the key ($key).
187 * Return the value, or false if the database update failed
\r
189 if(! function_exists('set_pconfig')) {
190 function set_pconfig($uid,$family,$key,$value) {
194 // manage array value
195 $dbvalue = (is_array($value)?serialize($value):$value);
196 $dbvalue = (is_bool($value)?serialize($value):$value);
199 $a->config[$uid][$family][$key] = $value;
200 $ret = q("REPLACE INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
214 if(! function_exists('del_pconfig')) {
215 function del_pconfig($uid,$family,$key) {
218 if(x($a->config[$uid][$family],$key))
219 unset($a->config[$uid][$family][$key]);
220 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",