]>
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 | ||
59404d79 A |
24 | /** |
25 | * @var string Config folder. | |
26 | */ | |
278d9ee2 | 27 | protected $configFile; |
59404d79 A |
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 | /** | |
278d9ee2 | 40 | * Constructor. |
7af9a418 A |
41 | * |
42 | * @param string $configFile Configuration file path without extension. | |
59404d79 | 43 | */ |
278d9ee2 | 44 | public function __construct($configFile = 'data/config') |
59404d79 | 45 | { |
278d9ee2 A |
46 | $this->configFile = $configFile; |
47 | $this->initialize(); | |
59404d79 A |
48 | } |
49 | ||
684e662a A |
50 | /** |
51 | * Reset the ConfigManager instance. | |
52 | */ | |
278d9ee2 | 53 | public function reset() |
684e662a | 54 | { |
278d9ee2 | 55 | $this->initialize(); |
684e662a A |
56 | } |
57 | ||
59404d79 A |
58 | /** |
59 | * Rebuild the loaded config array from config files. | |
60 | */ | |
61 | public function reload() | |
62 | { | |
684e662a | 63 | $this->load(); |
59404d79 A |
64 | } |
65 | ||
66 | /** | |
684e662a | 67 | * Initialize the ConfigIO and loaded the conf. |
59404d79 A |
68 | */ |
69 | protected function initialize() | |
70 | { | |
278d9ee2 | 71 | if (file_exists($this->configFile . '.php')) { |
59404d79 | 72 | $this->configIO = new ConfigPhp(); |
278d9ee2 A |
73 | } else { |
74 | $this->configIO = new ConfigJson(); | |
b74b96bf | 75 | } |
684e662a A |
76 | $this->load(); |
77 | } | |
78 | ||
79 | /** | |
80 | * Load configuration in the ConfigurationManager. | |
81 | */ | |
82 | protected function load() | |
83 | { | |
c6a4c288 A |
84 | try { |
85 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); | |
86 | } catch (\Exception $e) { | |
87 | die($e->getMessage()); | |
88 | } | |
59404d79 A |
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 | { | |
da10377b A |
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 | ||
59404d79 A |
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 string $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. | |
684e662a | 129 | * |
3c66e564 | 130 | * @throws \Exception Invalid |
59404d79 A |
131 | */ |
132 | public function set($setting, $value, $write = false, $isLoggedIn = false) | |
133 | { | |
684e662a | 134 | if (empty($setting) || ! is_string($setting)) { |
3c66e564 | 135 | throw new \Exception('Invalid setting key parameter. String expected, got: '. gettype($setting)); |
684e662a A |
136 | } |
137 | ||
da10377b A |
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 | ||
59404d79 A |
143 | $settings = explode('.', $setting); |
144 | self::setConfig($settings, $value, $this->loadedConfig); | |
145 | if ($write) { | |
146 | $this->write($isLoggedIn); | |
147 | } | |
148 | } | |
149 | ||
150 | /** | |
151 | * Check if a settings exists. | |
152 | * | |
153 | * Supports nested settings with dot separated keys. | |
154 | * | |
155 | * @param string $setting Asked setting, keys separated with dots. | |
156 | * | |
157 | * @return bool true if the setting exists, false otherwise. | |
158 | */ | |
159 | public function exists($setting) | |
160 | { | |
da10377b A |
161 | // During the ConfigIO transition, map legacy settings to the new ones. |
162 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
163 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
164 | } | |
165 | ||
59404d79 A |
166 | $settings = explode('.', $setting); |
167 | $value = self::getConfig($settings, $this->loadedConfig); | |
168 | if ($value === self::$NOT_FOUND) { | |
169 | return false; | |
170 | } | |
171 | return true; | |
172 | } | |
173 | ||
174 | /** | |
175 | * Call the config writer. | |
176 | * | |
177 | * @param bool $isLoggedIn User login state. | |
178 | * | |
684e662a A |
179 | * @return bool True if the configuration has been successfully written, false otherwise. |
180 | * | |
59404d79 A |
181 | * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf. |
182 | * @throws UnauthorizedConfigException: user is not authorize to change configuration. | |
3c66e564 | 183 | * @throws \IOException: an error occurred while writing the new config file. |
59404d79 A |
184 | */ |
185 | public function write($isLoggedIn) | |
186 | { | |
187 | // These fields are required in configuration. | |
188 | $mandatoryFields = array( | |
da10377b A |
189 | 'credentials.login', |
190 | 'credentials.hash', | |
191 | 'credentials.salt', | |
192 | 'security.session_protection_disabled', | |
193 | 'general.timezone', | |
194 | 'general.title', | |
195 | 'general.header_link', | |
894a3c4b A |
196 | 'privacy.default_private_links', |
197 | 'redirector.url', | |
59404d79 A |
198 | ); |
199 | ||
200 | // Only logged in user can alter config. | |
278d9ee2 | 201 | if (is_file($this->getConfigFileExt()) && !$isLoggedIn) { |
59404d79 A |
202 | throw new UnauthorizedConfigException(); |
203 | } | |
204 | ||
205 | // Check that all mandatory fields are provided in $conf. | |
206 | foreach ($mandatoryFields as $field) { | |
207 | if (! $this->exists($field)) { | |
208 | throw new MissingFieldConfigException($field); | |
209 | } | |
210 | } | |
211 | ||
278d9ee2 | 212 | return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig); |
59404d79 A |
213 | } |
214 | ||
215 | /** | |
278d9ee2 | 216 | * Set the config file path (without extension). |
59404d79 | 217 | * |
278d9ee2 A |
218 | * @param string $configFile File path. |
219 | */ | |
220 | public function setConfigFile($configFile) | |
221 | { | |
222 | $this->configFile = $configFile; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Return the configuration file path (without extension). | |
227 | * | |
228 | * @return string Config path. | |
59404d79 A |
229 | */ |
230 | public function getConfigFile() | |
231 | { | |
278d9ee2 A |
232 | return $this->configFile; |
233 | } | |
234 | ||
235 | /** | |
236 | * Get the configuration file path with its extension. | |
237 | * | |
238 | * @return string Config file path. | |
239 | */ | |
240 | public function getConfigFileExt() | |
241 | { | |
242 | return $this->configFile . $this->configIO->getExtension(); | |
59404d79 A |
243 | } |
244 | ||
245 | /** | |
246 | * Recursive function which find asked setting in the loaded config. | |
247 | * | |
248 | * @param array $settings Ordered array which contains keys to find. | |
249 | * @param array $conf Loaded settings, then sub-array. | |
250 | * | |
251 | * @return mixed Found setting or NOT_FOUND flag. | |
252 | */ | |
253 | protected static function getConfig($settings, $conf) | |
254 | { | |
255 | if (!is_array($settings) || count($settings) == 0) { | |
256 | return self::$NOT_FOUND; | |
257 | } | |
258 | ||
259 | $setting = array_shift($settings); | |
260 | if (!isset($conf[$setting])) { | |
261 | return self::$NOT_FOUND; | |
262 | } | |
263 | ||
264 | if (count($settings) > 0) { | |
265 | return self::getConfig($settings, $conf[$setting]); | |
266 | } | |
267 | return $conf[$setting]; | |
268 | } | |
269 | ||
270 | /** | |
271 | * Recursive function which find asked setting in the loaded config. | |
272 | * | |
273 | * @param array $settings Ordered array which contains keys to find. | |
274 | * @param mixed $value | |
275 | * @param array $conf Loaded settings, then sub-array. | |
276 | * | |
277 | * @return mixed Found setting or NOT_FOUND flag. | |
278 | */ | |
279 | protected static function setConfig($settings, $value, &$conf) | |
280 | { | |
281 | if (!is_array($settings) || count($settings) == 0) { | |
282 | return self::$NOT_FOUND; | |
283 | } | |
284 | ||
285 | $setting = array_shift($settings); | |
286 | if (count($settings) > 0) { | |
287 | return self::setConfig($settings, $value, $conf[$setting]); | |
288 | } | |
289 | $conf[$setting] = $value; | |
290 | } | |
291 | ||
292 | /** | |
293 | * Set a bunch of default values allowing Shaarli to start without a config file. | |
294 | */ | |
295 | protected function setDefaultValues() | |
296 | { | |
894a3c4b A |
297 | $this->setEmpty('resource.data_dir', 'data'); |
298 | $this->setEmpty('resource.config', 'data/config.php'); | |
299 | $this->setEmpty('resource.datastore', 'data/datastore.php'); | |
300 | $this->setEmpty('resource.ban_file', 'data/ipbans.php'); | |
301 | $this->setEmpty('resource.updates', 'data/updates.txt'); | |
302 | $this->setEmpty('resource.log', 'data/log.txt'); | |
303 | $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt'); | |
4306b184 | 304 | $this->setEmpty('resource.history', 'data/history.php'); |
894a3c4b | 305 | $this->setEmpty('resource.raintpl_tpl', 'tpl/'); |
adc4aee8 | 306 | $this->setEmpty('resource.theme', 'default'); |
894a3c4b A |
307 | $this->setEmpty('resource.raintpl_tmp', 'tmp/'); |
308 | $this->setEmpty('resource.thumbnails_cache', 'cache'); | |
309 | $this->setEmpty('resource.page_cache', 'pagecache'); | |
59404d79 | 310 | |
da10377b | 311 | $this->setEmpty('security.ban_after', 4); |
278d9ee2 | 312 | $this->setEmpty('security.ban_duration', 1800); |
7f179985 | 313 | $this->setEmpty('security.session_protection_disabled', false); |
894a3c4b | 314 | $this->setEmpty('security.open_shaarli', false); |
86ceea05 | 315 | $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']); |
59404d79 | 316 | |
7f179985 | 317 | $this->setEmpty('general.header_link', '?'); |
894a3c4b | 318 | $this->setEmpty('general.links_per_page', 20); |
18e67967 | 319 | $this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS); |
722caa20 | 320 | $this->setEmpty('general.default_note_title', 'Note: '); |
59404d79 | 321 | |
894a3c4b A |
322 | $this->setEmpty('updates.check_updates', false); |
323 | $this->setEmpty('updates.check_updates_branch', 'stable'); | |
324 | $this->setEmpty('updates.check_updates_interval', 86400); | |
325 | ||
326 | $this->setEmpty('feed.rss_permalinks', true); | |
2ea89aba | 327 | $this->setEmpty('feed.show_atom', true); |
894a3c4b A |
328 | |
329 | $this->setEmpty('privacy.default_private_links', false); | |
330 | $this->setEmpty('privacy.hide_public_links', false); | |
27e21231 | 331 | $this->setEmpty('privacy.force_login', false); |
894a3c4b | 332 | $this->setEmpty('privacy.hide_timestamps', false); |
2e07e775 WE |
333 | // default state of the 'remember me' checkbox of the login form |
334 | $this->setEmpty('privacy.remember_user_default', true); | |
894a3c4b A |
335 | |
336 | $this->setEmpty('thumbnail.enable_thumbnails', true); | |
337 | $this->setEmpty('thumbnail.enable_localcache', true); | |
338 | ||
339 | $this->setEmpty('redirector.url', ''); | |
340 | $this->setEmpty('redirector.encode_url', true); | |
59404d79 | 341 | |
59404d79 A |
342 | $this->setEmpty('plugins', array()); |
343 | } | |
344 | ||
345 | /** | |
346 | * Set only if the setting does not exists. | |
347 | * | |
348 | * @param string $key Setting key. | |
349 | * @param mixed $value Setting value. | |
350 | */ | |
7f179985 | 351 | public function setEmpty($key, $value) |
59404d79 A |
352 | { |
353 | if (! $this->exists($key)) { | |
354 | $this->set($key, $value); | |
355 | } | |
356 | } | |
684e662a A |
357 | |
358 | /** | |
359 | * @return ConfigIO | |
360 | */ | |
361 | public function getConfigIO() | |
362 | { | |
363 | return $this->configIO; | |
364 | } | |
365 | ||
366 | /** | |
367 | * @param ConfigIO $configIO | |
368 | */ | |
369 | public function setConfigIO($configIO) | |
370 | { | |
371 | $this->configIO = $configIO; | |
372 | } | |
59404d79 | 373 | } |