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 array("order" => array('uid', 'cat', 'k')));
41 if (dbm::is_result($r)) {
42 while ($rr = dba::fetch($r)) {
44 $a->config[$uid][$family][$k] = $rr['v'];
45 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),
93 array("order" => array('uid', 'cat', 'k'), 'limit' => 1));
94 if (dbm::is_result($ret)) {
95 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v']) ? unserialize($ret[0]['v']) : $ret[0]['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!
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
126 public static function set($uid, $family, $key, $value) {
130 // We store our setting values in a string variable.
131 // So we have to do the conversion here so that the compare below works.
132 // The exception are array values.
133 $dbvalue = (!is_array($value) ? (string)$value : $value);
135 $stored = self::get($uid, $family, $key, null, true);
137 if (($stored === $dbvalue) && self::$in_db[$uid][$family][$key]) {
141 $a->config[$uid][$family][$key] = $dbvalue;
143 // manage array value
144 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
146 if (is_null($stored) || !self::$in_db[$uid][$family][$key]) {
147 $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
155 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
164 self::$in_db[$uid][$family][$key] = true;
171 * @brief Deletes the given key from the users's configuration.
173 * Removes the configured value from the stored cache in $a->config[$uid]
174 * and removes it from the database.
176 * @param string $uid The user_id
177 * @param string $family
178 * The category of the configuration value
180 * The configuration key to delete
183 public static function delete($uid,$family,$key) {
187 if (x($a->config[$uid][$family], $key)) {
188 unset($a->config[$uid][$family][$key]);
189 unset(self::$in_db[$uid][$family][$key]);
192 $ret = dba::delete('pconfig', array('uid' => $uid, 'cat' => $family, 'k' => $key));