2 namespace Friendica\Core;
7 * @file include/Core/Config.php
9 * @brief Contains the class with methods for system configuration
14 * @brief Arbitrary sytem configuration storage
16 * Please do not store booleans - convert to 0/1 integer values
17 * The Config::get() functions return boolean false for keys that are unset,
18 * and this could lead to subtle bugs.
20 * There are a few places in the code (such as the admin panel) where boolean
21 * configurations need to be fixed as of 10/08/2011.
25 private static $cache;
28 * @brief Loads all configuration values of family into a cached storage.
30 * All configuration values of the system are stored in global cache
31 * which is available under the global variable $a->config
33 * @param string $family
34 * The category of the configuration value
37 public static function load($family = "config") {
39 // We don't preload "system" anymore.
40 // This reduces the number of database reads a lot.
41 if ($family === 'system') {
47 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
48 if (dbm::is_result($r)) {
51 if ($family === 'config') {
52 $a->config[$k] = $rr['v'];
54 $a->config[$family][$k] = $rr['v'];
55 self::$cache[$family][$k] = $rr['v'];
62 * @brief Get a particular user's config variable given the category name
63 * ($family) and a key.
65 * Get a particular config value from the given category ($family)
66 * and the $key from a cached storage in $a->config[$uid].
67 * $instore is only used by the set_config function
68 * to determine if the key already exists in the DB
69 * If a key is found in the DB but doesn't exist in
70 * local config cache, pull it into the cache so we don't have
71 * to hit the DB again for this item.
73 * @param string $family
74 * The category of the configuration value
76 * The configuration key to query
77 * @param mixed $default_value optional
78 * The value to return if key is not set (default: null)
79 * @param boolean $refresh optional
80 * If true the config is loaded from the db and not from the cache (default: false)
81 * @return mixed Stored value or null if it does not exist
83 public static function get($family, $key, $default_value = null, $refresh = false) {
89 // Do we have the cached value? Then return it
90 if (isset(self::$cache[$family][$key])) {
91 if (self::$cache[$family][$key] === '!<unset>!') {
92 return $default_value;
94 return self::$cache[$family][$key];
99 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
103 if (dbm::is_result($ret)) {
104 // manage array value
105 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
107 // Assign the value from the database to the cache
108 self::$cache[$family][$key] = $val;
110 } elseif (isset($a->config[$family][$key])) {
112 // Assign the value (mostly) from the .htconfig.php to the cache
113 self::$cache[$family][$key] = $a->config[$family][$key];
115 return $a->config[$family][$key];
118 self::$cache[$family][$key] = '!<unset>!';
120 return $default_value;
124 * @brief Sets a configuration value for system config
126 * Stores a config value ($value) in the category ($family) under the key ($key)
127 * for the user_id $uid.
129 * Note: Please do not store booleans - convert to 0/1 integer values!
131 * @param string $family
132 * The category of the configuration value
134 * The configuration key to set
135 * @param string $value
137 * @return mixed Stored $value or false if the database update failed
139 public static function set($family, $key, $value) {
142 // We always store boolean values as integer.
143 // And when fetching we don't convert them back to boolean.
144 // So we have to do the conversion here so that the compare below works.
145 $dbvalue = (is_bool($value) ? (string)intval($value) : $value);
147 // Convert the numeric values to string to make the compare work
148 $dbvalue = (is_numeric($value) ? (string)$value : $dbvalue);
150 $stored = self::get($family, $key);
152 if ($stored === $dbvalue) {
156 if ($family === 'config') {
157 $a->config[$key] = $dbvalue;
158 } elseif ($family != 'system') {
159 $a->config[$family][$key] = $dbvalue;
162 // Assign the just added value to the cache
163 self::$cache[$family][$key] = $dbvalue;
165 // manage array value
166 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
168 if (is_null($stored)) {
169 $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
176 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
189 * @brief Deletes the given key from the system configuration.
191 * Removes the configured value from the stored cache in $a->config
192 * and removes it from the database.
194 * @param string $family
195 * The category of the configuration value
197 * The configuration key to delete
200 public static function delete($family, $key) {
202 if (isset(self::$cache[$family][$key])) {
203 unset(self::$cache[$family][$key]);
205 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",