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 User space.
30 * @var string Config folder.
32 protected $configFile;
35 * @var array Loaded config array.
37 protected $loadedConfig;
40 * @var ConfigIO implementation instance.
47 * @param string $configFile Configuration file path without extension.
49 public function __construct($configFile = null, $userSpace = null)
51 $this->userSpace
= $this->findLDAPUser($userSpace);
52 if ($configFile !== null) {
53 $this->configFile
= $configFile;
55 $this->configFile
= ($this->userSpace
=== null) ? 'data/config' : 'data/' . $this->userSpace
. '/config';
60 public function findLDAPUser($login, $password = null) {
61 $connect = ldap_connect(getenv('SHAARLI_LDAP_HOST'));
62 ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION
, 3);
63 if (!$connect || !ldap_bind($connect, getenv('SHAARLI_LDAP_DN'), getenv('SHAARLI_LDAP_PASSWORD'))) {
67 $search_query = str_replace('%login%', ldap_escape($login), getenv('SHAARLI_LDAP_FILTER'));
69 $search = ldap_search($connect, getenv('SHAARLI_LDAP_BASE'), $search_query);
70 $info = ldap_get_entries($connect, $search);
72 if (ldap_count_entries($connect, $search) == 1 && (is_null($password) || ldap_bind($connect, $info[0]["dn"], $password))) {
80 * Reset the ConfigManager instance.
82 public function reset()
88 * Rebuild the loaded config array from config files.
90 public function reload()
96 * Initialize the ConfigIO and loaded the conf.
98 protected function initialize()
100 if (file_exists($this->configFile
. '.php')) {
101 $this->configIO
= new ConfigPhp();
103 $this->configIO
= new ConfigJson();
109 * Load configuration in the ConfigurationManager.
111 protected function load()
114 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
115 } catch (\Exception
$e) {
116 die($e->getMessage());
118 $this->setDefaultValues();
124 * Supports nested settings with dot separated keys.
125 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
127 * { "config": { "stuff": {"option": "mysetting" } } } }
129 * @param string $setting Asked setting, keys separated with dots.
130 * @param string $default Default value if not found.
132 * @return mixed Found setting, or the default value.
134 public function get($setting, $default = '')
136 // During the ConfigIO transition, map legacy settings to the new ones.
137 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
138 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
141 $settings = explode('.', $setting);
142 $value = self
::getConfig($settings, $this->loadedConfig
);
143 if ($value === self
::$NOT_FOUND) {
150 * Set a setting, and eventually write it.
152 * Supports nested settings with dot separated keys.
154 * @param string $setting Asked setting, keys separated with dots.
155 * @param mixed $value Value to set.
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 set($setting, $value, $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
::setConfig($settings, $value, $this->loadedConfig
);
175 $this->write($isLoggedIn);
180 * Remove a config element from the config file.
182 * @param string $setting Asked setting, keys separated with dots.
183 * @param bool $write Write the new setting in the config file, default false.
184 * @param bool $isLoggedIn User login state, default false.
186 * @throws \Exception Invalid
188 public function remove($setting, $write = false, $isLoggedIn = false)
190 if (empty($setting) || ! is_string($setting)) {
191 throw new \
Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
194 // During the ConfigIO transition, map legacy settings to the new ones.
195 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
196 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
199 $settings = explode('.', $setting);
200 self
::removeConfig($settings, $this->loadedConfig
);
202 $this->write($isLoggedIn);
207 * Check if a settings exists.
209 * Supports nested settings with dot separated keys.
211 * @param string $setting Asked setting, keys separated with dots.
213 * @return bool true if the setting exists, false otherwise.
215 public function exists($setting)
217 // During the ConfigIO transition, map legacy settings to the new ones.
218 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
219 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
222 $settings = explode('.', $setting);
223 $value = self
::getConfig($settings, $this->loadedConfig
);
224 if ($value === self
::$NOT_FOUND) {
231 * Call the config writer.
233 * @param bool $isLoggedIn User login state.
235 * @return bool True if the configuration has been successfully written, false otherwise.
237 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
238 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
239 * @throws \IOException: an error occurred while writing the new config file.
241 public function write($isLoggedIn)
243 // These fields are required in configuration.
244 $mandatoryFields = array(
248 'security.session_protection_disabled',
251 'general.header_link',
252 'privacy.default_private_links',
256 // Only logged in user can alter config.
257 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
258 throw new UnauthorizedConfigException();
261 // Check that all mandatory fields are provided in $conf.
262 foreach ($mandatoryFields as $field) {
263 if (! $this->exists($field)) {
264 throw new MissingFieldConfigException($field);
268 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
272 * Set the config file path (without extension).
274 * @param string $configFile File path.
276 public function setConfigFile($configFile)
278 $this->configFile
= $configFile;
282 * Return the configuration file path (without extension).
284 * @return string Config path.
286 public function getConfigFile()
288 return $this->configFile
;
292 * Get the configuration file path with its extension.
294 * @return string Config file path.
296 public function getConfigFileExt()
298 return $this->configFile
. $this->configIO
->getExtension();
302 * Get the current userspace.
304 * @return mixed User space.
306 public function getUserSpace()
308 return $this->userSpace
;
312 * Recursive function which find asked setting in the loaded config.
314 * @param array $settings Ordered array which contains keys to find.
315 * @param array $conf Loaded settings, then sub-array.
317 * @return mixed Found setting or NOT_FOUND flag.
319 protected static function getConfig($settings, $conf)
321 if (!is_array($settings) || count($settings) == 0) {
322 return self
::$NOT_FOUND;
325 $setting = array_shift($settings);
326 if (!isset($conf[$setting])) {
327 return self
::$NOT_FOUND;
330 if (count($settings) > 0) {
331 return self
::getConfig($settings, $conf[$setting]);
333 return $conf[$setting];
337 * Recursive function which find asked setting in the loaded config.
339 * @param array $settings Ordered array which contains keys to find.
340 * @param mixed $value
341 * @param array $conf Loaded settings, then sub-array.
343 * @return mixed Found setting or NOT_FOUND flag.
345 protected static function setConfig($settings, $value, &$conf)
347 if (!is_array($settings) || count($settings) == 0) {
348 return self
::$NOT_FOUND;
351 $setting = array_shift($settings);
352 if (count($settings) > 0) {
353 return self
::setConfig($settings, $value, $conf[$setting]);
355 $conf[$setting] = $value;
359 * Recursive function which find asked setting in the loaded config and deletes it.
361 * @param array $settings Ordered array which contains keys to find.
362 * @param array $conf Loaded settings, then sub-array.
364 * @return mixed Found setting or NOT_FOUND flag.
366 protected static function removeConfig($settings, &$conf)
368 if (!is_array($settings) || count($settings) == 0) {
369 return self
::$NOT_FOUND;
372 $setting = array_shift($settings);
373 if (count($settings) > 0) {
374 return self
::removeConfig($settings, $conf[$setting]);
376 unset($conf[$setting]);
380 * Set a bunch of default values allowing Shaarli to start without a config file.
382 protected function setDefaultValues()
384 if ($this->userSpace
=== null) {
388 $pagecache = 'pagecache';
390 $data = 'data/' . ($this->userSpace
);
391 $tmp = 'tmp/' . ($this->userSpace
);
392 $cache = 'cache/' . ($this->userSpace
);
393 $pagecache = 'pagecache/' . ($this->userSpace
);
396 $this->setEmpty('resource.data_dir', $data);
397 $this->setEmpty('resource.config', $data . '/config.php');
398 $this->setEmpty('resource.datastore', $data . '/datastore.php');
399 $this->setEmpty('resource.ban_file', $data . '/ipbans.php');
400 $this->setEmpty('resource.updates', $data . '/updates.txt');
401 $this->setEmpty('resource.log', $data . '/log.txt');
402 $this->setEmpty('resource.update_check', $data . '/lastupdatecheck.txt');
403 $this->setEmpty('resource.history', $data . '/history.php');
404 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
405 $this->setEmpty('resource.theme', 'default');
406 $this->setEmpty('resource.raintpl_tmp', $tmp);
407 $this->setEmpty('resource.thumbnails_cache', $cache);
408 $this->setEmpty('resource.page_cache', $pagecache);
410 $this->setEmpty('security.ban_after', 4);
411 $this->setEmpty('security.ban_duration', 1800);
412 $this->setEmpty('security.session_protection_disabled', false);
413 $this->setEmpty('security.open_shaarli', false);
414 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
416 $this->setEmpty('general.header_link', '?');
417 $this->setEmpty('general.links_per_page', 20);
418 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
419 $this->setEmpty('general.default_note_title', 'Note: ');
421 $this->setEmpty('updates.check_updates', false);
422 $this->setEmpty('updates.check_updates_branch', 'stable');
423 $this->setEmpty('updates.check_updates_interval', 86400);
425 $this->setEmpty('feed.rss_permalinks', true);
426 $this->setEmpty('feed.show_atom', true);
428 $this->setEmpty('privacy.default_private_links', false);
429 $this->setEmpty('privacy.hide_public_links', false);
430 $this->setEmpty('privacy.force_login', false);
431 $this->setEmpty('privacy.hide_timestamps', false);
432 // default state of the 'remember me' checkbox of the login form
433 $this->setEmpty('privacy.remember_user_default', true);
435 $this->setEmpty('redirector.url', '');
436 $this->setEmpty('redirector.encode_url', true);
438 $this->setEmpty('thumbnails.width', '125');
439 $this->setEmpty('thumbnails.height', '90');
441 $this->setEmpty('translation.language', 'auto');
442 $this->setEmpty('translation.mode', 'php');
443 $this->setEmpty('translation.extensions', []);
445 $this->setEmpty('plugins', array());
449 * Set only if the setting does not exists.
451 * @param string $key Setting key.
452 * @param mixed $value Setting value.
454 public function setEmpty($key, $value)
456 if (! $this->exists($key)) {
457 $this->set($key, $value);
464 public function getConfigIO()
466 return $this->configIO
;
470 * @param ConfigIO $configIO
472 public function setConfigIO($configIO)
474 $this->configIO
= $configIO;