2016-05-18 21:43:59 +02:00
|
|
|
<?php
|
2017-03-03 23:06:12 +01:00
|
|
|
namespace Shaarli\Config;
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2017-03-08 20:28:33 +01:00
|
|
|
use Shaarli\Config\Exception\MissingFieldConfigException;
|
|
|
|
use Shaarli\Config\Exception\UnauthorizedConfigException;
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
/**
|
|
|
|
* Class ConfigManager
|
|
|
|
*
|
2016-06-09 20:04:02 +02:00
|
|
|
* Manages all Shaarli's settings.
|
2016-05-30 20:15:36 +02:00
|
|
|
* See the documentation for more information on settings:
|
2017-08-26 09:40:57 +02:00
|
|
|
* - doc/md/Shaarli-configuration.md
|
|
|
|
* - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
|
|
|
class ConfigManager
|
|
|
|
{
|
|
|
|
/**
|
2016-06-09 20:04:02 +02:00
|
|
|
* @var string Flag telling a setting is not found.
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
2016-06-09 20:04:02 +02:00
|
|
|
protected static $NOT_FOUND = 'NOT_FOUND';
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2016-12-15 10:13:00 +01:00
|
|
|
public static $DEFAULT_PLUGINS = array('qrcode');
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
/**
|
|
|
|
* @var string Config folder.
|
|
|
|
*/
|
2016-06-09 20:04:02 +02:00
|
|
|
protected $configFile;
|
2016-05-18 21:43:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Loaded config array.
|
|
|
|
*/
|
|
|
|
protected $loadedConfig;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ConfigIO implementation instance.
|
|
|
|
*/
|
|
|
|
protected $configIO;
|
|
|
|
|
|
|
|
/**
|
2016-06-09 20:04:02 +02:00
|
|
|
* Constructor.
|
2016-10-20 11:31:52 +02:00
|
|
|
*
|
|
|
|
* @param string $configFile Configuration file path without extension.
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
2016-06-09 20:04:02 +02:00
|
|
|
public function __construct($configFile = 'data/config')
|
2016-05-18 21:43:59 +02:00
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->configFile = $configFile;
|
|
|
|
$this->initialize();
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 21:48:24 +02:00
|
|
|
/**
|
|
|
|
* Reset the ConfigManager instance.
|
|
|
|
*/
|
2016-06-09 20:04:02 +02:00
|
|
|
public function reset()
|
2016-05-18 21:48:24 +02:00
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->initialize();
|
2016-05-18 21:48:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
/**
|
|
|
|
* Rebuild the loaded config array from config files.
|
|
|
|
*/
|
|
|
|
public function reload()
|
|
|
|
{
|
2016-05-18 21:48:24 +02:00
|
|
|
$this->load();
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-18 21:48:24 +02:00
|
|
|
* Initialize the ConfigIO and loaded the conf.
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
|
|
|
protected function initialize()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
if (file_exists($this->configFile . '.php')) {
|
2016-05-18 21:43:59 +02:00
|
|
|
$this->configIO = new ConfigPhp();
|
2016-06-09 20:04:02 +02:00
|
|
|
} else {
|
|
|
|
$this->configIO = new ConfigJson();
|
2016-05-29 12:32:14 +02:00
|
|
|
}
|
2016-05-18 21:48:24 +02:00
|
|
|
$this->load();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load configuration in the ConfigurationManager.
|
|
|
|
*/
|
|
|
|
protected function load()
|
|
|
|
{
|
2017-03-12 16:09:34 +01:00
|
|
|
try {
|
|
|
|
$this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
die($e->getMessage());
|
|
|
|
}
|
2016-05-18 21:43:59 +02:00
|
|
|
$this->setDefaultValues();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a setting.
|
|
|
|
*
|
|
|
|
* Supports nested settings with dot separated keys.
|
|
|
|
* Eg. 'config.stuff.option' will find $conf[config][stuff][option],
|
|
|
|
* or in JSON:
|
|
|
|
* { "config": { "stuff": {"option": "mysetting" } } } }
|
|
|
|
*
|
|
|
|
* @param string $setting Asked setting, keys separated with dots.
|
|
|
|
* @param string $default Default value if not found.
|
|
|
|
*
|
|
|
|
* @return mixed Found setting, or the default value.
|
|
|
|
*/
|
|
|
|
public function get($setting, $default = '')
|
|
|
|
{
|
2016-05-29 16:10:32 +02:00
|
|
|
// During the ConfigIO transition, map legacy settings to the new ones.
|
|
|
|
if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
|
|
|
|
$setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
|
|
|
|
}
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
$settings = explode('.', $setting);
|
|
|
|
$value = self::getConfig($settings, $this->loadedConfig);
|
|
|
|
if ($value === self::$NOT_FOUND) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a setting, and eventually write it.
|
|
|
|
*
|
|
|
|
* Supports nested settings with dot separated keys.
|
|
|
|
*
|
|
|
|
* @param string $setting Asked setting, keys separated with dots.
|
2018-01-24 19:38:03 +01:00
|
|
|
* @param mixed $value Value to set.
|
2016-05-18 21:43:59 +02:00
|
|
|
* @param bool $write Write the new setting in the config file, default false.
|
|
|
|
* @param bool $isLoggedIn User login state, default false.
|
2016-05-18 21:48:24 +02:00
|
|
|
*
|
2017-03-03 23:06:12 +01:00
|
|
|
* @throws \Exception Invalid
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
|
|
|
public function set($setting, $value, $write = false, $isLoggedIn = false)
|
|
|
|
{
|
2016-05-18 21:48:24 +02:00
|
|
|
if (empty($setting) || ! is_string($setting)) {
|
2017-05-09 18:12:15 +02:00
|
|
|
throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
|
2016-05-18 21:48:24 +02:00
|
|
|
}
|
|
|
|
|
2016-05-29 16:10:32 +02:00
|
|
|
// During the ConfigIO transition, map legacy settings to the new ones.
|
|
|
|
if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
|
|
|
|
$setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
|
|
|
|
}
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
$settings = explode('.', $setting);
|
|
|
|
self::setConfig($settings, $value, $this->loadedConfig);
|
|
|
|
if ($write) {
|
|
|
|
$this->write($isLoggedIn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a settings exists.
|
|
|
|
*
|
|
|
|
* Supports nested settings with dot separated keys.
|
|
|
|
*
|
|
|
|
* @param string $setting Asked setting, keys separated with dots.
|
|
|
|
*
|
|
|
|
* @return bool true if the setting exists, false otherwise.
|
|
|
|
*/
|
|
|
|
public function exists($setting)
|
|
|
|
{
|
2016-05-29 16:10:32 +02:00
|
|
|
// During the ConfigIO transition, map legacy settings to the new ones.
|
|
|
|
if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
|
|
|
|
$setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
|
|
|
|
}
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
$settings = explode('.', $setting);
|
|
|
|
$value = self::getConfig($settings, $this->loadedConfig);
|
|
|
|
if ($value === self::$NOT_FOUND) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the config writer.
|
|
|
|
*
|
|
|
|
* @param bool $isLoggedIn User login state.
|
|
|
|
*
|
2016-05-18 21:48:24 +02:00
|
|
|
* @return bool True if the configuration has been successfully written, false otherwise.
|
|
|
|
*
|
2016-05-18 21:43:59 +02:00
|
|
|
* @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
|
|
|
|
* @throws UnauthorizedConfigException: user is not authorize to change configuration.
|
2017-03-03 23:06:12 +01:00
|
|
|
* @throws \IOException: an error occurred while writing the new config file.
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
|
|
|
public function write($isLoggedIn)
|
|
|
|
{
|
|
|
|
// These fields are required in configuration.
|
|
|
|
$mandatoryFields = array(
|
2016-05-29 16:10:32 +02:00
|
|
|
'credentials.login',
|
|
|
|
'credentials.hash',
|
|
|
|
'credentials.salt',
|
|
|
|
'security.session_protection_disabled',
|
|
|
|
'general.timezone',
|
|
|
|
'general.title',
|
|
|
|
'general.header_link',
|
2016-06-11 09:08:02 +02:00
|
|
|
'privacy.default_private_links',
|
|
|
|
'redirector.url',
|
2016-05-18 21:43:59 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Only logged in user can alter config.
|
2016-06-09 20:04:02 +02:00
|
|
|
if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
|
2016-05-18 21:43:59 +02:00
|
|
|
throw new UnauthorizedConfigException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that all mandatory fields are provided in $conf.
|
|
|
|
foreach ($mandatoryFields as $field) {
|
|
|
|
if (! $this->exists($field)) {
|
|
|
|
throw new MissingFieldConfigException($field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:04:02 +02:00
|
|
|
return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig);
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-09 20:04:02 +02:00
|
|
|
* Set the config file path (without extension).
|
2016-05-18 21:43:59 +02:00
|
|
|
*
|
2016-06-09 20:04:02 +02:00
|
|
|
* @param string $configFile File path.
|
|
|
|
*/
|
|
|
|
public function setConfigFile($configFile)
|
|
|
|
{
|
|
|
|
$this->configFile = $configFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the configuration file path (without extension).
|
|
|
|
*
|
|
|
|
* @return string Config path.
|
2016-05-18 21:43:59 +02:00
|
|
|
*/
|
|
|
|
public function getConfigFile()
|
|
|
|
{
|
2016-06-09 20:04:02 +02:00
|
|
|
return $this->configFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the configuration file path with its extension.
|
|
|
|
*
|
|
|
|
* @return string Config file path.
|
|
|
|
*/
|
|
|
|
public function getConfigFileExt()
|
|
|
|
{
|
|
|
|
return $this->configFile . $this->configIO->getExtension();
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursive function which find asked setting in the loaded config.
|
|
|
|
*
|
|
|
|
* @param array $settings Ordered array which contains keys to find.
|
|
|
|
* @param array $conf Loaded settings, then sub-array.
|
|
|
|
*
|
|
|
|
* @return mixed Found setting or NOT_FOUND flag.
|
|
|
|
*/
|
|
|
|
protected static function getConfig($settings, $conf)
|
|
|
|
{
|
|
|
|
if (!is_array($settings) || count($settings) == 0) {
|
|
|
|
return self::$NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
$setting = array_shift($settings);
|
|
|
|
if (!isset($conf[$setting])) {
|
|
|
|
return self::$NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($settings) > 0) {
|
|
|
|
return self::getConfig($settings, $conf[$setting]);
|
|
|
|
}
|
|
|
|
return $conf[$setting];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursive function which find asked setting in the loaded config.
|
|
|
|
*
|
|
|
|
* @param array $settings Ordered array which contains keys to find.
|
|
|
|
* @param mixed $value
|
|
|
|
* @param array $conf Loaded settings, then sub-array.
|
|
|
|
*
|
|
|
|
* @return mixed Found setting or NOT_FOUND flag.
|
|
|
|
*/
|
|
|
|
protected static function setConfig($settings, $value, &$conf)
|
|
|
|
{
|
|
|
|
if (!is_array($settings) || count($settings) == 0) {
|
|
|
|
return self::$NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
$setting = array_shift($settings);
|
|
|
|
if (count($settings) > 0) {
|
|
|
|
return self::setConfig($settings, $value, $conf[$setting]);
|
|
|
|
}
|
|
|
|
$conf[$setting] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a bunch of default values allowing Shaarli to start without a config file.
|
|
|
|
*/
|
|
|
|
protected function setDefaultValues()
|
|
|
|
{
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('resource.data_dir', 'data');
|
|
|
|
$this->setEmpty('resource.config', 'data/config.php');
|
|
|
|
$this->setEmpty('resource.datastore', 'data/datastore.php');
|
|
|
|
$this->setEmpty('resource.ban_file', 'data/ipbans.php');
|
|
|
|
$this->setEmpty('resource.updates', 'data/updates.txt');
|
|
|
|
$this->setEmpty('resource.log', 'data/log.txt');
|
|
|
|
$this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
|
2017-01-16 12:31:08 +01:00
|
|
|
$this->setEmpty('resource.history', 'data/history.php');
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('resource.raintpl_tpl', 'tpl/');
|
2016-12-07 11:58:25 +01:00
|
|
|
$this->setEmpty('resource.theme', 'default');
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('resource.raintpl_tmp', 'tmp/');
|
|
|
|
$this->setEmpty('resource.thumbnails_cache', 'cache');
|
|
|
|
$this->setEmpty('resource.page_cache', 'pagecache');
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2016-05-29 16:10:32 +02:00
|
|
|
$this->setEmpty('security.ban_after', 4);
|
2016-06-09 20:04:02 +02:00
|
|
|
$this->setEmpty('security.ban_duration', 1800);
|
2016-05-30 20:15:36 +02:00
|
|
|
$this->setEmpty('security.session_protection_disabled', false);
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('security.open_shaarli', false);
|
2017-05-25 14:52:42 +02:00
|
|
|
$this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2016-05-30 20:15:36 +02:00
|
|
|
$this->setEmpty('general.header_link', '?');
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('general.links_per_page', 20);
|
2016-12-15 10:13:00 +01:00
|
|
|
$this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS);
|
2017-10-01 14:19:57 +02:00
|
|
|
$this->setEmpty('general.default_note_title', 'Note: ');
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('updates.check_updates', false);
|
|
|
|
$this->setEmpty('updates.check_updates_branch', 'stable');
|
|
|
|
$this->setEmpty('updates.check_updates_interval', 86400);
|
|
|
|
|
|
|
|
$this->setEmpty('feed.rss_permalinks', true);
|
2017-03-11 14:11:06 +01:00
|
|
|
$this->setEmpty('feed.show_atom', true);
|
2016-06-11 09:08:02 +02:00
|
|
|
|
|
|
|
$this->setEmpty('privacy.default_private_links', false);
|
|
|
|
$this->setEmpty('privacy.hide_public_links', false);
|
2017-08-31 00:39:15 +02:00
|
|
|
$this->setEmpty('privacy.force_login', false);
|
2016-06-11 09:08:02 +02:00
|
|
|
$this->setEmpty('privacy.hide_timestamps', false);
|
2017-08-26 09:27:10 +02:00
|
|
|
// default state of the 'remember me' checkbox of the login form
|
|
|
|
$this->setEmpty('privacy.remember_user_default', true);
|
2016-06-11 09:08:02 +02:00
|
|
|
|
|
|
|
$this->setEmpty('thumbnail.enable_thumbnails', true);
|
|
|
|
$this->setEmpty('thumbnail.enable_localcache', true);
|
|
|
|
|
|
|
|
$this->setEmpty('redirector.url', '');
|
|
|
|
$this->setEmpty('redirector.encode_url', true);
|
2016-05-18 21:43:59 +02:00
|
|
|
|
2017-05-09 18:12:15 +02:00
|
|
|
$this->setEmpty('translation.language', 'auto');
|
|
|
|
$this->setEmpty('translation.mode', 'php');
|
|
|
|
$this->setEmpty('translation.extensions', []);
|
|
|
|
|
2016-05-18 21:43:59 +02:00
|
|
|
$this->setEmpty('plugins', array());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set only if the setting does not exists.
|
|
|
|
*
|
|
|
|
* @param string $key Setting key.
|
|
|
|
* @param mixed $value Setting value.
|
|
|
|
*/
|
2016-05-30 20:15:36 +02:00
|
|
|
public function setEmpty($key, $value)
|
2016-05-18 21:43:59 +02:00
|
|
|
{
|
|
|
|
if (! $this->exists($key)) {
|
|
|
|
$this->set($key, $value);
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 21:48:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ConfigIO
|
|
|
|
*/
|
|
|
|
public function getConfigIO()
|
|
|
|
{
|
|
|
|
return $this->configIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ConfigIO $configIO
|
|
|
|
*/
|
|
|
|
public function setConfigIO($configIO)
|
|
|
|
{
|
|
|
|
$this->configIO = $configIO;
|
|
|
|
}
|
2016-05-18 21:43:59 +02:00
|
|
|
}
|