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;
46 } else if ($family != 'config') {
48 $a->config[$uid][$family] = "!<unset>!";
53 * @brief Get a particular user's config variable given the category name
54 * ($family) and a key.
56 * Get a particular user's config value from the given category ($family)
57 * and the $key from a cached storage in $a->config[$uid].
61 * @param string $family
62 * The category of the configuration value
64 * The configuration key to query
65 * @param mixed $default_value optional
66 * The value to return if key is not set (default: null)
67 * @param boolean $refresh optional
68 * 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!
116 * @param string $family
117 * The category of the configuration value
119 * The configuration key to set
120 * @param string $value
122 * @return mixed Stored $value or false
124 public static function set($uid, $family, $key, $value) {
128 // We store our setting values in a string variable.
129 // So we have to do the conversion here so that the compare below works.
130 // The exception are array values.
131 $dbvalue = (!is_array($value) ? (string)$value : $value);
133 $stored = self::get($uid, $family, $key, null, true);
135 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
139 $a->config[$uid][$family][$key] = $dbvalue;
141 // manage array value
142 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
144 if (is_null($stored) || !self::$in_db[$uid][$family][$key]) {
145 dba::insert('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key, 'v' => $dbvalue), true);
147 dba::update('pconfig', array('v' => $dbvalue), array('uid' => $uid, 'cat' => $family, 'k' => $key), true);
151 self::$in_db[$uid][$family][$key] = true;
158 * @brief Deletes the given key from the users's configuration.
160 * Removes the configured value from the stored cache in $a->config[$uid]
161 * and removes it from the database.
163 * @param string $uid The user_id
164 * @param string $family
165 * The category of the configuration value
167 * The configuration key to delete
170 public static function delete($uid,$family,$key) {
174 if (x($a->config[$uid][$family], $key)) {
175 unset($a->config[$uid][$family][$key]);
176 unset(self::$in_db[$uid][$family][$key]);
179 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));