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.
26 * @brief Loads all configuration values of family into a cached storage.
28 * All configuration values of the system are stored in global cache
29 * which is available under the global variable $a->config
31 * @param string $family
32 * The category of the configuration value
35 public static function load($family) {
38 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family));
39 if (dbm::is_result($r)) {
42 if ($family === 'config') {
43 $a->config[$k] = $rr['v'];
45 $a->config[$family][$k] = $rr['v'];
48 } else if ($family != 'config') {
50 $a->config[$family] = "!<unset>!";
55 * @brief Get a particular user's config variable given the category name
56 * ($family) and a key.
58 * Get a particular config value from the given category ($family)
59 * and the $key from a cached storage in $a->config[$uid].
60 * $instore is only used by the set_config function
61 * to determine if the key already exists in the DB
62 * If a key is found in the DB but doesn't exist in
63 * local config cache, pull it into the cache so we don't have
64 * to hit the DB again for this item.
66 * @param string $family
67 * The category of the configuration value
69 * The configuration key to query
70 * @param mixed $default_value optional
71 * The value to return if key is not set (default: null)
72 * @param boolean $refresh optional
73 * If true the config is loaded from the db and not from the cache (default: false)
74 * @return mixed Stored value or null if it does not exist
76 public static function get($family, $key, $default_value = null, $refresh = false) {
81 // Looking if the whole family isn't set
82 if (isset($a->config[$family])) {
83 if ($a->config[$family] === '!<unset>!') {
84 return $default_value;
88 if (isset($a->config[$family][$key])) {
89 if ($a->config[$family][$key] === '!<unset>!') {
90 return $default_value;
92 return $a->config[$family][$key];
96 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
100 if (dbm::is_result($ret)) {
101 // manage array value
102 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
103 $a->config[$family][$key] = $val;
107 $a->config[$family][$key] = '!<unset>!';
109 return $default_value;
113 * @brief Sets a configuration value for system config
115 * Stores a config value ($value) in the category ($family) under the key ($key)
116 * for the user_id $uid.
118 * Note: Please do not store booleans - convert to 0/1 integer values!
120 * @param string $family
121 * The category of the configuration value
123 * The configuration key to set
124 * @param string $value
126 * @return mixed Stored $value or false if the database update failed
128 public static function set($family, $key, $value) {
131 $stored = self::get($family, $key);
133 if ($stored == $value) {
137 $a->config[$family][$key] = $value;
139 // manage array value
140 $dbvalue = (is_array($value) ? serialize($value) : $value);
141 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
143 if (is_null($stored)) {
144 $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
151 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
164 * @brief Deletes the given key from the system configuration.
166 * Removes the configured value from the stored cache in $a->config
167 * and removes it from the database.
169 * @param string $family
170 * The category of the configuration value
172 * The configuration key to delete
175 public static function delete($family, $key) {
178 if (x($a->config[$family],$key)) {
179 unset($a->config[$family][$key]);
181 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",