]> git.mxchange.org Git - friendica.git/blob - include/Core/Config.php
bbae746b44dae2f3fffa457aab61988e819fc802
[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 boolean $refresh
68          *  If true the config is loaded from the db and not from the cache
69          * @return mixed Stored value or null if it does not exist
70          */
71         public static function get($family, $key, $refresh = false) {
72
73                 global $a;
74
75                 if(! $instore) {
76                         // Looking if the whole family isn't set
77                         if(isset($a->config[$family])) {
78                                 if($a->config[$family] === '!<unset>!') {
79                                         return null;
80                                 }
81                         }
82
83                         if(isset($a->config[$family][$key])) {
84                                 if($a->config[$family][$key] === '!<unset>!') {
85                                         return null;
86                                 }
87                                 return $a->config[$family][$key];
88                         }
89                 }
90
91                 // If APC is enabled then fetch the data from there, else try XCache
92                 /*if (function_exists("apc_fetch") AND function_exists("apc_exists"))
93                         if (apc_exists($family."|".$key)) {
94                                 $val = apc_fetch($family."|".$key);
95                                 $a->config[$family][$key] = $val;
96
97                                 if ($val === '!<unset>!')
98                                         return false;
99                                 else
100                                         return $val;
101                         }
102                 elseif (function_exists("xcache_fetch") AND function_exists("xcache_isset"))
103                         if (xcache_isset($family."|".$key)) {
104                                 $val = xcache_fetch($family."|".$key);
105                                 $a->config[$family][$key] = $val;
106
107                                 if ($val === '!<unset>!')
108                                         return false;
109                                 else
110                                         return $val;
111                         }
112                 */
113
114                 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
115                         dbesc($family),
116                         dbesc($key)
117                 );
118                 if(count($ret)) {
119                         // manage array value
120                         $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
121                         $a->config[$family][$key] = $val;
122
123                         // If APC is enabled then store the data there, else try XCache
124                         /*if (function_exists("apc_store"))
125                                 apc_store($family."|".$key, $val, 600);
126                         elseif (function_exists("xcache_set"))
127                                 xcache_set($family."|".$key, $val, 600);*/
128
129                         return $val;
130                 }
131                 else {
132                         $a->config[$family][$key] = '!<unset>!';
133
134                         // If APC is enabled then store the data there, else try XCache
135                         /*if (function_exists("apc_store"))
136                                 apc_store($family."|".$key, '!<unset>!', 600);
137                         elseif (function_exists("xcache_set"))
138                                 xcache_set($family."|".$key, '!<unset>!', 600);*/
139                 }
140                 return null;
141         }
142
143         /**
144          * @brief Sets a configuration value for system config
145          *
146          * Stores a config value ($value) in the category ($family) under the key ($key)
147          * for the user_id $uid.
148          *
149          * Note: Please do not store booleans - convert to 0/1 integer values!
150          *
151          * @param string $family
152          *  The category of the configuration value
153          * @param string $key
154          *  The configuration key to set
155          * @param string $value
156          *  The value to store
157          * @return mixed Stored $value or false if the database update failed
158          */
159         public static function set($family,$key,$value) {
160                 global $a;
161
162                 // If $a->config[$family] has been previously set to '!<unset>!', then
163                 // $a->config[$family][$key] will evaluate to $a->config[$family][0], and
164                 // $a->config[$family][$key] = $value will be equivalent to
165                 // $a->config[$family][0] = $value[0] (this causes infuriating bugs),
166                 // so unset the family before assigning a value to a family's key
167                 if($a->config[$family] === '!<unset>!')
168                         unset($a->config[$family]);
169
170                 // manage array value
171                 $dbvalue = (is_array($value)?serialize($value):$value);
172                 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
173                 if(self::get($family,$key,true) === false) {
174                         $a->config[$family][$key] = $value;
175                         $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
176                                 dbesc($family),
177                                 dbesc($key),
178                                 dbesc($dbvalue)
179                         );
180                         if($ret)
181                                 return $value;
182                         return $ret;
183                 }
184
185                 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
186                         dbesc($dbvalue),
187                         dbesc($family),
188                         dbesc($key)
189                 );
190
191                 $a->config[$family][$key] = $value;
192
193                 // If APC is enabled then store the data there, else try XCache
194                 /*if (function_exists("apc_store"))
195                         apc_store($family."|".$key, $value, 600);
196                 elseif (function_exists("xcache_set"))
197                         xcache_set($family."|".$key, $value, 600);*/
198
199                 if($ret)
200                         return $value;
201                 return $ret;
202         }
203
204         /**
205          * @brief Deletes the given key from the system configuration.
206          *
207          * Removes the configured value from the stored cache in $a->config
208          * and removes it from the database.
209          *
210          * @param string $family
211          *  The category of the configuration value
212          * @param string $key
213          *  The configuration key to delete
214          * @return mixed
215          */
216         public static function delete($family,$key) {
217
218                 global $a;
219                 if(x($a->config[$family],$key))
220                         unset($a->config[$family][$key]);
221                 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
222                         dbesc($family),
223                         dbesc($key)
224                 );
225                 // If APC is enabled then delete the data from there, else try XCache
226                 /*if (function_exists("apc_delete"))
227                         apc_delete($family."|".$key);
228                 elseif (function_exists("xcache_unset"))
229                         xcache_unset($family."|".$key);*/
230
231                 return $ret;
232         }
233
234 }