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';
24 * @var string Config folder.
26 protected $configFile;
29 * @var array Loaded config array.
31 protected $loadedConfig;
34 * @var ConfigIO implementation instance.
41 * @param string $configFile Configuration file path without extension.
43 public function __construct($configFile = 'data/config')
45 $this->configFile
= $configFile;
50 * Reset the ConfigManager instance.
52 public function reset()
58 * Rebuild the loaded config array from config files.
60 public function reload()
66 * Initialize the ConfigIO and loaded the conf.
68 protected function initialize()
70 if (file_exists($this->configFile
. '.php')) {
71 $this->configIO
= new ConfigPhp();
73 $this->configIO
= new ConfigJson();
79 * Load configuration in the ConfigurationManager.
81 protected function load()
83 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
84 $this->setDefaultValues();
90 * Supports nested settings with dot separated keys.
91 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
93 * { "config": { "stuff": {"option": "mysetting" } } } }
95 * @param string $setting Asked setting, keys separated with dots.
96 * @param string $default Default value if not found.
98 * @return mixed Found setting, or the default value.
100 public function get($setting, $default = '')
102 // During the ConfigIO transition, map legacy settings to the new ones.
103 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
104 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
107 $settings = explode('.', $setting);
108 $value = self
::getConfig($settings, $this->loadedConfig
);
109 if ($value === self
::$NOT_FOUND) {
116 * Set a setting, and eventually write it.
118 * Supports nested settings with dot separated keys.
120 * @param string $setting Asked setting, keys separated with dots.
121 * @param string $value Value to set.
122 * @param bool $write Write the new setting in the config file, default false.
123 * @param bool $isLoggedIn User login state, default false.
125 * @throws Exception Invalid
127 public function set($setting, $value, $write = false, $isLoggedIn = false)
129 if (empty($setting) || ! is_string($setting)) {
130 throw new Exception('Invalid setting key parameter. String expected, got: '. gettype($setting));
133 // During the ConfigIO transition, map legacy settings to the new ones.
134 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
135 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
138 $settings = explode('.', $setting);
139 self
::setConfig($settings, $value, $this->loadedConfig
);
141 $this->write($isLoggedIn);
146 * Check if a settings exists.
148 * Supports nested settings with dot separated keys.
150 * @param string $setting Asked setting, keys separated with dots.
152 * @return bool true if the setting exists, false otherwise.
154 public function exists($setting)
156 // During the ConfigIO transition, map legacy settings to the new ones.
157 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
158 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
161 $settings = explode('.', $setting);
162 $value = self
::getConfig($settings, $this->loadedConfig
);
163 if ($value === self
::$NOT_FOUND) {
170 * Call the config writer.
172 * @param bool $isLoggedIn User login state.
174 * @return bool True if the configuration has been successfully written, false otherwise.
176 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
177 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
178 * @throws IOException: an error occurred while writing the new config file.
180 public function write($isLoggedIn)
182 // These fields are required in configuration.
183 $mandatoryFields = array(
187 'security.session_protection_disabled',
190 'general.header_link',
191 'privacy.default_private_links',
195 // Only logged in user can alter config.
196 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
197 throw new UnauthorizedConfigException();
200 // Check that all mandatory fields are provided in $conf.
201 foreach ($mandatoryFields as $field) {
202 if (! $this->exists($field)) {
203 throw new MissingFieldConfigException($field);
207 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
211 * Set the config file path (without extension).
213 * @param string $configFile File path.
215 public function setConfigFile($configFile)
217 $this->configFile
= $configFile;
221 * Return the configuration file path (without extension).
223 * @return string Config path.
225 public function getConfigFile()
227 return $this->configFile
;
231 * Get the configuration file path with its extension.
233 * @return string Config file path.
235 public function getConfigFileExt()
237 return $this->configFile
. $this->configIO
->getExtension();
241 * Recursive function which find asked setting in the loaded config.
243 * @param array $settings Ordered array which contains keys to find.
244 * @param array $conf Loaded settings, then sub-array.
246 * @return mixed Found setting or NOT_FOUND flag.
248 protected static function getConfig($settings, $conf)
250 if (!is_array($settings) || count($settings) == 0) {
251 return self
::$NOT_FOUND;
254 $setting = array_shift($settings);
255 if (!isset($conf[$setting])) {
256 return self
::$NOT_FOUND;
259 if (count($settings) > 0) {
260 return self
::getConfig($settings, $conf[$setting]);
262 return $conf[$setting];
266 * Recursive function which find asked setting in the loaded config.
268 * @param array $settings Ordered array which contains keys to find.
269 * @param mixed $value
270 * @param array $conf Loaded settings, then sub-array.
272 * @return mixed Found setting or NOT_FOUND flag.
274 protected static function setConfig($settings, $value, &$conf)
276 if (!is_array($settings) || count($settings) == 0) {
277 return self
::$NOT_FOUND;
280 $setting = array_shift($settings);
281 if (count($settings) > 0) {
282 return self
::setConfig($settings, $value, $conf[$setting]);
284 $conf[$setting] = $value;
288 * Set a bunch of default values allowing Shaarli to start without a config file.
290 protected function setDefaultValues()
292 $this->setEmpty('resource.data_dir', 'data');
293 $this->setEmpty('resource.config', 'data/config.php');
294 $this->setEmpty('resource.datastore', 'data/datastore.php');
295 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
296 $this->setEmpty('resource.updates', 'data/updates.txt');
297 $this->setEmpty('resource.log', 'data/log.txt');
298 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
299 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
300 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
301 $this->setEmpty('resource.thumbnails_cache', 'cache');
302 $this->setEmpty('resource.page_cache', 'pagecache');
304 $this->setEmpty('security.ban_after', 4);
305 $this->setEmpty('security.ban_duration', 1800);
306 $this->setEmpty('security.session_protection_disabled', false);
307 $this->setEmpty('security.open_shaarli', false);
309 $this->setEmpty('general.header_link', '?');
310 $this->setEmpty('general.links_per_page', 20);
311 $this->setEmpty('general.enabled_plugins', array('qrcode'));
313 $this->setEmpty('updates.check_updates', false);
314 $this->setEmpty('updates.check_updates_branch', 'stable');
315 $this->setEmpty('updates.check_updates_interval', 86400);
317 $this->setEmpty('feed.rss_permalinks', true);
318 $this->setEmpty('feed.show_atom', false);
320 $this->setEmpty('privacy.default_private_links', false);
321 $this->setEmpty('privacy.hide_public_links', false);
322 $this->setEmpty('privacy.hide_timestamps', false);
324 $this->setEmpty('thumbnail.enable_thumbnails', true);
325 $this->setEmpty('thumbnail.enable_localcache', true);
327 $this->setEmpty('redirector.url', '');
328 $this->setEmpty('redirector.encode_url', true);
330 $this->setEmpty('plugins', array());
334 * Set only if the setting does not exists.
336 * @param string $key Setting key.
337 * @param mixed $value Setting value.
339 public function setEmpty($key, $value)
341 if (! $this->exists($key)) {
342 $this->set($key, $value);
349 public function getConfigIO()
351 return $this->configIO
;
355 * @param ConfigIO $configIO
357 public function setConfigIO($configIO)
359 $this->configIO
= $configIO;
364 * Exception used if a mandatory field is missing in given configuration.
366 class MissingFieldConfigException
extends Exception
371 * Construct exception.
373 * @param string $field field name missing.
375 public function __construct($field)
377 $this->field
= $field;
378 $this->message
= 'Configuration value is required for '. $this->field
;
383 * Exception used if an unauthorized attempt to edit configuration has been made.
385 class UnauthorizedConfigException
extends Exception
388 * Construct exception.
390 public function __construct()
392 $this->message
= 'You are not authorized to alter config.';