]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig.php
2) Refactor App->config[] into Core\PConfig
[friendica.git] / src / Core / PConfig.php
1 <?php
2 /**
3  * User Configuration Class
4  *
5  * @file include/Core/PConfig.php
6  *
7  * @brief Contains the class with methods for user configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\App;
12 use Friendica\BaseObject;
13
14 /**
15  * @brief Management of user configuration storage
16  * Note:
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.
20  */
21 class PConfig extends BaseObject
22 {
23         private static $config;
24
25         /**
26          * @var \Friendica\Core\Config\IPConfigAdapter
27          */
28         private static $adapter = null;
29
30         public static function init($uid)
31         {
32                 $a = self::getApp();
33
34                 // Database isn't ready or populated yet
35                 if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
36                         return;
37                 }
38
39                 if (Config::getConfigValue('system', 'config_adapter') == 'preload') {
40                         self::$adapter = new Config\PreloadPConfigAdapter($uid);
41                 } else {
42                         self::$adapter = new Config\JITPConfigAdapter();
43                 }
44         }
45
46         /**
47          * @brief Loads all configuration values of a user's config family into a cached storage.
48          *
49          * All configuration values of the given user are stored in global cache
50          * which is available under the global variable self::$config[$uid].
51          *
52          * @param string $uid    The user_id
53          * @param string $family The category of the configuration value
54          *
55          * @return void
56          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
57          */
58         public static function load($uid, $family)
59         {
60                 // Database isn't ready or populated yet
61                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
62                         return;
63                 }
64
65                 if (empty(self::$adapter)) {
66                         self::init($uid);
67                 }
68
69                 self::$adapter->load($uid, $family);
70         }
71
72         /**
73          * @brief Get a particular user's config variable given the category name
74          * ($family) and a key.
75          *
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].
78          *
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)
84          *
85          * @return mixed Stored value or null if it does not exist
86          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
87          */
88         public static function get($uid, $family, $key, $default_value = null, $refresh = false)
89         {
90                 // Database isn't ready or populated yet
91                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
92                         return;
93                 }
94
95                 if (empty(self::$adapter)) {
96                         self::init($uid);
97                 }
98
99                 return self::$adapter->get($uid, $family, $key, $default_value, $refresh);
100         }
101
102         /**
103          * @brief Sets a configuration value for a user
104          *
105          * Stores a config value ($value) in the category ($family) under the key ($key)
106          * for the user_id $uid.
107          *
108          * @note  Please do not store booleans - convert to 0/1 integer values!
109          *
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
114          *
115          * @return bool Operation success
116          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
117          */
118         public static function set($uid, $family, $key, $value)
119         {
120                 // Database isn't ready or populated yet
121                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
122                         return false;
123                 }
124
125                 if (empty(self::$adapter)) {
126                         self::init($uid);
127                 }
128
129                 return self::$adapter->set($uid, $family, $key, $value);
130         }
131
132         /**
133          * @brief Deletes the given key from the users's configuration.
134          *
135          * Removes the configured value from the stored cache in self::$config[$uid]
136          * and removes it from the database.
137          *
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
141          *
142          * @return mixed
143          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
144          */
145         public static function delete($uid, $family, $key)
146         {
147                 // Database isn't ready or populated yet
148                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
149                         return false;
150                 }
151
152                 if (empty(self::$adapter)) {
153                         self::init($uid);
154                 }
155
156                 return self::$adapter->delete($uid, $family, $key);
157         }
158
159
160         /**
161          * Retrieves a value from the user config cache
162          *
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
167          *
168          * @return string The value of the config entry
169          */
170         public static function getPConfigValue($uid, $cat, $k = null, $default = null)
171         {
172                 $return = $default;
173
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];
178                 }
179
180                 return $return;
181         }
182
183         /**
184          * Sets a value in the user config cache
185          *
186          * Accepts raw output from the pconfig table
187          *
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
192          */
193         public static function setPConfigValue($uid, $cat, $k, $v)
194         {
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;
197
198                 if (!isset(self::$config[$uid]) || !is_array(self::$config[$uid])) {
199                         self::$config[$uid] = [];
200                 }
201
202                 if (!isset(self::$config[$uid][$cat]) || !is_array(self::$config[$uid][$cat])) {
203                         self::$config[$uid][$cat] = [];
204                 }
205
206                 if ($k === null) {
207                         self::$config[$uid][$cat] = $value;
208                 } else {
209                         self::$config[$uid][$cat][$k] = $value;
210                 }
211         }
212
213         /**
214          * Deletes a value from the user config cache
215          *
216          * @param int    $uid User Id
217          * @param string $cat Config category
218          * @param string $k   Config key
219          */
220         public static function deletePConfigValue($uid, $cat, $k)
221         {
222                 if (isset(self::$config[$uid][$cat][$k])) {
223                         unset(self::$config[$uid][$cat][$k]);
224                 }
225         }
226 }