2 namespace Shaarli\Config
;
4 use Shaarli\Config\Exception\MissingFieldConfigException
;
5 use Shaarli\Config\Exception\UnauthorizedConfigException
;
10 * Manages all Shaarli's settings.
11 * See the documentation for more information on settings:
12 * - doc/md/Shaarli-configuration.md
13 * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration
18 * @var string Flag telling a setting is not found.
20 protected static $NOT_FOUND = 'NOT_FOUND';
22 public static $DEFAULT_PLUGINS = array('qrcode');
25 * @var string Config folder.
27 protected $configFile;
30 * @var array Loaded config array.
32 protected $loadedConfig;
35 * @var ConfigIO implementation instance.
42 * @param string $configFile Configuration file path without extension.
44 public function __construct($configFile = 'data/config')
46 $this->configFile
= $configFile;
51 * Reset the ConfigManager instance.
53 public function reset()
59 * Rebuild the loaded config array from config files.
61 public function reload()
67 * Initialize the ConfigIO and loaded the conf.
69 protected function initialize()
71 if (file_exists($this->configFile
. '.php')) {
72 $this->configIO
= new ConfigPhp();
74 $this->configIO
= new ConfigJson();
80 * Load configuration in the ConfigurationManager.
82 protected function load()
85 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
86 } catch (\Exception
$e) {
87 die($e->getMessage());
89 $this->setDefaultValues();
95 * Supports nested settings with dot separated keys.
96 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
98 * { "config": { "stuff": {"option": "mysetting" } } } }
100 * @param string $setting Asked setting, keys separated with dots.
101 * @param string $default Default value if not found.
103 * @return mixed Found setting, or the default value.
105 public function get($setting, $default = '')
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];
112 $settings = explode('.', $setting);
113 $value = self
::getConfig($settings, $this->loadedConfig
);
114 if ($value === self
::$NOT_FOUND) {
121 * Set a setting, and eventually write it.
123 * Supports nested settings with dot separated keys.
125 * @param string $setting Asked setting, keys separated with dots.
126 * @param mixed $value Value to set.
127 * @param bool $write Write the new setting in the config file, default false.
128 * @param bool $isLoggedIn User login state, default false.
130 * @throws \Exception Invalid
132 public function set($setting, $value, $write = false, $isLoggedIn = false)
134 if (empty($setting) || ! is_string($setting)) {
135 throw new \
Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
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];
143 $settings = explode('.', $setting);
144 self
::setConfig($settings, $value, $this->loadedConfig
);
146 $this->write($isLoggedIn);
151 * Remove a config element from the config file.
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.
157 * @throws \Exception Invalid
159 public function remove($setting, $write = false, $isLoggedIn = false)
161 if (empty($setting) || ! is_string($setting)) {
162 throw new \
Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
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];
170 $settings = explode('.', $setting);
171 self
::removeConfig($settings, $this->loadedConfig
);
173 $this->write($isLoggedIn);
178 * Check if a settings exists.
180 * Supports nested settings with dot separated keys.
182 * @param string $setting Asked setting, keys separated with dots.
184 * @return bool true if the setting exists, false otherwise.
186 public function exists($setting)
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];
193 $settings = explode('.', $setting);
194 $value = self
::getConfig($settings, $this->loadedConfig
);
195 if ($value === self
::$NOT_FOUND) {
202 * Call the config writer.
204 * @param bool $isLoggedIn User login state.
206 * @return bool True if the configuration has been successfully written, false otherwise.
208 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
209 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
210 * @throws \Shaarli\Exceptions\IOException: an error occurred while writing the new config file.
212 public function write($isLoggedIn)
214 // These fields are required in configuration.
215 $mandatoryFields = array(
219 'security.session_protection_disabled',
222 'general.header_link',
223 'privacy.default_private_links',
226 // Only logged in user can alter config.
227 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
228 throw new UnauthorizedConfigException();
231 // Check that all mandatory fields are provided in $conf.
232 foreach ($mandatoryFields as $field) {
233 if (! $this->exists($field)) {
234 throw new MissingFieldConfigException($field);
238 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
242 * Set the config file path (without extension).
244 * @param string $configFile File path.
246 public function setConfigFile($configFile)
248 $this->configFile
= $configFile;
252 * Return the configuration file path (without extension).
254 * @return string Config path.
256 public function getConfigFile()
258 return $this->configFile
;
262 * Get the configuration file path with its extension.
264 * @return string Config file path.
266 public function getConfigFileExt()
268 return $this->configFile
. $this->configIO
->getExtension();
272 * Recursive function which find asked setting in the loaded config.
274 * @param array $settings Ordered array which contains keys to find.
275 * @param array $conf Loaded settings, then sub-array.
277 * @return mixed Found setting or NOT_FOUND flag.
279 protected static function getConfig($settings, $conf)
281 if (!is_array($settings) || count($settings) == 0) {
282 return self
::$NOT_FOUND;
285 $setting = array_shift($settings);
286 if (!isset($conf[$setting])) {
287 return self
::$NOT_FOUND;
290 if (count($settings) > 0) {
291 return self
::getConfig($settings, $conf[$setting]);
293 return $conf[$setting];
297 * Recursive function which find asked setting in the loaded config.
299 * @param array $settings Ordered array which contains keys to find.
300 * @param mixed $value
301 * @param array $conf Loaded settings, then sub-array.
303 * @return mixed Found setting or NOT_FOUND flag.
305 protected static function setConfig($settings, $value, &$conf)
307 if (!is_array($settings) || count($settings) == 0) {
308 return self
::$NOT_FOUND;
311 $setting = array_shift($settings);
312 if (count($settings) > 0) {
313 return self
::setConfig($settings, $value, $conf[$setting]);
315 $conf[$setting] = $value;
319 * Recursive function which find asked setting in the loaded config and deletes it.
321 * @param array $settings Ordered array which contains keys to find.
322 * @param array $conf Loaded settings, then sub-array.
324 * @return mixed Found setting or NOT_FOUND flag.
326 protected static function removeConfig($settings, &$conf)
328 if (!is_array($settings) || count($settings) == 0) {
329 return self
::$NOT_FOUND;
332 $setting = array_shift($settings);
333 if (count($settings) > 0) {
334 return self
::removeConfig($settings, $conf[$setting]);
336 unset($conf[$setting]);
340 * Set a bunch of default values allowing Shaarli to start without a config file.
342 protected function setDefaultValues()
344 $this->setEmpty('resource.data_dir', 'data');
345 $this->setEmpty('resource.config', 'data/config.php');
346 $this->setEmpty('resource.datastore', 'data/datastore.php');
347 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
348 $this->setEmpty('resource.updates', 'data/updates.txt');
349 $this->setEmpty('resource.log', 'data/log.txt');
350 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
351 $this->setEmpty('resource.history', 'data/history.php');
352 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
353 $this->setEmpty('resource.theme', 'default');
354 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
355 $this->setEmpty('resource.thumbnails_cache', 'cache');
356 $this->setEmpty('resource.page_cache', 'pagecache');
358 $this->setEmpty('security.ban_after', 4);
359 $this->setEmpty('security.ban_duration', 1800);
360 $this->setEmpty('security.session_protection_disabled', false);
361 $this->setEmpty('security.open_shaarli', false);
362 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
364 $this->setEmpty('general.header_link', '?');
365 $this->setEmpty('general.links_per_page', 20);
366 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
367 $this->setEmpty('general.default_note_title', 'Note: ');
369 $this->setEmpty('updates.check_updates', false);
370 $this->setEmpty('updates.check_updates_branch', 'stable');
371 $this->setEmpty('updates.check_updates_interval', 86400);
373 $this->setEmpty('feed.rss_permalinks', true);
374 $this->setEmpty('feed.show_atom', true);
376 $this->setEmpty('privacy.default_private_links', false);
377 $this->setEmpty('privacy.hide_public_links', false);
378 $this->setEmpty('privacy.force_login', false);
379 $this->setEmpty('privacy.hide_timestamps', false);
380 // default state of the 'remember me' checkbox of the login form
381 $this->setEmpty('privacy.remember_user_default', true);
383 $this->setEmpty('thumbnails.width', '125');
384 $this->setEmpty('thumbnails.height', '90');
386 $this->setEmpty('translation.language', 'auto');
387 $this->setEmpty('translation.mode', 'php');
388 $this->setEmpty('translation.extensions', []);
390 $this->setEmpty('plugins', array());
394 * Set only if the setting does not exists.
396 * @param string $key Setting key.
397 * @param mixed $value Setting value.
399 public function setEmpty($key, $value)
401 if (! $this->exists($key)) {
402 $this->set($key, $value);
409 public function getConfigIO()
411 return $this->configIO
;
415 * @param ConfigIO $configIO
417 public function setConfigIO($configIO)
419 $this->configIO
= $configIO;