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