2 namespace Friendica\Core;
4 * @file include/Core/PConfig.php
5 * @brief contains the class with methods for the management
6 * of the user configuration
10 * @brief Management of user configuration storage
12 * Please do not store booleans - convert to 0/1 integer values
13 * The PConfig::get() functions return boolean false for keys that are unset,
14 * and this could lead to subtle bugs.
19 * @brief Loads all configuration values of a user's config family into a cached storage.
21 * All configuration values of the given user are stored in global cache
22 * which is available under the global variable $a->config[$uid].
26 * @param string $family
27 * The category of the configuration value
30 public static function load($uid,$family) {
32 $r = q("SELECT `v`,`k` FROM `pconfig` WHERE `cat` = '%s' AND `uid` = %d",
39 $a->config[$uid][$family][$k] = $rr['v'];
41 } else if ($family != 'config') {
43 $a->config[$uid][$family] = "!<unset>!";
48 * @brief Get a particular user's config variable given the category name
49 * ($family) and a key.
51 * Get a particular user's config value from the given category ($family)
52 * and the $key from a cached storage in $a->config[$uid].
56 * @param string $family
57 * The category of the configuration value
59 * The configuration key to query
60 * @param mixed $default_value optional
61 * The value to return if key is not set (default: null)
62 * @param boolean $refresh optional
63 * If true the config is loaded from the db and not from the cache (default: false)
64 * @return mixed Stored value or null if it does not exist
66 public static function get($uid, $family, $key, $default_value = null, $refresh = false) {
71 // Looking if the whole family isn't set
72 if(isset($a->config[$uid][$family])) {
73 if($a->config[$uid][$family] === '!<unset>!') {
74 return $default_value;
78 if(isset($a->config[$uid][$family][$key])) {
79 if($a->config[$uid][$family][$key] === '!<unset>!') {
80 return $default_value;
82 return $a->config[$uid][$family][$key];
86 $ret = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s' LIMIT 1",
93 $val = (preg_match("|^a:[0-9]+:{.*}$|s", $ret[0]['v'])?unserialize( $ret[0]['v']):$ret[0]['v']);
94 $a->config[$uid][$family][$key] = $val;
99 $a->config[$uid][$family][$key] = '!<unset>!';
101 return $default_value;
105 * @brief Sets a configuration value for a user
107 * Stores a config value ($value) in the category ($family) under the key ($key)
108 * for the user_id $uid.
110 * @note Please do not store booleans - convert to 0/1 integer values!
114 * @param string $family
115 * The category of the configuration value
117 * The configuration key to set
118 * @param string $value
120 * @return mixed Stored $value or false
122 public static function set($uid,$family,$key,$value) {
126 // manage array value
127 $dbvalue = (is_array($value)?serialize($value):$value);
129 if(is_null(self::get($uid,$family,$key,null, true))) {
130 $a->config[$uid][$family][$key] = $value;
131 $ret = q("INSERT INTO `pconfig` ( `uid`, `cat`, `k`, `v` ) VALUES ( %d, '%s', '%s', '%s' ) ",
141 $ret = q("UPDATE `pconfig` SET `v` = '%s' WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",
148 $a->config[$uid][$family][$key] = $value;
156 * @brief Deletes the given key from the users's configuration.
158 * Removes the configured value from the stored cache in $a->config[$uid]
159 * and removes it from the database.
161 * @param string $uid The user_id
162 * @param string $family
163 * The category of the configuration value
165 * The configuration key to delete
168 public static function delete($uid,$family,$key) {
171 if(x($a->config[$uid][$family],$key))
172 unset($a->config[$uid][$family][$key]);
173 $ret = q("DELETE FROM `pconfig` WHERE `uid` = %d AND `cat` = '%s' AND `k` = '%s'",