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