]> 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         $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         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
66                 dbesc($family),
67                 dbesc($key)
68         );
69         if(count($ret)) {
70                 // manage array value
71                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
72                 $a->config[$family][$key] = $val;
73                 return $val;
74         }
75         else {
76                 $a->config[$family][$key] = '!<unset>!';
77         }
78         return false;
79 }}
80
81 // Store a config value ($value) in the category ($family)
82 // under the key ($key)
83 // Return the value, or false if the database update failed
84
85 if(! function_exists('set_config')) {
86 function set_config($family,$key,$value) {
87         global $a;
88
89         // If $a->config[$family] has been previously set to '!<unset>!', then
90         // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
91         // $a->config[$family][$key] = $value will be equivalent to
92         // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
93         // so unset the family before assigning a value to a family's key
94         if($a->config[$family] === '!<unset>!')
95                 unset($a->config[$family]);
96
97         // manage array value
98         $dbvalue = (is_array($value)?serialize($value):$value);
99         $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
100         if(get_config($family,$key,true) === false) {
101                 $a->config[$family][$key] = $value;
102                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
103                         dbesc($family),
104                         dbesc($key),
105                         dbesc($dbvalue)
106                 );
107                 if($ret)
108                         return $value;
109                 return $ret;
110         }
111
112         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
113                 dbesc($dbvalue),
114                 dbesc($family),
115                 dbesc($key)
116         );
117
118         $a->config[$family][$key] = $value;
119
120         if($ret)
121                 return $value;
122         return $ret;
123 }}
124
125
126 if(! function_exists('load_pconfig')) {
127 function load_pconfig($uid,$family) {
128         global $a;
129         $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
130                 dbesc($family),
131                 intval($uid)
132         );
133         if(count($r)) {
134                 foreach($r as $rr) {
135                         $k = $rr['k'];
136                         $a->config[$uid][$family][$k] = $rr['v'];
137                 }
138         } else if ($family != 'config') {
139                 // Negative caching
140                 $a->config[$uid][$family] = "!<unset>!";
141         }
142 }}
143
144
145
146 if(! function_exists('get_pconfig')) {
147 function get_pconfig($uid,$family, $key, $instore = false) {
148
149         global $a;
150
151         if(! $instore) {
152                 // Looking if the whole family isn't set
153                 if(isset($a->config[$uid][$family])) {
154                         if($a->config[$uid][$family] === '!<unset>!') {
155                                 return false;
156                         }
157                 }
158
159                 if(isset($a->config[$uid][$family][$key])) {
160                         if($a->config[$uid][$family][$key] === '!<unset>!') {
161                                 return false;
162                         }
163                         return $a->config[$uid][$family][$key];
164                 }
165         }
166
167         $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
168                 intval($uid),
169                 dbesc($family),
170                 dbesc($key)
171         );
172
173         if(count($ret)) {
174                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
175                 $a->config[$uid][$family][$key] = $val;
176                 return $val;
177         }
178         else {
179                 $a->config[$uid][$family][$key] = '!<unset>!';
180         }
181         return false;
182 }}
183
184 if(! function_exists('del_config')) {
185 function del_config($family,$key) {
186
187         global $a;
188         if(x($a->config[$family],$key))
189                 unset($a->config[$family][$key]);
190         $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
191                 dbesc($family),
192                 dbesc($key)
193         );
194         return $ret;
195 }}
196
197
198
199 // Same as above functions except these are for personal config storage and take an
200 // additional $uid argument.
201
202 if(! function_exists('set_pconfig')) {
203 function set_pconfig($uid,$family,$key,$value) {
204
205         global $a;
206
207         // manage array value
208         $dbvalue = (is_array($value)?serialize($value):$value);
209
210         if(get_pconfig($uid,$family,$key,true) === false) {
211                 $a->config[$uid][$family][$key] = $value;
212                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
213                         intval($uid),
214                         dbesc($family),
215                         dbesc($key),
216                         dbesc($dbvalue)
217                 );
218                 if($ret) 
219                         return $value;
220                 return $ret;
221         }
222         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
223                 dbesc($dbvalue),
224                 intval($uid),
225                 dbesc($family),
226                 dbesc($key)
227         );
228
229         $a->config[$uid][$family][$key] = $value;
230
231         if($ret)
232                 return $value;
233         return $ret;
234 }}
235
236 if(! function_exists('del_pconfig')) {
237 function del_pconfig($uid,$family,$key) {
238
239         global $a;
240         if(x($a->config[$uid][$family],$key))
241                 unset($a->config[$uid][$family][$key]);
242         $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
243                 intval($uid),
244                 dbesc($family),
245                 dbesc($key)
246         );
247         return $ret;
248 }}