]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | namespace Shaarli\Config; | |
3 | ||
4 | use Shaarli\Config\Exception\MissingFieldConfigException; | |
5 | use Shaarli\Config\Exception\UnauthorizedConfigException; | |
6 | ||
7 | /** | |
8 | * Class ConfigManager | |
9 | * | |
10 | * Manages all Shaarli's settings. | |
11 | * See the documentation for more information on settings: | |
12 | * - doc/md/Shaarli-configuration.md | |
13 | * - https://shaarli.readthedocs.io/en/master/Shaarli-configuration/#configuration | |
14 | */ | |
15 | class ConfigManager | |
16 | { | |
17 | /** | |
18 | * @var string Flag telling a setting is not found. | |
19 | */ | |
20 | protected static $NOT_FOUND = 'NOT_FOUND'; | |
21 | ||
22 | public static $DEFAULT_PLUGINS = array('qrcode'); | |
23 | ||
24 | /** | |
25 | * @var string User space. | |
26 | */ | |
27 | protected $userSpace; | |
28 | ||
29 | /** | |
30 | * @var string Config folder. | |
31 | */ | |
32 | protected $configFile; | |
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 | /** | |
45 | * Constructor. | |
46 | * | |
47 | * @param string $configFile Configuration file path without extension. | |
48 | */ | |
49 | public function __construct($configFile = null, $userSpace = null) | |
50 | { | |
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 | } | |
57 | $this->initialize(); | |
58 | } | |
59 | ||
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 | ||
79 | /** | |
80 | * Reset the ConfigManager instance. | |
81 | */ | |
82 | public function reset() | |
83 | { | |
84 | $this->initialize(); | |
85 | } | |
86 | ||
87 | /** | |
88 | * Rebuild the loaded config array from config files. | |
89 | */ | |
90 | public function reload() | |
91 | { | |
92 | $this->load(); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Initialize the ConfigIO and loaded the conf. | |
97 | */ | |
98 | protected function initialize() | |
99 | { | |
100 | if (file_exists($this->configFile . '.php')) { | |
101 | $this->configIO = new ConfigPhp(); | |
102 | } else { | |
103 | $this->configIO = new ConfigJson(); | |
104 | } | |
105 | $this->load(); | |
106 | } | |
107 | ||
108 | /** | |
109 | * Load configuration in the ConfigurationManager. | |
110 | */ | |
111 | protected function load() | |
112 | { | |
113 | try { | |
114 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); | |
115 | } catch (\Exception $e) { | |
116 | die($e->getMessage()); | |
117 | } | |
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 | { | |
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 | ||
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. | |
155 | * @param mixed $value Value to set. | |
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 set($setting, $value, $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::setConfig($settings, $value, $this->loadedConfig); | |
174 | if ($write) { | |
175 | $this->write($isLoggedIn); | |
176 | } | |
177 | } | |
178 | ||
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 | ||
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 | { | |
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 | ||
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 | * | |
235 | * @return bool True if the configuration has been successfully written, false otherwise. | |
236 | * | |
237 | * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf. | |
238 | * @throws UnauthorizedConfigException: user is not authorize to change configuration. | |
239 | * @throws \IOException: an error occurred while writing the new config file. | |
240 | */ | |
241 | public function write($isLoggedIn) | |
242 | { | |
243 | // These fields are required in configuration. | |
244 | $mandatoryFields = array( | |
245 | 'credentials.login', | |
246 | 'credentials.hash', | |
247 | 'credentials.salt', | |
248 | 'security.session_protection_disabled', | |
249 | 'general.timezone', | |
250 | 'general.title', | |
251 | 'general.header_link', | |
252 | 'privacy.default_private_links', | |
253 | 'redirector.url', | |
254 | ); | |
255 | ||
256 | // Only logged in user can alter config. | |
257 | if (is_file($this->getConfigFileExt()) && !$isLoggedIn) { | |
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 | ||
268 | return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig); | |
269 | } | |
270 | ||
271 | /** | |
272 | * Set the config file path (without extension). | |
273 | * | |
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. | |
285 | */ | |
286 | public function getConfigFile() | |
287 | { | |
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(); | |
299 | } | |
300 | ||
301 | /** | |
302 | * Get the current userspace. | |
303 | * | |
304 | * @return mixed User space. | |
305 | */ | |
306 | public function getUserSpace() | |
307 | { | |
308 | return $this->userSpace; | |
309 | } | |
310 | ||
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 | |
341 | * @param array $conf Loaded settings, then sub-array. | |
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 | ||
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 | ||
379 | /** | |
380 | * Set a bunch of default values allowing Shaarli to start without a config file. | |
381 | */ | |
382 | protected function setDefaultValues() | |
383 | { | |
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'); | |
404 | $this->setEmpty('resource.raintpl_tpl', 'tpl/'); | |
405 | $this->setEmpty('resource.theme', 'default'); | |
406 | $this->setEmpty('resource.raintpl_tmp', $tmp); | |
407 | $this->setEmpty('resource.thumbnails_cache', $cache); | |
408 | $this->setEmpty('resource.page_cache', $pagecache); | |
409 | ||
410 | $this->setEmpty('security.ban_after', 4); | |
411 | $this->setEmpty('security.ban_duration', 1800); | |
412 | $this->setEmpty('security.session_protection_disabled', false); | |
413 | $this->setEmpty('security.open_shaarli', false); | |
414 | $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']); | |
415 | ||
416 | $this->setEmpty('general.header_link', '?'); | |
417 | $this->setEmpty('general.links_per_page', 20); | |
418 | $this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS); | |
419 | $this->setEmpty('general.default_note_title', 'Note: '); | |
420 | ||
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); | |
426 | $this->setEmpty('feed.show_atom', true); | |
427 | ||
428 | $this->setEmpty('privacy.default_private_links', false); | |
429 | $this->setEmpty('privacy.hide_public_links', false); | |
430 | $this->setEmpty('privacy.force_login', false); | |
431 | $this->setEmpty('privacy.hide_timestamps', false); | |
432 | // default state of the 'remember me' checkbox of the login form | |
433 | $this->setEmpty('privacy.remember_user_default', true); | |
434 | ||
435 | $this->setEmpty('redirector.url', ''); | |
436 | $this->setEmpty('redirector.encode_url', true); | |
437 | ||
438 | $this->setEmpty('thumbnails.width', '125'); | |
439 | $this->setEmpty('thumbnails.height', '90'); | |
440 | ||
441 | $this->setEmpty('translation.language', 'auto'); | |
442 | $this->setEmpty('translation.mode', 'php'); | |
443 | $this->setEmpty('translation.extensions', []); | |
444 | ||
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 | */ | |
454 | public function setEmpty($key, $value) | |
455 | { | |
456 | if (! $this->exists($key)) { | |
457 | $this->set($key, $value); | |
458 | } | |
459 | } | |
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 | } | |
476 | } |