]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge pull request #336 from hauke68/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 ($rr['cat'] === 'config') {
26                                 $a->config[$k] = $rr['v'];
27                         } else {
28                                 $a->config[$family][$k] = $rr['v'];
29                         }
30                 }
31         } else if (isset($rr) && ($rr['cat'] != '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]+:{.*}$|", $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         // manage array value
89         $dbvalue = (is_array($value)?serialize($value):$value);
90         $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
91         if(get_config($family,$key,true) === false) {
92                 $a->config[$family][$key] = $value;
93                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
94                         dbesc($family),
95                         dbesc($key),
96                         dbesc($dbvalue)
97                 );
98                 if($ret)
99                         return $value;
100                 return $ret;
101         }
102
103         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
104                 dbesc($dbvalue),
105                 dbesc($family),
106                 dbesc($key)
107         );
108
109         $a->config[$family][$key] = $value;
110
111         if($ret)
112                 return $value;
113         return $ret;
114 }}
115
116
117 if(! function_exists('load_pconfig')) {
118 function load_pconfig($uid,$family) {
119         global $a;
120         $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
121                 dbesc($family),
122                 intval($uid)
123         );
124         if(count($r)) {
125                 foreach($r as $rr) {
126                         $k = $rr['k'];
127                         $a->config[$uid][$family][$k] = $rr['v'];
128                 }
129         } else if ($rr['cat'] != 'config') {
130                 // Negative caching
131                 $a->config[$uid][$family] = "!<unset>!";
132         }
133 }}
134
135
136
137 if(! function_exists('get_pconfig')) {
138 function get_pconfig($uid,$family, $key, $instore = false) {
139
140         global $a;
141
142         if(! $instore) {
143                 // Looking if the whole family isn't set
144                 if(isset($a->config[$uid][$family])) {
145                         if($a->config[$uid][$family] === '!<unset>!') {
146                                 return false;
147                         }
148                 }
149
150                 if(isset($a->config[$uid][$family][$key])) {
151                         if($a->config[$uid][$family][$key] === '!<unset>!') {
152                                 return false;
153                         }
154                         return $a->config[$uid][$family][$key];
155                 }
156         }
157
158         $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
159                 intval($uid),
160                 dbesc($family),
161                 dbesc($key)
162         );
163
164         if(count($ret)) {
165                 $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
166                 $a->config[$uid][$family][$key] = $val;
167                 return $val;
168         }
169         else {
170                 $a->config[$uid][$family][$key] = '!<unset>!';
171         }
172         return false;
173 }}
174
175 if(! function_exists('del_config')) {
176 function del_config($family,$key) {
177
178         global $a;
179         if(x($a->config[$family],$key))
180                 unset($a->config[$family][$key]);
181         $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
182                 dbesc($family),
183                 dbesc($key)
184         );
185         return $ret;
186 }}
187
188
189
190 // Same as above functions except these are for personal config storage and take an
191 // additional $uid argument.
192
193 if(! function_exists('set_pconfig')) {
194 function set_pconfig($uid,$family,$key,$value) {
195
196         global $a;
197
198         // manage array value
199         $dbvalue = (is_array($value)?serialize($value):$value);
200
201         if(get_pconfig($uid,$family,$key,true) === false) {
202                 $a->config[$uid][$family][$key] = $value;
203                 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
204                         intval($uid),
205                         dbesc($family),
206                         dbesc($key),
207                         dbesc($dbvalue)
208                 );
209                 if($ret) 
210                         return $value;
211                 return $ret;
212         }
213         $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
214                 dbesc($dbvalue),
215                 intval($uid),
216                 dbesc($family),
217                 dbesc($key)
218         );
219
220         $a->config[$uid][$family][$key] = $value;
221
222         if($ret)
223                 return $value;
224         return $ret;
225 }}
226
227 if(! function_exists('del_pconfig')) {
228 function del_pconfig($uid,$family,$key) {
229
230         global $a;
231         if(x($a->config[$uid][$family],$key))
232                 unset($a->config[$uid][$family][$key]);
233         $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
234                 intval($uid),
235                 dbesc($family),
236                 dbesc($key)
237         );
238         return $ret;
239 }}