]>
Commit | Line | Data |
---|---|---|
510377d2 A |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class Updater. | |
5 | * Used to update stuff when a new Shaarli's version is reached. | |
6 | * Update methods are ran only once, and the stored in a JSON file. | |
7 | */ | |
8 | class Updater | |
9 | { | |
10 | /** | |
11 | * @var array Updates which are already done. | |
12 | */ | |
13 | protected $doneUpdates; | |
14 | ||
510377d2 A |
15 | /** |
16 | * @var LinkDB instance. | |
17 | */ | |
18 | protected $linkDB; | |
19 | ||
278d9ee2 A |
20 | /** |
21 | * @var ConfigManager $conf Configuration Manager instance. | |
22 | */ | |
23 | protected $conf; | |
24 | ||
510377d2 A |
25 | /** |
26 | * @var bool True if the user is logged in, false otherwise. | |
27 | */ | |
28 | protected $isLoggedIn; | |
29 | ||
30 | /** | |
31 | * @var ReflectionMethod[] List of current class methods. | |
32 | */ | |
33 | protected $methods; | |
34 | ||
35 | /** | |
36 | * Object constructor. | |
37 | * | |
278d9ee2 A |
38 | * @param array $doneUpdates Updates which are already done. |
39 | * @param LinkDB $linkDB LinkDB instance. | |
7af9a418 | 40 | * @param ConfigManager $conf Configuration Manager instance. |
278d9ee2 | 41 | * @param boolean $isLoggedIn True if the user is logged in. |
510377d2 | 42 | */ |
278d9ee2 | 43 | public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) |
510377d2 A |
44 | { |
45 | $this->doneUpdates = $doneUpdates; | |
510377d2 | 46 | $this->linkDB = $linkDB; |
278d9ee2 | 47 | $this->conf = $conf; |
510377d2 A |
48 | $this->isLoggedIn = $isLoggedIn; |
49 | ||
50 | // Retrieve all update methods. | |
51 | $class = new ReflectionClass($this); | |
52 | $this->methods = $class->getMethods(); | |
53 | } | |
54 | ||
55 | /** | |
56 | * Run all new updates. | |
57 | * Update methods have to start with 'updateMethod' and return true (on success). | |
58 | * | |
59 | * @return array An array containing ran updates. | |
60 | * | |
61 | * @throws UpdaterException If something went wrong. | |
62 | */ | |
63 | public function update() | |
64 | { | |
65 | $updatesRan = array(); | |
66 | ||
67 | // If the user isn't logged in, exit without updating. | |
68 | if ($this->isLoggedIn !== true) { | |
69 | return $updatesRan; | |
70 | } | |
71 | ||
ee6f4b64 | 72 | if ($this->methods === null) { |
510377d2 A |
73 | throw new UpdaterException('Couldn\'t retrieve Updater class methods.'); |
74 | } | |
75 | ||
76 | foreach ($this->methods as $method) { | |
77 | // Not an update method or already done, pass. | |
78 | if (! startsWith($method->getName(), 'updateMethod') | |
79 | || in_array($method->getName(), $this->doneUpdates) | |
80 | ) { | |
81 | continue; | |
82 | } | |
83 | ||
84 | try { | |
85 | $method->setAccessible(true); | |
86 | $res = $method->invoke($this); | |
87 | // Update method must return true to be considered processed. | |
88 | if ($res === true) { | |
89 | $updatesRan[] = $method->getName(); | |
90 | } | |
91 | } catch (Exception $e) { | |
92 | throw new UpdaterException($method, $e); | |
93 | } | |
94 | } | |
95 | ||
96 | $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan); | |
97 | ||
98 | return $updatesRan; | |
99 | } | |
100 | ||
101 | /** | |
102 | * @return array Updates methods already processed. | |
103 | */ | |
104 | public function getDoneUpdates() | |
105 | { | |
106 | return $this->doneUpdates; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Move deprecated options.php to config.php. | |
111 | * | |
112 | * Milestone 0.9 (old versioning) - shaarli/Shaarli#41: | |
113 | * options.php is not supported anymore. | |
114 | */ | |
115 | public function updateMethodMergeDeprecatedConfigFile() | |
116 | { | |
894a3c4b A |
117 | if (is_file($this->conf->get('resource.data_dir') . '/options.php')) { |
118 | include $this->conf->get('resource.data_dir') . '/options.php'; | |
510377d2 A |
119 | |
120 | // Load GLOBALS into config | |
684e662a A |
121 | $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS); |
122 | $allowedKeys[] = 'config'; | |
510377d2 | 123 | foreach ($GLOBALS as $key => $value) { |
684e662a | 124 | if (in_array($key, $allowedKeys)) { |
278d9ee2 | 125 | $this->conf->set($key, $value); |
684e662a | 126 | } |
510377d2 | 127 | } |
278d9ee2 | 128 | $this->conf->write($this->isLoggedIn); |
894a3c4b | 129 | unlink($this->conf->get('resource.data_dir').'/options.php'); |
510377d2 A |
130 | } |
131 | ||
132 | return true; | |
133 | } | |
21979ff1 | 134 | |
b74b96bf A |
135 | /** |
136 | * Move old configuration in PHP to the new config system in JSON format. | |
137 | * | |
da10377b A |
138 | * Will rename 'config.php' into 'config.save.php' and create 'config.json.php'. |
139 | * It will also convert legacy setting keys to the new ones. | |
b74b96bf A |
140 | */ |
141 | public function updateMethodConfigToJson() | |
142 | { | |
b74b96bf | 143 | // JSON config already exists, nothing to do. |
278d9ee2 | 144 | if ($this->conf->getConfigIO() instanceof ConfigJson) { |
b74b96bf A |
145 | return true; |
146 | } | |
147 | ||
148 | $configPhp = new ConfigPhp(); | |
149 | $configJson = new ConfigJson(); | |
278d9ee2 A |
150 | $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php'); |
151 | rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save.php'); | |
152 | $this->conf->setConfigIO($configJson); | |
153 | $this->conf->reload(); | |
b74b96bf | 154 | |
da10377b | 155 | $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING); |
b74b96bf | 156 | foreach (ConfigPhp::$ROOT_KEYS as $key) { |
278d9ee2 | 157 | $this->conf->set($legacyMap[$key], $oldConfig[$key]); |
b74b96bf A |
158 | } |
159 | ||
160 | // Set sub config keys (config and plugins) | |
161 | $subConfig = array('config', 'plugins'); | |
162 | foreach ($subConfig as $sub) { | |
163 | foreach ($oldConfig[$sub] as $key => $value) { | |
da10377b A |
164 | if (isset($legacyMap[$sub .'.'. $key])) { |
165 | $configKey = $legacyMap[$sub .'.'. $key]; | |
166 | } else { | |
167 | $configKey = $sub .'.'. $key; | |
168 | } | |
278d9ee2 | 169 | $this->conf->set($configKey, $value); |
b74b96bf A |
170 | } |
171 | } | |
172 | ||
173 | try{ | |
278d9ee2 | 174 | $this->conf->write($this->isLoggedIn); |
b74b96bf A |
175 | return true; |
176 | } catch (IOException $e) { | |
177 | error_log($e->getMessage()); | |
178 | return false; | |
179 | } | |
180 | } | |
7f179985 A |
181 | |
182 | /** | |
183 | * Escape settings which have been manually escaped in every request in previous versions: | |
184 | * - general.title | |
185 | * - general.header_link | |
b9f8b837 | 186 | * - redirector.url |
7f179985 A |
187 | * |
188 | * @return bool true if the update is successful, false otherwise. | |
189 | */ | |
b9f8b837 | 190 | public function updateMethodEscapeUnescapedConfig() |
7f179985 | 191 | { |
7f179985 | 192 | try { |
278d9ee2 A |
193 | $this->conf->set('general.title', escape($this->conf->get('general.title'))); |
194 | $this->conf->set('general.header_link', escape($this->conf->get('general.header_link'))); | |
894a3c4b | 195 | $this->conf->set('redirector.url', escape($this->conf->get('redirector.url'))); |
278d9ee2 | 196 | $this->conf->write($this->isLoggedIn); |
7f179985 A |
197 | } catch (Exception $e) { |
198 | error_log($e->getMessage()); | |
199 | return false; | |
200 | } | |
201 | return true; | |
202 | } | |
1dc37f9c A |
203 | |
204 | /** | |
205 | * Update the database to use the new ID system, which replaces linkdate primary keys. | |
01878a75 | 206 | * Also, creation and update dates are now DateTime objects (done by LinkDB). |
1dc37f9c A |
207 | * |
208 | * Since this update is very sensitve (changing the whole database), the datastore will be | |
209 | * automatically backed up into the file datastore.<datetime>.php. | |
210 | * | |
d592daea A |
211 | * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash), |
212 | * which will be saved by this method. | |
213 | * | |
1dc37f9c A |
214 | * @return bool true if the update is successful, false otherwise. |
215 | */ | |
216 | public function updateMethodDatastoreIds() | |
217 | { | |
218 | // up to date database | |
219 | if (isset($this->linkDB[0])) { | |
220 | return true; | |
221 | } | |
222 | ||
223 | $save = $this->conf->get('resource.data_dir') .'/datastore.'. date('YmdHis') .'.php'; | |
224 | copy($this->conf->get('resource.datastore'), $save); | |
225 | ||
226 | $links = array(); | |
227 | foreach ($this->linkDB as $offset => $value) { | |
228 | $links[] = $value; | |
229 | unset($this->linkDB[$offset]); | |
230 | } | |
231 | $links = array_reverse($links); | |
232 | $cpt = 0; | |
233 | foreach ($links as $l) { | |
1dc37f9c A |
234 | unset($l['linkdate']); |
235 | $l['id'] = $cpt; | |
236 | $this->linkDB[$cpt++] = $l; | |
237 | } | |
238 | ||
239 | $this->linkDB->save($this->conf->get('resource.page_cache')); | |
240 | $this->linkDB->reorder(); | |
241 | ||
242 | return true; | |
243 | } | |
cbfdcff2 | 244 | |
c03455af A |
245 | /** |
246 | * Rename tags starting with a '-' to work with tag exclusion search. | |
247 | */ | |
248 | public function updateMethodRenameDashTags() | |
249 | { | |
250 | $linklist = $this->linkDB->filterSearch(); | |
251 | foreach ($linklist as $key => $link) { | |
252 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); | |
253 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | |
254 | $this->linkDB[$key] = $link; | |
255 | } | |
256 | $this->linkDB->save($this->conf->get('resource.page_cache')); | |
257 | return true; | |
258 | } | |
259 | ||
cbfdcff2 A |
260 | /** |
261 | * Initialize API settings: | |
262 | * - api.enabled: true | |
263 | * - api.secret: generated secret | |
264 | */ | |
265 | public function updateMethodApiSettings() | |
266 | { | |
267 | if ($this->conf->exists('api.secret')) { | |
268 | return true; | |
269 | } | |
270 | ||
271 | $this->conf->set('api.enabled', true); | |
272 | $this->conf->set( | |
273 | 'api.secret', | |
274 | generate_api_secret( | |
275 | $this->conf->get('credentials.login'), | |
276 | $this->conf->get('credentials.salt') | |
277 | ) | |
278 | ); | |
279 | $this->conf->write($this->isLoggedIn); | |
280 | return true; | |
281 | } | |
04a0e8ea A |
282 | |
283 | /** | |
284 | * New setting: theme name. If the default theme is used, nothing to do. | |
285 | * | |
286 | * If the user uses a custom theme, raintpl_tpl dir is updated to the parent directory, | |
287 | * and the current theme is set as default in the theme setting. | |
288 | * | |
289 | * @return bool true if the update is successful, false otherwise. | |
290 | */ | |
291 | public function updateMethodDefaultTheme() | |
292 | { | |
293 | // raintpl_tpl isn't the root template directory anymore. | |
294 | // We run the update only if this folder still contains the template files. | |
295 | $tplDir = $this->conf->get('resource.raintpl_tpl'); | |
296 | $tplFile = $tplDir . '/linklist.html'; | |
297 | if (! file_exists($tplFile)) { | |
298 | return true; | |
299 | } | |
300 | ||
301 | $parent = dirname($tplDir); | |
302 | $this->conf->set('resource.raintpl_tpl', $parent); | |
303 | $this->conf->set('resource.theme', trim(str_replace($parent, '', $tplDir), '/')); | |
304 | $this->conf->write($this->isLoggedIn); | |
305 | ||
306 | // Dependency injection gore | |
307 | RainTPL::$tpl_dir = $tplDir; | |
308 | ||
309 | return true; | |
310 | } | |
7282418b A |
311 | |
312 | /** | |
313 | * Move the file to inc/user.css to data/user.css. | |
314 | * | |
315 | * Note: Due to hardcoded paths, it's not unit testable. But one line of code should be fine. | |
316 | * | |
317 | * @return bool true if the update is successful, false otherwise. | |
318 | */ | |
319 | public function updateMethodMoveUserCss() | |
320 | { | |
321 | if (! is_file('inc/user.css')) { | |
322 | return true; | |
323 | } | |
324 | ||
325 | return rename('inc/user.css', 'data/user.css'); | |
326 | } | |
7dcbfde5 A |
327 | |
328 | /** | |
329 | * While the new default theme is in an unstable state | |
330 | * continue to use the vintage theme | |
331 | */ | |
332 | public function updateMethodDefaultThemeVintage() | |
333 | { | |
334 | if ($this->conf->get('resource.theme') !== 'default') { | |
335 | return true; | |
336 | } | |
337 | $this->conf->set('resource.theme', 'vintage'); | |
338 | $this->conf->write($this->isLoggedIn); | |
e0376101 A |
339 | |
340 | return true; | |
341 | } | |
342 | ||
343 | /** | |
344 | * * `markdown_escape` is a new setting, set to true as default. | |
345 | * | |
346 | * If the markdown plugin was already enabled, escaping is disabled to avoid | |
347 | * breaking existing entries. | |
348 | */ | |
349 | public function updateMethodEscapeMarkdown() | |
350 | { | |
351 | if ($this->conf->exists('security.markdown_escape')) { | |
352 | return true; | |
353 | } | |
354 | ||
355 | if (in_array('markdown', $this->conf->get('general.enabled_plugins'))) { | |
356 | $this->conf->set('security.markdown_escape', false); | |
357 | } else { | |
358 | $this->conf->set('security.markdown_escape', true); | |
359 | } | |
360 | $this->conf->write($this->isLoggedIn); | |
361 | ||
7dcbfde5 A |
362 | return true; |
363 | } | |
510377d2 A |
364 | } |
365 | ||
366 | /** | |
367 | * Class UpdaterException. | |
368 | */ | |
369 | class UpdaterException extends Exception | |
370 | { | |
371 | /** | |
372 | * @var string Method where the error occurred. | |
373 | */ | |
374 | protected $method; | |
375 | ||
376 | /** | |
377 | * @var Exception The parent exception. | |
378 | */ | |
379 | protected $previous; | |
380 | ||
381 | /** | |
382 | * Constructor. | |
383 | * | |
384 | * @param string $message Force the error message if set. | |
385 | * @param string $method Method where the error occurred. | |
386 | * @param Exception|bool $previous Parent exception. | |
387 | */ | |
388 | public function __construct($message = '', $method = '', $previous = false) | |
389 | { | |
390 | $this->method = $method; | |
391 | $this->previous = $previous; | |
392 | $this->message = $this->buildMessage($message); | |
393 | } | |
394 | ||
395 | /** | |
396 | * Build the exception error message. | |
397 | * | |
398 | * @param string $message Optional given error message. | |
399 | * | |
400 | * @return string The built error message. | |
401 | */ | |
402 | private function buildMessage($message) | |
403 | { | |
404 | $out = ''; | |
405 | if (! empty($message)) { | |
406 | $out .= $message . PHP_EOL; | |
407 | } | |
408 | ||
409 | if (! empty($this->method)) { | |
410 | $out .= 'An error occurred while running the update '. $this->method . PHP_EOL; | |
411 | } | |
412 | ||
413 | if (! empty($this->previous)) { | |
414 | $out .= ' '. $this->previous->getMessage(); | |
415 | } | |
416 | ||
417 | return $out; | |
418 | } | |
419 | } | |
420 | ||
510377d2 A |
421 | /** |
422 | * Read the updates file, and return already done updates. | |
423 | * | |
424 | * @param string $updatesFilepath Updates file path. | |
425 | * | |
426 | * @return array Already done update methods. | |
427 | */ | |
428 | function read_updates_file($updatesFilepath) | |
429 | { | |
430 | if (! empty($updatesFilepath) && is_file($updatesFilepath)) { | |
431 | $content = file_get_contents($updatesFilepath); | |
432 | if (! empty($content)) { | |
433 | return explode(';', $content); | |
434 | } | |
435 | } | |
436 | return array(); | |
437 | } | |
438 | ||
439 | /** | |
440 | * Write updates file. | |
441 | * | |
442 | * @param string $updatesFilepath Updates file path. | |
443 | * @param array $updates Updates array to write. | |
444 | * | |
445 | * @throws Exception Couldn't write version number. | |
446 | */ | |
447 | function write_updates_file($updatesFilepath, $updates) | |
448 | { | |
449 | if (empty($updatesFilepath)) { | |
450 | throw new Exception('Updates file path is not set, can\'t write updates.'); | |
451 | } | |
452 | ||
453 | $res = file_put_contents($updatesFilepath, implode(';', $updates)); | |
454 | if ($res === false) { | |
455 | throw new Exception('Unable to write updates in '. $updatesFilepath . '.'); | |
456 | } | |
457 | } |