2 namespace Shaarli\Config
;
4 use Shaarli\Config\Exception\MissingFieldConfigException
;
5 use Shaarli\Config\Exception\UnauthorizedConfigException
;
6 use Shaarli\Thumbnailer
;
11 * Manages all Shaarli's settings.
12 * See the documentation for more information on settings:
13 * - doc/md/Shaarli-configuration.md
14 * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration
19 * @var string Flag telling a setting is not found.
21 protected static $NOT_FOUND = 'NOT_FOUND';
23 public static $DEFAULT_PLUGINS = array('qrcode');
26 * @var string Config folder.
28 protected $configFile;
31 * @var array Loaded config array.
33 protected $loadedConfig;
36 * @var ConfigIO implementation instance.
43 * @param string $configFile Configuration file path without extension.
45 public function __construct($configFile = 'data/config')
47 $this->configFile
= $configFile;
52 * Reset the ConfigManager instance.
54 public function reset()
60 * Rebuild the loaded config array from config files.
62 public function reload()
68 * Initialize the ConfigIO and loaded the conf.
70 protected function initialize()
72 if (file_exists($this->configFile
. '.php')) {
73 $this->configIO
= new ConfigPhp();
75 $this->configIO
= new ConfigJson();
81 * Load configuration in the ConfigurationManager.
83 protected function load()
86 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
87 } catch (\Exception
$e) {
88 die($e->getMessage());
90 $this->setDefaultValues();
96 * Supports nested settings with dot separated keys.
97 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
99 * { "config": { "stuff": {"option": "mysetting" } } } }
101 * @param string $setting Asked setting, keys separated with dots.
102 * @param string $default Default value if not found.
104 * @return mixed Found setting, or the default value.
106 public function get($setting, $default = '')
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];
113 $settings = explode('.', $setting);
114 $value = self
::getConfig($settings, $this->loadedConfig
);
115 if ($value === self
::$NOT_FOUND) {
122 * Set a setting, and eventually write it.
124 * Supports nested settings with dot separated keys.
126 * @param string $setting Asked setting, keys separated with dots.
127 * @param mixed $value Value to set.
128 * @param bool $write Write the new setting in the config file, default false.
129 * @param bool $isLoggedIn User login state, default false.
131 * @throws \Exception Invalid
133 public function set($setting, $value, $write = false, $isLoggedIn = false)
135 if (empty($setting) || ! is_string($setting)) {
136 throw new \
Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
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];
144 $settings = explode('.', $setting);
145 self
::setConfig($settings, $value, $this->loadedConfig
);
147 $this->write($isLoggedIn);
152 * Remove a config element from the config file.
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.
158 * @throws \Exception Invalid
160 public function remove($setting, $write = false, $isLoggedIn = false)
162 if (empty($setting) || ! is_string($setting)) {
163 throw new \
Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
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];
171 $settings = explode('.', $setting);
172 self
::removeConfig($settings, $this->loadedConfig
);
174 $this->write($isLoggedIn);
179 * Check if a settings exists.
181 * Supports nested settings with dot separated keys.
183 * @param string $setting Asked setting, keys separated with dots.
185 * @return bool true if the setting exists, false otherwise.
187 public function exists($setting)
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];
194 $settings = explode('.', $setting);
195 $value = self
::getConfig($settings, $this->loadedConfig
);
196 if ($value === self
::$NOT_FOUND) {
203 * Call the config writer.
205 * @param bool $isLoggedIn User login state.
207 * @return bool True if the configuration has been successfully written, false otherwise.
209 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
210 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
211 * @throws \Shaarli\Exceptions\IOException: an error occurred while writing the new config file.
213 public function write($isLoggedIn)
215 // These fields are required in configuration.
216 $mandatoryFields = array(
220 'security.session_protection_disabled',
223 'general.header_link',
224 'privacy.default_private_links',
227 // Only logged in user can alter config.
228 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
229 throw new UnauthorizedConfigException();
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);
239 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
243 * Set the config file path (without extension).
245 * @param string $configFile File path.
247 public function setConfigFile($configFile)
249 $this->configFile
= $configFile;
253 * Return the configuration file path (without extension).
255 * @return string Config path.
257 public function getConfigFile()
259 return $this->configFile
;
263 * Get the configuration file path with its extension.
265 * @return string Config file path.
267 public function getConfigFileExt()
269 return $this->configFile
. $this->configIO
->getExtension();
273 * Recursive function which find asked setting in the loaded config.
275 * @param array $settings Ordered array which contains keys to find.
276 * @param array $conf Loaded settings, then sub-array.
278 * @return mixed Found setting or NOT_FOUND flag.
280 protected static function getConfig($settings, $conf)
282 if (!is_array($settings) || count($settings) == 0) {
283 return self
::$NOT_FOUND;
286 $setting = array_shift($settings);
287 if (!isset($conf[$setting])) {
288 return self
::$NOT_FOUND;
291 if (count($settings) > 0) {
292 return self
::getConfig($settings, $conf[$setting]);
294 return $conf[$setting];
298 * Recursive function which find asked setting in the loaded config.
300 * @param array $settings Ordered array which contains keys to find.
301 * @param mixed $value
302 * @param array $conf Loaded settings, then sub-array.
304 * @return mixed Found setting or NOT_FOUND flag.
306 protected static function setConfig($settings, $value, &$conf)
308 if (!is_array($settings) || count($settings) == 0) {
309 return self
::$NOT_FOUND;
312 $setting = array_shift($settings);
313 if (count($settings) > 0) {
314 return self
::setConfig($settings, $value, $conf[$setting]);
316 $conf[$setting] = $value;
320 * Recursive function which find asked setting in the loaded config and deletes it.
322 * @param array $settings Ordered array which contains keys to find.
323 * @param array $conf Loaded settings, then sub-array.
325 * @return mixed Found setting or NOT_FOUND flag.
327 protected static function removeConfig($settings, &$conf)
329 if (!is_array($settings) || count($settings) == 0) {
330 return self
::$NOT_FOUND;
333 $setting = array_shift($settings);
334 if (count($settings) > 0) {
335 return self
::removeConfig($settings, $conf[$setting]);
337 unset($conf[$setting]);
341 * Set a bunch of default values allowing Shaarli to start without a config file.
343 protected function setDefaultValues()
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');
352 $this->setEmpty('resource.history', 'data/history.php');
353 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
354 $this->setEmpty('resource.theme', 'default');
355 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
356 $this->setEmpty('resource.thumbnails_cache', 'cache');
357 $this->setEmpty('resource.page_cache', 'pagecache');
359 $this->setEmpty('security.ban_after', 4);
360 $this->setEmpty('security.ban_duration', 1800);
361 $this->setEmpty('security.session_protection_disabled', false);
362 $this->setEmpty('security.open_shaarli', false);
363 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
365 $this->setEmpty('general.header_link', '/');
366 $this->setEmpty('general.links_per_page', 20);
367 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
368 $this->setEmpty('general.default_note_title', 'Note: ');
369 $this->setEmpty('general.retrieve_description', true);
370 $this->setEmpty('general.enable_async_metadata', true);
372 $this->setEmpty('updates.check_updates', false);
373 $this->setEmpty('updates.check_updates_branch', 'stable');
374 $this->setEmpty('updates.check_updates_interval', 86400);
376 $this->setEmpty('feed.rss_permalinks', true);
377 $this->setEmpty('feed.show_atom', true);
379 $this->setEmpty('privacy.default_private_links', false);
380 $this->setEmpty('privacy.hide_public_links', false);
381 $this->setEmpty('privacy.force_login', false);
382 $this->setEmpty('privacy.hide_timestamps', false);
383 // default state of the 'remember me' checkbox of the login form
384 $this->setEmpty('privacy.remember_user_default', true);
386 $this->setEmpty('thumbnails.mode', Thumbnailer
::MODE_ALL
);
387 $this->setEmpty('thumbnails.width', '125');
388 $this->setEmpty('thumbnails.height', '90');
390 $this->setEmpty('translation.language', 'auto');
391 $this->setEmpty('translation.mode', 'php');
392 $this->setEmpty('translation.extensions', []);
394 $this->setEmpty('plugins', array());
396 $this->setEmpty('formatter', 'markdown');
400 * Set only if the setting does not exists.
402 * @param string $key Setting key.
403 * @param mixed $value Setting value.
405 public function setEmpty($key, $value)
407 if (! $this->exists($key)) {
408 $this->set($key, $value);
415 public function getConfigIO()
417 return $this->configIO
;
421 * @param ConfigIO $configIO
423 public function setConfigIO($configIO)
425 $this->configIO
= $configIO;