2 namespace Friendica\Core;
4 * @file include/Core/Config.php
6 * @brief Contains the class with methods for system configuration
11 * @brief Arbitrary sytem configuration storage
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.
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.
23 * @brief Loads all configuration values of family into a cached storage.
25 * All configuration values of the system are stored in global cache
26 * which is available under the global variable $a->config
28 * @param string $family
29 * The category of the configuration value
32 public static function load($family) {
35 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
39 if ($family === 'config') {
40 $a->config[$k] = $rr['v'];
42 $a->config[$family][$k] = $rr['v'];
45 } else if ($family != 'config') {
47 $a->config[$family] = "!<unset>!";
52 * @brief Get a particular user's config variable given the category name
53 * ($family) and a key.
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.
63 * @param string $family
64 * The category of the configuration value
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
73 public static function get($family, $key, $default_value=null, $refresh = false) {
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;
85 if(isset($a->config[$family][$key])) {
86 if($a->config[$family][$key] === '!<unset>!') {
87 return $default_value;
89 return $a->config[$family][$key];
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;
99 if ($val === '!<unset>!')
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;
109 if ($val === '!<unset>!')
116 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
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;
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);*/
134 $a->config[$family][$key] = '!<unset>!';
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);*/
142 return $default_value;
146 * @brief Sets a configuration value for system config
148 * Stores a config value ($value) in the category ($family) under the key ($key)
149 * for the user_id $uid.
151 * Note: Please do not store booleans - convert to 0/1 integer values!
153 * @param string $family
154 * The category of the configuration value
156 * The configuration key to set
157 * @param string $value
159 * @return mixed Stored $value or false if the database update failed
161 public static function set($family,$key,$value) {
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]);
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' ) ",
187 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
193 $a->config[$family][$key] = $value;
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);*/
207 * @brief Deletes the given key from the system configuration.
209 * Removes the configured value from the stored cache in $a->config
210 * and removes it from the database.
212 * @param string $family
213 * The category of the configuration value
215 * The configuration key to delete
218 public static function delete($family,$key) {
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'",
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);*/