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.
22 * @brief Loads all configuration values of a user's config family into a cached storage.
24 * All configuration values of the given user are stored in global cache
25 * which is available under the global variable $a->config[$uid].
29 * @param string $family
30 * The category of the configuration value
33 public static function load($uid, $family) {
35 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d ORDER BY `cat`, `k`, `id`",
39 if (dbm::is_result($r)) {
42 $a->config[$uid][$family][$k] = $rr['v'];
44 } else if ($family != 'config') {
46 $a->config[$uid][$family] = "!<unset>!";
51 * @brief Get a particular user's config variable given the category name
52 * ($family) and a key.
54 * Get a particular user's config value from the given category ($family)
55 * and the $key from a cached storage in $a->config[$uid].
59 * @param string $family
60 * The category of the configuration value
62 * The configuration key to query
63 * @param mixed $default_value optional
64 * The value to return if key is not set (default: null)
65 * @param boolean $refresh optional
66 * If true the config is loaded from the db and not from the cache (default: false)
67 * @return mixed Stored value or null if it does not exist
69 public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
74 // Looking if the whole family isn't set
75 if (isset($a->config[$uid][$family])) {
76 if ($a->config[$uid][$family] === '!<unset>!') {
77 return $default_value;
81 if (isset($a->config[$uid][$family][$key])) {
82 if ($a->config[$uid][$family][$key] === '!<unset>!') {
83 return $default_value;
85 return $a->config[$uid][$family][$key];
89 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' ORDER BY `id` DESC LIMIT 1",
96 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
97 $a->config[$uid][$family][$key] = $val;
101 $a->config[$uid][$family][$key] = '!<unset>!';
103 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 $stored = self::get($uid, $family, $key);
130 if ($stored == $value) {
134 // manage array value
135 $dbvalue = (is_array($value) ? serialize($value):$value);
137 $a->config[$uid][$family][$key] = $value;
139 if (is_null($stored)) {
140 $ret = q("INSERT INTO `pconfig` (`uid`, `cat`, `k`, `v`) VALUES (%d, '%s', '%s', '%s') ON DUPLICATE KEY UPDATE `v` = '%s'",
148 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
163 * @brief Deletes the given key from the users's configuration.
165 * Removes the configured value from the stored cache in $a->config[$uid]
166 * and removes it from the database.
168 * @param string $uid The user_id
169 * @param string $family
170 * The category of the configuration value
172 * The configuration key to delete
175 public static function delete($uid,$family,$key) {
179 if (x($a->config[$uid][$family], $key)) {
180 unset($a->config[$uid][$family][$key]);
183 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",