3 * System Configuration Class
5 * @file include/Core/Config.php
7 * @brief Contains the class with methods for system configuration
9 namespace Friendica\Core;
11 use Friendica\Database\DBM;
15 * @brief Arbitrary sytem configuration storage
18 * If we ever would decide to return exactly the variable type as entered,
19 * we will have fun with the additional features. :-)
21 * The config class always returns strings but in the default features
22 * we use a "false" to determine if the config value isn't set.
27 private static $cache;
28 private static $in_db;
31 * @brief Loads all configuration values of family into a cached storage.
33 * All configuration values of the system are stored in global cache
34 * which is available under the global variable $a->config
36 * @param string $family The category of the configuration value
40 public static function load($family = "config")
42 // We don't preload "system" anymore.
43 // This reduces the number of database reads a lot.
44 if ($family === 'system') {
50 $r = dba::select('config', array('v', 'k'), array('cat' => $family));
51 while ($rr = dba::fetch($r)) {
53 if ($family === 'config') {
54 $a->config[$k] = $rr['v'];
56 $a->config[$family][$k] = $rr['v'];
57 self::$cache[$family][$k] = $rr['v'];
58 self::$in_db[$family][$k] = true;
65 * @brief Get a particular user's config variable given the category name
66 * ($family) and a key.
68 * Get a particular config value from the given category ($family)
69 * and the $key from a cached storage in $a->config[$uid].
70 * $instore is only used by the set_config function
71 * to determine if the key already exists in the DB
72 * If a key is found in the DB but doesn't exist in
73 * local config cache, pull it into the cache so we don't have
74 * to hit the DB again for this item.
76 * @param string $family The category of the configuration value
77 * @param string $key The configuration key to query
78 * @param mixed $default_value optional, The value to return if key is not set (default: null)
79 * @param boolean $refresh optional, 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)
88 // Do we have the cached value? Then return it
89 if (isset(self::$cache[$family][$key])) {
90 if (self::$cache[$family][$key] === '!<unset>!') {
91 return $default_value;
93 return self::$cache[$family][$key];
98 $ret = dba::select('config', array('v'), array('cat' => $family, 'k' => $key), array('limit' => 1));
99 if (DBM::is_result($ret)) {
100 // manage array value
101 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
103 // Assign the value from the database to the cache
104 self::$cache[$family][$key] = $val;
105 self::$in_db[$family][$key] = true;
107 } elseif (isset($a->config[$family][$key])) {
108 // Assign the value (mostly) from the .htconfig.php to the cache
109 self::$cache[$family][$key] = $a->config[$family][$key];
110 self::$in_db[$family][$key] = false;
112 return $a->config[$family][$key];
115 self::$cache[$family][$key] = '!<unset>!';
116 self::$in_db[$family][$key] = false;
118 return $default_value;
122 * @brief Sets a configuration value for system config
124 * Stores a config value ($value) in the category ($family) under the key ($key)
125 * for the user_id $uid.
127 * Note: Please do not store booleans - convert to 0/1 integer values!
129 * @param string $family The category of the configuration value
130 * @param string $key The configuration key to set
131 * @param string $value The value to store
133 * @return mixed Stored $value or false if the database update failed
135 public static function set($family, $key, $value)
139 // We store our setting values in a string variable.
140 // So we have to do the conversion here so that the compare below works.
141 // The exception are array values.
142 $dbvalue = (!is_array($value) ? (string)$value : $value);
144 $stored = self::get($family, $key, null, true);
146 if (($stored === $dbvalue) && self::$in_db[$family][$key]) {
150 if ($family === 'config') {
151 $a->config[$key] = $dbvalue;
152 } elseif ($family != 'system') {
153 $a->config[$family][$key] = $dbvalue;
156 // Assign the just added value to the cache
157 self::$cache[$family][$key] = $dbvalue;
159 // manage array value
160 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
162 $ret = dba::update('config', array('v' => $dbvalue), array('cat' => $family, 'k' => $key), true);
165 self::$in_db[$family][$key] = true;
172 * @brief Deletes the given key from the system configuration.
174 * Removes the configured value from the stored cache in $a->config
175 * and removes it from the database.
177 * @param string $family The category of the configuration value
178 * @param string $key The configuration key to delete
182 public static function delete($family, $key)
184 if (isset(self::$cache[$family][$key])) {
185 unset(self::$cache[$family][$key]);
186 unset(self::$in_db[$family][$key]);
189 $ret = dba::delete('config', array('cat' => $family, 'k' => $key));