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