]>
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 A |
11 | * See the documentation for more information on settings: |
12 | * - doc/Shaarli-configuration.html | |
13 | * - https://github.com/shaarli/Shaarli/wiki/Shaarli-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 | { | |
278d9ee2 | 84 | $this->loadedConfig = $this->configIO->read($this->getConfigFileExt()); |
59404d79 A |
85 | $this->setDefaultValues(); |
86 | } | |
87 | ||
88 | /** | |
89 | * Get a setting. | |
90 | * | |
91 | * Supports nested settings with dot separated keys. | |
92 | * Eg. 'config.stuff.option' will find $conf[config][stuff][option], | |
93 | * or in JSON: | |
94 | * { "config": { "stuff": {"option": "mysetting" } } } } | |
95 | * | |
96 | * @param string $setting Asked setting, keys separated with dots. | |
97 | * @param string $default Default value if not found. | |
98 | * | |
99 | * @return mixed Found setting, or the default value. | |
100 | */ | |
101 | public function get($setting, $default = '') | |
102 | { | |
da10377b A |
103 | // During the ConfigIO transition, map legacy settings to the new ones. |
104 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
105 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
106 | } | |
107 | ||
59404d79 A |
108 | $settings = explode('.', $setting); |
109 | $value = self::getConfig($settings, $this->loadedConfig); | |
110 | if ($value === self::$NOT_FOUND) { | |
111 | return $default; | |
112 | } | |
113 | return $value; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Set a setting, and eventually write it. | |
118 | * | |
119 | * Supports nested settings with dot separated keys. | |
120 | * | |
121 | * @param string $setting Asked setting, keys separated with dots. | |
122 | * @param string $value Value to set. | |
123 | * @param bool $write Write the new setting in the config file, default false. | |
124 | * @param bool $isLoggedIn User login state, default false. | |
684e662a | 125 | * |
3c66e564 | 126 | * @throws \Exception Invalid |
59404d79 A |
127 | */ |
128 | public function set($setting, $value, $write = false, $isLoggedIn = false) | |
129 | { | |
684e662a | 130 | if (empty($setting) || ! is_string($setting)) { |
3c66e564 | 131 | throw new \Exception('Invalid setting key parameter. String expected, got: '. gettype($setting)); |
684e662a A |
132 | } |
133 | ||
da10377b A |
134 | // During the ConfigIO transition, map legacy settings to the new ones. |
135 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
136 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
137 | } | |
138 | ||
59404d79 A |
139 | $settings = explode('.', $setting); |
140 | self::setConfig($settings, $value, $this->loadedConfig); | |
141 | if ($write) { | |
142 | $this->write($isLoggedIn); | |
143 | } | |
144 | } | |
145 | ||
146 | /** | |
147 | * Check if a settings exists. | |
148 | * | |
149 | * Supports nested settings with dot separated keys. | |
150 | * | |
151 | * @param string $setting Asked setting, keys separated with dots. | |
152 | * | |
153 | * @return bool true if the setting exists, false otherwise. | |
154 | */ | |
155 | public function exists($setting) | |
156 | { | |
da10377b A |
157 | // During the ConfigIO transition, map legacy settings to the new ones. |
158 | if ($this->configIO instanceof ConfigPhp && isset(ConfigPhp::$LEGACY_KEYS_MAPPING[$setting])) { | |
159 | $setting = ConfigPhp::$LEGACY_KEYS_MAPPING[$setting]; | |
160 | } | |
161 | ||
59404d79 A |
162 | $settings = explode('.', $setting); |
163 | $value = self::getConfig($settings, $this->loadedConfig); | |
164 | if ($value === self::$NOT_FOUND) { | |
165 | return false; | |
166 | } | |
167 | return true; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Call the config writer. | |
172 | * | |
173 | * @param bool $isLoggedIn User login state. | |
174 | * | |
684e662a A |
175 | * @return bool True if the configuration has been successfully written, false otherwise. |
176 | * | |
59404d79 A |
177 | * @throws MissingFieldConfigException: a mandatory field has not been provided in $conf. |
178 | * @throws UnauthorizedConfigException: user is not authorize to change configuration. | |
3c66e564 | 179 | * @throws \IOException: an error occurred while writing the new config file. |
59404d79 A |
180 | */ |
181 | public function write($isLoggedIn) | |
182 | { | |
183 | // These fields are required in configuration. | |
184 | $mandatoryFields = array( | |
da10377b A |
185 | 'credentials.login', |
186 | 'credentials.hash', | |
187 | 'credentials.salt', | |
188 | 'security.session_protection_disabled', | |
189 | 'general.timezone', | |
190 | 'general.title', | |
191 | 'general.header_link', | |
894a3c4b A |
192 | 'privacy.default_private_links', |
193 | 'redirector.url', | |
59404d79 A |
194 | ); |
195 | ||
196 | // Only logged in user can alter config. | |
278d9ee2 | 197 | if (is_file($this->getConfigFileExt()) && !$isLoggedIn) { |
59404d79 A |
198 | throw new UnauthorizedConfigException(); |
199 | } | |
200 | ||
201 | // Check that all mandatory fields are provided in $conf. | |
202 | foreach ($mandatoryFields as $field) { | |
203 | if (! $this->exists($field)) { | |
204 | throw new MissingFieldConfigException($field); | |
205 | } | |
206 | } | |
207 | ||
278d9ee2 | 208 | return $this->configIO->write($this->getConfigFileExt(), $this->loadedConfig); |
59404d79 A |
209 | } |
210 | ||
211 | /** | |
278d9ee2 | 212 | * Set the config file path (without extension). |
59404d79 | 213 | * |
278d9ee2 A |
214 | * @param string $configFile File path. |
215 | */ | |
216 | public function setConfigFile($configFile) | |
217 | { | |
218 | $this->configFile = $configFile; | |
219 | } | |
220 | ||
221 | /** | |
222 | * Return the configuration file path (without extension). | |
223 | * | |
224 | * @return string Config path. | |
59404d79 A |
225 | */ |
226 | public function getConfigFile() | |
227 | { | |
278d9ee2 A |
228 | return $this->configFile; |
229 | } | |
230 | ||
231 | /** | |
232 | * Get the configuration file path with its extension. | |
233 | * | |
234 | * @return string Config file path. | |
235 | */ | |
236 | public function getConfigFileExt() | |
237 | { | |
238 | return $this->configFile . $this->configIO->getExtension(); | |
59404d79 A |
239 | } |
240 | ||
241 | /** | |
242 | * Recursive function which find asked setting in the loaded config. | |
243 | * | |
244 | * @param array $settings Ordered array which contains keys to find. | |
245 | * @param array $conf Loaded settings, then sub-array. | |
246 | * | |
247 | * @return mixed Found setting or NOT_FOUND flag. | |
248 | */ | |
249 | protected static function getConfig($settings, $conf) | |
250 | { | |
251 | if (!is_array($settings) || count($settings) == 0) { | |
252 | return self::$NOT_FOUND; | |
253 | } | |
254 | ||
255 | $setting = array_shift($settings); | |
256 | if (!isset($conf[$setting])) { | |
257 | return self::$NOT_FOUND; | |
258 | } | |
259 | ||
260 | if (count($settings) > 0) { | |
261 | return self::getConfig($settings, $conf[$setting]); | |
262 | } | |
263 | return $conf[$setting]; | |
264 | } | |
265 | ||
266 | /** | |
267 | * Recursive function which find asked setting in the loaded config. | |
268 | * | |
269 | * @param array $settings Ordered array which contains keys to find. | |
270 | * @param mixed $value | |
271 | * @param array $conf Loaded settings, then sub-array. | |
272 | * | |
273 | * @return mixed Found setting or NOT_FOUND flag. | |
274 | */ | |
275 | protected static function setConfig($settings, $value, &$conf) | |
276 | { | |
277 | if (!is_array($settings) || count($settings) == 0) { | |
278 | return self::$NOT_FOUND; | |
279 | } | |
280 | ||
281 | $setting = array_shift($settings); | |
282 | if (count($settings) > 0) { | |
283 | return self::setConfig($settings, $value, $conf[$setting]); | |
284 | } | |
285 | $conf[$setting] = $value; | |
286 | } | |
287 | ||
288 | /** | |
289 | * Set a bunch of default values allowing Shaarli to start without a config file. | |
290 | */ | |
291 | protected function setDefaultValues() | |
292 | { | |
894a3c4b A |
293 | $this->setEmpty('resource.data_dir', 'data'); |
294 | $this->setEmpty('resource.config', 'data/config.php'); | |
295 | $this->setEmpty('resource.datastore', 'data/datastore.php'); | |
296 | $this->setEmpty('resource.ban_file', 'data/ipbans.php'); | |
297 | $this->setEmpty('resource.updates', 'data/updates.txt'); | |
298 | $this->setEmpty('resource.log', 'data/log.txt'); | |
299 | $this->setEmpty('resource.update_check', 'data/lastupdatecheck.txt'); | |
300 | $this->setEmpty('resource.raintpl_tpl', 'tpl/'); | |
adc4aee8 | 301 | $this->setEmpty('resource.theme', 'default'); |
894a3c4b A |
302 | $this->setEmpty('resource.raintpl_tmp', 'tmp/'); |
303 | $this->setEmpty('resource.thumbnails_cache', 'cache'); | |
304 | $this->setEmpty('resource.page_cache', 'pagecache'); | |
59404d79 | 305 | |
da10377b | 306 | $this->setEmpty('security.ban_after', 4); |
278d9ee2 | 307 | $this->setEmpty('security.ban_duration', 1800); |
7f179985 | 308 | $this->setEmpty('security.session_protection_disabled', false); |
894a3c4b | 309 | $this->setEmpty('security.open_shaarli', false); |
59404d79 | 310 | |
7f179985 | 311 | $this->setEmpty('general.header_link', '?'); |
894a3c4b | 312 | $this->setEmpty('general.links_per_page', 20); |
18e67967 | 313 | $this->setEmpty('general.enabled_plugins', self::$DEFAULT_PLUGINS); |
59404d79 | 314 | |
894a3c4b A |
315 | $this->setEmpty('updates.check_updates', false); |
316 | $this->setEmpty('updates.check_updates_branch', 'stable'); | |
317 | $this->setEmpty('updates.check_updates_interval', 86400); | |
318 | ||
319 | $this->setEmpty('feed.rss_permalinks', true); | |
320 | $this->setEmpty('feed.show_atom', false); | |
321 | ||
322 | $this->setEmpty('privacy.default_private_links', false); | |
323 | $this->setEmpty('privacy.hide_public_links', false); | |
324 | $this->setEmpty('privacy.hide_timestamps', false); | |
325 | ||
326 | $this->setEmpty('thumbnail.enable_thumbnails', true); | |
327 | $this->setEmpty('thumbnail.enable_localcache', true); | |
328 | ||
329 | $this->setEmpty('redirector.url', ''); | |
330 | $this->setEmpty('redirector.encode_url', true); | |
59404d79 | 331 | |
59404d79 A |
332 | $this->setEmpty('plugins', array()); |
333 | } | |
334 | ||
335 | /** | |
336 | * Set only if the setting does not exists. | |
337 | * | |
338 | * @param string $key Setting key. | |
339 | * @param mixed $value Setting value. | |
340 | */ | |
7f179985 | 341 | public function setEmpty($key, $value) |
59404d79 A |
342 | { |
343 | if (! $this->exists($key)) { | |
344 | $this->set($key, $value); | |
345 | } | |
346 | } | |
684e662a A |
347 | |
348 | /** | |
349 | * @return ConfigIO | |
350 | */ | |
351 | public function getConfigIO() | |
352 | { | |
353 | return $this->configIO; | |
354 | } | |
355 | ||
356 | /** | |
357 | * @param ConfigIO $configIO | |
358 | */ | |
359 | public function setConfigIO($configIO) | |
360 | { | |
361 | $this->configIO = $configIO; | |
362 | } | |
59404d79 | 363 | } |