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;
105 $a->config[$family][$key] = '!<unset>!';
107 return $default_value;
111 * @brief Sets a configuration value for system config
113 * Stores a config value ($value) in the category ($family) under the key ($key)
114 * for the user_id $uid.
116 * Note: Please do not store booleans - convert to 0/1 integer values!
118 * @param string $family
119 * The category of the configuration value
121 * The configuration key to set
122 * @param string $value
124 * @return mixed Stored $value or false if the database update failed
126 public static function set($family, $key, $value) {
129 $a->config[$family][$key] = $value;
131 // manage array value
132 $dbvalue = is_array($value) ? serialize($value) : $value;
133 $dbvalue = is_bool($dbvalue) ? intval($dbvalue) : $dbvalue;
135 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' )
136 ON DUPLICATE KEY UPDATE `v` = '%s'",
149 * @brief Deletes the given key from the system configuration.
151 * Removes the configured value from the stored cache in $a->config
152 * and removes it from the database.
154 * @param string $family
155 * The category of the configuration value
157 * The configuration key to delete
160 public static function delete($family,$key) {
163 if(x($a->config[$family],$key))
164 unset($a->config[$family][$key]);
165 $ret = q("DELETE FROM `config` WHERE `cat` = '%s' AND `k` = '%s'",