]>
Commit | Line | Data |
---|---|---|
59404d79 | 1 | <?php |
3c66e564 | 2 | namespace Shaarli\Config; |
59404d79 | 3 | |
5ba55f0c A |
4 | use Shaarli\Config\Exception\MissingFieldConfigException; |
5 | use Shaarli\Config\Exception\UnauthorizedConfigException; | |
3ee8351e | 6 | use Shaarli\Thumbnailer; |
5ba55f0c | 7 | |
59404d79 A |
8 | /** |
9 | * Class ConfigManager | |
10 | * | |
278d9ee2 | 11 | * Manages all Shaarli's settings. |
7f179985 | 12 | * See the documentation for more information on settings: |
cc8f572b WE |
13 | * - doc/md/Shaarli-configuration.md |
14 | * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration | |
59404d79 A |
15 | */ |
16 | class ConfigManager | |
17 | { | |
18 | /** | |
278d9ee2 | 19 | * @var string Flag telling a setting is not found. |
59404d79 | 20 | */ |
278d9ee2 | 21 | protected static $NOT_FOUND = 'NOT_FOUND'; |
59404d79 | 22 | |
18e67967 A |
23 | public static $DEFAULT_PLUGINS = array('qrcode'); |
24 | ||
59404d79 A |
25 | /** |
26 | * @var string Config folder. | |
27 | */ | |
278d9ee2 | 28 | protected $configFile; |
59404d79 A |
29 | |
30 | /** | |
31 | * @var array Loaded config array. | |
32 | */ | |
33 | protected $loadedConfig; | |
34 | ||
35 | /** | |
36 | * @var ConfigIO implementation instance. | |
37 | */ | |
38 | protected $configIO; | |
39 | ||
40 | /** | |
278d9ee2 | 41 | * Constructor. |
7af9a418 A |
42 | * |
43 | * @param string $configFile Configuration file path without extension. | |
59404d79 | 44 | */ |
278d9ee2 | 45 | public function __construct($configFile = 'data/config') |
59404d79 | 46 | { |
278d9ee2 A |
47 | $this->configFile = $configFile; |
48 | $this->initialize(); | |
59404d79 A |
49 | } |
50 | ||
684e662a A |
51 | /** |
52 | * Reset the ConfigManager instance. | |
53 | */ | |
278d9ee2 | 54 | public function reset() |
684e662a | 55 | { |
278d9ee2 | 56 | $this->initialize(); |
684e662a A |
57 | } |
58 | ||
59404d79 A |
59 | /** |
60 | * Rebuild the loaded config array from config files. | |
61 | */ | |
62 | public function reload() | |
63 | { | |
684e662a | 64 | $this->load(); |
59404d79 A |
65 | } |
66 | ||
67 | /** | |
684e662a | 68 | * Initialize the ConfigIO and loaded the conf. |
59404d79 A |
69 | */ |
70 | protected function initialize() | |
71 | { | |
278d9ee2 | 72 | if (file_exists($this->configFile . '.php')) { |
59404d79 | 73 | $this->configIO = new ConfigPhp(); |
278d9ee2 A |
74 | } else { |
75 | $this->configIO = new ConfigJson(); | |
b74b96bf | 76 | } |
684e662a A |
77 | $this->load(); |
78 | } | |
79 | ||
80 | /** | |
81 | * Load configuration in the ConfigurationManager. | |
82 | */ | |
83 | protected function load() | |
84 | { | |
c6a4c288 A |
85 | try { |
86 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); | |
87 | } catch (\Exception $e) { | |
88 | die($e->getMessage()); | |
89 | } | |
59404d79 A |
90 | $this->setDefaultValues(); |
91 | } | |
92 | ||
93 | /** | |
94 | * Get a setting. | |
95 | * | |
96 | * Supports nested settings with dot separated keys. | |
97 | * Eg. 'config.stuff.option' will find $conf[config][stuff][option], | |
98 | * or in JSON: | |
99 | * { "config": { "stuff": {"option": "mysetting" } } } } | |
100 | * | |
101 | * @param string $setting Asked setting, keys separated with dots. | |
102 | * @param string $default Default value if not found. | |
103 | * | |
104 | * @return mixed Found setting, or the default value. | |
105 | */ | |
106 | public function get($setting, $default = '') | |
107 | { | |
da10377b A |
108 | // During the ConfigIO transition, map legacy settings to the new ones. |
109 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
110 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
111 | } | |
112 | ||
59404d79 A |
113 | $settings = explode('.', $setting); |
114 | $value = self::getConfig($settings, $this->loadedConfig); | |
115 | if ($value === self::$NOT_FOUND) { | |
116 | return $default; | |
117 | } | |
118 | return $value; | |
119 | } | |
120 | ||
121 | /** | |
122 | * Set a setting, and eventually write it. | |
123 | * | |
124 | * Supports nested settings with dot separated keys. | |
125 | * | |
126 | * @param string $setting Asked setting, keys separated with dots. | |
980efd6c | 127 | * @param mixed $value Value to set. |
59404d79 A |
128 | * @param bool $write Write the new setting in the config file, default false. |
129 | * @param bool $isLoggedIn User login state, default false. | |
684e662a | 130 | * |
3c66e564 | 131 | * @throws \Exception Invalid |
59404d79 A |
132 | */ |
133 | public function set($setting, $value, $write = false, $isLoggedIn = false) | |
134 | { | |
684e662a | 135 | if (empty($setting) || ! is_string($setting)) { |
12266213 | 136 | throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting)); |
684e662a A |
137 | } |
138 | ||
da10377b A |
139 | // During the ConfigIO transition, map legacy settings to the new ones. |
140 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
141 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
142 | } | |
143 | ||
59404d79 A |
144 | $settings = explode('.', $setting); |
145 | self::setConfig($settings, $value, $this->loadedConfig); | |
146 | if ($write) { | |
147 | $this->write($isLoggedIn); | |
148 | } | |
149 | } | |
150 | ||
a3724717 A |
151 | /** |
152 | * Remove a config element from the config file. | |
153 | * | |
154 | * @param string $setting Asked setting, keys separated with dots. | |
155 | * @param bool $write Write the new setting in the config file, default false. | |
156 | * @param bool $isLoggedIn User login state, default false. | |
157 | * | |
158 | * @throws \Exception Invalid | |
159 | */ | |
160 | public function remove($setting, $write = false, $isLoggedIn = false) | |
161 | { | |
162 | if (empty($setting) || ! is_string($setting)) { | |
163 | throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting)); | |
164 | } | |
165 | ||
166 | // During the ConfigIO transition, map legacy settings to the new ones. | |
167 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
168 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
169 | } | |
170 | ||
171 | $settings = explode('.', $setting); | |
172 | self::removeConfig($settings, $this->loadedConfig); | |
173 | if ($write) { | |
174 | $this->write($isLoggedIn); | |
175 | } | |
176 | } | |
177 | ||
59404d79 A |
178 | /** |
179 | * Check if a settings exists. | |
180 | * | |
181 | * Supports nested settings with dot separated keys. | |
182 | * | |
183 | * @param string $setting Asked setting, keys separated with dots. | |
184 | * | |
185 | * @return bool true if the setting exists, false otherwise. | |
186 | */ | |
187 | public function exists($setting) | |
188 | { | |
da10377b A |
189 | // During the ConfigIO transition, map legacy settings to the new ones. |
190 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
191 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
192 | } | |
193 | ||
59404d79 A |
194 | $settings = explode('.', $setting); |
195 | $value = self::getConfig($settings, $this->loadedConfig); | |
196 | if ($value === self::$NOT_FOUND) { | |
197 | return false; | |
198 | } | |
199 | return true; | |
200 | } | |
201 | ||
202 | /** | |
203 | * Call the config writer. | |
204 | * | |
205 | * @param bool $isLoggedIn User login state. | |
206 | * | |
684e662a A |
207 | * @return bool True if the configuration has been successfully written, false otherwise. |
208 | * | |
59404d79 A |
209 | * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf. |
210 | * @throws UnauthorizedConfigException: user is not authorize to change configuration. | |
f3d2f257 | 211 | * @throws \Shaarli\Exceptions\IOException: an error occurred while writing the new config file. |
59404d79 A |
212 | */ |
213 | public function write($isLoggedIn) | |
214 | { | |
215 | // These fields are required in configuration. | |
216 | $mandatoryFields = array( | |
da10377b A |
217 | 'credentials.login', |
218 | 'credentials.hash', | |
219 | 'credentials.salt', | |
220 | 'security.session_protection_disabled', | |
221 | 'general.timezone', | |
222 | 'general.title', | |
223 | 'general.header_link', | |
894a3c4b | 224 | 'privacy.default_private_links', |
59404d79 A |
225 | ); |
226 | ||
227 | // Only logged in user can alter config. | |
278d9ee2 | 228 | if (is_file($this->getConfigFileExt()) && !$isLoggedIn) { |
59404d79 A |
229 | throw new UnauthorizedConfigException(); |
230 | } | |
231 | ||
232 | // Check that all mandatory fields are provided in $conf. | |
233 | foreach ($mandatoryFields as $field) { | |
234 | if (! $this->exists($field)) { | |
235 | throw new MissingFieldConfigException($field); | |
236 | } | |
237 | } | |
238 | ||
278d9ee2 | 239 | return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig); |
59404d79 A |
240 | } |
241 | ||
242 | /** | |
278d9ee2 | 243 | * Set the config file path (without extension). |
59404d79 | 244 | * |
278d9ee2 A |
245 | * @param string $configFile File path. |
246 | */ | |
247 | public function setConfigFile($configFile) | |
248 | { | |
249 | $this->configFile = $configFile; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Return the configuration file path (without extension). | |
254 | * | |
255 | * @return string Config path. | |
59404d79 A |
256 | */ |
257 | public function getConfigFile() | |
258 | { | |
278d9ee2 A |
259 | return $this->configFile; |
260 | } | |
261 | ||
262 | /** | |
263 | * Get the configuration file path with its extension. | |
264 | * | |
265 | * @return string Config file path. | |
266 | */ | |
267 | public function getConfigFileExt() | |
268 | { | |
269 | return $this->configFile . $this->configIO->getExtension(); | |
59404d79 A |
270 | } |
271 | ||
272 | /** | |
273 | * Recursive function which find asked setting in the loaded config. | |
274 | * | |
275 | * @param array $settings Ordered array which contains keys to find. | |
276 | * @param array $conf Loaded settings, then sub-array. | |
277 | * | |
278 | * @return mixed Found setting or NOT_FOUND flag. | |
279 | */ | |
280 | protected static function getConfig($settings, $conf) | |
281 | { | |
282 | if (!is_array($settings) || count($settings) == 0) { | |
283 | return self::$NOT_FOUND; | |
284 | } | |
285 | ||
286 | $setting = array_shift($settings); | |
287 | if (!isset($conf[$setting])) { | |
288 | return self::$NOT_FOUND; | |
289 | } | |
290 | ||
291 | if (count($settings) > 0) { | |
292 | return self::getConfig($settings, $conf[$setting]); | |
293 | } | |
294 | return $conf[$setting]; | |
295 | } | |
296 | ||
297 | /** | |
298 | * Recursive function which find asked setting in the loaded config. | |
299 | * | |
300 | * @param array $settings Ordered array which contains keys to find. | |
301 | * @param mixed $value | |
a3724717 | 302 | * @param array $conf Loaded settings, then sub-array. |
59404d79 A |
303 | * |
304 | * @return mixed Found setting or NOT_FOUND flag. | |
305 | */ | |
306 | protected static function setConfig($settings, $value, &$conf) | |
307 | { | |
308 | if (!is_array($settings) || count($settings) == 0) { | |
309 | return self::$NOT_FOUND; | |
310 | } | |
311 | ||
312 | $setting = array_shift($settings); | |
313 | if (count($settings) > 0) { | |
314 | return self::setConfig($settings, $value, $conf[$setting]); | |
315 | } | |
316 | $conf[$setting] = $value; | |
317 | } | |
318 | ||
a3724717 A |
319 | /** |
320 | * Recursive function which find asked setting in the loaded config and deletes it. | |
321 | * | |
322 | * @param array $settings Ordered array which contains keys to find. | |
323 | * @param array $conf Loaded settings, then sub-array. | |
324 | * | |
325 | * @return mixed Found setting or NOT_FOUND flag. | |
326 | */ | |
327 | protected static function removeConfig($settings, &$conf) | |
328 | { | |
329 | if (!is_array($settings) || count($settings) == 0) { | |
330 | return self::$NOT_FOUND; | |
331 | } | |
332 | ||
333 | $setting = array_shift($settings); | |
334 | if (count($settings) > 0) { | |
335 | return self::removeConfig($settings, $conf[$setting]); | |
336 | } | |
337 | unset($conf[$setting]); | |
338 | } | |
339 | ||
59404d79 A |
340 | /** |
341 | * Set a bunch of default values allowing Shaarli to start without a config file. | |
342 | */ | |
343 | protected function setDefaultValues() | |
344 | { | |
894a3c4b A |
345 | $this->setEmpty('resource.data_dir', 'data'); |
346 | $this->setEmpty('resource.config', 'data/config.php'); | |
347 | $this->setEmpty('resource.datastore', 'data/datastore.php'); | |
348 | $this->setEmpty('resource.ban_file', 'data/ipbans.php'); | |
349 | $this->setEmpty('resource.updates', 'data/updates.txt'); | |
350 | $this->setEmpty('resource.log', 'data/log.txt'); | |
351 | $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt'); | |
4306b184 | 352 | $this->setEmpty('resource.history', 'data/history.php'); |
894a3c4b | 353 | $this->setEmpty('resource.raintpl_tpl', 'tpl/'); |
adc4aee8 | 354 | $this->setEmpty('resource.theme', 'default'); |
894a3c4b A |
355 | $this->setEmpty('resource.raintpl_tmp', 'tmp/'); |
356 | $this->setEmpty('resource.thumbnails_cache', 'cache'); | |
357 | $this->setEmpty('resource.page_cache', 'pagecache'); | |
59404d79 | 358 | |
da10377b | 359 | $this->setEmpty('security.ban_after', 4); |
278d9ee2 | 360 | $this->setEmpty('security.ban_duration', 1800); |
7f179985 | 361 | $this->setEmpty('security.session_protection_disabled', false); |
894a3c4b | 362 | $this->setEmpty('security.open_shaarli', false); |
86ceea05 | 363 | $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']); |
59404d79 | 364 | |
3ee8351e | 365 | $this->setEmpty('general.header_link', '/'); |
894a3c4b | 366 | $this->setEmpty('general.links_per_page', 20); |
18e67967 | 367 | $this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS); |
722caa20 | 368 | $this->setEmpty('general.default_note_title', 'Note: '); |
4cf3564d A |
369 | $this->setEmpty('general.retrieve_description', true); |
370 | $this->setEmpty('general.enable_async_metadata', true); | |
b3bd8c3e | 371 | $this->setEmpty('general.tags_separator', ' '); |
59404d79 | 372 | |
894a3c4b A |
373 | $this->setEmpty('updates.check_updates', false); |
374 | $this->setEmpty('updates.check_updates_branch', 'stable'); | |
375 | $this->setEmpty('updates.check_updates_interval', 86400); | |
376 | ||
377 | $this->setEmpty('feed.rss_permalinks', true); | |
2ea89aba | 378 | $this->setEmpty('feed.show_atom', true); |
894a3c4b A |
379 | |
380 | $this->setEmpty('privacy.default_private_links', false); | |
381 | $this->setEmpty('privacy.hide_public_links', false); | |
27e21231 | 382 | $this->setEmpty('privacy.force_login', false); |
894a3c4b | 383 | $this->setEmpty('privacy.hide_timestamps', false); |
2e07e775 WE |
384 | // default state of the 'remember me' checkbox of the login form |
385 | $this->setEmpty('privacy.remember_user_default', true); | |
894a3c4b | 386 | |
3ee8351e | 387 | $this->setEmpty('thumbnails.mode', Thumbnailer::MODE_ALL); |
7b4fea0e A |
388 | $this->setEmpty('thumbnails.width', '125'); |
389 | $this->setEmpty('thumbnails.height', '90'); | |
390 | ||
12266213 A |
391 | $this->setEmpty('translation.language', 'auto'); |
392 | $this->setEmpty('translation.mode', 'php'); | |
393 | $this->setEmpty('translation.extensions', []); | |
394 | ||
59404d79 | 395 | $this->setEmpty('plugins', array()); |
cf92b4dd A |
396 | |
397 | $this->setEmpty('formatter', 'markdown'); | |
59404d79 A |
398 | } |
399 | ||
400 | /** | |
401 | * Set only if the setting does not exists. | |
402 | * | |
403 | * @param string $key Setting key. | |
404 | * @param mixed $value Setting value. | |
405 | */ | |
7f179985 | 406 | public function setEmpty($key, $value) |
59404d79 A |
407 | { |
408 | if (! $this->exists($key)) { | |
409 | $this->set($key, $value); | |
410 | } | |
411 | } | |
684e662a A |
412 | |
413 | /** | |
414 | * @return ConfigIO | |
415 | */ | |
416 | public function getConfigIO() | |
417 | { | |
418 | return $this->configIO; | |
419 | } | |
420 | ||
421 | /** | |
422 | * @param ConfigIO $configIO | |
423 | */ | |
424 | public function setConfigIO($configIO) | |
425 | { | |
426 | $this->configIO = $configIO; | |
427 | } | |
59404d79 | 428 | } |