3 // FIXME! Namespaces...
4 require_once 'ConfigIO.php';
5 require_once 'ConfigJson.php';
6 require_once 'ConfigPhp.php';
11 * Manages all Shaarli's settings.
12 * See the documentation for more information on settings:
13 * - doc/Shaarli-configuration.html
14 * - https://github.com/shaarli/Shaarli/wiki/Shaarli-configuration
19 * @var string Flag telling a setting is not found.
21 protected static $NOT_FOUND = 'NOT_FOUND';
23 public static $DEFAULT_PLUGINS = array('qrcode');
26 * @var string Config folder.
28 protected $configFile;
31 * @var array Loaded config array.
33 protected $loadedConfig;
36 * @var ConfigIO implementation instance.
43 * @param string $configFile Configuration file path without extension.
45 public function __construct($configFile = 'data/config')
47 $this->configFile
= $configFile;
52 * Reset the ConfigManager instance.
54 public function reset()
60 * Rebuild the loaded config array from config files.
62 public function reload()
68 * Initialize the ConfigIO and loaded the conf.
70 protected function initialize()
72 if (file_exists($this->configFile
. '.php')) {
73 $this->configIO
= new ConfigPhp();
75 $this->configIO
= new ConfigJson();
81 * Load configuration in the ConfigurationManager.
83 protected function load()
85 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
86 $this->setDefaultValues();
92 * Supports nested settings with dot separated keys.
93 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
95 * { "config": { "stuff": {"option": "mysetting" } } } }
97 * @param string $setting Asked setting, keys separated with dots.
98 * @param string $default Default value if not found.
100 * @return mixed Found setting, or the default value.
102 public function get($setting, $default = '')
104 // During the ConfigIO transition, map legacy settings to the new ones.
105 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
106 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
109 $settings = explode('.', $setting);
110 $value = self
::getConfig($settings, $this->loadedConfig
);
111 if ($value === self
::$NOT_FOUND) {
118 * Set a setting, and eventually write it.
120 * Supports nested settings with dot separated keys.
122 * @param string $setting Asked setting, keys separated with dots.
123 * @param string $value Value to set.
124 * @param bool $write Write the new setting in the config file, default false.
125 * @param bool $isLoggedIn User login state, default false.
127 * @throws Exception Invalid
129 public function set($setting, $value, $write = false, $isLoggedIn = false)
131 if (empty($setting) || ! is_string($setting)) {
132 throw new Exception('Invalid setting key parameter. String expected, got: '. gettype($setting));
135 // During the ConfigIO transition, map legacy settings to the new ones.
136 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
137 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
140 $settings = explode('.', $setting);
141 self
::setConfig($settings, $value, $this->loadedConfig
);
143 $this->write($isLoggedIn);
148 * Check if a settings exists.
150 * Supports nested settings with dot separated keys.
152 * @param string $setting Asked setting, keys separated with dots.
154 * @return bool true if the setting exists, false otherwise.
156 public function exists($setting)
158 // During the ConfigIO transition, map legacy settings to the new ones.
159 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
160 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
163 $settings = explode('.', $setting);
164 $value = self
::getConfig($settings, $this->loadedConfig
);
165 if ($value === self
::$NOT_FOUND) {
172 * Call the config writer.
174 * @param bool $isLoggedIn User login state.
176 * @return bool True if the configuration has been successfully written, false otherwise.
178 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
179 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
180 * @throws IOException: an error occurred while writing the new config file.
182 public function write($isLoggedIn)
184 // These fields are required in configuration.
185 $mandatoryFields = array(
189 'security.session_protection_disabled',
192 'general.header_link',
193 'privacy.default_private_links',
197 // Only logged in user can alter config.
198 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
199 throw new UnauthorizedConfigException();
202 // Check that all mandatory fields are provided in $conf.
203 foreach ($mandatoryFields as $field) {
204 if (! $this->exists($field)) {
205 throw new MissingFieldConfigException($field);
209 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
213 * Set the config file path (without extension).
215 * @param string $configFile File path.
217 public function setConfigFile($configFile)
219 $this->configFile
= $configFile;
223 * Return the configuration file path (without extension).
225 * @return string Config path.
227 public function getConfigFile()
229 return $this->configFile
;
233 * Get the configuration file path with its extension.
235 * @return string Config file path.
237 public function getConfigFileExt()
239 return $this->configFile
. $this->configIO
->getExtension();
243 * Recursive function which find asked setting in the loaded config.
245 * @param array $settings Ordered array which contains keys to find.
246 * @param array $conf Loaded settings, then sub-array.
248 * @return mixed Found setting or NOT_FOUND flag.
250 protected static function getConfig($settings, $conf)
252 if (!is_array($settings) || count($settings) == 0) {
253 return self
::$NOT_FOUND;
256 $setting = array_shift($settings);
257 if (!isset($conf[$setting])) {
258 return self
::$NOT_FOUND;
261 if (count($settings) > 0) {
262 return self
::getConfig($settings, $conf[$setting]);
264 return $conf[$setting];
268 * Recursive function which find asked setting in the loaded config.
270 * @param array $settings Ordered array which contains keys to find.
271 * @param mixed $value
272 * @param array $conf Loaded settings, then sub-array.
274 * @return mixed Found setting or NOT_FOUND flag.
276 protected static function setConfig($settings, $value, &$conf)
278 if (!is_array($settings) || count($settings) == 0) {
279 return self
::$NOT_FOUND;
282 $setting = array_shift($settings);
283 if (count($settings) > 0) {
284 return self
::setConfig($settings, $value, $conf[$setting]);
286 $conf[$setting] = $value;
290 * Set a bunch of default values allowing Shaarli to start without a config file.
292 protected function setDefaultValues()
294 $this->setEmpty('resource.data_dir', 'data');
295 $this->setEmpty('resource.config', 'data/config.php');
296 $this->setEmpty('resource.datastore', 'data/datastore.php');
297 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
298 $this->setEmpty('resource.updates', 'data/updates.txt');
299 $this->setEmpty('resource.log', 'data/log.txt');
300 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
301 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
302 $this->setEmpty('resource.theme', 'default');
303 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
304 $this->setEmpty('resource.thumbnails_cache', 'cache');
305 $this->setEmpty('resource.page_cache', 'pagecache');
307 $this->setEmpty('security.ban_after', 4);
308 $this->setEmpty('security.ban_duration', 1800);
309 $this->setEmpty('security.session_protection_disabled', false);
310 $this->setEmpty('security.open_shaarli', false);
312 $this->setEmpty('general.header_link', '?');
313 $this->setEmpty('general.links_per_page', 20);
314 $this->setEmpty('general.enabled_plugins', self
::$DEFAULT_PLUGINS);
316 $this->setEmpty('updates.check_updates', false);
317 $this->setEmpty('updates.check_updates_branch', 'stable');
318 $this->setEmpty('updates.check_updates_interval', 86400);
320 $this->setEmpty('feed.rss_permalinks', true);
321 $this->setEmpty('feed.show_atom', false);
323 $this->setEmpty('privacy.default_private_links', false);
324 $this->setEmpty('privacy.hide_public_links', false);
325 $this->setEmpty('privacy.hide_timestamps', false);
327 $this->setEmpty('thumbnail.enable_thumbnails', true);
328 $this->setEmpty('thumbnail.enable_localcache', true);
330 $this->setEmpty('redirector.url', '');
331 $this->setEmpty('redirector.encode_url', true);
333 $this->setEmpty('plugins', array());
337 * Set only if the setting does not exists.
339 * @param string $key Setting key.
340 * @param mixed $value Setting value.
342 public function setEmpty($key, $value)
344 if (! $this->exists($key)) {
345 $this->set($key, $value);
352 public function getConfigIO()
354 return $this->configIO
;
358 * @param ConfigIO $configIO
360 public function setConfigIO($configIO)
362 $this->configIO
= $configIO;
367 * Exception used if a mandatory field is missing in given configuration.
369 class MissingFieldConfigException
extends Exception
374 * Construct exception.
376 * @param string $field field name missing.
378 public function __construct($field)
380 $this->field
= $field;
381 $this->message
= 'Configuration value is required for '. $this->field
;
386 * Exception used if an unauthorized attempt to edit configuration has been made.
388 class UnauthorizedConfigException
extends Exception
391 * Construct exception.
393 public function __construct()
395 $this->message
= 'You are not authorized to alter config.';