]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge pull request #758 from annando/master
[friendica.git] / include / config.php
1 <?php
2
3 /**
4  *
5  * Arbitrary configuration storage
6  * Note:
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.
10  *
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.
13  */
14
15
16 // retrieve a "family" of config variables from database to cached storage
17
18 if(! function_exists('load_config')) {
19 function load_config($family) {
20         global $a;
21
22         // To-Do: How to integrate APC here?
23
24         $r = q("SELECT * FROM `config` WHERE `cat` = '%s'", dbesc($family));
25         if(count($r)) {
26                 foreach($r as $rr) {
27                         $k = $rr['k'];
28                         if ($family === 'config') {
29                                 $a->config[$k] = $rr['v'];
30                         } else {
31                                 $a->config[$family][$k] = $rr['v'];
32                         }
33                 }
34         } else if ($family != 'config') {
35                 // Negative caching
36                 $a->config[$family] = "!<unset>!";
37         }
38 }}
39
40 // get a particular config variable given the family name
41 // and key. Returns false if not set.
42 // $instore is only used by the set_config function
43 // to determine if the key already exists in the DB
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.
47
48 if(! function_exists('get_config')) {
49 function get_config($family, $key, $instore = false) {
50
51         global $a;
52
53         if(! $instore) {
54                 // Looking if the whole family isn't set
55                 if(isset($a->config[$family])) {
56                         if($a->config[$family] === '!<unset>!') {
57                                 return false;
58                         }
59                 }
60
61                 if(isset($a->config[$family][$key])) {
62                         if($a->config[$family][$key] === '!<unset>!') {
63                                 return false;
64                         }
65                         return $a->config[$family][$key];
66                 }
67         }
68
69         // If APC is enabled then fetch the data from there
70         if (function_exists("apc_fetch") AND function_exists("apc_exists"))
71                 if (apc_exists($family."|".$key)) {
72                         $val = apc_fetch($family."|".$key);
73                         $a->config[$family][$key] = $val;
74
75                         if ($val === '!<unset>!')
76                                 return false;
77                         else
78                                 return $val;
79                 }
80
81         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
82                 dbesc($family),
83                 dbesc($key)
84         );
85         if(count($ret)) {
86                 // manage array value
87                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
88                 $a->config[$family][$key] = $val;
89
90                 // If APC is enabled then store the data there
91                 if (function_exists("apc_store"))
92                         apc_store($family."|".$key, $val, 600);
93
94                 return $val;
95         }
96         else {
97                 $a->config[$family][$key] = '!<unset>!';
98
99                 // If APC is enabled then store the data there
100                 if (function_exists("apc_store"))
101                         apc_store($family."|".$key, '!<unset>!', 600);
102         }
103         return false;
104 }}
105
106 // Store a config value ($value) in the category ($family)
107 // under the key ($key)
108 // Return the value, or false if the database update failed
109
110 if(! function_exists('set_config')) {
111 function set_config($family,$key,$value) {
112         global $a;
113
114         // If $a->config[$family] has been previously set to '!<unset>!', then
115         // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
116         // $a->config[$family][$key] = $value will be equivalent to
117         // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
118         // so unset the family before assigning a value to a family's key
119         if($a->config[$family] === '!<unset>!')
120                 unset($a->config[$family]);
121
122         // manage array value
123         $dbvalue = (is_array($value)?serialize($value):$value);
124         $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
125         if(get_config($family,$key,true) === false) {
126                 $a->config[$family][$key] = $value;
127                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
128                         dbesc($family),
129                         dbesc($key),
130                         dbesc($dbvalue)
131                 );
132                 if($ret)
133                         return $value;
134                 return $ret;
135         }
136
137         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
138                 dbesc($dbvalue),
139                 dbesc($family),
140                 dbesc($key)
141         );
142
143         $a->config[$family][$key] = $value;
144
145         // If APC is enabled then store the data there
146         if (function_exists("apc_store"))
147                 apc_store($family."|".$key, $value, 600);
148
149         if($ret)
150                 return $value;
151         return $ret;
152 }}
153
154
155 if(! function_exists('load_pconfig')) {
156 function load_pconfig($uid,$family) {
157         global $a;
158         $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
159                 dbesc($family),
160                 intval($uid)
161         );
162         if(count($r)) {
163                 foreach($r as $rr) {
164                         $k = $rr['k'];
165                         $a->config[$uid][$family][$k] = $rr['v'];
166                 }
167         } else if ($family != 'config') {
168                 // Negative caching
169                 $a->config[$uid][$family] = "!<unset>!";
170         }
171 }}
172
173
174
175 if(! function_exists('get_pconfig')) {
176 function get_pconfig($uid,$family, $key, $instore = false) {
177
178         global $a;
179
180         if(! $instore) {
181                 // Looking if the whole family isn't set
182                 if(isset($a->config[$uid][$family])) {
183                         if($a->config[$uid][$family] === '!<unset>!') {
184                                 return false;
185                         }
186                 }
187
188                 if(isset($a->config[$uid][$family][$key])) {
189                         if($a->config[$uid][$family][$key] === '!<unset>!') {
190                                 return false;
191                         }
192                         return $a->config[$uid][$family][$key];
193                 }
194         }
195
196         // If APC is enabled then fetch the data from there
197         if (function_exists("apc_fetch") AND function_exists("apc_exists"))
198                 if (apc_exists($uid."|".$family."|".$key)) {
199                         $val = apc_fetch($uid."|".$family."|".$key);
200                         $a->config[$uid][$family][$key] = $val;
201
202                         if ($val === '!<unset>!')
203                                 return false;
204                         else
205                                 return $val;
206                 }
207
208
209         $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
210                 intval($uid),
211                 dbesc($family),
212                 dbesc($key)
213         );
214
215         if(count($ret)) {
216                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
217                 $a->config[$uid][$family][$key] = $val;
218
219                 // If APC is enabled then store the data there
220                 if (function_exists("apc_store"))
221                         apc_store($uid."|".$family."|".$key, $val, 600);
222
223                 return $val;
224         }
225         else {
226                 $a->config[$uid][$family][$key] = '!<unset>!';
227
228                 // If APC is enabled then store the data there
229                 if (function_exists("apc_store"))
230                         apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
231         }
232         return false;
233 }}
234
235 if(! function_exists('del_config')) {
236 function del_config($family,$key) {
237
238         global $a;
239         if(x($a->config[$family],$key))
240                 unset($a->config[$family][$key]);
241         $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
242                 dbesc($family),
243                 dbesc($key)
244         );
245         // If APC is enabled then store the data there
246         if (function_exists("apc_delete"))
247                 apc_delete($family."|".$key);
248
249         return $ret;
250 }}
251
252
253
254 // Same as above functions except these are for personal config storage and take an
255 // additional $uid argument.
256
257 if(! function_exists('set_pconfig')) {
258 function set_pconfig($uid,$family,$key,$value) {
259
260         global $a;
261
262         // manage array value
263         $dbvalue = (is_array($value)?serialize($value):$value);
264
265         if(get_pconfig($uid,$family,$key,true) === false) {
266                 $a->config[$uid][$family][$key] = $value;
267                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
268                         intval($uid),
269                         dbesc($family),
270                         dbesc($key),
271                         dbesc($dbvalue)
272                 );
273                 if($ret) 
274                         return $value;
275                 return $ret;
276         }
277         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
278                 dbesc($dbvalue),
279                 intval($uid),
280                 dbesc($family),
281                 dbesc($key)
282         );
283
284         $a->config[$uid][$family][$key] = $value;
285
286         // If APC is enabled then store the data there
287         if (function_exists("apc_store"))
288                 apc_store($uid."|".$family."|".$key, $value, 600);
289
290
291         if($ret)
292                 return $value;
293         return $ret;
294 }}
295
296 if(! function_exists('del_pconfig')) {
297 function del_pconfig($uid,$family,$key) {
298
299         global $a;
300         if(x($a->config[$uid][$family],$key))
301                 unset($a->config[$uid][$family][$key]);
302         $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
303                 intval($uid),
304                 dbesc($family),
305                 dbesc($key)
306         );
307         return $ret;
308 }}