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