2 namespace Friendica\Core;
8 * @file include/Core/PConfig.php
9 * @brief contains the class with methods for the management
10 * of the user configuration
14 * @brief Management of user configuration storage
16 * Please do not store booleans - convert to 0/1 integer values
17 * The PConfig::get() functions return boolean false for keys that are unset,
18 * and this could lead to subtle bugs.
22 private static $in_db;
25 * @brief Loads all configuration values of a user's config family into a cached storage.
27 * All configuration values of the given user are stored in global cache
28 * which is available under the global variable $a->config[$uid].
32 * @param string $family
33 * The category of the configuration value
36 public static function load($uid, $family) {
39 $r = dba::select('pconfig', array('v', 'k'), array('cat' => $family, 'uid' => $uid));
40 if (dbm::is_result($r)) {
41 while ($rr = dba::fetch($r)) {
43 $a->config[$uid][$family][$k] = $rr['v'];
44 self::$in_db[$uid][$family][$k] = true;
47 } else if ($family != 'config') {
49 $a->config[$uid][$family] = "!<unset>!";
54 * @brief Get a particular user's config variable given the category name
55 * ($family) and a key.
57 * Get a particular user's config value from the given category ($family)
58 * and the $key from a cached storage in $a->config[$uid].
62 * @param string $family
63 * The category of the configuration value
65 * The configuration key to query
66 * @param mixed $default_value optional
67 * The value to return if key is not set (default: null)
68 * @param boolean $refresh optional
69 * If true the config is loaded from the db and not from the cache (default: false)
70 * @return mixed Stored value or null if it does not exist
72 public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
77 // Looking if the whole family isn't set
78 if (isset($a->config[$uid][$family])) {
79 if ($a->config[$uid][$family] === '!<unset>!') {
80 return $default_value;
84 if (isset($a->config[$uid][$family][$key])) {
85 if ($a->config[$uid][$family][$key] === '!<unset>!') {
86 return $default_value;
88 return $a->config[$uid][$family][$key];
92 $ret = dba::select('pconfig', array('v'), array('uid' => $uid, 'cat' => $family, 'k' => $key), array('limit' => 1));
93 if (dbm::is_result($ret)) {
94 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret['v']) ? unserialize($ret['v']) : $ret['v']);
95 $a->config[$uid][$family][$key] = $val;
96 self::$in_db[$uid][$family][$key] = true;
100 $a->config[$uid][$family][$key] = '!<unset>!';
101 self::$in_db[$uid][$family][$key] = false;
103 return $default_value;
108 * @brief Sets a configuration value for a user
110 * Stores a config value ($value) in the category ($family) under the key ($key)
111 * for the user_id $uid.
113 * @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
125 public static function set($uid, $family, $key, $value) {
129 // We store our setting values in a string variable.
130 // So we have to do the conversion here so that the compare below works.
131 // The exception are array values.
132 $dbvalue = (!is_array($value) ? (string)$value : $value);
134 $stored = self::get($uid, $family, $key, null, true);
136 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
140 $a->config[$uid][$family][$key] = $dbvalue;
142 // manage array value
143 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
145 dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
148 self::$in_db[$uid][$family][$key] = true;
155 * @brief Deletes the given key from the users's configuration.
157 * Removes the configured value from the stored cache in $a->config[$uid]
158 * and removes it from the database.
160 * @param string $uid The user_id
161 * @param string $family
162 * The category of the configuration value
164 * The configuration key to delete
167 public static function delete($uid,$family,$key) {
171 if (x($a->config[$uid][$family], $key)) {
172 unset($a->config[$uid][$family][$key]);
173 unset(self::$in_db[$uid][$family][$key]);
176 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));