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