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;
26 private static $in_db;
29 * @brief Loads all configuration values of family into a cached storage.
31 * All configuration values of the system are stored in global cache
32 * which is available under the global variable $a->config
34 * @param string $family
35 * The category of the configuration value
38 public static function load($family = "config") {
40 // We don't preload "system" anymore.
41 // This reduces the number of database reads a lot.
42 if ($family === 'system') {
48 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s'", dbesc($family));
49 if (dbm::is_result($r)) {
52 if ($family === 'config') {
53 $a->config[$k] = $rr['v'];
55 $a->config[$family][$k] = $rr['v'];
56 self::$cache[$family][$k] = $rr['v'];
57 self::$in_db[$family][$k] = true;
64 * @brief Get a particular user's config variable given the category name
65 * ($family) and a key.
67 * Get a particular config value from the given category ($family)
68 * and the $key from a cached storage in $a->config[$uid].
69 * $instore is only used by the set_config function
70 * to determine if the key already exists in the DB
71 * If a key is found in the DB but doesn't exist in
72 * local config cache, pull it into the cache so we don't have
73 * to hit the DB again for this item.
75 * @param string $family
76 * The category of the configuration value
78 * The configuration key to query
79 * @param mixed $default_value optional
80 * The value to return if key is not set (default: null)
81 * @param boolean $refresh optional
82 * If true the config is loaded from the db and not from the cache (default: false)
83 * @return mixed Stored value or null if it does not exist
85 public static function get($family, $key, $default_value = null, $refresh = false) {
91 // Do we have the cached value? Then return it
92 if (isset(self::$cache[$family][$key])) {
93 if (self::$cache[$family][$key] === '!<unset>!') {
94 return $default_value;
96 return self::$cache[$family][$key];
101 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",
105 if (dbm::is_result($ret)) {
106 // manage array value
107 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v']) ? unserialize($ret[0]['v']) : $ret[0]['v']);
109 // Assign the value from the database to the cache
110 self::$cache[$family][$key] = $val;
111 self::$in_db[$family][$key] = true;
113 } elseif (isset($a->config[$family][$key])) {
115 // Assign the value (mostly) from the .htconfig.php to the cache
116 self::$cache[$family][$key] = $a->config[$family][$key];
117 self::$in_db[$family][$key] = false;
119 return $a->config[$family][$key];
122 self::$cache[$family][$key] = '!<unset>!';
123 self::$in_db[$family][$key] = false;
125 return $default_value;
129 * @brief Sets a configuration value for system config
131 * Stores a config value ($value) in the category ($family) under the key ($key)
132 * for the user_id $uid.
134 * Note: Please do not store booleans - convert to 0/1 integer values!
136 * @param string $family
137 * The category of the configuration value
139 * The configuration key to set
140 * @param string $value
142 * @return mixed Stored $value or false if the database update failed
144 public static function set($family, $key, $value) {
147 // We store our setting values in a string variable.
148 // So we have to do the conversion here so that the compare below works.
149 // The exception are array values.
150 $dbvalue = (!is_array($value) ? (string)$value : $value);
152 $stored = self::get($family, $key, null, true);
154 if (($stored === $dbvalue) AND self::$in_db[$family][$key]) {
158 if ($family === 'config') {
159 $a->config[$key] = $dbvalue;
160 } elseif ($family != 'system') {
161 $a->config[$family][$key] = $dbvalue;
164 // Assign the just added value to the cache
165 self::$cache[$family][$key] = $dbvalue;
167 // manage array value
168 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
170 if (is_null($stored) OR !self::$in_db[$family][$key]) {
171 $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
178 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
185 self::$in_db[$family][$key] = true;
192 * @brief Deletes the given key from the system configuration.
194 * Removes the configured value from the stored cache in $a->config
195 * and removes it from the database.
197 * @param string $family
198 * The category of the configuration value
200 * The configuration key to delete
203 public static function delete($family, $key) {
205 if (isset(self::$cache[$family][$key])) {
206 unset(self::$cache[$family][$key]);
207 unset(self::$in_db[$family][$key]);
209 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",