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