]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - application/config/ConfigManager.php
Use web-thumbnailer to retrieve thumbnails
[github/shaarli/Shaarli.git] / application / config / ConfigManager.php
CommitLineData
59404d79 1<?php
3c66e564 2namespace Shaarli\Config;
59404d79 3
5ba55f0c
A
4use Shaarli\Config\Exception\MissingFieldConfigException;
5use Shaarli\Config\Exception\UnauthorizedConfigException;
6
59404d79
A
7/**
8 * Class ConfigManager
9 *
278d9ee2 10 * Manages all Shaarli's settings.
7f179985 11 * See the documentation for more information on settings:
cc8f572b
WE
12 * - doc/md/Shaarli-configuration.md
13 * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration
59404d79
A
14 */
15class ConfigManager
16{
17 /**
278d9ee2 18 * @var string Flag telling a setting is not found.
59404d79 19 */
278d9ee2 20 protected static $NOT_FOUND = 'NOT_FOUND';
59404d79 21
18e67967
A
22 public static $DEFAULT_PLUGINS = array('qrcode');
23
59404d79
A
24 /**
25 * @var string Config folder.
26 */
278d9ee2 27 protected $configFile;
59404d79
A
28
29 /**
30 * @var array Loaded config array.
31 */
32 protected $loadedConfig;
33
34 /**
35 * @var ConfigIO implementation instance.
36 */
37 protected $configIO;
38
39 /**
278d9ee2 40 * Constructor.
7af9a418
A
41 *
42 * @param string $configFile Configuration file path without extension.
59404d79 43 */
278d9ee2 44 public function __construct($configFile = 'data/config')
59404d79 45 {
278d9ee2
A
46 $this->configFile = $configFile;
47 $this->initialize();
59404d79
A
48 }
49
684e662a
A
50 /**
51 * Reset the ConfigManager instance.
52 */
278d9ee2 53 public function reset()
684e662a 54 {
278d9ee2 55 $this->initialize();
684e662a
A
56 }
57
59404d79
A
58 /**
59 * Rebuild the loaded config array from config files.
60 */
61 public function reload()
62 {
684e662a 63 $this->load();
59404d79
A
64 }
65
66 /**
684e662a 67 * Initialize the ConfigIO and loaded the conf.
59404d79
A
68 */
69 protected function initialize()
70 {
278d9ee2 71 if (file_exists($this->configFile . '.php')) {
59404d79 72 $this->configIO = new ConfigPhp();
278d9ee2
A
73 } else {
74 $this->configIO = new ConfigJson();
b74b96bf 75 }
684e662a
A
76 $this->load();
77 }
78
79 /**
80 * Load configuration in the ConfigurationManager.
81 */
82 protected function load()
83 {
c6a4c288
A
84 try {
85 $this->loadedConfig = $this->configIO->read($this->getConfigFileExt());
86 } catch (\Exception $e) {
87 die($e->getMessage());
88 }
59404d79
A
89 $this->setDefaultValues();
90 }
91
92 /**
93 * Get a setting.
94 *
95 * Supports nested settings with dot separated keys.
96 * Eg. 'config.stuff.option' will find $conf[config][stuff][option],
97 * or in JSON:
98 * { "config": { "stuff": {"option": "mysetting" } } } }
99 *
100 * @param string $setting Asked setting, keys separated with dots.
101 * @param string $default Default value if not found.
102 *
103 * @return mixed Found setting, or the default value.
104 */
105 public function get($setting, $default = '')
106 {
da10377b
A
107 // During the ConfigIO transition, map legacy settings to the new ones.
108 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
109 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
110 }
111
59404d79
A
112 $settings = explode('.', $setting);
113 $value = self::getConfig($settings, $this->loadedConfig);
114 if ($value === self::$NOT_FOUND) {
115 return $default;
116 }
117 return $value;
118 }
119
120 /**
121 * Set a setting, and eventually write it.
122 *
123 * Supports nested settings with dot separated keys.
124 *
125 * @param string $setting Asked setting, keys separated with dots.
980efd6c 126 * @param mixed $value Value to set.
59404d79
A
127 * @param bool $write Write the new setting in the config file, default false.
128 * @param bool $isLoggedIn User login state, default false.
684e662a 129 *
3c66e564 130 * @throws \Exception Invalid
59404d79
A
131 */
132 public function set($setting, $value, $write = false, $isLoggedIn = false)
133 {
684e662a 134 if (empty($setting) || ! is_string($setting)) {
12266213 135 throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting));
684e662a
A
136 }
137
da10377b
A
138 // During the ConfigIO transition, map legacy settings to the new ones.
139 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
140 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
141 }
142
59404d79
A
143 $settings = explode('.', $setting);
144 self::setConfig($settings, $value, $this->loadedConfig);
145 if ($write) {
146 $this->write($isLoggedIn);
147 }
148 }
149
150 /**
151 * Check if a settings exists.
152 *
153 * Supports nested settings with dot separated keys.
154 *
155 * @param string $setting Asked setting, keys separated with dots.
156 *
157 * @return bool true if the setting exists, false otherwise.
158 */
159 public function exists($setting)
160 {
da10377b
A
161 // During the ConfigIO transition, map legacy settings to the new ones.
162 if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) {
163 $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting];
164 }
165
59404d79
A
166 $settings = explode('.', $setting);
167 $value = self::getConfig($settings, $this->loadedConfig);
168 if ($value === self::$NOT_FOUND) {
169 return false;
170 }
171 return true;
172 }
173
174 /**
175 * Call the config writer.
176 *
177 * @param bool $isLoggedIn User login state.
178 *
684e662a
A
179 * @return bool True if the configuration has been successfully written, false otherwise.
180 *
59404d79
A
181 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
182 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
3c66e564 183 * @throws \IOException: an error occurred while writing the new config file.
59404d79
A
184 */
185 public function write($isLoggedIn)
186 {
187 // These fields are required in configuration.
188 $mandatoryFields = array(
da10377b
A
189 'credentials.login',
190 'credentials.hash',
191 'credentials.salt',
192 'security.session_protection_disabled',
193 'general.timezone',
194 'general.title',
195 'general.header_link',
894a3c4b
A
196 'privacy.default_private_links',
197 'redirector.url',
59404d79
A
198 );
199
200 // Only logged in user can alter config.
278d9ee2 201 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
59404d79
A
202 throw new UnauthorizedConfigException();
203 }
204
205 // Check that all mandatory fields are provided in $conf.
206 foreach ($mandatoryFields as $field) {
207 if (! $this->exists($field)) {
208 throw new MissingFieldConfigException($field);
209 }
210 }
211
278d9ee2 212 return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig);
59404d79
A
213 }
214
215 /**
278d9ee2 216 * Set the config file path (without extension).
59404d79 217 *
278d9ee2
A
218 * @param string $configFile File path.
219 */
220 public function setConfigFile($configFile)
221 {
222 $this->configFile = $configFile;
223 }
224
225 /**
226 * Return the configuration file path (without extension).
227 *
228 * @return string Config path.
59404d79
A
229 */
230 public function getConfigFile()
231 {
278d9ee2
A
232 return $this->configFile;
233 }
234
235 /**
236 * Get the configuration file path with its extension.
237 *
238 * @return string Config file path.
239 */
240 public function getConfigFileExt()
241 {
242 return $this->configFile . $this->configIO->getExtension();
59404d79
A
243 }
244
245 /**
246 * Recursive function which find asked setting in the loaded config.
247 *
248 * @param array $settings Ordered array which contains keys to find.
249 * @param array $conf Loaded settings, then sub-array.
250 *
251 * @return mixed Found setting or NOT_FOUND flag.
252 */
253 protected static function getConfig($settings, $conf)
254 {
255 if (!is_array($settings) || count($settings) == 0) {
256 return self::$NOT_FOUND;
257 }
258
259 $setting = array_shift($settings);
260 if (!isset($conf[$setting])) {
261 return self::$NOT_FOUND;
262 }
263
264 if (count($settings) > 0) {
265 return self::getConfig($settings, $conf[$setting]);
266 }
267 return $conf[$setting];
268 }
269
270 /**
271 * Recursive function which find asked setting in the loaded config.
272 *
273 * @param array $settings Ordered array which contains keys to find.
274 * @param mixed $value
275 * @param array $conf Loaded settings, then sub-array.
276 *
277 * @return mixed Found setting or NOT_FOUND flag.
278 */
279 protected static function setConfig($settings, $value, &$conf)
280 {
281 if (!is_array($settings) || count($settings) == 0) {
282 return self::$NOT_FOUND;
283 }
284
285 $setting = array_shift($settings);
286 if (count($settings) > 0) {
287 return self::setConfig($settings, $value, $conf[$setting]);
288 }
289 $conf[$setting] = $value;
290 }
291
292 /**
293 * Set a bunch of default values allowing Shaarli to start without a config file.
294 */
295 protected function setDefaultValues()
296 {
894a3c4b
A
297 $this->setEmpty('resource.data_dir', 'data');
298 $this->setEmpty('resource.config', 'data/config.php');
299 $this->setEmpty('resource.datastore', 'data/datastore.php');
300 $this->setEmpty('resource.ban_file', 'data/ipbans.php');
301 $this->setEmpty('resource.updates', 'data/updates.txt');
302 $this->setEmpty('resource.log', 'data/log.txt');
303 $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt');
4306b184 304 $this->setEmpty('resource.history', 'data/history.php');
894a3c4b 305 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
adc4aee8 306 $this->setEmpty('resource.theme', 'default');
894a3c4b
A
307 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
308 $this->setEmpty('resource.thumbnails_cache', 'cache');
309 $this->setEmpty('resource.page_cache', 'pagecache');
59404d79 310
da10377b 311 $this->setEmpty('security.ban_after', 4);
278d9ee2 312 $this->setEmpty('security.ban_duration', 1800);
7f179985 313 $this->setEmpty('security.session_protection_disabled', false);
894a3c4b 314 $this->setEmpty('security.open_shaarli', false);
86ceea05 315 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
59404d79 316
7f179985 317 $this->setEmpty('general.header_link', '?');
894a3c4b 318 $this->setEmpty('general.links_per_page', 20);
18e67967 319 $this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS);
722caa20 320 $this->setEmpty('general.default_note_title', 'Note: ');
59404d79 321
1b93137e
A
322 $this->setEmpty('thumbnails.enabled', true);
323 $this->setEmpty('thumbnails.width', 120);
324 $this->setEmpty('thumbnails.height', 120);
325
894a3c4b
A
326 $this->setEmpty('updates.check_updates', false);
327 $this->setEmpty('updates.check_updates_branch', 'stable');
328 $this->setEmpty('updates.check_updates_interval', 86400);
329
330 $this->setEmpty('feed.rss_permalinks', true);
2ea89aba 331 $this->setEmpty('feed.show_atom', true);
894a3c4b
A
332
333 $this->setEmpty('privacy.default_private_links', false);
334 $this->setEmpty('privacy.hide_public_links', false);
27e21231 335 $this->setEmpty('privacy.force_login', false);
894a3c4b 336 $this->setEmpty('privacy.hide_timestamps', false);
2e07e775
WE
337 // default state of the 'remember me' checkbox of the login form
338 $this->setEmpty('privacy.remember_user_default', true);
894a3c4b
A
339
340 $this->setEmpty('thumbnail.enable_thumbnails', true);
341 $this->setEmpty('thumbnail.enable_localcache', true);
342
343 $this->setEmpty('redirector.url', '');
344 $this->setEmpty('redirector.encode_url', true);
59404d79 345
12266213
A
346 $this->setEmpty('translation.language', 'auto');
347 $this->setEmpty('translation.mode', 'php');
348 $this->setEmpty('translation.extensions', []);
349
59404d79
A
350 $this->setEmpty('plugins', array());
351 }
352
353 /**
354 * Set only if the setting does not exists.
355 *
356 * @param string $key Setting key.
357 * @param mixed $value Setting value.
358 */
7f179985 359 public function setEmpty($key, $value)
59404d79
A
360 {
361 if (! $this->exists($key)) {
362 $this->set($key, $value);
363 }
364 }
684e662a
A
365
366 /**
367 * @return ConfigIO
368 */
369 public function getConfigIO()
370 {
371 return $this->configIO;
372 }
373
374 /**
375 * @param ConfigIO $configIO
376 */
377 public function setConfigIO($configIO)
378 {
379 $this->configIO = $configIO;
380 }
59404d79 381}