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 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
71 public static function get($family, $key, $refresh = false) {
76 // Looking if the whole family isn't set
77 if(isset($a->config[$family])) {
78 if($a->config[$family] === '!<unset>!') {
83 if(isset($a->config[$family][$key])) {
84 if($a->config[$family][$key] === '!<unset>!') {
87 return $a->config[$family][$key];
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;
97 if ($val === '!<unset>!')
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;
107 if ($val === '!<unset>!')
114 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
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;
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);*/
132 $a->config[$family][$key] = '!<unset>!';
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);*/
144 * @brief Sets a configuration value for system config
146 * Stores a config value ($value) in the category ($family) under the key ($key)
147 * for the user_id $uid.
149 * Note: Please do not store booleans - convert to 0/1 integer values!
151 * @param string $family
152 * The category of the configuration value
154 * The configuration key to set
155 * @param string $value
157 * @return mixed Stored $value or false if the database update failed
159 public static function set($family,$key,$value) {
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]);
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' ) ",
185 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
191 $a->config[$family][$key] = $value;
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);*/
205 * @brief Deletes the given key from the system configuration.
207 * Removes the configured value from the stored cache in $a->config
208 * and removes it from the database.
210 * @param string $family
211 * The category of the configuration value
213 * The configuration key to delete
216 public static function delete($family,$key) {
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'",
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);*/