]> git.mxchange.org Git - friendica.git/blob - src/Core/Config.php
6b9015c2eb96634d6d09ddbea7ab319136d2432e
[friendica.git] / src / Core / Config.php
1 <?php
2 /**
3  * System Configuration Class
4  *
5  * @file include/Core/Config.php
6  *
7  * @brief Contains the class with methods for system configuration
8  */
9 namespace Friendica\Core;
10
11 use Friendica\App;
12 use Friendica\BaseObject;
13
14 /**
15  * @brief Arbitrary system configuration storage
16  *
17  * Note:
18  * If we ever would decide to return exactly the variable type as entered,
19  * we will have fun with the additional features. :-)
20  */
21 class Config extends BaseObject
22 {
23         public static $config = [];
24
25         /**
26          * @var \Friendica\Core\Config\IConfigAdapter
27          */
28         private static $adapter = null;
29
30         public static function init()
31         {
32                 // Database isn't ready or populated yet
33                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
34                         return;
35                 }
36
37                 if (self::getConfigValue('system', 'config_adapter') == 'preload') {
38                         self::$adapter = new Config\PreloadConfigAdapter();
39                 } else {
40                         self::$adapter = new Config\JITConfigAdapter();
41                 }
42         }
43
44         /**
45          * @brief Loads all configuration values of family into a cached storage.
46          *
47          * All configuration values of the system are stored in global cache
48          * which is available under the global variable self::$config
49          *
50          * @param string $family The category of the configuration value
51          *
52          * @return void
53          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
54          */
55         public static function load($family = "config")
56         {
57                 // Database isn't ready or populated yet
58                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
59                         return;
60                 }
61
62                 if (empty(self::$adapter)) {
63                         self::init();
64                 }
65
66                 self::$adapter->load($family);
67         }
68
69         /**
70          * @brief Get a particular user's config variable given the category name
71          * ($family) and a key.
72          *
73          * Get a particular config value from the given category ($family)
74          * and the $key from a cached storage in static::config[$uid].
75          * $instore is only used by the set_config function
76          * to determine if the key already exists in the DB
77          * If a key is found in the DB but doesn't exist in
78          * local config cache, pull it into the cache so we don't have
79          * to hit the DB again for this item.
80          *
81          * @param string  $family        The category of the configuration value
82          * @param string  $key           The configuration key to query
83          * @param mixed   $default_value optional, The value to return if key is not set (default: null)
84          * @param boolean $refresh       optional, If true the config is loaded from the db and not from the cache (default: false)
85          *
86          * @return mixed Stored value or null if it does not exist
87          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
88          */
89         public static function get($family, $key, $default_value = null, $refresh = false)
90         {
91                 // Database isn't ready or populated yet, fallback to file config
92                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
93                         return self::getConfigValue($family, $key, $default_value);
94                 }
95
96                 if (empty(self::$adapter)) {
97                         self::init();
98                 }
99
100                 return self::$adapter->get($family, $key, $default_value, $refresh);
101         }
102
103         /**
104          * @brief Sets a configuration value for system config
105          *
106          * Stores a config value ($value) in the category ($family) under the key ($key)
107          * for the user_id $uid.
108          *
109          * Note: Please do not store booleans - convert to 0/1 integer values!
110          *
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($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();
127                 }
128
129                 return self::$adapter->set($family, $key, $value);
130         }
131
132         /**
133          * @brief Deletes the given key from the system configuration.
134          *
135          * Removes the configured value from the stored cache in Config::$config
136          * and removes it from the database.
137          *
138          * @param string $family The category of the configuration value
139          * @param string $key    The configuration key to delete
140          *
141          * @return mixed
142          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
143          */
144         public static function delete($family, $key)
145         {
146                 // Database isn't ready or populated yet
147                 if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
148                         return false;
149                 }
150
151                 if (empty(self::$adapter)) {
152                         self::init();
153                 }
154
155                 return self::$adapter->delete($family, $key);
156         }
157
158         /**
159          * Tries to load the specified configuration array into the App->config array.
160          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
161          *
162          * @param array $config
163          * @param bool  $overwrite Force value overwrite if the config key already exists
164          */
165         public function loadConfigArray(array $config, $overwrite = false)
166         {
167                 foreach ($config as $category => $values) {
168                         foreach ($values as $key => $value) {
169                                 if ($overwrite) {
170                                         self::setConfigValue($category, $key, $value);
171                                 } else {
172                                         self::setDefaultConfigValue($category, $key, $value);
173                                 }
174                         }
175                 }
176         }
177
178         /**
179          * @param string $cat     Config category
180          * @param string $k       Config key
181          * @param mixed  $default Default value if it isn't set
182          *
183          * @return string Returns the value of the Config entry
184          */
185         public static function getConfigValue($cat, $k = null, $default = null)
186         {
187                 $return = $default;
188
189                 if ($cat === 'config') {
190                         if (isset(self::$config[$k])) {
191                                 $return = self::$config[$k];
192                         }
193                 } else {
194                         if (isset(self::$config[$cat][$k])) {
195                                 $return = self::$config[$cat][$k];
196                         } elseif ($k == null && isset(self::$config[$cat])) {
197                                 $return = self::$config[$cat];
198                         }
199                 }
200
201                 return $return;
202         }
203
204         /**
205          * Sets a default value in the config cache. Ignores already existing keys.
206          *
207          * @param string $cat Config category
208          * @param string $k   Config key
209          * @param mixed  $v   Default value to set
210          */
211         private static function setDefaultConfigValue($cat, $k, $v)
212         {
213                 if (!isset(self::$config[$cat][$k])) {
214                         self::setConfigValue($cat, $k, $v);
215                 }
216         }
217
218         /**
219          * Sets a value in the config cache. Accepts raw output from the config table
220          *
221          * @param string $cat Config category
222          * @param string $k   Config key
223          * @param mixed  $v   Value to set
224          */
225         public static function setConfigValue($cat, $k, $v)
226         {
227                 // Only arrays are serialized in database, so we have to unserialize sparingly
228                 $value = is_string($v) && preg_match("|^a:[0-9]+:{.*}$|s", $v) ? unserialize($v) : $v;
229
230                 if ($cat === 'config') {
231                         self::$config[$k] = $value;
232                 } else {
233                         if (!isset(self::$config[$cat])) {
234                                 self::$config[$cat] = [];
235                         }
236
237                         self::$config[$cat][$k] = $value;
238                 }
239         }
240
241         /**
242          * Deletes a value from the config cache
243          *
244          * @param string $cat Config category
245          * @param string $k   Config key
246          */
247         public static function deleteConfigValue($cat, $k)
248         {
249                 if ($cat === 'config') {
250                         if (isset(self::$config[$k])) {
251                                 unset(self::$config[$k]);
252                         }
253                 } else {
254                         if (isset(self::$config[$cat][$k])) {
255                                 unset(self::$config[$cat][$k]);
256                         }
257                 }
258         }
259
260         /**
261          * Returns the whole configuration
262          *
263          * @return array The configuration
264          */
265         public static function getAll()
266         {
267                 return self::$config;
268         }
269 }