3 * @file src/Core/PConfig.php
5 namespace Friendica\Core;
7 use Friendica\Database\DBM;
11 * @file include/Core/PConfig.php
12 * @brief contains the class with methods for the management
13 * of the user configuration
17 * @brief Management of user configuration storage
19 * Please do not store booleans - convert to 0/1 integer values
20 * The PConfig::get() functions return boolean false for keys that are unset,
21 * and this could lead to subtle bugs.
25 private static $in_db;
28 * @brief Loads all configuration values of a user's config family into a cached storage.
30 * All configuration values of the given user are stored in global cache
31 * which is available under the global variable $a->config[$uid].
33 * @param string $uid The user_id
34 * @param string $family The category of the configuration value
38 public static function load($uid, $family)
42 $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
43 if (DBM::is_result($r)) {
44 while ($rr = dba::fetch($r)) {
46 $a->config[$uid][$family][$k] = $rr['v'];
47 self::$in_db[$uid][$family][$k] = true;
49 } else if ($family != 'config') {
51 $a->config[$uid][$family] = "!<unset>!";
57 * @brief Get a particular user's config variable given the category name
58 * ($family) and a key.
60 * Get a particular user's config value from the given category ($family)
61 * and the $key from a cached storage in $a->config[$uid].
63 * @param string $uid The user_id
64 * @param string $family The category of the configuration value
65 * @param string $key The configuration key to query
66 * @param mixed $default_value optional, The value to return if key is not set (default: null)
67 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
69 * @return mixed Stored value or null if it does not exist
71 public static function get($uid, $family, $key, $default_value = null, $refresh = false)
76 // Looking if the whole family isn't set
77 if (isset($a->config[$uid][$family])) {
78 if ($a->config[$uid][$family] === '!<unset>!') {
79 return $default_value;
83 if (isset($a->config[$uid][$family][$key])) {
84 if ($a->config[$uid][$family][$key] === '!<unset>!') {
85 return $default_value;
87 return $a->config[$uid][$family][$key];
91 $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), array('limit' => 1));
92 if (DBM::is_result($ret)) {
93 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
94 $a->config[$uid][$family][$key] = $val;
95 self::$in_db[$uid][$family][$key] = true;
99 $a->config[$uid][$family][$key] = '!<unset>!';
100 self::$in_db[$uid][$family][$key] = false;
102 return $default_value;
107 * @brief Sets a configuration value for a user
109 * Stores a config value ($value) in the category ($family) under the key ($key)
110 * for the user_id $uid.
112 * @note Please do not store booleans - convert to 0/1 integer values!
114 * @param string $uid The user_id
115 * @param string $family The category of the configuration value
116 * @param string $key The configuration key to set
117 * @param string $value The value to store
119 * @return mixed Stored $value or false
121 public static function set($uid, $family, $key, $value)
125 // We store our setting values in a string variable.
126 // So we have to do the conversion here so that the compare below works.
127 // The exception are array values.
128 $dbvalue = (!is_array($value) ? (string)$value : $value);
130 $stored = self::get($uid, $family, $key, null, true);
132 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
136 $a->config[$uid][$family][$key] = $dbvalue;
138 // manage array value
139 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
141 dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
144 self::$in_db[$uid][$family][$key] = true;
151 * @brief Deletes the given key from the users's configuration.
153 * Removes the configured value from the stored cache in $a->config[$uid]
154 * and removes it from the database.
156 * @param string $uid The user_id
157 * @param string $family The category of the configuration value
158 * @param string $key The configuration key to delete
162 public static function delete($uid, $family, $key)
166 if (x($a->config[$uid][$family], $key)) {
167 unset($a->config[$uid][$family][$key]);
168 unset(self::$in_db[$uid][$family][$key]);
171 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));