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