3 namespace Shaarli\Config
;
5 use Shaarli\Config\Exception\MissingFieldConfigException
;
6 use Shaarli\Config\Exception\UnauthorizedConfigException
;
7 use Shaarli\Thumbnailer
;
12 * Manages all Shaarli's settings.
13 * See the documentation for more information on settings:
14 * - doc/md/Shaarli-configuration.md
15 * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration
20 * @var string Flag telling a setting is not found.
22 protected static $NOT_FOUND = 'NOT_FOUND';
24 public static $DEFAULT_PLUGINS = ['qrcode'];
27 * @var string Config folder.
29 protected $configFile;
32 * @var array Loaded config array.
34 protected $loadedConfig;
37 * @var ConfigIO implementation instance.
44 * @param string $configFile Configuration file path without extension.
46 public function __construct($configFile = 'data/config')
48 $this->configFile
= $configFile;
53 * Reset the ConfigManager instance.
55 public function reset()
61 * Rebuild the loaded config array from config files.
63 public function reload()
69 * Initialize the ConfigIO and loaded the conf.
71 protected function initialize()
73 if (file_exists($this->configFile
. '.php')) {
74 $this->configIO
= new ConfigPhp();
76 $this->configIO
= new ConfigJson();
82 * Load configuration in the ConfigurationManager.
84 protected function load()
87 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
88 } catch (\Exception
$e) {
89 die($e->getMessage());
91 $this->setDefaultValues();
97 * Supports nested settings with dot separated keys.
98 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
100 * { "config": { "stuff": {"option": "mysetting" } } } }
102 * @param string $setting Asked setting, keys separated with dots.
103 * @param string $default Default value if not found.
105 * @return mixed Found setting, or the default value.
107 public function get($setting, $default = '')
109 // During the ConfigIO transition, map legacy settings to the new ones.
110 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
111 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
114 $settings = explode('.', $setting);
115 $value = self
::getConfig($settings, $this->loadedConfig
);
116 if ($value === self
::$NOT_FOUND) {
123 * Set a setting, and eventually write it.
125 * Supports nested settings with dot separated keys.
127 * @param string $setting Asked setting, keys separated with dots.
128 * @param mixed $value Value to set.
129 * @param bool $write Write the new setting in the config file, default false.
130 * @param bool $isLoggedIn User login state, default false.
132 * @throws \Exception Invalid
134 public function set($setting, $value, $write = false, $isLoggedIn = false)
136 if (empty($setting) || ! is_string($setting)) {
137 throw new \
Exception(t('Invalid setting key parameter. String expected, got: ') . gettype($setting));
140 // During the ConfigIO transition, map legacy settings to the new ones.
141 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
142 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
145 $settings = explode('.', $setting);
146 self
::setConfig($settings, $value, $this->loadedConfig
);
148 $this->write($isLoggedIn);
153 * Remove a config element from the config file.
155 * @param string $setting Asked setting, keys separated with dots.
156 * @param bool $write Write the new setting in the config file, default false.
157 * @param bool $isLoggedIn User login state, default false.
159 * @throws \Exception Invalid
161 public function remove($setting, $write = false, $isLoggedIn = false)
163 if (empty($setting) || ! is_string($setting)) {
164 throw new \
Exception(t('Invalid setting key parameter. String expected, got: ') . gettype($setting));
167 // During the ConfigIO transition, map legacy settings to the new ones.
168 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
169 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
172 $settings = explode('.', $setting);
173 self
::removeConfig($settings, $this->loadedConfig
);
175 $this->write($isLoggedIn);
180 * Check if a settings exists.
182 * Supports nested settings with dot separated keys.
184 * @param string $setting Asked setting, keys separated with dots.
186 * @return bool true if the setting exists, false otherwise.
188 public function exists($setting)
190 // During the ConfigIO transition, map legacy settings to the new ones.
191 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
192 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
195 $settings = explode('.', $setting);
196 $value = self
::getConfig($settings, $this->loadedConfig
);
197 if ($value === self
::$NOT_FOUND) {
204 * Call the config writer.
206 * @param bool $isLoggedIn User login state.
208 * @return bool True if the configuration has been successfully written, false otherwise.
210 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
211 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
212 * @throws \Shaarli\Exceptions\IOException: an error occurred while writing the new config file.
214 public function write($isLoggedIn)
216 // These fields are required in configuration.
221 'security.session_protection_disabled',
224 'general.header_link',
225 'privacy.default_private_links',
228 // Only logged in user can alter config.
229 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
230 throw new UnauthorizedConfigException();
233 // Check that all mandatory fields are provided in $conf.
234 foreach ($mandatoryFields as $field) {
235 if (! $this->exists($field)) {
236 throw new MissingFieldConfigException($field);
240 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
244 * Set the config file path (without extension).
246 * @param string $configFile File path.
248 public function setConfigFile($configFile)
250 $this->configFile
= $configFile;
254 * Return the configuration file path (without extension).
256 * @return string Config path.
258 public function getConfigFile()
260 return $this->configFile
;
264 * Get the configuration file path with its extension.
266 * @return string Config file path.
268 public function getConfigFileExt()
270 return $this->configFile
. $this->configIO
->getExtension();
274 * Recursive function which find asked setting in the loaded config.
276 * @param array $settings Ordered array which contains keys to find.
277 * @param array $conf Loaded settings, then sub-array.
279 * @return mixed Found setting or NOT_FOUND flag.
281 protected static function getConfig($settings, $conf)
283 if (!is_array($settings) || count($settings) == 0) {
284 return self
::$NOT_FOUND;
287 $setting = array_shift($settings);
288 if (!isset($conf[$setting])) {
289 return self
::$NOT_FOUND;
292 if (count($settings) > 0) {
293 return self
::getConfig($settings, $conf[$setting]);
295 return $conf[$setting];
299 * Recursive function which find asked setting in the loaded config.
301 * @param array $settings Ordered array which contains keys to find.
302 * @param mixed $value
303 * @param array $conf Loaded settings, then sub-array.
305 * @return mixed Found setting or NOT_FOUND flag.
307 protected static function setConfig($settings, $value, &$conf)
309 if (!is_array($settings) || count($settings) == 0) {
310 return self
::$NOT_FOUND;
313 $setting = array_shift($settings);
314 if (count($settings) > 0) {
315 return self
::setConfig($settings, $value, $conf[$setting]);
317 $conf[$setting] = $value;
321 * Recursive function which find asked setting in the loaded config and deletes it.
323 * @param array $settings Ordered array which contains keys to find.
324 * @param array $conf Loaded settings, then sub-array.
326 * @return mixed Found setting or NOT_FOUND flag.
328 protected static function removeConfig($settings, &$conf)
330 if (!is_array($settings) || count($settings) == 0) {
331 return self
::$NOT_FOUND;
334 $setting = array_shift($settings);
335 if (count($settings) > 0) {
336 return self
::removeConfig($settings, $conf[$setting]);
338 unset($conf[$setting]);
342 * Set a bunch of default values allowing Shaarli to start without a config file.
344 protected function setDefaultValues()
346 $this->setEmpty('resource.data_dir', 'data');
347 $this->setEmpty('resource.config', 'data/config.php');
348 $this->setEmpty('resource.datastore', 'data/datastore.php');
349 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
350 $this->setEmpty('resource.updates', 'data/updates.txt');
351 $this->setEmpty('resource.log', 'data/log.txt');
352 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
353 $this->setEmpty('resource.history', 'data/history.php');
354 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
355 $this->setEmpty('resource.theme', 'default');
356 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
357 $this->setEmpty('resource.thumbnails_cache', 'cache');
358 $this->setEmpty('resource.page_cache', 'pagecache');
360 $this->setEmpty('security.ban_after', 4);
361 $this->setEmpty('security.ban_duration', 1800);
362 $this->setEmpty('security.session_protection_disabled', false);
363 $this->setEmpty('security.open_shaarli', false);
364 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
366 $this->setEmpty('general.header_link', '/');
367 $this->setEmpty('general.links_per_page', 20);
368 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
369 $this->setEmpty('general.default_note_title', 'Note: ');
370 $this->setEmpty('general.retrieve_description', true);
371 $this->setEmpty('general.enable_async_metadata', true);
372 $this->setEmpty('general.tags_separator', ' ');
374 $this->setEmpty('updates.check_updates', true);
375 $this->setEmpty('updates.check_updates_branch', 'latest');
376 $this->setEmpty('updates.check_updates_interval', 86400);
378 $this->setEmpty('feed.rss_permalinks', true);
379 $this->setEmpty('feed.show_atom', true);
381 $this->setEmpty('privacy.default_private_links', false);
382 $this->setEmpty('privacy.hide_public_links', false);
383 $this->setEmpty('privacy.force_login', false);
384 $this->setEmpty('privacy.hide_timestamps', false);
385 // default state of the 'remember me' checkbox of the login form
386 $this->setEmpty('privacy.remember_user_default', true);
388 $this->setEmpty('thumbnails.mode', Thumbnailer
::MODE_ALL
);
389 $this->setEmpty('thumbnails.width', '125');
390 $this->setEmpty('thumbnails.height', '90');
392 $this->setEmpty('translation.language', 'auto');
393 $this->setEmpty('translation.mode', 'php');
394 $this->setEmpty('translation.extensions', []);
396 $this->setEmpty('plugins', []);
398 $this->setEmpty('formatter', 'markdown');
402 * Set only if the setting does not exists.
404 * @param string $key Setting key.
405 * @param mixed $value Setting value.
407 public function setEmpty($key, $value)
409 if (! $this->exists($key)) {
410 $this->set($key, $value);
417 public function getConfigIO()
419 return $this->configIO
;
423 * @param ConfigIO $configIO
425 public function setConfigIO($configIO)
427 $this->configIO
= $configIO;