3 * User Configuration Class
5 * @file include/Core/PConfig.php
7 * @brief Contains the class with methods for user configuration
9 namespace Friendica\Core;
12 use Friendica\BaseObject;
15 * @brief Management of user configuration storage
17 * Please do not store booleans - convert to 0/1 integer values
18 * The PConfig::get() functions return boolean false for keys that are unset,
19 * and this could lead to subtle bugs.
21 class PConfig extends BaseObject
23 private static $config;
26 * @var \Friendica\Core\Config\IPConfigAdapter
28 private static $adapter = null;
30 public static function init($uid)
34 // Database isn't ready or populated yet
35 if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
39 if (Config::getConfigValue('system', 'config_adapter') == 'preload') {
40 self::$adapter = new Config\PreloadPConfigAdapter($uid);
42 self::$adapter = new Config\JITPConfigAdapter();
47 * @brief Loads all configuration values of a user's config family into a cached storage.
49 * All configuration values of the given user are stored in global cache
50 * which is available under the global variable self::$config[$uid].
52 * @param string $uid The user_id
53 * @param string $family The category of the configuration value
56 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
58 public static function load($uid, $family)
60 // Database isn't ready or populated yet
61 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
65 if (empty(self::$adapter)) {
69 self::$adapter->load($uid, $family);
73 * @brief Get a particular user's config variable given the category name
74 * ($family) and a key.
76 * Get a particular user's config value from the given category ($family)
77 * and the $key from a cached storage in self::$config[$uid].
79 * @param string $uid The user_id
80 * @param string $family The category of the configuration value
81 * @param string $key The configuration key to query
82 * @param mixed $default_value optional, The value to return if key is not set (default: null)
83 * @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
85 * @return mixed Stored value or null if it does not exist
86 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
88 public static function get($uid, $family, $key, $default_value = null, $refresh = false)
90 // Database isn't ready or populated yet
91 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
95 if (empty(self::$adapter)) {
99 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
103 * @brief Sets a configuration value for a user
105 * Stores a config value ($value) in the category ($family) under the key ($key)
106 * for the user_id $uid.
108 * @note Please do not store booleans - convert to 0/1 integer values!
110 * @param string $uid The user_id
111 * @param string $family The category of the configuration value
112 * @param string $key The configuration key to set
113 * @param mixed $value The value to store
115 * @return bool Operation success
116 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
118 public static function set($uid, $family, $key, $value)
120 // Database isn't ready or populated yet
121 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
125 if (empty(self::$adapter)) {
129 return self::$adapter->set($uid, $family, $key, $value);
133 * @brief Deletes the given key from the users's configuration.
135 * Removes the configured value from the stored cache in self::$config[$uid]
136 * and removes it from the database.
138 * @param string $uid The user_id
139 * @param string $family The category of the configuration value
140 * @param string $key The configuration key to delete
143 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
145 public static function delete($uid, $family, $key)
147 // Database isn't ready or populated yet
148 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
152 if (empty(self::$adapter)) {
156 return self::$adapter->delete($uid, $family, $key);
161 * Retrieves a value from the user config cache
163 * @param int $uid User Id
164 * @param string $cat Config category
165 * @param string $k Config key
166 * @param mixed $default Default value if key isn't set
168 * @return string The value of the config entry
170 public static function getPConfigValue($uid, $cat, $k = null, $default = null)
174 if (isset(self::$config[$uid][$cat][$k])) {
175 $return = self::$config[$uid][$cat][$k];
176 } elseif ($k == null && isset(self::$config[$uid][$cat])) {
177 $return = self::$config[$uid][$cat];
184 * Sets a value in the user config cache
186 * Accepts raw output from the pconfig table
188 * @param int $uid User Id
189 * @param string $cat Config category
190 * @param string $k Config key
191 * @param mixed $v Value to set
193 public static function setPConfigValue($uid, $cat, $k, $v)
195 // Only arrays are serialized in database, so we have to unserialize sparingly
196 $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
198 if (!isset(self::$config[$uid]) || !is_array(self::$config[$uid])) {
199 self::$config[$uid] = [];
202 if (!isset(self::$config[$uid][$cat]) || !is_array(self::$config[$uid][$cat])) {
203 self::$config[$uid][$cat] = [];
207 self::$config[$uid][$cat] = $value;
209 self::$config[$uid][$cat][$k] = $value;
214 * Deletes a value from the user config cache
216 * @param int $uid User Id
217 * @param string $cat Config category
218 * @param string $k Config key
220 public static function deletePConfigValue($uid, $cat, $k)
222 if (isset(self::$config[$uid][$cat][$k])) {
223 unset(self::$config[$uid][$cat][$k]);