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