]> git.mxchange.org Git - friendica.git/blob - include/config.php
Merge pull request #2565 from rabuzarus/0606-confic-docu
[friendica.git] / include / config.php
1 <?php
2
3 require_once("include/PConfig.php");
4
5 /**
6  * @file include/config.php
7  * 
8  *  @brief Arbitrary configuration storage
9  * Note:
10  * Please do not store booleans - convert to 0/1 integer values
11  * The get_?config() functions return boolean false for keys that are unset,
12  * and this could lead to subtle bugs.
13  *
14  * There are a few places in the code (such as the admin panel) where boolean
15  * configurations need to be fixed as of 10/08/2011.
16  */
17
18 /**
19  * @brief Loads all configuration values of family into a cached storage.
20  *
21  * All configuration values of the system are stored in global cache
22  * which is available under the global variable $a->config
23  *
24  * @param string $family
25  *  The category of the configuration value
26  * @return void
27  */
28 function load_config($family) {
29         global $a;
30
31         $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
32         if(count($r)) {
33                 foreach($r as $rr) {
34                         $k = $rr['k'];
35                         if ($family === 'config') {
36                                 $a->config[$k] = $rr['v'];
37                         } else {
38                                 $a->config[$family][$k] = $rr['v'];
39                         }
40                 }
41         } else if ($family != 'config') {
42                 // Negative caching
43                 $a->config[$family] = "!<unset>!";
44         }
45 }
46
47 /**
48  * @brief Get a particular user's config variable given the category name
49  * ($family) and a key.
50  *
51  * Get a particular config value from the given category ($family)
52  * and the $key from a cached storage in $a->config[$uid].
53  * $instore is only used by the set_config function
54  * to determine if the key already exists in the DB
55  * If a key is found in the DB but doesn't exist in
56  * local config cache, pull it into the cache so we don't have
57  * to hit the DB again for this item.
58  *
59  * @param string $family
60  *  The category of the configuration value
61  * @param string $key
62  *  The configuration key to query
63  * @param boolean $instore Determines if the key already exists in the DB
64  * @return mixed Stored value or false if it does not exist
65  */
66 function get_config($family, $key, $instore = false) {
67
68         global $a;
69
70         if(! $instore) {
71                 // Looking if the whole family isn't set
72                 if(isset($a->config[$family])) {
73                         if($a->config[$family] === '!<unset>!') {
74                                 return false;
75                         }
76                 }
77
78                 if(isset($a->config[$family][$key])) {
79                         if($a->config[$family][$key] === '!<unset>!') {
80                                 return false;
81                         }
82                         return $a->config[$family][$key];
83                 }
84         }
85
86         // If APC is enabled then fetch the data from there, else try XCache
87         /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
88                 if (apc_exists($family."|".$key)) {
89                         $val = apc_fetch($family."|".$key);
90                         $a->config[$family][$key] = $val;
91
92                         if ($val === '!<unset>!')
93                                 return false;
94                         else
95                                 return $val;
96                 }
97         elseif (function_exists("xcache_fetch") AND function_exists("xcache_isset"))
98                 if (xcache_isset($family."|".$key)) {
99                         $val = xcache_fetch($family."|".$key);
100                         $a->config[$family][$key] = $val;
101
102                         if ($val === '!<unset>!')
103                                 return false;
104                         else
105                                 return $val;
106                 }
107         */
108
109         $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
110                 dbesc($family),
111                 dbesc($key)
112         );
113         if(count($ret)) {
114                 // manage array value
115                 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
116                 $a->config[$family][$key] = $val;
117
118                 // If APC is enabled then store the data there, else try XCache
119                 /*if (function_exists("apc_store"))
120                         apc_store($family."|".$key, $val, 600);
121                 elseif (function_exists("xcache_set"))
122                         xcache_set($family."|".$key, $val, 600);*/
123
124                 return $val;
125         }
126         else {
127                 $a->config[$family][$key] = '!<unset>!';
128
129                 // If APC is enabled then store the data there, else try XCache
130                 /*if (function_exists("apc_store"))
131                         apc_store($family."|".$key, '!<unset>!', 600);
132                 elseif (function_exists("xcache_set"))
133                         xcache_set($family."|".$key, '!<unset>!', 600);*/
134         }
135         return false;
136 }
137
138 /**
139  * @brief Sets a configuration value for system config
140  *
141  * Stores a config value ($value) in the category ($family) under the key ($key)
142  * for the user_id $uid.
143  *
144  * Note: Please do not store booleans - convert to 0/1 integer values!
145  *
146  * @param string $family
147  *  The category of the configuration value
148  * @param string $key
149  *  The configuration key to set
150  * @param string $value
151  *  The value to store
152  * @return mixed Stored $value or false if the database update failed
153  */
154 function set_config($family,$key,$value) {
155         global $a;
156
157         // If $a->config[$family] has been previously set to '!<unset>!', then
158         // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
159         // $a->config[$family][$key] = $value will be equivalent to
160         // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
161         // so unset the family before assigning a value to a family's key
162         if($a->config[$family] === '!<unset>!')
163                 unset($a->config[$family]);
164
165         // manage array value
166         $dbvalue = (is_array($value)?serialize($value):$value);
167         $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
168         if(get_config($family,$key,true) === false) {
169                 $a->config[$family][$key] = $value;
170                 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
171                         dbesc($family),
172                         dbesc($key),
173                         dbesc($dbvalue)
174                 );
175                 if($ret)
176                         return $value;
177                 return $ret;
178         }
179
180         $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
181                 dbesc($dbvalue),
182                 dbesc($family),
183                 dbesc($key)
184         );
185
186         $a->config[$family][$key] = $value;
187
188         // If APC is enabled then store the data there, else try XCache
189         /*if (function_exists("apc_store"))
190                 apc_store($family."|".$key, $value, 600);
191         elseif (function_exists("xcache_set"))
192                 xcache_set($family."|".$key, $value, 600);*/
193
194         if($ret)
195                 return $value;
196         return $ret;
197 }
198
199 /**
200  * @brief Deletes the given key from the system configuration.
201  *
202  * Removes the configured value from the stored cache in $a->config
203  * and removes it from the database.
204  *
205  * @param string $family
206  *  The category of the configuration value
207  * @param string $key
208  *  The configuration key to delete
209  * @return mixed
210  */
211 function del_config($family,$key) {
212
213         global $a;
214         if(x($a->config[$family],$key))
215                 unset($a->config[$family][$key]);
216         $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
217                 dbesc($family),
218                 dbesc($key)
219         );
220         // If APC is enabled then delete the data from there, else try XCache
221         /*if (function_exists("apc_delete"))
222                 apc_delete($family."|".$key);
223         elseif (function_exists("xcache_unset"))
224                 xcache_unset($family."|".$key);*/
225
226         return $ret;
227 }
228
229 /**
230  * @brief (Deprecated) Loads all configuration values of a user's config family into a cached storage.
231  *
232  * Note: This function is deprecated. Use PConfig::load() instead.
233  *
234  * @param string $uid
235  *  The user_id
236  * @param string $family
237  *  The category of the configuration value
238  * @return void
239  */
240 function load_pconfig($uid,$family) {
241         return PConfig::load($uid, $family);
242 }
243
244 /**
245  * @brief (Deprecated) Get a particular user's config variable given the category name
246  * ($family) and a key.
247  *
248  * Note: This function is deprecated. Use PConfig::get() instead.
249  *
250  * @param string $uid
251  *  The user_id
252  * @param string $family
253  *  The category of the configuration value
254  * @param string $key
255  *  The configuration key to query
256  * @param boolean $instore
257  * Determines if the key already exists in the DB
258  * @return mixed Stored value or false if it does not exist
259  */
260 function get_pconfig($uid,$family, $key, $instore = false) {
261         return PConfig::get($uid, $family, $key, $instore);
262 }
263
264 /**
265  * @brief (Deprecated) Sets a configuration value for a user
266  *
267  * Note: This function is deprecated. Use PConfig::set() instead.
268  *
269  * @param string $uid
270  *  The user_id
271  * @param string $family
272  *  The category of the configuration value
273  * @param string $key
274  *  The configuration key to set
275  * @param string $value
276  *  The value to store
277  * @return mixed Stored $value or false
278  */
279 function set_pconfig($uid,$family,$key,$value) {
280         return PConfig::set($uid, $family, $key, $value);
281 }
282
283 /**
284  * @brief (Deprecated) Deletes the given key from the users's configuration.
285  *
286  * Note: This function is deprecated. Use PConfig::delete() instead.
287  *
288  * @param string $uid The user_id
289  * @param string $family
290  *  The category of the configuration value
291  * @param string $key
292  *  The configuration key to delete
293  * @return mixed
294  */
295 function del_pconfig($uid,$family,$key) {
296         return PConfig::delete($uid, $family, $key);
297 }