]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge pull request #927 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, else try XCache
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         elseif (function_exists("xcache_fetch") AND function_exists("xcache_isset"))
81                 if (xcache_isset($family."|".$key)) {
82                         $val = xcache_fetch($family."|".$key);
83                         $a->config[$family][$key] = $val;
84
85                         if ($val === '!<unset>!')
86                                 return false;
87                         else
88                                 return $val;
89                 }
90
91         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
92                 dbesc($family),
93                 dbesc($key)
94         );
95         if(count($ret)) {
96                 // manage array value
97                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
98                 $a->config[$family][$key] = $val;
99
100                 // If APC is enabled then store the data there, else try XCache
101                 if (function_exists("apc_store"))
102                         apc_store($family."|".$key, $val, 600);
103                 elseif (function_exists("xcache_set"))
104                         xcache_set($family."|".$key, $val, 600);
105
106                 return $val;
107         }
108         else {
109                 $a->config[$family][$key] = '!<unset>!';
110
111                 // If APC is enabled then store the data there, else try XCache
112                 if (function_exists("apc_store"))
113                         apc_store($family."|".$key, '!<unset>!', 600);
114                 elseif (function_exists("xcache_set"))
115                         xcache_set($family."|".$key, '!<unset>!', 600);
116         }
117         return false;
118 }}
119
120 // Store a config value ($value) in the category ($family)
121 // under the key ($key)
122 // Return the value, or false if the database update failed
123
124 if(! function_exists('set_config')) {
125 function set_config($family,$key,$value) {
126         global $a;
127
128         // If $a->config[$family] has been previously set to '!<unset>!', then
129         // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
130         // $a->config[$family][$key] = $value will be equivalent to
131         // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
132         // so unset the family before assigning a value to a family's key
133         if($a->config[$family] === '!<unset>!')
134                 unset($a->config[$family]);
135
136         // manage array value
137         $dbvalue = (is_array($value)?serialize($value):$value);
138         $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
139         if(get_config($family,$key,true) === false) {
140                 $a->config[$family][$key] = $value;
141                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
142                         dbesc($family),
143                         dbesc($key),
144                         dbesc($dbvalue)
145                 );
146                 if($ret)
147                         return $value;
148                 return $ret;
149         }
150
151         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
152                 dbesc($dbvalue),
153                 dbesc($family),
154                 dbesc($key)
155         );
156
157         $a->config[$family][$key] = $value;
158
159         // If APC is enabled then store the data there, else try XCache
160         if (function_exists("apc_store"))
161                 apc_store($family."|".$key, $value, 600);
162         elseif (function_exists("xcache_set"))
163                 xcache_set($family."|".$key, $value, 600);
164
165         if($ret)
166                 return $value;
167         return $ret;
168 }}
169
170
171 if(! function_exists('load_pconfig')) {
172 function load_pconfig($uid,$family) {
173         global $a;
174         $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
175                 dbesc($family),
176                 intval($uid)
177         );
178         if(count($r)) {
179                 foreach($r as $rr) {
180                         $k = $rr['k'];
181                         $a->config[$uid][$family][$k] = $rr['v'];
182                 }
183         } else if ($family != 'config') {
184                 // Negative caching
185                 $a->config[$uid][$family] = "!<unset>!";
186         }
187 }}
188
189
190
191 if(! function_exists('get_pconfig')) {
192 function get_pconfig($uid,$family, $key, $instore = false) {
193
194         global $a;
195
196         if(! $instore) {
197                 // Looking if the whole family isn't set
198                 if(isset($a->config[$uid][$family])) {
199                         if($a->config[$uid][$family] === '!<unset>!') {
200                                 return false;
201                         }
202                 }
203
204                 if(isset($a->config[$uid][$family][$key])) {
205                         if($a->config[$uid][$family][$key] === '!<unset>!') {
206                                 return false;
207                         }
208                         return $a->config[$uid][$family][$key];
209                 }
210         }
211
212         // If APC is enabled then fetch the data from there, else try XCache
213         if (function_exists("apc_fetch") AND function_exists("apc_exists"))
214                 if (apc_exists($uid."|".$family."|".$key)) {
215                         $val = apc_fetch($uid."|".$family."|".$key);
216                         $a->config[$uid][$family][$key] = $val;
217
218                         if ($val === '!<unset>!')
219                                 return false;
220                         else
221                                 return $val;
222                 }
223         elseif (function_exists("xcache_get") AND function_exists("xcache_isset"))
224                 if (xcache_isset($uid."|".$family."|".$key)) {
225                         $val = xcache_get($uid."|".$family."|".$key);
226                         $a->config[$uid][$family][$key] = $val;
227
228                         if ($val === '!<unset>!')
229                                 return false;
230                         else
231                                 return $val;
232                 }
233
234
235         $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
236                 intval($uid),
237                 dbesc($family),
238                 dbesc($key)
239         );
240
241         if(count($ret)) {
242                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
243                 $a->config[$uid][$family][$key] = $val;
244
245                 // If APC is enabled then store the data there, else try XCache
246                 if (function_exists("apc_store"))
247                         apc_store($uid."|".$family."|".$key, $val, 600);
248                 elseif (function_exists("xcache_set"))
249                         xcache_set($uid."|".$family."|".$key, $val, 600);
250
251                 return $val;
252         }
253         else {
254                 $a->config[$uid][$family][$key] = '!<unset>!';
255
256                 // If APC is enabled then store the data there, else try XCache
257                 if (function_exists("apc_store"))
258                         apc_store($uid."|".$family."|".$key, '!<unset>!', 600);
259                 elseif (function_exists("xcache_set"))
260                         xcache_set($uid."|".$family."|".$key, '!<unset>!', 600);
261         }
262         return false;
263 }}
264
265 if(! function_exists('del_config')) {
266 function del_config($family,$key) {
267
268         global $a;
269         if(x($a->config[$family],$key))
270                 unset($a->config[$family][$key]);
271         $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
272                 dbesc($family),
273                 dbesc($key)
274         );
275         // If APC is enabled then delete the data from there, else try XCache
276         if (function_exists("apc_delete"))
277                 apc_delete($family."|".$key);
278         elseif (function_exists("xcache_unset"))
279                 xcache_unset($family."|".$key);
280
281         return $ret;
282 }}
283
284
285
286 // Same as above functions except these are for personal config storage and take an
287 // additional $uid argument.
288
289 if(! function_exists('set_pconfig')) {
290 function set_pconfig($uid,$family,$key,$value) {
291
292         global $a;
293
294         // manage array value
295         $dbvalue = (is_array($value)?serialize($value):$value);
296
297         if(get_pconfig($uid,$family,$key,true) === false) {
298                 $a->config[$uid][$family][$key] = $value;
299                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
300                         intval($uid),
301                         dbesc($family),
302                         dbesc($key),
303                         dbesc($dbvalue)
304                 );
305                 if($ret) 
306                         return $value;
307                 return $ret;
308         }
309         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
310                 dbesc($dbvalue),
311                 intval($uid),
312                 dbesc($family),
313                 dbesc($key)
314         );
315
316         $a->config[$uid][$family][$key] = $value;
317
318         // If APC is enabled then store the data there, else try XCache
319         if (function_exists("apc_store"))
320                 apc_store($uid."|".$family."|".$key, $value, 600);
321         elseif (function_exists("xcache_set"))
322                 xcache_set($uid."|".$family."|".$key, $value, 600);
323
324
325         if($ret)
326                 return $value;
327         return $ret;
328 }}
329
330 if(! function_exists('del_pconfig')) {
331 function del_pconfig($uid,$family,$key) {
332
333         global $a;
334         if(x($a->config[$uid][$family],$key))
335                 unset($a->config[$uid][$family][$key]);
336         $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
337                 intval($uid),
338                 dbesc($family),
339                 dbesc($key)
340         );
341         return $ret;
342 }}