]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/config/ConfigManager.php
Shaarli's translation
[github/shaarli/Shaarli.git] / application / config / ConfigManager.php
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 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.
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 * 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 {
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
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 *
179 * @return bool True if the configuration has been successfully written, false otherwise.
180 *
181 * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf.
182 * @throws UnauthorizedConfigException: user is not authorize to change configuration.
183 * @throws \IOException: an error occurred while writing the new config file.
184 */
185 public function write($isLoggedIn)
186 {
187 // These fields are required in configuration.
188 $mandatoryFields = array(
189 'credentials.login',
190 'credentials.hash',
191 'credentials.salt',
192 'security.session_protection_disabled',
193 'general.timezone',
194 'general.title',
195 'general.header_link',
196 'privacy.default_private_links',
197 'redirector.url',
198 );
199
200 // Only logged in user can alter config.
201 if (is_file($this->getConfigFileExt()) && !$isLoggedIn) {
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
212 return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig);
213 }
214
215 /**
216 * Set the config file path (without extension).
217 *
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.
229 */
230 public function getConfigFile()
231 {
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();
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 {
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');
304 $this->setEmpty('resource.history', 'data/history.php');
305 $this->setEmpty('resource.raintpl_tpl', 'tpl/');
306 $this->setEmpty('resource.theme', 'default');
307 $this->setEmpty('resource.raintpl_tmp', 'tmp/');
308 $this->setEmpty('resource.thumbnails_cache', 'cache');
309 $this->setEmpty('resource.page_cache', 'pagecache');
310
311 $this->setEmpty('security.ban_after', 4);
312 $this->setEmpty('security.ban_duration', 1800);
313 $this->setEmpty('security.session_protection_disabled', false);
314 $this->setEmpty('security.open_shaarli', false);
315 $this->setEmpty('security.allowed_protocols', ['ftp', 'ftps', 'magnet']);
316
317 $this->setEmpty('general.header_link', '?');
318 $this->setEmpty('general.links_per_page', 20);
319 $this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS);
320 $this->setEmpty('general.default_note_title', 'Note: ');
321
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);
327 $this->setEmpty('feed.show_atom', true);
328
329 $this->setEmpty('privacy.default_private_links', false);
330 $this->setEmpty('privacy.hide_public_links', false);
331 $this->setEmpty('privacy.force_login', false);
332 $this->setEmpty('privacy.hide_timestamps', false);
333 // default state of the 'remember me' checkbox of the login form
334 $this->setEmpty('privacy.remember_user_default', true);
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);
341
342 $this->setEmpty('translation.language', 'auto');
343 $this->setEmpty('translation.mode', 'php');
344 $this->setEmpty('translation.extensions', []);
345
346 $this->setEmpty('plugins', array());
347 }
348
349 /**
350 * Set only if the setting does not exists.
351 *
352 * @param string $key Setting key.
353 * @param mixed $value Setting value.
354 */
355 public function setEmpty($key, $value)
356 {
357 if (! $this->exists($key)) {
358 $this->set($key, $value);
359 }
360 }
361
362 /**
363 * @return ConfigIO
364 */
365 public function getConfigIO()
366 {
367 return $this->configIO;
368 }
369
370 /**
371 * @param ConfigIO $configIO
372 */
373 public function setConfigIO($configIO)
374 {
375 $this->configIO = $configIO;
376 }
377 }