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