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 store our setting values in a string variable.
143 // So we have to do the conversion here so that the compare below works.
144 // The exception are array values.
145 $dbvalue = (!is_array($value) ? (string)$value : $value);
147 $stored = self::get($family, $key);
149 if ($stored === $dbvalue) {
153 if ($family === 'config') {
154 $a->config[$key] = $dbvalue;
155 } elseif ($family != 'system') {
156 $a->config[$family][$key] = $dbvalue;
159 // Assign the just added value to the cache
160 self::$cache[$family][$key] = $dbvalue;
162 // manage array value
163 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
165 if (is_null($stored)) {
166 $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
173 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
186 * @brief Deletes the given key from the system configuration.
188 * Removes the configured value from the stored cache in $a->config
189 * and removes it from the database.
191 * @param string $family
192 * The category of the configuration value
194 * The configuration key to delete
197 public static function delete($family, $key) {
199 if (isset(self::$cache[$family][$key])) {
200 unset(self::$cache[$family][$key]);
202 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",