diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:07:13 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:07:13 +0200 |
commit | d9f6275ebca035fec8331652c677981056793ccc (patch) | |
tree | 37a64baf4f0eba6b781040605965383d8aded2cc /application/updater/Updater.php | |
parent | 38672ba0d1c722e5d6d33a58255ceb55e9410e46 (diff) | |
parent | d63ff87a009313141ae684ec447b902562ff6ee7 (diff) | |
download | Shaarli-stable.tar.gz Shaarli-stable.tar.zst Shaarli-stable.zip |
Merge branch 'v0.11' into stablestable
Diffstat (limited to 'application/updater/Updater.php')
-rw-r--r-- | application/updater/Updater.php | 564 |
1 files changed, 564 insertions, 0 deletions
diff --git a/application/updater/Updater.php b/application/updater/Updater.php new file mode 100644 index 00000000..30e5247b --- /dev/null +++ b/application/updater/Updater.php | |||
@@ -0,0 +1,564 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Updater; | ||
4 | |||
5 | use Exception; | ||
6 | use RainTPL; | ||
7 | use ReflectionClass; | ||
8 | use ReflectionException; | ||
9 | use ReflectionMethod; | ||
10 | use Shaarli\ApplicationUtils; | ||
11 | use Shaarli\Bookmark\LinkDB; | ||
12 | use Shaarli\Bookmark\LinkFilter; | ||
13 | use Shaarli\Config\ConfigJson; | ||
14 | use Shaarli\Config\ConfigManager; | ||
15 | use Shaarli\Config\ConfigPhp; | ||
16 | use Shaarli\Exceptions\IOException; | ||
17 | use Shaarli\Thumbnailer; | ||
18 | use Shaarli\Updater\Exception\UpdaterException; | ||
19 | |||
20 | /** | ||
21 | * Class updater. | ||
22 | * Used to update stuff when a new Shaarli's version is reached. | ||
23 | * Update methods are ran only once, and the stored in a JSON file. | ||
24 | */ | ||
25 | class Updater | ||
26 | { | ||
27 | /** | ||
28 | * @var array Updates which are already done. | ||
29 | */ | ||
30 | protected $doneUpdates; | ||
31 | |||
32 | /** | ||
33 | * @var LinkDB instance. | ||
34 | */ | ||
35 | protected $linkDB; | ||
36 | |||
37 | /** | ||
38 | * @var ConfigManager $conf Configuration Manager instance. | ||
39 | */ | ||
40 | protected $conf; | ||
41 | |||
42 | /** | ||
43 | * @var bool True if the user is logged in, false otherwise. | ||
44 | */ | ||
45 | protected $isLoggedIn; | ||
46 | |||
47 | /** | ||
48 | * @var array $_SESSION | ||
49 | */ | ||
50 | protected $session; | ||
51 | |||
52 | /** | ||
53 | * @var ReflectionMethod[] List of current class methods. | ||
54 | */ | ||
55 | protected $methods; | ||
56 | |||
57 | /** | ||
58 | * Object constructor. | ||
59 | * | ||
60 | * @param array $doneUpdates Updates which are already done. | ||
61 | * @param LinkDB $linkDB LinkDB instance. | ||
62 | * @param ConfigManager $conf Configuration Manager instance. | ||
63 | * @param boolean $isLoggedIn True if the user is logged in. | ||
64 | * @param array $session $_SESSION (by reference) | ||
65 | * | ||
66 | * @throws ReflectionException | ||
67 | */ | ||
68 | public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn, &$session = []) | ||
69 | { | ||
70 | $this->doneUpdates = $doneUpdates; | ||
71 | $this->linkDB = $linkDB; | ||
72 | $this->conf = $conf; | ||
73 | $this->isLoggedIn = $isLoggedIn; | ||
74 | $this->session = &$session; | ||
75 | |||
76 | // Retrieve all update methods. | ||
77 | $class = new ReflectionClass($this); | ||
78 | $this->methods = $class->getMethods(); | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Run all new updates. | ||
83 | * Update methods have to start with 'updateMethod' and return true (on success). | ||
84 | * | ||
85 | * @return array An array containing ran updates. | ||
86 | * | ||
87 | * @throws UpdaterException If something went wrong. | ||
88 | */ | ||
89 | public function update() | ||
90 | { | ||
91 | $updatesRan = array(); | ||
92 | |||
93 | // If the user isn't logged in, exit without updating. | ||
94 | if ($this->isLoggedIn !== true) { | ||
95 | return $updatesRan; | ||
96 | } | ||
97 | |||
98 | if ($this->methods === null) { | ||
99 | throw new UpdaterException(t('Couldn\'t retrieve updater class methods.')); | ||
100 | } | ||
101 | |||
102 | foreach ($this->methods as $method) { | ||
103 | // Not an update method or already done, pass. | ||
104 | if (!startsWith($method->getName(), 'updateMethod') | ||
105 | || in_array($method->getName(), $this->doneUpdates) | ||
106 | ) { | ||
107 | continue; | ||
108 | } | ||
109 | |||
110 | try { | ||
111 | $method->setAccessible(true); | ||
112 | $res = $method->invoke($this); | ||
113 | // Update method must return true to be considered processed. | ||
114 | if ($res === true) { | ||
115 | $updatesRan[] = $method->getName(); | ||
116 | } | ||
117 | } catch (Exception $e) { | ||
118 | throw new UpdaterException($method, $e); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | $this->doneUpdates = array_merge($this->doneUpdates, $updatesRan); | ||
123 | |||
124 | return $updatesRan; | ||
125 | } | ||
126 | |||
127 | /** | ||
128 | * @return array Updates methods already processed. | ||
129 | */ | ||
130 | public function getDoneUpdates() | ||
131 | { | ||
132 | return $this->doneUpdates; | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Move deprecated options.php to config.php. | ||
137 | * | ||
138 | * Milestone 0.9 (old versioning) - shaarli/Shaarli#41: | ||
139 | * options.php is not supported anymore. | ||
140 | */ | ||
141 | public function updateMethodMergeDeprecatedConfigFile() | ||
142 | { | ||
143 | if (is_file($this->conf->get('resource.data_dir') . '/options.php')) { | ||
144 | include $this->conf->get('resource.data_dir') . '/options.php'; | ||
145 | |||
146 | // Load GLOBALS into config | ||
147 | $allowedKeys = array_merge(ConfigPhp::$ROOT_KEYS); | ||
148 | $allowedKeys[] = 'config'; | ||
149 | foreach ($GLOBALS as $key => $value) { | ||
150 | if (in_array($key, $allowedKeys)) { | ||
151 | $this->conf->set($key, $value); | ||
152 | } | ||
153 | } | ||
154 | $this->conf->write($this->isLoggedIn); | ||
155 | unlink($this->conf->get('resource.data_dir') . '/options.php'); | ||
156 | } | ||
157 | |||
158 | return true; | ||
159 | } | ||
160 | |||
161 | /** | ||
162 | * Move old configuration in PHP to the new config system in JSON format. | ||
163 | * | ||
164 | * Will rename 'config.php' into 'config.save.php' and create 'config.json.php'. | ||
165 | * It will also convert legacy setting keys to the new ones. | ||
166 | */ | ||
167 | public function updateMethodConfigToJson() | ||
168 | { | ||
169 | // JSON config already exists, nothing to do. | ||
170 | if ($this->conf->getConfigIO() instanceof ConfigJson) { | ||
171 | return true; | ||
172 | } | ||
173 | |||
174 | $configPhp = new ConfigPhp(); | ||
175 | $configJson = new ConfigJson(); | ||
176 | $oldConfig = $configPhp->read($this->conf->getConfigFile() . '.php'); | ||
177 | rename($this->conf->getConfigFileExt(), $this->conf->getConfigFile() . '.save.php'); | ||
178 | $this->conf->setConfigIO($configJson); | ||
179 | $this->conf->reload(); | ||
180 | |||
181 | $legacyMap = array_flip(ConfigPhp::$LEGACY_KEYS_MAPPING); | ||
182 | foreach (ConfigPhp::$ROOT_KEYS as $key) { | ||
183 | $this->conf->set($legacyMap[$key], $oldConfig[$key]); | ||
184 | } | ||
185 | |||
186 | // Set sub config keys (config and plugins) | ||
187 | $subConfig = array('config', 'plugins'); | ||
188 | foreach ($subConfig as $sub) { | ||
189 | foreach ($oldConfig[$sub] as $key => $value) { | ||
190 | if (isset($legacyMap[$sub . '.' . $key])) { | ||
191 | $configKey = $legacyMap[$sub . '.' . $key]; | ||
192 | } else { | ||
193 | $configKey = $sub . '.' . $key; | ||
194 | } | ||
195 | $this->conf->set($configKey, $value); | ||
196 | } | ||
197 | } | ||
198 | |||
199 | try { | ||
200 | $this->conf->write($this->isLoggedIn); | ||
201 | return true; | ||
202 | } catch (IOException $e) { | ||
203 | error_log($e->getMessage()); | ||
204 | return false; | ||
205 | } | ||
206 | } | ||
207 | |||
208 | /** | ||
209 | * Escape settings which have been manually escaped in every request in previous versions: | ||
210 | * - general.title | ||
211 | * - general.header_link | ||
212 | * - redirector.url | ||
213 | * | ||
214 | * @return bool true if the update is successful, false otherwise. | ||
215 | */ | ||
216 | public function updateMethodEscapeUnescapedConfig() | ||
217 | { | ||
218 | try { | ||
219 | $this->conf->set('general.title', escape($this->conf->get('general.title'))); | ||
220 | $this->conf->set('general.header_link', escape($this->conf->get('general.header_link'))); | ||
221 | $this->conf->write($this->isLoggedIn); | ||
222 | } catch (Exception $e) { | ||
223 | error_log($e->getMessage()); | ||
224 | return false; | ||
225 | } | ||
226 | return true; | ||
227 | } | ||
228 | |||
229 | /** | ||
230 | * Update the database to use the new ID system, which replaces linkdate primary keys. | ||
231 | * Also, creation and update dates are now DateTime objects (done by LinkDB). | ||
232 | * | ||
233 | * Since this update is very sensitve (changing the whole database), the datastore will be | ||
234 | * automatically backed up into the file datastore.<datetime>.php. | ||
235 | * | ||
236 | * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash), | ||
237 | * which will be saved by this method. | ||
238 | * | ||
239 | * @return bool true if the update is successful, false otherwise. | ||
240 | */ | ||
241 | public function updateMethodDatastoreIds() | ||
242 | { | ||
243 | // up to date database | ||
244 | if (isset($this->linkDB[0])) { | ||
245 | return true; | ||
246 | } | ||
247 | |||
248 | $save = $this->conf->get('resource.data_dir') . '/datastore.' . date('YmdHis') . '.php'; | ||
249 | copy($this->conf->get('resource.datastore'), $save); | ||
250 | |||
251 | $links = array(); | ||
252 | foreach ($this->linkDB as $offset => $value) { | ||
253 | $links[] = $value; | ||
254 | unset($this->linkDB[$offset]); | ||
255 | } | ||
256 | $links = array_reverse($links); | ||
257 | $cpt = 0; | ||
258 | foreach ($links as $l) { | ||
259 | unset($l['linkdate']); | ||
260 | $l['id'] = $cpt; | ||
261 | $this->linkDB[$cpt++] = $l; | ||
262 | } | ||
263 | |||
264 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
265 | $this->linkDB->reorder(); | ||
266 | |||
267 | return true; | ||
268 | } | ||
269 | |||
270 | /** | ||
271 | <<<<<<< HEAD | ||
272 | ======= | ||
273 | * Rename tags starting with a '-' to work with tag exclusion search. | ||
274 | */ | ||
275 | public function updateMethodRenameDashTags() | ||
276 | { | ||
277 | $linklist = $this->linkDB->filterSearch(); | ||
278 | foreach ($linklist as $key => $link) { | ||
279 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); | ||
280 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | ||
281 | $this->linkDB[$key] = $link; | ||
282 | } | ||
283 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
284 | return true; | ||
285 | } | ||
286 | |||
287 | /** | ||
288 | * Initialize API settings: | ||
289 | * - api.enabled: true | ||
290 | * - api.secret: generated secret | ||
291 | */ | ||
292 | public function updateMethodApiSettings() | ||
293 | { | ||
294 | if ($this->conf->exists('api.secret')) { | ||
295 | return true; | ||
296 | } | ||
297 | |||
298 | $this->conf->set('api.enabled', true); | ||
299 | $this->conf->set( | ||
300 | 'api.secret', | ||
301 | generate_api_secret( | ||
302 | $this->conf->get('credentials.login'), | ||
303 | $this->conf->get('credentials.salt') | ||
304 | ) | ||
305 | ); | ||
306 | $this->conf->write($this->isLoggedIn); | ||
307 | return true; | ||
308 | } | ||
309 | |||
310 | /** | ||
311 | * New setting: theme name. If the default theme is used, nothing to do. | ||
312 | * | ||
313 | * If the user uses a custom theme, raintpl_tpl dir is updated to the parent directory, | ||
314 | * and the current theme is set as default in the theme setting. | ||
315 | * | ||
316 | * @return bool true if the update is successful, false otherwise. | ||
317 | */ | ||
318 | public function updateMethodDefaultTheme() | ||
319 | { | ||
320 | // raintpl_tpl isn't the root template directory anymore. | ||
321 | // We run the update only if this folder still contains the template files. | ||
322 | $tplDir = $this->conf->get('resource.raintpl_tpl'); | ||
323 | $tplFile = $tplDir . '/linklist.html'; | ||
324 | if (!file_exists($tplFile)) { | ||
325 | return true; | ||
326 | } | ||
327 | |||
328 | $parent = dirname($tplDir); | ||
329 | $this->conf->set('resource.raintpl_tpl', $parent); | ||
330 | $this->conf->set('resource.theme', trim(str_replace($parent, '', $tplDir), '/')); | ||
331 | $this->conf->write($this->isLoggedIn); | ||
332 | |||
333 | // Dependency injection gore | ||
334 | RainTPL::$tpl_dir = $tplDir; | ||
335 | |||
336 | return true; | ||
337 | } | ||
338 | |||
339 | /** | ||
340 | * Move the file to inc/user.css to data/user.css. | ||
341 | * | ||
342 | * Note: Due to hardcoded paths, it's not unit testable. But one line of code should be fine. | ||
343 | * | ||
344 | * @return bool true if the update is successful, false otherwise. | ||
345 | */ | ||
346 | public function updateMethodMoveUserCss() | ||
347 | { | ||
348 | if (!is_file('inc/user.css')) { | ||
349 | return true; | ||
350 | } | ||
351 | |||
352 | return rename('inc/user.css', 'data/user.css'); | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * * `markdown_escape` is a new setting, set to true as default. | ||
357 | * | ||
358 | * If the markdown plugin was already enabled, escaping is disabled to avoid | ||
359 | * breaking existing entries. | ||
360 | */ | ||
361 | public function updateMethodEscapeMarkdown() | ||
362 | { | ||
363 | if ($this->conf->exists('security.markdown_escape')) { | ||
364 | return true; | ||
365 | } | ||
366 | |||
367 | if (in_array('markdown', $this->conf->get('general.enabled_plugins'))) { | ||
368 | $this->conf->set('security.markdown_escape', false); | ||
369 | } else { | ||
370 | $this->conf->set('security.markdown_escape', true); | ||
371 | } | ||
372 | $this->conf->write($this->isLoggedIn); | ||
373 | |||
374 | return true; | ||
375 | } | ||
376 | |||
377 | /** | ||
378 | * Add 'http://' to Piwik URL the setting is set. | ||
379 | * | ||
380 | * @return bool true if the update is successful, false otherwise. | ||
381 | */ | ||
382 | public function updateMethodPiwikUrl() | ||
383 | { | ||
384 | if (!$this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) { | ||
385 | return true; | ||
386 | } | ||
387 | |||
388 | $this->conf->set('plugins.PIWIK_URL', 'http://' . $this->conf->get('plugins.PIWIK_URL')); | ||
389 | $this->conf->write($this->isLoggedIn); | ||
390 | |||
391 | return true; | ||
392 | } | ||
393 | |||
394 | /** | ||
395 | * Use ATOM feed as default. | ||
396 | */ | ||
397 | public function updateMethodAtomDefault() | ||
398 | { | ||
399 | if (!$this->conf->exists('feed.show_atom') || $this->conf->get('feed.show_atom') === true) { | ||
400 | return true; | ||
401 | } | ||
402 | |||
403 | $this->conf->set('feed.show_atom', true); | ||
404 | $this->conf->write($this->isLoggedIn); | ||
405 | |||
406 | return true; | ||
407 | } | ||
408 | |||
409 | /** | ||
410 | * Update updates.check_updates_branch setting. | ||
411 | * | ||
412 | * If the current major version digit matches the latest branch | ||
413 | * major version digit, we set the branch to `latest`, | ||
414 | * otherwise we'll check updates on the `stable` branch. | ||
415 | * | ||
416 | * No update required for the dev version. | ||
417 | * | ||
418 | * Note: due to hardcoded URL and lack of dependency injection, this is not unit testable. | ||
419 | * | ||
420 | * FIXME! This needs to be removed when we switch to first digit major version | ||
421 | * instead of the second one since the versionning process will change. | ||
422 | */ | ||
423 | public function updateMethodCheckUpdateRemoteBranch() | ||
424 | { | ||
425 | if (SHAARLI_VERSION === 'dev' || $this->conf->get('updates.check_updates_branch') === 'latest') { | ||
426 | return true; | ||
427 | } | ||
428 | |||
429 | // Get latest branch major version digit | ||
430 | $latestVersion = ApplicationUtils::getLatestGitVersionCode( | ||
431 | 'https://raw.githubusercontent.com/shaarli/Shaarli/latest/shaarli_version.php', | ||
432 | 5 | ||
433 | ); | ||
434 | if (preg_match('/(\d+)\.\d+$/', $latestVersion, $matches) === false) { | ||
435 | return false; | ||
436 | } | ||
437 | $latestMajor = $matches[1]; | ||
438 | |||
439 | // Get current major version digit | ||
440 | preg_match('/(\d+)\.\d+$/', SHAARLI_VERSION, $matches); | ||
441 | $currentMajor = $matches[1]; | ||
442 | |||
443 | if ($currentMajor === $latestMajor) { | ||
444 | $branch = 'latest'; | ||
445 | } else { | ||
446 | $branch = 'stable'; | ||
447 | } | ||
448 | $this->conf->set('updates.check_updates_branch', $branch); | ||
449 | $this->conf->write($this->isLoggedIn); | ||
450 | return true; | ||
451 | } | ||
452 | |||
453 | /** | ||
454 | * Reset history store file due to date format change. | ||
455 | */ | ||
456 | public function updateMethodResetHistoryFile() | ||
457 | { | ||
458 | if (is_file($this->conf->get('resource.history'))) { | ||
459 | unlink($this->conf->get('resource.history')); | ||
460 | } | ||
461 | return true; | ||
462 | } | ||
463 | |||
464 | /** | ||
465 | * Save the datastore -> the link order is now applied when links are saved. | ||
466 | */ | ||
467 | public function updateMethodReorderDatastore() | ||
468 | { | ||
469 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
470 | return true; | ||
471 | } | ||
472 | |||
473 | /** | ||
474 | * Change privateonly session key to visibility. | ||
475 | */ | ||
476 | public function updateMethodVisibilitySession() | ||
477 | { | ||
478 | if (isset($_SESSION['privateonly'])) { | ||
479 | unset($_SESSION['privateonly']); | ||
480 | $_SESSION['visibility'] = 'private'; | ||
481 | } | ||
482 | return true; | ||
483 | } | ||
484 | |||
485 | /** | ||
486 | * Add download size and timeout to the configuration file | ||
487 | * | ||
488 | * @return bool true if the update is successful, false otherwise. | ||
489 | */ | ||
490 | public function updateMethodDownloadSizeAndTimeoutConf() | ||
491 | { | ||
492 | if ($this->conf->exists('general.download_max_size') | ||
493 | && $this->conf->exists('general.download_timeout') | ||
494 | ) { | ||
495 | return true; | ||
496 | } | ||
497 | |||
498 | if (!$this->conf->exists('general.download_max_size')) { | ||
499 | $this->conf->set('general.download_max_size', 1024 * 1024 * 4); | ||
500 | } | ||
501 | |||
502 | if (!$this->conf->exists('general.download_timeout')) { | ||
503 | $this->conf->set('general.download_timeout', 30); | ||
504 | } | ||
505 | |||
506 | $this->conf->write($this->isLoggedIn); | ||
507 | return true; | ||
508 | } | ||
509 | |||
510 | /** | ||
511 | * * Move thumbnails management to WebThumbnailer, coming with new settings. | ||
512 | */ | ||
513 | public function updateMethodWebThumbnailer() | ||
514 | { | ||
515 | if ($this->conf->exists('thumbnails.mode')) { | ||
516 | return true; | ||
517 | } | ||
518 | |||
519 | $thumbnailsEnabled = extension_loaded('gd') && $this->conf->get('thumbnail.enable_thumbnails', true); | ||
520 | $this->conf->set('thumbnails.mode', $thumbnailsEnabled ? Thumbnailer::MODE_ALL : Thumbnailer::MODE_NONE); | ||
521 | $this->conf->set('thumbnails.width', 125); | ||
522 | $this->conf->set('thumbnails.height', 90); | ||
523 | $this->conf->remove('thumbnail'); | ||
524 | $this->conf->write(true); | ||
525 | |||
526 | if ($thumbnailsEnabled) { | ||
527 | $this->session['warnings'][] = t( | ||
528 | 'You have enabled or changed thumbnails mode. <a href="?do=thumbs_update">Please synchronize them</a>.' | ||
529 | ); | ||
530 | } | ||
531 | |||
532 | return true; | ||
533 | } | ||
534 | |||
535 | /** | ||
536 | * Set sticky = false on all links | ||
537 | * | ||
538 | * @return bool true if the update is successful, false otherwise. | ||
539 | */ | ||
540 | public function updateMethodSetSticky() | ||
541 | { | ||
542 | foreach ($this->linkDB as $key => $link) { | ||
543 | if (isset($link['sticky'])) { | ||
544 | return true; | ||
545 | } | ||
546 | $link['sticky'] = false; | ||
547 | $this->linkDB[$key] = $link; | ||
548 | } | ||
549 | |||
550 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
551 | |||
552 | return true; | ||
553 | } | ||
554 | |||
555 | /** | ||
556 | * Remove redirector settings. | ||
557 | */ | ||
558 | public function updateMethodRemoveRedirector() | ||
559 | { | ||
560 | $this->conf->remove('redirector'); | ||
561 | $this->conf->write(true); | ||
562 | return true; | ||
563 | } | ||
564 | } | ||