2 namespace Friendica\Core;
4 * @file include/Core/Config.php
6 * @brief Contains the class with methods for system configuration
11 * @brief Arbitrary sytem configuration storage
13 * Please do not store booleans - convert to 0/1 integer values
14 * The Config::get() functions return boolean false for keys that are unset,
15 * and this could lead to subtle bugs.
17 * There are a few places in the code (such as the admin panel) where boolean
18 * configurations need to be fixed as of 10/08/2011.
23 * @brief Loads all configuration values of family into a cached storage.
25 * All configuration values of the system are stored in global cache
26 * which is available under the global variable $a->config
28 * @param string $family
29 * The category of the configuration value
32 public static function load($family) {
35 $r = q("SELECT `v`, `k` FROM `config` WHERE `cat` = '%s' ORDER BY `cat`, `k`, `id`", dbesc($family));
39 if ($family === 'config') {
40 $a->config[$k] = $rr['v'];
42 $a->config[$family][$k] = $rr['v'];
45 } else if ($family != 'config') {
47 $a->config[$family] = "!<unset>!";
52 * @brief Get a particular user's config variable given the category name
53 * ($family) and a key.
55 * Get a particular config value from the given category ($family)
56 * and the $key from a cached storage in $a->config[$uid].
57 * $instore is only used by the set_config function
58 * to determine if the key already exists in the DB
59 * If a key is found in the DB but doesn't exist in
60 * local config cache, pull it into the cache so we don't have
61 * to hit the DB again for this item.
63 * @param string $family
64 * The category of the configuration value
66 * The configuration key to query
67 * @param mixed $default_value optional
68 * The value to return if key is not set (default: null)
69 * @param boolean $refresh optional
70 * If true the config is loaded from the db and not from the cache (default: false)
71 * @return mixed Stored value or null if it does not exist
73 public static function get($family, $key, $default_value = null, $refresh = false) {
78 // Looking if the whole family isn't set
79 if (isset($a->config[$family])) {
80 if ($a->config[$family] === '!<unset>!') {
81 return $default_value;
85 if (isset($a->config[$family][$key])) {
86 if ($a->config[$family][$key] === '!<unset>!') {
87 return $default_value;
89 return $a->config[$family][$key];
93 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
99 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
100 $a->config[$family][$key] = $val;
104 $a->config[$family][$key] = '!<unset>!';
106 return $default_value;
110 * @brief Sets a configuration value for system config
112 * Stores a config value ($value) in the category ($family) under the key ($key)
113 * for the user_id $uid.
115 * Note: Please do not store booleans - convert to 0/1 integer values!
117 * @param string $family
118 * The category of the configuration value
120 * The configuration key to set
121 * @param string $value
123 * @return mixed Stored $value or false if the database update failed
125 public static function set($family, $key, $value) {
128 $stored = self::get($family, $key);
130 if ($stored == $value) {
134 $a->config[$family][$key] = $value;
136 // manage array value
137 $dbvalue = (is_array($value) ? serialize($value):$value);
138 $dbvalue = (is_bool($dbvalue) ? intval($dbvalue) : $dbvalue);
140 if (is_null($stored)) {
141 $ret = q("INSERT INTO `config` (`cat`, `k`, `v`) VALUES ('%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
148 $ret = q("UPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s'",
161 * @brief Deletes the given key from the system configuration.
163 * Removes the configured value from the stored cache in $a->config
164 * and removes it from the database.
166 * @param string $family
167 * The category of the configuration value
169 * The configuration key to delete
172 public static function delete($family, $key) {
175 if (x($a->config[$family],$key)) {
176 unset($a->config[$family][$key]);
178 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",