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