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