2 namespace Friendica\Core;
7 * @file include/Core/PConfig.php
8 * @brief contains the class with methods for the management
9 * of the user configuration
13 * @brief Management of user configuration storage
15 * Please do not store booleans - convert to 0/1 integer values
16 * The PConfig::get() functions return boolean false for keys that are unset,
17 * and this could lead to subtle bugs.
21 private static $in_db;
24 * @brief Loads all configuration values of a user's config family into a cached storage.
26 * All configuration values of the given user are stored in global cache
27 * which is available under the global variable $a->config[$uid].
31 * @param string $family
32 * The category of the configuration value
35 public static function load($uid, $family) {
37 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
41 if (dbm::is_result($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 = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
99 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
100 $a->config[$uid][$family][$key] = $val;
101 self::$in_db[$uid][$family][$key] = true;
105 $a->config[$uid][$family][$key] = '!<unset>!';
106 self::$in_db[$uid][$family][$key] = false;
108 return $default_value;
113 * @brief Sets a configuration value for a user
115 * Stores a config value ($value) in the category ($family) under the key ($key)
116 * for the user_id $uid.
118 * @note Please do not store booleans - convert to 0/1 integer values!
122 * @param string $family
123 * The category of the configuration value
125 * The configuration key to set
126 * @param string $value
128 * @return mixed Stored $value or false
130 public static function set($uid, $family, $key, $value) {
134 // We store our setting values in a string variable.
135 // So we have to do the conversion here so that the compare below works.
136 // The exception are array values.
137 $dbvalue = (!is_array($value) ? (string)$value : $value);
139 $stored = self::get($uid, $family, $key, null, true);
141 if (($stored === $dbvalue) AND self::$in_db[$uid][$family][$key]) {
145 $a->config[$uid][$family][$key] = $dbvalue;
147 // manage array value
148 $dbvalue = (is_array($value) ? serialize($value) : $dbvalue);
150 if (is_null($stored) OR !self::$in_db[$uid][$family][$key]) {
151 $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
159 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
168 self::$in_db[$uid][$family][$key] = true;
175 * @brief Deletes the given key from the users's configuration.
177 * Removes the configured value from the stored cache in $a->config[$uid]
178 * and removes it from the database.
180 * @param string $uid The user_id
181 * @param string $family
182 * The category of the configuration value
184 * The configuration key to delete
187 public static function delete($uid,$family,$key) {
191 if (x($a->config[$uid][$family], $key)) {
192 unset($a->config[$uid][$family][$key]);
193 unset(self::$in_db[$uid][$family][$key]);
196 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",