3 * @file src/Core/PConfig.php
5 namespace Friendica\Core;
7 use Friendica\Database\DBM;
10 require_once 'include/dba.php';
13 * @file include/Core/PConfig.php
14 * @brief contains the class with methods for the management
15 * of the user configuration
19 * @brief Management of user configuration storage
21 * Please do not store booleans - convert to 0/1 integer values
22 * The PConfig::get() functions return boolean false for keys that are unset,
23 * and this could lead to subtle bugs.
27 private static $in_db;
30 * @brief Loads all configuration values of a user's config family into a cached storage.
32 * All configuration values of the given user are stored in global cache
33 * which is available under the global variable $a->config[$uid].
35 * @param string $uid The user_id
36 * @param string $family The category of the configuration value
40 public static function load($uid, $family)
44 $r = dba::select('pconfig', ['v', 'k'], ['cat' => $family, 'uid' => $uid]);
45 if (DBM::is_result($r)) {
46 while ($rr = dba::fetch($r)) {
48 $a->config[$uid][$family][$k] = $rr['v'];
49 self::$in_db[$uid][$family][$k] = true;
51 } else if ($family != 'config') {
53 $a->config[$uid][$family] = "!<unset>!";
59 * @brief Get a particular user's config variable given the category name
60 * ($family) and a key.
62 * Get a particular user's config value from the given category ($family)
63 * and the $key from a cached storage in $a->config[$uid].
65 * @param string $uid The user_id
66 * @param string $family The category of the configuration value
67 * @param string $key The configuration key to query
68 * @param mixed $default_value optional, The value to return if key is not set (default: null)
69 * @param boolean $refresh optional, 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($uid, $family, $key, $default_value = null, $refresh = false)
78 // Looking if the whole family isn't set
79 if (isset($a->config[$uid][$family])) {
80 if ($a->config[$uid][$family] === '!<unset>!') {
81 return $default_value;
85 if (isset($a->config[$uid][$family][$key])) {
86 if ($a->config[$uid][$family][$key] === '!<unset>!') {
87 return $default_value;
89 return $a->config[$uid][$family][$key];
93 $pconfig = dba::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $family, 'k' => $key]);
94 if (DBM::is_result($pconfig)) {
95 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $pconfig['v']) ? unserialize($pconfig['v']) : $pconfig['v']);
96 $a->config[$uid][$family][$key] = $val;
97 self::$in_db[$uid][$family][$key] = true;
101 $a->config[$uid][$family][$key] = '!<unset>!';
102 self::$in_db[$uid][$family][$key] = false;
104 return $default_value;
109 * @brief Sets a configuration value for a user
111 * Stores a config value ($value) in the category ($family) under the key ($key)
112 * for the user_id $uid.
114 * @note Please do not store booleans - convert to 0/1 integer values!
116 * @param string $uid The user_id
117 * @param string $family The category of the configuration value
118 * @param string $key The configuration key to set
119 * @param string $value The value to store
121 * @return mixed Stored $value or false
123 public static function set($uid, $family, $key, $value)
127 // We store our setting values in a string variable.
128 // So we have to do the conversion here so that the compare below works.
129 // The exception are array values.
130 $dbvalue = (!is_array($value) ? (string)$value : $value);
132 $stored = self::get($uid, $family, $key, null, true);
134 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
138 $a->config[$uid][$family][$key] = $dbvalue;
140 // manage array value
141 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
143 $ret = dba::update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $family, 'k' => $key], true);
146 self::$in_db[$uid][$family][$key] = true;
153 * @brief Deletes the given key from the users's configuration.
155 * Removes the configured value from the stored cache in $a->config[$uid]
156 * and removes it from the database.
158 * @param string $uid The user_id
159 * @param string $family The category of the configuration value
160 * @param string $key The configuration key to delete
164 public static function delete($uid, $family, $key)
168 if (x($a->config[$uid][$family], $key)) {
169 unset($a->config[$uid][$family][$key]);
170 unset(self::$in_db[$uid][$family][$key]);
173 $ret = dba::delete('pconfig', ['uid' => $uid, 'cat' => $family, 'k' => $key]);