]>
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 Config folder. | |
26 | */ | |
27 | protected $configFile; | |
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 | /** | |
40 | * Constructor. | |
41 | * | |
42 | * @param string $configFile Configuration file path without extension. | |
43 | */ | |
44 | public function __construct($configFile = 'data/config') | |
45 | { | |
46 | $this->configFile = $configFile; | |
47 | $this->initialize(); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Reset the ConfigManager instance. | |
52 | */ | |
53 | public function reset() | |
54 | { | |
55 | $this->initialize(); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Rebuild the loaded config array from config files. | |
60 | */ | |
61 | public function reload() | |
62 | { | |
63 | $this->load(); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Initialize the ConfigIO and loaded the conf. | |
68 | */ | |
69 | protected function initialize() | |
70 | { | |
71 | if (file_exists($this->configFile . '.php')) { | |
72 | $this->configIO = new ConfigPhp(); | |
73 | } else { | |
74 | $this->configIO = new ConfigJson(); | |
75 | } | |
76 | $this->load(); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Load configuration in the ConfigurationManager. | |
81 | */ | |
82 | protected function load() | |
83 | { | |
84 | try { | |
85 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); | |
86 | } catch (\Exception $e) { | |
87 | die($e->getMessage()); | |
88 | } | |
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 | { | |
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 | ||
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. | |
126 | * @param mixed $value Value to set. | |
127 | * @param bool $write Write the new setting in the config file, default false. | |
128 | * @param bool $isLoggedIn User login state, default false. | |
129 | * | |
130 | * @throws \Exception Invalid | |
131 | */ | |
132 | public function set($setting, $value, $write = false, $isLoggedIn = false) | |
133 | { | |
134 | if (empty($setting) || ! is_string($setting)) { | |
135 | throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting)); | |
136 | } | |
137 | ||
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 | ||
143 | $settings = explode('.', $setting); | |
144 | self::setConfig($settings, $value, $this->loadedConfig); | |
145 | if ($write) { | |
146 | $this->write($isLoggedIn); | |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
151 | * Remove a config element from the config file. | |
152 | * | |
153 | * @param string $setting Asked setting, keys separated with dots. | |
154 | * @param bool $write Write the new setting in the config file, default false. | |
155 | * @param bool $isLoggedIn User login state, default false. | |
156 | * | |
157 | * @throws \Exception Invalid | |
158 | */ | |
159 | public function remove($setting, $write = false, $isLoggedIn = false) | |
160 | { | |
161 | if (empty($setting) || ! is_string($setting)) { | |
162 | throw new \Exception(t('Invalid setting key parameter. String expected, got: '). gettype($setting)); | |
163 | } | |
164 | ||
165 | // During the ConfigIO transition, map legacy settings to the new ones. | |
166 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
167 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
168 | } | |
169 | ||
170 | $settings = explode('.', $setting); | |
171 | self::removeConfig($settings, $this->loadedConfig); | |
172 | if ($write) { | |
173 | $this->write($isLoggedIn); | |
174 | } | |
175 | } | |
176 | ||
177 | /** | |
178 | * Check if a settings exists. | |
179 | * | |
180 | * Supports nested settings with dot separated keys. | |
181 | * | |
182 | * @param string $setting Asked setting, keys separated with dots. | |
183 | * | |
184 | * @return bool true if the setting exists, false otherwise. | |
185 | */ | |
186 | public function exists($setting) | |
187 | { | |
188 | // During the ConfigIO transition, map legacy settings to the new ones. | |
189 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
190 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
191 | } | |
192 | ||
193 | $settings = explode('.', $setting); | |
194 | $value = self::getConfig($settings, $this->loadedConfig); | |
195 | if ($value === self::$NOT_FOUND) { | |
196 | return false; | |
197 | } | |
198 | return true; | |
199 | } | |
200 | ||
201 | /** | |
202 | * Call the config writer. | |
203 | * | |
204 | * @param bool $isLoggedIn User login state. | |
205 | * | |
206 | * @return bool True if the configuration has been successfully written, false otherwise. | |
207 | * | |
208 | * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf. | |
209 | * @throws UnauthorizedConfigException: user is not authorize to change configuration. | |
210 | * @throws \IOException: an error occurred while writing the new config file. | |
211 | */ | |
212 | public function write($isLoggedIn) | |
213 | { | |
214 | // These fields are required in configuration. | |
215 | $mandatoryFields = array( | |
216 | 'credentials.login', | |
217 | 'credentials.hash', | |
218 | 'credentials.salt', | |
219 | 'security.session_protection_disabled', | |
220 | 'general.timezone', | |
221 | 'general.title', | |
222 | 'general.header_link', | |
223 | 'privacy.default_private_links', | |
224 | 'redirector.url', | |
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 | ||
370 | $this->setEmpty('updates.check_updates', false); | |
371 | $this->setEmpty('updates.check_updates_branch', 'stable'); | |
372 | $this->setEmpty('updates.check_updates_interval', 86400); | |
373 | ||
374 | $this->setEmpty('feed.rss_permalinks', true); | |
375 | $this->setEmpty('feed.show_atom', true); | |
376 | ||
377 | $this->setEmpty('privacy.default_private_links', false); | |
378 | $this->setEmpty('privacy.hide_public_links', false); | |
379 | $this->setEmpty('privacy.force_login', false); | |
380 | $this->setEmpty('privacy.hide_timestamps', false); | |
381 | // default state of the 'remember me' checkbox of the login form | |
382 | $this->setEmpty('privacy.remember_user_default', true); | |
383 | ||
384 | $this->setEmpty('redirector.url', ''); | |
385 | $this->setEmpty('redirector.encode_url', true); | |
386 | ||
387 | $this->setEmpty('thumbnails.width', '125'); | |
388 | $this->setEmpty('thumbnails.height', '90'); | |
389 | ||
390 | $this->setEmpty('translation.language', 'auto'); | |
391 | $this->setEmpty('translation.mode', 'php'); | |
392 | $this->setEmpty('translation.extensions', []); | |
393 | ||
394 | $this->setEmpty('plugins', array()); | |
395 | } | |
396 | ||
397 | /** | |
398 | * Set only if the setting does not exists. | |
399 | * | |
400 | * @param string $key Setting key. | |
401 | * @param mixed $value Setting value. | |
402 | */ | |
403 | public function setEmpty($key, $value) | |
404 | { | |
405 | if (! $this->exists($key)) { | |
406 | $this->set($key, $value); | |
407 | } | |
408 | } | |
409 | ||
410 | /** | |
411 | * @return ConfigIO | |
412 | */ | |
413 | public function getConfigIO() | |
414 | { | |
415 | return $this->configIO; | |
416 | } | |
417 | ||
418 | /** | |
419 | * @param ConfigIO $configIO | |
420 | */ | |
421 | public function setConfigIO($configIO) | |
422 | { | |
423 | $this->configIO = $configIO; | |
424 | } | |
425 | } |