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 * Check if a settings exists.
153 * Supports nested settings with dot separated keys.
155 * @param string $setting Asked setting, keys separated with dots.
157 * @return bool true if the setting exists, false otherwise.
159 public function exists($setting)
161 // During the ConfigIO transition, map legacy settings to the new ones.
162 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
163 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
166 $settings = explode('.', $setting);
167 $value = self
::getConfig($settings, $this->loadedConfig
);
168 if ($value === self
::$NOT_FOUND) {
175 * Call the config writer.
177 * @param bool $isLoggedIn User login state.
179 * @return bool True if the configuration has been successfully written, false otherwise.
181 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
182 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
183 * @throws \IOException: an error occurred while writing the new config file.
185 public function write($isLoggedIn)
187 // These fields are required in configuration.
188 $mandatoryFields = array(
192 'security.session_protection_disabled',
195 'general.header_link',
196 'privacy.default_private_links',
200 // Only logged in user can alter config.
201 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
202 throw new UnauthorizedConfigException();
205 // Check that all mandatory fields are provided in $conf.
206 foreach ($mandatoryFields as $field) {
207 if (! $this->exists($field)) {
208 throw new MissingFieldConfigException($field);
212 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
216 * Set the config file path (without extension).
218 * @param string $configFile File path.
220 public function setConfigFile($configFile)
222 $this->configFile
= $configFile;
226 * Return the configuration file path (without extension).
228 * @return string Config path.
230 public function getConfigFile()
232 return $this->configFile
;
236 * Get the configuration file path with its extension.
238 * @return string Config file path.
240 public function getConfigFileExt()
242 return $this->configFile
. $this->configIO
->getExtension();
246 * Recursive function which find asked setting in the loaded config.
248 * @param array $settings Ordered array which contains keys to find.
249 * @param array $conf Loaded settings, then sub-array.
251 * @return mixed Found setting or NOT_FOUND flag.
253 protected static function getConfig($settings, $conf)
255 if (!is_array($settings) || count($settings) == 0) {
256 return self
::$NOT_FOUND;
259 $setting = array_shift($settings);
260 if (!isset($conf[$setting])) {
261 return self
::$NOT_FOUND;
264 if (count($settings) > 0) {
265 return self
::getConfig($settings, $conf[$setting]);
267 return $conf[$setting];
271 * Recursive function which find asked setting in the loaded config.
273 * @param array $settings Ordered array which contains keys to find.
274 * @param mixed $value
275 * @param array $conf Loaded settings, then sub-array.
277 * @return mixed Found setting or NOT_FOUND flag.
279 protected static function setConfig($settings, $value, &$conf)
281 if (!is_array($settings) || count($settings) == 0) {
282 return self
::$NOT_FOUND;
285 $setting = array_shift($settings);
286 if (count($settings) > 0) {
287 return self
::setConfig($settings, $value, $conf[$setting]);
289 $conf[$setting] = $value;
293 * Set a bunch of default values allowing Shaarli to start without a config file.
295 protected function setDefaultValues()
297 $this->setEmpty('resource.data_dir', 'data');
298 $this->setEmpty('resource.config', 'data/config.php');
299 $this->setEmpty('resource.datastore', 'data/datastore.php');
300 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
301 $this->setEmpty('resource.updates', 'data/updates.txt');
302 $this->setEmpty('resource.log', 'data/log.txt');
303 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
304 $this->setEmpty('resource.history', 'data/history.php');
305 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
306 $this->setEmpty('resource.theme', 'default');
307 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
308 $this->setEmpty('resource.thumbnails_cache', 'cache');
309 $this->setEmpty('resource.page_cache', 'pagecache');
311 $this->setEmpty('security.ban_after', 4);
312 $this->setEmpty('security.ban_duration', 1800);
313 $this->setEmpty('security.session_protection_disabled', false);
314 $this->setEmpty('security.open_shaarli', false);
315 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
317 $this->setEmpty('general.header_link', '?');
318 $this->setEmpty('general.links_per_page', 20);
319 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
320 $this->setEmpty('general.default_note_title', 'Note: ');
322 $this->setEmpty('updates.check_updates', false);
323 $this->setEmpty('updates.check_updates_branch', 'stable');
324 $this->setEmpty('updates.check_updates_interval', 86400);
326 $this->setEmpty('feed.rss_permalinks', true);
327 $this->setEmpty('feed.show_atom', true);
329 $this->setEmpty('privacy.default_private_links', false);
330 $this->setEmpty('privacy.hide_public_links', false);
331 $this->setEmpty('privacy.force_login', false);
332 $this->setEmpty('privacy.hide_timestamps', false);
333 // default state of the 'remember me' checkbox of the login form
334 $this->setEmpty('privacy.remember_user_default', true);
336 $this->setEmpty('thumbnail.enable_thumbnails', true);
337 $this->setEmpty('thumbnail.enable_localcache', true);
339 $this->setEmpty('redirector.url', '');
340 $this->setEmpty('redirector.encode_url', true);
342 $this->setEmpty('translation.language', 'auto');
343 $this->setEmpty('translation.mode', 'php');
344 $this->setEmpty('translation.extensions', []);
346 $this->setEmpty('plugins', array());
350 * Set only if the setting does not exists.
352 * @param string $key Setting key.
353 * @param mixed $value Setting value.
355 public function setEmpty($key, $value)
357 if (! $this->exists($key)) {
358 $this->set($key, $value);
365 public function getConfigIO()
367 return $this->configIO
;
371 * @param ConfigIO $configIO
373 public function setConfigIO($configIO)
375 $this->configIO
= $configIO;