3 // FIXME! Namespaces...
4 require_once 'ConfigIO.php';
5 require_once 'ConfigPhp.php';
6 require_once 'ConfigJson.php';
11 * Singleton, 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 ConfigManager instance.
21 protected static $instance = null;
24 * @var string Config folder.
26 public static $CONFIG_FILE = 'data/config';
29 * @var string Flag telling a setting is not found.
31 protected static $NOT_FOUND = 'NOT_FOUND';
34 * @var array Loaded config array.
36 protected $loadedConfig;
39 * @var ConfigIO implementation instance.
44 * Private constructor: new instances not allowed.
46 private function __construct() {}
49 * Cloning isn't allowed either.
51 private function __clone() {}
54 * Return existing instance of PluginManager, or create it.
56 * @return ConfigManager instance.
58 public static function getInstance()
60 if (!(self
::$instance instanceof self
)) {
61 self
::$instance = new self();
62 self
::$instance->initialize();
65 return self
::$instance;
69 * Reset the ConfigManager instance.
71 public static function reset()
73 self
::$instance = null;
74 return self
::getInstance();
78 * Rebuild the loaded config array from config files.
80 public function reload()
86 * Initialize the ConfigIO and loaded the conf.
88 protected function initialize()
90 if (! file_exists(self
::$CONFIG_FILE .'.php')) {
91 $this->configIO
= new ConfigJson();
93 $this->configIO
= new ConfigPhp();
99 * Load configuration in the ConfigurationManager.
101 protected function load()
103 $this->loadedConfig
= $this->configIO
->read($this->getConfigFile());
104 $this->setDefaultValues();
110 * Supports nested settings with dot separated keys.
111 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
113 * { "config": { "stuff": {"option": "mysetting" } } } }
115 * @param string $setting Asked setting, keys separated with dots.
116 * @param string $default Default value if not found.
118 * @return mixed Found setting, or the default value.
120 public function get($setting, $default = '')
122 // During the ConfigIO transition, map legacy settings to the new ones.
123 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
124 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
127 $settings = explode('.', $setting);
128 $value = self
::getConfig($settings, $this->loadedConfig
);
129 if ($value === self
::$NOT_FOUND) {
136 * Set a setting, and eventually write it.
138 * Supports nested settings with dot separated keys.
140 * @param string $setting Asked setting, keys separated with dots.
141 * @param string $value Value to set.
142 * @param bool $write Write the new setting in the config file, default false.
143 * @param bool $isLoggedIn User login state, default false.
145 * @throws Exception Invalid
147 public function set($setting, $value, $write = false, $isLoggedIn = false)
149 if (empty($setting) || ! is_string($setting)) {
150 throw new Exception('Invalid setting key parameter. String expected, got: '. gettype($setting));
153 // During the ConfigIO transition, map legacy settings to the new ones.
154 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
155 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
158 $settings = explode('.', $setting);
159 self
::setConfig($settings, $value, $this->loadedConfig
);
161 $this->write($isLoggedIn);
166 * Check if a settings exists.
168 * Supports nested settings with dot separated keys.
170 * @param string $setting Asked setting, keys separated with dots.
172 * @return bool true if the setting exists, false otherwise.
174 public function exists($setting)
176 // During the ConfigIO transition, map legacy settings to the new ones.
177 if ($this->configIO
instanceof ConfigPhp
&& isset(ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting])) {
178 $setting = ConfigPhp
::$LEGACY_KEYS_MAPPING[$setting];
181 $settings = explode('.', $setting);
182 $value = self
::getConfig($settings, $this->loadedConfig
);
183 if ($value === self
::$NOT_FOUND) {
190 * Call the config writer.
192 * @param bool $isLoggedIn User login state.
194 * @return bool True if the configuration has been successfully written, false otherwise.
196 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
197 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
198 * @throws IOException: an error occurred while writing the new config file.
200 public function write($isLoggedIn)
202 // These fields are required in configuration.
203 $mandatoryFields = array(
207 'security.session_protection_disabled',
210 'general.header_link',
211 'general.default_private_links',
215 // Only logged in user can alter config.
216 if (is_file(self
::$CONFIG_FILE) && !$isLoggedIn) {
217 throw new UnauthorizedConfigException();
220 // Check that all mandatory fields are provided in $conf.
221 foreach ($mandatoryFields as $field) {
222 if (! $this->exists($field)) {
223 throw new MissingFieldConfigException($field);
227 return $this->configIO
->write($this->getConfigFile(), $this->loadedConfig
);
231 * Get the configuration file path.
233 * @return string Config file path.
235 public function getConfigFile()
237 return self
::$CONFIG_FILE . $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('path.data_dir', 'data');
293 $this->setEmpty('path.config', 'data/config.php');
294 $this->setEmpty('path.datastore', 'data/datastore.php');
295 $this->setEmpty('path.ban_file', 'data/ipbans.php');
296 $this->setEmpty('path.updates', 'data/updates.txt');
297 $this->setEmpty('path.log', 'data/log.txt');
298 $this->setEmpty('path.update_check', 'data/lastupdatecheck.txt');
299 $this->setEmpty('path.raintpl_tpl', 'tpl/');
300 $this->setEmpty('path.raintpl_tmp', 'tmp/');
301 $this->setEmpty('path.thumbnails_cache', 'cache');
302 $this->setEmpty('path.page_cache', 'pagecache');
304 $this->setEmpty('security.ban_after', 4);
305 $this->setEmpty('security.ban_after', 1800);
306 $this->setEmpty('security.session_protection_disabled', false);
308 $this->setEmpty('general.check_updates', false);
309 $this->setEmpty('general.rss_permalinks', true);
310 $this->setEmpty('general.links_per_page', 20);
311 $this->setEmpty('general.default_private_links', false);
312 $this->setEmpty('general.enable_thumbnails', true);
313 $this->setEmpty('general.enable_localcache', true);
314 $this->setEmpty('general.check_updates_branch', 'stable');
315 $this->setEmpty('general.check_updates_interval', 86400);
316 $this->setEmpty('general.header_link', '?');
317 $this->setEmpty('general.enabled_plugins', array('qrcode'));
319 $this->setEmpty('extras.show_atom', false);
320 $this->setEmpty('extras.hide_public_links', false);
321 $this->setEmpty('extras.hide_timestamps', false);
322 $this->setEmpty('extras.open_shaarli', false);
323 $this->setEmpty('extras.redirector', '');
324 $this->setEmpty('extras.redirector_encode_url', true);
326 $this->setEmpty('plugins', array());
330 * Set only if the setting does not exists.
332 * @param string $key Setting key.
333 * @param mixed $value Setting value.
335 public function setEmpty($key, $value)
337 if (! $this->exists($key)) {
338 $this->set($key, $value);
345 public function getConfigIO()
347 return $this->configIO
;
351 * @param ConfigIO $configIO
353 public function setConfigIO($configIO)
355 $this->configIO
= $configIO;
360 * Exception used if a mandatory field is missing in given configuration.
362 class MissingFieldConfigException
extends Exception
367 * Construct exception.
369 * @param string $field field name missing.
371 public function __construct($field)
373 $this->field
= $field;
374 $this->message
= 'Configuration value is required for '. $this->field
;
379 * Exception used if an unauthorized attempt to edit configuration has been made.
381 class UnauthorizedConfigException
extends Exception
384 * Construct exception.
386 public function __construct()
388 $this->message
= 'You are not authorized to alter config.';