]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge branch 'master' of https://github.com/friendica/friendica
[friendica.git] / include / config.php
1 <?php
2 /**
3  *
4  * Arbitrary configuration storage
5  * Note:
6  * Please do not store booleans - convert to 0/1 integer values
7  * The get_?config() functions return boolean false for keys that are unset,
8  * and this could lead to subtle bugs.
9  *
10  * There are a few places in the code (such as the admin panel) where boolean
11  * configurations need to be fixed as of 10/08/2011.
12  *
13  * @package config;
14  */
15
16
17 /**
18  * retrieve a "family" of config variables
19  * from database to cached storage
20  */
21 if(! function_exists('load_config')) {
22         function load_config($family) {
23                 global $a;
24                 $r = q("SELECT * FROM `config` WHERE `cat` = '%s'",
25                                 dbesc($family)
26                 );
27                 if(count($r)) {
28                         foreach($r as $rr) {
29                                 $k = $rr['k'];
30                                 if ($rr['cat'] === 'config') {
31                                         $a->config[$k] = $rr['v'];
32                                 } else {
33                                         $a->config[$family][$k] = $rr['v'];
34                                 }
35                         }
36                 }
37         }
38 }
39
40 /**
41  * get a particular config variable given the family name
42  * and key. Returns false if not set.
43  *
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) {
50
51                 global $a;
52
53
54                 if(isset($a->config[$family][$key])) {
55                         if($a->config[$family][$key] === '!<unset>!') {
56                                 return false;
57                         }
58                         return $a->config[$family][$key];
59                 }
60                 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
61                                 dbesc($family),
62                                 dbesc($key)
63                 );
64                 if(count($ret)) {
65                         // manage array value
66                         $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
67                         $a->config[$family][$key] = $val;
68                         return $val;
69                 }
70                 else {
71                         $a->config[$family][$key] = '!<unset>!';
72                 }
73                 return false;
74         }
75 }
76
77 /**
78  * Store a config value ($value) in the category ($family)
79  * under the key ($key)
80  * 
81  * Return the value, or false if the database update failed
82  */
83 if(! function_exists('set_config')) {
84
85 function set_config($family,$key,$value) {
86         global $a;
87         
88         // manage array value
89         $dbvalue = (is_array($value)?serialize($value):$value);
90         $dbvalue = (is_bool($value) ? intval($value) : $value);
91
92                 $a->config[$family][$key] = $value;
93                 $ret = q("REPLACE 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                 }
101                 return $ret;
102
103         }
104 }
105
106
107 if(! function_exists('load_pconfig')) {
108         function load_pconfig($uid,$family) {
109                 global $a;
110                 $r = q("SELECT * FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
111                                 dbesc($family),
112                                 intval($uid)
113                 );
114                 if(count($r)) {
115                         foreach($r as $rr) {
116                                 $k = $rr['k'];
117                                 $a->config[$uid][$family][$k] = $rr['v'];
118                         }
119                 }
120         }
121 }
122
123
124 /**\r
125  * get a particular user-specific config variable given the family name, 
126  * the user id and key. Returns false if not set.\r
127  *\r
128  * If a key is found in the DB but doesn't exist in\r
129  * local config cache, pull it into the cache so we don't have\r
130  * to hit the DB again for this item.\r
131  */
132 if(! function_exists('get_pconfig')) {
133         function get_pconfig($uid,$family, $key) {
134
135                 global $a;
136
137
138                 if(isset($a->config[$uid][$family][$key])) {
139                         if($a->config[$uid][$family][$key] === '!<unset>!') {
140                                 return false;
141                         }
142                         return $a->config[$uid][$family][$key];
143                 }
144
145
146                 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
147                                 intval($uid),
148                                 dbesc($family),
149                                 dbesc($key)
150                 );
151
152                 if(count($ret)) {
153                         $val = (preg_match("|^a:[0-9]+:{.*}$|", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
154                         $a->config[$uid][$family][$key] = $val;
155                         return $val;
156                 }
157                 else {
158                         $a->config[$uid][$family][$key] = '!<unset>!';
159                 }
160                 return false;
161         }
162 }
163
164 /**
165  * Delete a value from config. This function 
166  * deletes both: db value and cache entry. 
167  */
168 if(! function_exists('del_config')) {
169         function del_config($family,$key) {
170
171                 global $a;
172                 if(x($a->config[$family],$key))
173                         unset($a->config[$family][$key]);
174                 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
175                                 dbesc($family),
176                                 dbesc($key)
177                 );
178                 return $ret;
179         }
180 }
181
182
183 /**\r
184  * Store a user-specific config value ($value) for user $uid in the category ($family)\r
185  * under the key ($key). 
186  * \r
187  * Return the value, or false if the database update failed\r
188  */
189 if(! function_exists('set_pconfig')) {
190         function set_pconfig($uid,$family,$key,$value) {
191
192                 global $a;
193
194                 // manage array value
195                 $dbvalue = (is_array($value)?serialize($value):$value);
196                 $dbvalue = (is_bool($value)?serialize($value):$value);
197
198
199                 $a->config[$uid][$family][$key] = $value;
200                 $ret = q("REPLACE INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
201                                 intval($uid),
202                                 dbesc($family),
203                                 dbesc($key),
204                                 dbesc($dbvalue)
205                 );
206                 if($ret) {
207                         return $value;
208                 }
209                 return $ret;
210
211         }
212 }
213
214 if(! function_exists('del_pconfig')) {
215         function del_pconfig($uid,$family,$key) {
216
217                 global $a;
218                 if(x($a->config[$uid][$family],$key))
219                         unset($a->config[$uid][$family][$key]);
220                 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
221                                 intval($uid),
222                                 dbesc($family),
223                                 dbesc($key)
224                 );
225                 return $ret;
226         }
227 }