3 * System Configuration Class
5 * @file include/Core/Config.php
7 * @brief Contains the class with methods for system configuration
9 namespace Friendica\Core;
12 use Friendica\BaseObject;
15 * @brief Arbitrary system 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 class Config extends BaseObject
23 public static $config = [];
26 * @var \Friendica\Core\Config\IConfigAdapter
28 private static $adapter = null;
30 public static function init()
32 // Database isn't ready or populated yet
33 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
37 if (self::getConfigValue('system', 'config_adapter') == 'preload') {
38 self::$adapter = new Config\PreloadConfigAdapter();
40 self::$adapter = new Config\JITConfigAdapter();
45 * @brief Loads all configuration values of family into a cached storage.
47 * All configuration values of the system are stored in global cache
48 * which is available under the global variable $a->config
50 * @param string $family The category of the configuration value
53 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
55 public static function load($family = "config")
57 // Database isn't ready or populated yet
58 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
62 if (empty(self::$adapter)) {
66 self::$adapter->load($family);
70 * @brief Get a particular user's config variable given the category name
71 * ($family) and a key.
73 * Get a particular config value from the given category ($family)
74 * and the $key from a cached storage in static::config[$uid].
75 * $instore is only used by the set_config function
76 * to determine if the key already exists in the DB
77 * If a key is found in the DB but doesn't exist in
78 * local config cache, pull it into the cache so we don't have
79 * to hit the DB again for this item.
81 * @param string $family The category of the configuration value
82 * @param string $key The configuration key to query
83 * @param mixed $default_value optional, The value to return if key is not set (default: null)
84 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
86 * @return mixed Stored value or null if it does not exist
87 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
89 public static function get($family, $key, $default_value = null, $refresh = false)
91 // Database isn't ready or populated yet, fallback to file config
92 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
93 return self::getConfigValue($family, $key, $default_value);
96 if (empty(self::$adapter)) {
100 return self::$adapter->get($family, $key, $default_value, $refresh);
104 * @brief Sets a configuration value for system config
106 * Stores a config value ($value) in the category ($family) under the key ($key)
107 * for the user_id $uid.
109 * Note: Please do not store booleans - convert to 0/1 integer values!
111 * @param string $family The category of the configuration value
112 * @param string $key The configuration key to set
113 * @param mixed $value The value to store
115 * @return bool Operation success
116 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
118 public static function set($family, $key, $value)
120 // Database isn't ready or populated yet
121 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
125 if (empty(self::$adapter)) {
129 return self::$adapter->set($family, $key, $value);
133 * @brief Deletes the given key from the system configuration.
135 * Removes the configured value from the stored cache in $a->config
136 * and removes it from the database.
138 * @param string $family The category of the configuration value
139 * @param string $key The configuration key to delete
142 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144 public static function delete($family, $key)
146 // Database isn't ready or populated yet
147 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
151 if (empty(self::$adapter)) {
155 return self::$adapter->delete($family, $key);
159 * Tries to load the specified configuration array into the App->config array.
160 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
162 * @param array $config
163 * @param bool $overwrite Force value overwrite if the config key already exists
165 public function loadConfigArray(array $config, $overwrite = false)
167 foreach ($config as $category => $values) {
168 foreach ($values as $key => $value) {
170 self::setConfigValue($category, $key, $value);
172 self::setDefaultConfigValue($category, $key, $value);
179 * @param string $cat Config category
180 * @param string $k Config key
181 * @param mixed $default Default value if it isn't set
183 * @return string Returns the value of the Config entry
185 public static function getConfigValue($cat, $k = null, $default = null)
189 if ($cat === 'config') {
190 if (isset(self::$config[$k])) {
191 $return = self::$config[$k];
194 if (isset(self::$config[$cat][$k])) {
195 $return = self::$config[$cat][$k];
196 } elseif ($k == null && isset(self::$config[$cat])) {
197 $return = self::$config[$cat];
205 * Sets a default value in the config cache. Ignores already existing keys.
207 * @param string $cat Config category
208 * @param string $k Config key
209 * @param mixed $v Default value to set
211 private static function setDefaultConfigValue($cat, $k, $v)
213 if (!isset(self::$config[$cat][$k])) {
214 self::setConfigValue($cat, $k, $v);
219 * Sets a value in the config cache. Accepts raw output from the config table
221 * @param string $cat Config category
222 * @param string $k Config key
223 * @param mixed $v Value to set
225 public static function setConfigValue($cat, $k, $v)
227 // Only arrays are serialized in database, so we have to unserialize sparingly
228 $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
230 if ($cat === 'config') {
231 self::$config[$k] = $value;
233 if (!isset(self::$config[$cat])) {
234 self::$config[$cat] = [];
237 self::$config[$cat][$k] = $value;
242 * Deletes a value from the config cache
244 * @param string $cat Config category
245 * @param string $k Config key
247 public static function deleteConfigValue($cat, $k)
249 if ($cat === 'config') {
250 if (isset(self::$config[$k])) {
251 unset(self::$config[$k]);
254 if (isset(self::$config[$cat][$k])) {
255 unset(self::$config[$cat][$k]);
261 * Returns the whole configuration
263 * @return array The configuration
265 public static function getAll()
267 return self::$config;