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 public function __construct($configFile = 'data/config')
43 $this->configFile
= $configFile;
48 * Reset the ConfigManager instance.
50 public function reset()
56 * Rebuild the loaded config array from config files.
58 public function reload()
64 * Initialize the ConfigIO and loaded the conf.
66 protected function initialize()
68 if (file_exists($this->configFile
. '.php')) {
69 $this->configIO
= new ConfigPhp();
71 $this->configIO
= new ConfigJson();
77 * Load configuration in the ConfigurationManager.
79 protected function load()
81 $this->loadedConfig
= $this->configIO
->read($this->getConfigFileExt());
82 $this->setDefaultValues();
88 * Supports nested settings with dot separated keys.
89 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
91 * { "config": { "stuff": {"option": "mysetting" } } } }
93 * @param string $setting Asked setting, keys separated with dots.
94 * @param string $default Default value if not found.
96 * @return mixed Found setting, or the default value.
98 public function get($setting, $default = '')
100 // During the ConfigIO transition, map legacy settings to the new ones.
101 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
102 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
105 $settings = explode('.', $setting);
106 $value = self
::getConfig($settings, $this->loadedConfig
);
107 if ($value === self
::$NOT_FOUND) {
114 * Set a setting, and eventually write it.
116 * Supports nested settings with dot separated keys.
118 * @param string $setting Asked setting, keys separated with dots.
119 * @param string $value Value to set.
120 * @param bool $write Write the new setting in the config file, default false.
121 * @param bool $isLoggedIn User login state, default false.
123 * @throws Exception Invalid
125 public function set($setting, $value, $write = false, $isLoggedIn = false)
127 if (empty($setting) || ! is_string($setting)) {
128 throw new Exception('Invalid setting key parameter. String expected, got: '. gettype($setting));
131 // During the ConfigIO transition, map legacy settings to the new ones.
132 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
133 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
136 $settings = explode('.', $setting);
137 self
::setConfig($settings, $value, $this->loadedConfig
);
139 $this->write($isLoggedIn);
144 * Check if a settings exists.
146 * Supports nested settings with dot separated keys.
148 * @param string $setting Asked setting, keys separated with dots.
150 * @return bool true if the setting exists, false otherwise.
152 public function exists($setting)
154 // During the ConfigIO transition, map legacy settings to the new ones.
155 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
156 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
159 $settings = explode('.', $setting);
160 $value = self
::getConfig($settings, $this->loadedConfig
);
161 if ($value === self
::$NOT_FOUND) {
168 * Call the config writer.
170 * @param bool $isLoggedIn User login state.
172 * @return bool True if the configuration has been successfully written, false otherwise.
174 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
175 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
176 * @throws IOException: an error occurred while writing the new config file.
178 public function write($isLoggedIn)
180 // These fields are required in configuration.
181 $mandatoryFields = array(
185 'security.session_protection_disabled',
188 'general.header_link',
189 'privacy.default_private_links',
193 // Only logged in user can alter config.
194 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
195 throw new UnauthorizedConfigException();
198 // Check that all mandatory fields are provided in $conf.
199 foreach ($mandatoryFields as $field) {
200 if (! $this->exists($field)) {
201 throw new MissingFieldConfigException($field);
205 return $this->configIO
->write($this->getConfigFileExt(), $this->loadedConfig
);
209 * Set the config file path (without extension).
211 * @param string $configFile File path.
213 public function setConfigFile($configFile)
215 $this->configFile
= $configFile;
219 * Return the configuration file path (without extension).
221 * @return string Config path.
223 public function getConfigFile()
225 return $this->configFile
;
229 * Get the configuration file path with its extension.
231 * @return string Config file path.
233 public function getConfigFileExt()
235 return $this->configFile
. $this->configIO
->getExtension();
239 * Recursive function which find asked setting in the loaded config.
241 * @param array $settings Ordered array which contains keys to find.
242 * @param array $conf Loaded settings, then sub-array.
244 * @return mixed Found setting or NOT_FOUND flag.
246 protected static function getConfig($settings, $conf)
248 if (!is_array($settings) || count($settings) == 0) {
249 return self
::$NOT_FOUND;
252 $setting = array_shift($settings);
253 if (!isset($conf[$setting])) {
254 return self
::$NOT_FOUND;
257 if (count($settings) > 0) {
258 return self
::getConfig($settings, $conf[$setting]);
260 return $conf[$setting];
264 * Recursive function which find asked setting in the loaded config.
266 * @param array $settings Ordered array which contains keys to find.
267 * @param mixed $value
268 * @param array $conf Loaded settings, then sub-array.
270 * @return mixed Found setting or NOT_FOUND flag.
272 protected static function setConfig($settings, $value, &$conf)
274 if (!is_array($settings) || count($settings) == 0) {
275 return self
::$NOT_FOUND;
278 $setting = array_shift($settings);
279 if (count($settings) > 0) {
280 return self
::setConfig($settings, $value, $conf[$setting]);
282 $conf[$setting] = $value;
286 * Set a bunch of default values allowing Shaarli to start without a config file.
288 protected function setDefaultValues()
290 $this->setEmpty('resource.data_dir', 'data');
291 $this->setEmpty('resource.config', 'data/config.php');
292 $this->setEmpty('resource.datastore', 'data/datastore.php');
293 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
294 $this->setEmpty('resource.updates', 'data/updates.txt');
295 $this->setEmpty('resource.log', 'data/log.txt');
296 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
297 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
298 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
299 $this->setEmpty('resource.thumbnails_cache', 'cache');
300 $this->setEmpty('resource.page_cache', 'pagecache');
302 $this->setEmpty('security.ban_after', 4);
303 $this->setEmpty('security.ban_duration', 1800);
304 $this->setEmpty('security.session_protection_disabled', false);
305 $this->setEmpty('security.open_shaarli', false);
307 $this->setEmpty('general.header_link', '?');
308 $this->setEmpty('general.links_per_page', 20);
309 $this->setEmpty('general.enabled_plugins', array('qrcode'));
311 $this->setEmpty('updates.check_updates', false);
312 $this->setEmpty('updates.check_updates_branch', 'stable');
313 $this->setEmpty('updates.check_updates_interval', 86400);
315 $this->setEmpty('feed.rss_permalinks', true);
316 $this->setEmpty('feed.show_atom', false);
318 $this->setEmpty('privacy.default_private_links', false);
319 $this->setEmpty('privacy.hide_public_links', false);
320 $this->setEmpty('privacy.hide_timestamps', false);
322 $this->setEmpty('thumbnail.enable_thumbnails', true);
323 $this->setEmpty('thumbnail.enable_localcache', true);
325 $this->setEmpty('redirector.url', '');
326 $this->setEmpty('redirector.encode_url', true);
328 $this->setEmpty('plugins', array());
332 * Set only if the setting does not exists.
334 * @param string $key Setting key.
335 * @param mixed $value Setting value.
337 public function setEmpty($key, $value)
339 if (! $this->exists($key)) {
340 $this->set($key, $value);
347 public function getConfigIO()
349 return $this->configIO
;
353 * @param ConfigIO $configIO
355 public function setConfigIO($configIO)
357 $this->configIO
= $configIO;
362 * Exception used if a mandatory field is missing in given configuration.
364 class MissingFieldConfigException
extends Exception
369 * Construct exception.
371 * @param string $field field name missing.
373 public function __construct($field)
375 $this->field
= $field;
376 $this->message
= 'Configuration value is required for '. $this->field
;
381 * Exception used if an unauthorized attempt to edit configuration has been made.
383 class UnauthorizedConfigException
extends Exception
386 * Construct exception.
388 public function __construct()
390 $this->message
= 'You are not authorized to alter config.';