]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/config/ConfigManager.php
namespacing: \Shaarli\Exceptions\IOException
[github/shaarli/Shaarli.git] / application / config / ConfigManager.php
CommitLineData
59404d79 1<?php
3c66e564 2namespace Shaarli\Config;
59404d79 3
5ba55f0c
A
4use Shaarli\Config\Exception\MissingFieldConfigException;
5use Shaarli\Config\Exception\UnauthorizedConfigException;
6
59404d79
A
7/**
8 * Class ConfigManager
9 *
278d9ee2 10 * Manages all Shaarli's settings.
7f179985 11 * See the documentation for more information on settings:
cc8f572b
WE
12 * - doc/md/Shaarli-configuration.md
13 * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration
59404d79
A
14 */
15class ConfigManager
16{
17 /**
278d9ee2 18 * @var string Flag telling a setting is not found.
59404d79 19 */
278d9ee2 20 protected static $NOT_FOUND = 'NOT_FOUND';
59404d79 21
18e67967
A
22 public static $DEFAULT_PLUGINS = array('qrcode');
23
59404d79
A
24 /**
25 * @var string Config folder.
26 */
278d9ee2 27 protected $configFile;
59404d79
A
28
29 /**
30 * @var array Loaded config array.
31 */
32 protected $loadedConfig;
33
34 /**
35 * @var ConfigIO implementation instance.
36 */
37 protected $configIO;
38
39 /**
278d9ee2 40 * Constructor.
7af9a418
A
41 *
42 * @param string $configFile Configuration file path without extension.
59404d79 43 */
278d9ee2 44 public function __construct($configFile = 'data/config')
59404d79 45 {
278d9ee2
A
46 $this->configFile = $configFile;
47 $this->initialize();
59404d79
A
48 }
49
684e662a
A
50 /**
51 * Reset the ConfigManager instance.
52 */
278d9ee2 53 public function reset()
684e662a 54 {
278d9ee2 55 $this->initialize();
684e662a
A
56 }
57
59404d79
A
58 /**
59 * Rebuild the loaded config array from config files.
60 */
61 public function reload()
62 {
684e662a 63 $this->load();
59404d79
A
64 }
65
66 /**
684e662a 67 * Initialize the ConfigIO and loaded the conf.
59404d79
A
68 */
69 protected function initialize()
70 {
278d9ee2 71 if (file_exists($this->configFile . '.php')) {
59404d79 72 $this->configIO = new ConfigPhp();
278d9ee2
A
73 } else {
74 $this->configIO = new ConfigJson();
b74b96bf 75 }
684e662a
A
76 $this->load();
77 }
78
79 /**
80 * Load configuration in the ConfigurationManager.
81 */
82 protected function load()
83 {
c6a4c288
A
84 try {
85 $this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
86 } catch (\Exception $e) {
87 die($e->getMessage());
88 }
59404d79
A
89 $this->setDefaultValues();
90 }
91
92 /**
93 * Get a setting.
94 *
95 * Supports nested settings with dot separated keys.
96 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
97 * or in JSON:
98 * { "config": { "stuff": {"option": "mysetting" } } } }
99 *
100 * @param string $setting Asked setting, keys separated with dots.
101 * @param string $default Default value if not found.
102 *
103 * @return mixed Found setting, or the default value.
104 */
105 public function get($setting, $default = '')
106 {
da10377b
A
107 // During the ConfigIO transition, map legacy settings to the new ones.
108 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
109 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
110 }
111
59404d79
A
112 $settings = explode('.', $setting);
113 $value = self::getConfig($settings, $this->loadedConfig);
114 if ($value === self::$NOT_FOUND) {
115 return $default;
116 }
117 return $value;
118 }
119
120 /**
121 * Set a setting, and eventually write it.
122 *
123 * Supports nested settings with dot separated keys.
124 *
125 * @param string $setting Asked setting, keys separated with dots.
980efd6c 126 * @param mixed $value Value to set.
59404d79
A
127 * @param bool $write Write the new setting in the config file, default false.
128 * @param bool $isLoggedIn User login state, default false.
684e662a 129 *
3c66e564 130 * @throws \Exception Invalid
59404d79
A
131 */
132 public function set($setting, $value, $write = false, $isLoggedIn = false)
133 {
684e662a 134 if (empty($setting) || ! is_string($setting)) {
12266213 135 throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
684e662a
A
136 }
137
da10377b
A
138 // During the ConfigIO transition, map legacy settings to the new ones.
139 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
140 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
141 }
142
59404d79
A
143 $settings = explode('.', $setting);
144 self::setConfig($settings, $value, $this->loadedConfig);
145 if ($write) {
146 $this->write($isLoggedIn);
147 }
148 }
149
a3724717
A
150 /**
151 * Remove a config element from the config file.
152 *
153 * @param string $setting Asked setting, keys separated with dots.
154 * @param bool $write Write the new setting in the config file, default false.
155 * @param bool $isLoggedIn User login state, default false.
156 *
157 * @throws \Exception Invalid
158 */
159 public function remove($setting, $write = false, $isLoggedIn = false)
160 {
161 if (empty($setting) || ! is_string($setting)) {
162 throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
163 }
164
165 // During the ConfigIO transition, map legacy settings to the new ones.
166 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
167 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
168 }
169
170 $settings = explode('.', $setting);
171 self::removeConfig($settings, $this->loadedConfig);
172 if ($write) {
173 $this->write($isLoggedIn);
174 }
175 }
176
59404d79
A
177 /**
178 * Check if a settings exists.
179 *
180 * Supports nested settings with dot separated keys.
181 *
182 * @param string $setting Asked setting, keys separated with dots.
183 *
184 * @return bool true if the setting exists, false otherwise.
185 */
186 public function exists($setting)
187 {
da10377b
A
188 // During the ConfigIO transition, map legacy settings to the new ones.
189 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
190 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
191 }
192
59404d79
A
193 $settings = explode('.', $setting);
194 $value = self::getConfig($settings, $this->loadedConfig);
195 if ($value === self::$NOT_FOUND) {
196 return false;
197 }
198 return true;
199 }
200
201 /**
202 * Call the config writer.
203 *
204 * @param bool $isLoggedIn User login state.
205 *
684e662a
A
206 * @return bool True if the configuration has been successfully written, false otherwise.
207 *
59404d79
A
208 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
209 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
f3d2f257 210 * @throws \Shaarli\Exceptions\IOException: an error occurred while writing the new config file.
59404d79
A
211 */
212 public function write($isLoggedIn)
213 {
214 // These fields are required in configuration.
215 $mandatoryFields = array(
da10377b
A
216 'credentials.login',
217 'credentials.hash',
218 'credentials.salt',
219 'security.session_protection_disabled',
220 'general.timezone',
221 'general.title',
222 'general.header_link',
894a3c4b
A
223 'privacy.default_private_links',
224 'redirector.url',
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
7f179985 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: ');
59404d79 369
894a3c4b
A
370 $this->setEmpty('updates.check_updates', false);
371 $this->setEmpty('updates.check_updates_branch', 'stable');
372 $this->setEmpty('updates.check_updates_interval', 86400);
373
374 $this->setEmpty('feed.rss_permalinks', true);
2ea89aba 375 $this->setEmpty('feed.show_atom', true);
894a3c4b
A
376
377 $this->setEmpty('privacy.default_private_links', false);
378 $this->setEmpty('privacy.hide_public_links', false);
27e21231 379 $this->setEmpty('privacy.force_login', false);
894a3c4b 380 $this->setEmpty('privacy.hide_timestamps', false);
2e07e775
WE
381 // default state of the 'remember me' checkbox of the login form
382 $this->setEmpty('privacy.remember_user_default', true);
894a3c4b 383
894a3c4b
A
384 $this->setEmpty('redirector.url', '');
385 $this->setEmpty('redirector.encode_url', true);
59404d79 386
7b4fea0e
A
387 $this->setEmpty('thumbnails.width', '125');
388 $this->setEmpty('thumbnails.height', '90');
389
12266213
A
390 $this->setEmpty('translation.language', 'auto');
391 $this->setEmpty('translation.mode', 'php');
392 $this->setEmpty('translation.extensions', []);
393
59404d79
A
394 $this->setEmpty('plugins', array());
395 }
396
397 /**
398 * Set only if the setting does not exists.
399 *
400 * @param string $key Setting key.
401 * @param mixed $value Setting value.
402 */
7f179985 403 public function setEmpty($key, $value)
59404d79
A
404 {
405 if (! $this->exists($key)) {
406 $this->set($key, $value);
407 }
408 }
684e662a
A
409
410 /**
411 * @return ConfigIO
412 */
413 public function getConfigIO()
414 {
415 return $this->configIO;
416 }
417
418 /**
419 * @param ConfigIO $configIO
420 */
421 public function setConfigIO($configIO)
422 {
423 $this->configIO = $configIO;
424 }
59404d79 425}