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/Shaarli-configuration.html
13 * - https://github.com/shaarli/Shaarli/wiki/Shaarli-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()
84 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
85 $this->setDefaultValues();
91 * Supports nested settings with dot separated keys.
92 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
94 * { "config": { "stuff": {"option": "mysetting" } } } }
96 * @param string $setting Asked setting, keys separated with dots.
97 * @param string $default Default value if not found.
99 * @return mixed Found setting, or the default value.
101 public function get($setting, $default = '')
103 // During the ConfigIO transition, map legacy settings to the new ones.
104 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
105 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
108 $settings = explode('.', $setting);
109 $value = self
::getConfig($settings, $this->loadedConfig
);
110 if ($value === self
::$NOT_FOUND) {
117 * Set a setting, and eventually write it.
119 * Supports nested settings with dot separated keys.
121 * @param string $setting Asked setting, keys separated with dots.
122 * @param string $value Value to set.
123 * @param bool $write Write the new setting in the config file, default false.
124 * @param bool $isLoggedIn User login state, default false.
126 * @throws \Exception Invalid
128 public function set($setting, $value, $write = false, $isLoggedIn = false)
130 if (empty($setting) || ! is_string($setting)) {
131 throw new \
Exception('Invalid setting key parameter. String expected, got: '. gettype($setting));
134 // During the ConfigIO transition, map legacy settings to the new ones.
135 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
136 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
139 $settings = explode('.', $setting);
140 self
::setConfig($settings, $value, $this->loadedConfig
);
142 $this->write($isLoggedIn);
147 * Check if a settings exists.
149 * Supports nested settings with dot separated keys.
151 * @param string $setting Asked setting, keys separated with dots.
153 * @return bool true if the setting exists, false otherwise.
155 public function exists($setting)
157 // During the ConfigIO transition, map legacy settings to the new ones.
158 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
159 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
162 $settings = explode('.', $setting);
163 $value = self
::getConfig($settings, $this->loadedConfig
);
164 if ($value === self
::$NOT_FOUND) {
171 * Call the config writer.
173 * @param bool $isLoggedIn User login state.
175 * @return bool True if the configuration has been successfully written, false otherwise.
177 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
178 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
179 * @throws \IOException: an error occurred while writing the new config file.
181 public function write($isLoggedIn)
183 // These fields are required in configuration.
184 $mandatoryFields = array(
188 'security.session_protection_disabled',
191 'general.header_link',
192 'privacy.default_private_links',
196 // Only logged in user can alter config.
197 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
198 throw new UnauthorizedConfigException();
201 // Check that all mandatory fields are provided in $conf.
202 foreach ($mandatoryFields as $field) {
203 if (! $this->exists($field)) {
204 throw new MissingFieldConfigException($field);
208 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
212 * Set the config file path (without extension).
214 * @param string $configFile File path.
216 public function setConfigFile($configFile)
218 $this->configFile
= $configFile;
222 * Return the configuration file path (without extension).
224 * @return string Config path.
226 public function getConfigFile()
228 return $this->configFile
;
232 * Get the configuration file path with its extension.
234 * @return string Config file path.
236 public function getConfigFileExt()
238 return $this->configFile
. $this->configIO
->getExtension();
242 * Recursive function which find asked setting in the loaded config.
244 * @param array $settings Ordered array which contains keys to find.
245 * @param array $conf Loaded settings, then sub-array.
247 * @return mixed Found setting or NOT_FOUND flag.
249 protected static function getConfig($settings, $conf)
251 if (!is_array($settings) || count($settings) == 0) {
252 return self
::$NOT_FOUND;
255 $setting = array_shift($settings);
256 if (!isset($conf[$setting])) {
257 return self
::$NOT_FOUND;
260 if (count($settings) > 0) {
261 return self
::getConfig($settings, $conf[$setting]);
263 return $conf[$setting];
267 * Recursive function which find asked setting in the loaded config.
269 * @param array $settings Ordered array which contains keys to find.
270 * @param mixed $value
271 * @param array $conf Loaded settings, then sub-array.
273 * @return mixed Found setting or NOT_FOUND flag.
275 protected static function setConfig($settings, $value, &$conf)
277 if (!is_array($settings) || count($settings) == 0) {
278 return self
::$NOT_FOUND;
281 $setting = array_shift($settings);
282 if (count($settings) > 0) {
283 return self
::setConfig($settings, $value, $conf[$setting]);
285 $conf[$setting] = $value;
289 * Set a bunch of default values allowing Shaarli to start without a config file.
291 protected function setDefaultValues()
293 $this->setEmpty('resource.data_dir', 'data');
294 $this->setEmpty('resource.config', 'data/config.php');
295 $this->setEmpty('resource.datastore', 'data/datastore.php');
296 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
297 $this->setEmpty('resource.updates', 'data/updates.txt');
298 $this->setEmpty('resource.log', 'data/log.txt');
299 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
300 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
301 $this->setEmpty('resource.theme', 'default');
302 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
303 $this->setEmpty('resource.thumbnails_cache', 'cache');
304 $this->setEmpty('resource.page_cache', 'pagecache');
306 $this->setEmpty('security.ban_after', 4);
307 $this->setEmpty('security.ban_duration', 1800);
308 $this->setEmpty('security.session_protection_disabled', false);
309 $this->setEmpty('security.open_shaarli', false);
311 $this->setEmpty('general.header_link', '?');
312 $this->setEmpty('general.links_per_page', 20);
313 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
315 $this->setEmpty('updates.check_updates', false);
316 $this->setEmpty('updates.check_updates_branch', 'stable');
317 $this->setEmpty('updates.check_updates_interval', 86400);
319 $this->setEmpty('feed.rss_permalinks', true);
320 $this->setEmpty('feed.show_atom', false);
322 $this->setEmpty('privacy.default_private_links', false);
323 $this->setEmpty('privacy.hide_public_links', false);
324 $this->setEmpty('privacy.hide_timestamps', false);
326 $this->setEmpty('thumbnail.enable_thumbnails', true);
327 $this->setEmpty('thumbnail.enable_localcache', true);
329 $this->setEmpty('redirector.url', '');
330 $this->setEmpty('redirector.encode_url', true);
332 $this->setEmpty('plugins', array());
336 * Set only if the setting does not exists.
338 * @param string $key Setting key.
339 * @param mixed $value Setting value.
341 public function setEmpty($key, $value)
343 if (! $this->exists($key)) {
344 $this->set($key, $value);
351 public function getConfigIO()
353 return $this->configIO
;
357 * @param ConfigIO $configIO
359 public function setConfigIO($configIO)
361 $this->configIO
= $configIO;