diff options
author | VirtualTam <virtualtam@flibidi.net> | 2018-12-03 23:49:20 +0100 |
---|---|---|
committer | VirtualTam <virtualtam@flibidi.net> | 2019-01-12 23:11:19 +0100 |
commit | bcf056c9d92e5240e645c76a4cdc8ae159693f9a (patch) | |
tree | 3991d2e0703276474a3fa5b0f3cccc63b2e74f19 /application/updater/Updater.php | |
parent | 92c6439dbc41d8a2873dfebbc44e30d75c0c473d (diff) | |
download | Shaarli-bcf056c9d92e5240e645c76a4cdc8ae159693f9a.tar.gz Shaarli-bcf056c9d92e5240e645c76a4cdc8ae159693f9a.tar.zst Shaarli-bcf056c9d92e5240e645c76a4cdc8ae159693f9a.zip |
namespacing: \Shaarli\Updater
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'application/updater/Updater.php')
-rw-r--r-- | application/updater/Updater.php | 553 |
1 files changed, 553 insertions, 0 deletions
diff --git a/application/updater/Updater.php b/application/updater/Updater.php new file mode 100644 index 00000000..55251a30 --- /dev/null +++ b/application/updater/Updater.php | |||
@@ -0,0 +1,553 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Updater; | ||
4 | |||
5 | use ApplicationUtils; | ||
6 | use Exception; | ||
7 | use RainTPL; | ||
8 | use ReflectionClass; | ||
9 | use ReflectionException; | ||
10 | use ReflectionMethod; | ||
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->set('redirector.url', escape($this->conf->get('redirector.url'))); | ||
222 | $this->conf->write($this->isLoggedIn); | ||
223 | } catch (Exception $e) { | ||
224 | error_log($e->getMessage()); | ||
225 | return false; | ||
226 | } | ||
227 | return true; | ||
228 | } | ||
229 | |||
230 | /** | ||
231 | * Update the database to use the new ID system, which replaces linkdate primary keys. | ||
232 | * Also, creation and update dates are now DateTime objects (done by LinkDB). | ||
233 | * | ||
234 | * Since this update is very sensitve (changing the whole database), the datastore will be | ||
235 | * automatically backed up into the file datastore.<datetime>.php. | ||
236 | * | ||
237 | * LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash), | ||
238 | * which will be saved by this method. | ||
239 | * | ||
240 | * @return bool true if the update is successful, false otherwise. | ||
241 | */ | ||
242 | public function updateMethodDatastoreIds() | ||
243 | { | ||
244 | // up to date database | ||
245 | if (isset($this->linkDB[0])) { | ||
246 | return true; | ||
247 | } | ||
248 | |||
249 | $save = $this->conf->get('resource.data_dir') . '/datastore.' . date('YmdHis') . '.php'; | ||
250 | copy($this->conf->get('resource.datastore'), $save); | ||
251 | |||
252 | $links = array(); | ||
253 | foreach ($this->linkDB as $offset => $value) { | ||
254 | $links[] = $value; | ||
255 | unset($this->linkDB[$offset]); | ||
256 | } | ||
257 | $links = array_reverse($links); | ||
258 | $cpt = 0; | ||
259 | foreach ($links as $l) { | ||
260 | unset($l['linkdate']); | ||
261 | $l['id'] = $cpt; | ||
262 | $this->linkDB[$cpt++] = $l; | ||
263 | } | ||
264 | |||
265 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
266 | $this->linkDB->reorder(); | ||
267 | |||
268 | return true; | ||
269 | } | ||
270 | |||
271 | /** | ||
272 | * Rename tags starting with a '-' to work with tag exclusion search. | ||
273 | */ | ||
274 | public function updateMethodRenameDashTags() | ||
275 | { | ||
276 | $linklist = $this->linkDB->filterSearch(); | ||
277 | foreach ($linklist as $key => $link) { | ||
278 | $link['tags'] = preg_replace('/(^| )\-/', '$1', $link['tags']); | ||
279 | $link['tags'] = implode(' ', array_unique(LinkFilter::tagsStrToArray($link['tags'], true))); | ||
280 | $this->linkDB[$key] = $link; | ||
281 | } | ||
282 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
283 | return true; | ||
284 | } | ||
285 | |||
286 | /** | ||
287 | * Initialize API settings: | ||
288 | * - api.enabled: true | ||
289 | * - api.secret: generated secret | ||
290 | */ | ||
291 | public function updateMethodApiSettings() | ||
292 | { | ||
293 | if ($this->conf->exists('api.secret')) { | ||
294 | return true; | ||
295 | } | ||
296 | |||
297 | $this->conf->set('api.enabled', true); | ||
298 | $this->conf->set( | ||
299 | 'api.secret', | ||
300 | generate_api_secret( | ||
301 | $this->conf->get('credentials.login'), | ||
302 | $this->conf->get('credentials.salt') | ||
303 | ) | ||
304 | ); | ||
305 | $this->conf->write($this->isLoggedIn); | ||
306 | return true; | ||
307 | } | ||
308 | |||
309 | /** | ||
310 | * New setting: theme name. If the default theme is used, nothing to do. | ||
311 | * | ||
312 | * If the user uses a custom theme, raintpl_tpl dir is updated to the parent directory, | ||
313 | * and the current theme is set as default in the theme setting. | ||
314 | * | ||
315 | * @return bool true if the update is successful, false otherwise. | ||
316 | */ | ||
317 | public function updateMethodDefaultTheme() | ||
318 | { | ||
319 | // raintpl_tpl isn't the root template directory anymore. | ||
320 | // We run the update only if this folder still contains the template files. | ||
321 | $tplDir = $this->conf->get('resource.raintpl_tpl'); | ||
322 | $tplFile = $tplDir . '/linklist.html'; | ||
323 | if (!file_exists($tplFile)) { | ||
324 | return true; | ||
325 | } | ||
326 | |||
327 | $parent = dirname($tplDir); | ||
328 | $this->conf->set('resource.raintpl_tpl', $parent); | ||
329 | $this->conf->set('resource.theme', trim(str_replace($parent, '', $tplDir), '/')); | ||
330 | $this->conf->write($this->isLoggedIn); | ||
331 | |||
332 | // Dependency injection gore | ||
333 | RainTPL::$tpl_dir = $tplDir; | ||
334 | |||
335 | return true; | ||
336 | } | ||
337 | |||
338 | /** | ||
339 | * Move the file to inc/user.css to data/user.css. | ||
340 | * | ||
341 | * Note: Due to hardcoded paths, it's not unit testable. But one line of code should be fine. | ||
342 | * | ||
343 | * @return bool true if the update is successful, false otherwise. | ||
344 | */ | ||
345 | public function updateMethodMoveUserCss() | ||
346 | { | ||
347 | if (!is_file('inc/user.css')) { | ||
348 | return true; | ||
349 | } | ||
350 | |||
351 | return rename('inc/user.css', 'data/user.css'); | ||
352 | } | ||
353 | |||
354 | /** | ||
355 | * * `markdown_escape` is a new setting, set to true as default. | ||
356 | * | ||
357 | * If the markdown plugin was already enabled, escaping is disabled to avoid | ||
358 | * breaking existing entries. | ||
359 | */ | ||
360 | public function updateMethodEscapeMarkdown() | ||
361 | { | ||
362 | if ($this->conf->exists('security.markdown_escape')) { | ||
363 | return true; | ||
364 | } | ||
365 | |||
366 | if (in_array('markdown', $this->conf->get('general.enabled_plugins'))) { | ||
367 | $this->conf->set('security.markdown_escape', false); | ||
368 | } else { | ||
369 | $this->conf->set('security.markdown_escape', true); | ||
370 | } | ||
371 | $this->conf->write($this->isLoggedIn); | ||
372 | |||
373 | return true; | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * Add 'http://' to Piwik URL the setting is set. | ||
378 | * | ||
379 | * @return bool true if the update is successful, false otherwise. | ||
380 | */ | ||
381 | public function updateMethodPiwikUrl() | ||
382 | { | ||
383 | if (!$this->conf->exists('plugins.PIWIK_URL') || startsWith($this->conf->get('plugins.PIWIK_URL'), 'http')) { | ||
384 | return true; | ||
385 | } | ||
386 | |||
387 | $this->conf->set('plugins.PIWIK_URL', 'http://' . $this->conf->get('plugins.PIWIK_URL')); | ||
388 | $this->conf->write($this->isLoggedIn); | ||
389 | |||
390 | return true; | ||
391 | } | ||
392 | |||
393 | /** | ||
394 | * Use ATOM feed as default. | ||
395 | */ | ||
396 | public function updateMethodAtomDefault() | ||
397 | { | ||
398 | if (!$this->conf->exists('feed.show_atom') || $this->conf->get('feed.show_atom') === true) { | ||
399 | return true; | ||
400 | } | ||
401 | |||
402 | $this->conf->set('feed.show_atom', true); | ||
403 | $this->conf->write($this->isLoggedIn); | ||
404 | |||
405 | return true; | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * Update updates.check_updates_branch setting. | ||
410 | * | ||
411 | * If the current major version digit matches the latest branch | ||
412 | * major version digit, we set the branch to `latest`, | ||
413 | * otherwise we'll check updates on the `stable` branch. | ||
414 | * | ||
415 | * No update required for the dev version. | ||
416 | * | ||
417 | * Note: due to hardcoded URL and lack of dependency injection, this is not unit testable. | ||
418 | * | ||
419 | * FIXME! This needs to be removed when we switch to first digit major version | ||
420 | * instead of the second one since the versionning process will change. | ||
421 | */ | ||
422 | public function updateMethodCheckUpdateRemoteBranch() | ||
423 | { | ||
424 | if (SHAARLI_VERSION === 'dev' || $this->conf->get('updates.check_updates_branch') === 'latest') { | ||
425 | return true; | ||
426 | } | ||
427 | |||
428 | // Get latest branch major version digit | ||
429 | $latestVersion = ApplicationUtils::getLatestGitVersionCode( | ||
430 | 'https://raw.githubusercontent.com/shaarli/Shaarli/latest/shaarli_version.php', | ||
431 | 5 | ||
432 | ); | ||
433 | if (preg_match('/(\d+)\.\d+$/', $latestVersion, $matches) === false) { | ||
434 | return false; | ||
435 | } | ||
436 | $latestMajor = $matches[1]; | ||
437 | |||
438 | // Get current major version digit | ||
439 | preg_match('/(\d+)\.\d+$/', SHAARLI_VERSION, $matches); | ||
440 | $currentMajor = $matches[1]; | ||
441 | |||
442 | if ($currentMajor === $latestMajor) { | ||
443 | $branch = 'latest'; | ||
444 | } else { | ||
445 | $branch = 'stable'; | ||
446 | } | ||
447 | $this->conf->set('updates.check_updates_branch', $branch); | ||
448 | $this->conf->write($this->isLoggedIn); | ||
449 | return true; | ||
450 | } | ||
451 | |||
452 | /** | ||
453 | * Reset history store file due to date format change. | ||
454 | */ | ||
455 | public function updateMethodResetHistoryFile() | ||
456 | { | ||
457 | if (is_file($this->conf->get('resource.history'))) { | ||
458 | unlink($this->conf->get('resource.history')); | ||
459 | } | ||
460 | return true; | ||
461 | } | ||
462 | |||
463 | /** | ||
464 | * Save the datastore -> the link order is now applied when links are saved. | ||
465 | */ | ||
466 | public function updateMethodReorderDatastore() | ||
467 | { | ||
468 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
469 | return true; | ||
470 | } | ||
471 | |||
472 | /** | ||
473 | * Change privateonly session key to visibility. | ||
474 | */ | ||
475 | public function updateMethodVisibilitySession() | ||
476 | { | ||
477 | if (isset($_SESSION['privateonly'])) { | ||
478 | unset($_SESSION['privateonly']); | ||
479 | $_SESSION['visibility'] = 'private'; | ||
480 | } | ||
481 | return true; | ||
482 | } | ||
483 | |||
484 | /** | ||
485 | * Add download size and timeout to the configuration file | ||
486 | * | ||
487 | * @return bool true if the update is successful, false otherwise. | ||
488 | */ | ||
489 | public function updateMethodDownloadSizeAndTimeoutConf() | ||
490 | { | ||
491 | if ($this->conf->exists('general.download_max_size') | ||
492 | && $this->conf->exists('general.download_timeout') | ||
493 | ) { | ||
494 | return true; | ||
495 | } | ||
496 | |||
497 | if (!$this->conf->exists('general.download_max_size')) { | ||
498 | $this->conf->set('general.download_max_size', 1024 * 1024 * 4); | ||
499 | } | ||
500 | |||
501 | if (!$this->conf->exists('general.download_timeout')) { | ||
502 | $this->conf->set('general.download_timeout', 30); | ||
503 | } | ||
504 | |||
505 | $this->conf->write($this->isLoggedIn); | ||
506 | return true; | ||
507 | } | ||
508 | |||
509 | /** | ||
510 | * * Move thumbnails management to WebThumbnailer, coming with new settings. | ||
511 | */ | ||
512 | public function updateMethodWebThumbnailer() | ||
513 | { | ||
514 | if ($this->conf->exists('thumbnails.mode')) { | ||
515 | return true; | ||
516 | } | ||
517 | |||
518 | $thumbnailsEnabled = extension_loaded('gd') && $this->conf->get('thumbnail.enable_thumbnails', true); | ||
519 | $this->conf->set('thumbnails.mode', $thumbnailsEnabled ? Thumbnailer::MODE_ALL : Thumbnailer::MODE_NONE); | ||
520 | $this->conf->set('thumbnails.width', 125); | ||
521 | $this->conf->set('thumbnails.height', 90); | ||
522 | $this->conf->remove('thumbnail'); | ||
523 | $this->conf->write(true); | ||
524 | |||
525 | if ($thumbnailsEnabled) { | ||
526 | $this->session['warnings'][] = t( | ||
527 | 'You have enabled or changed thumbnails mode. <a href="?do=thumbs_update">Please synchronize them</a>.' | ||
528 | ); | ||
529 | } | ||
530 | |||
531 | return true; | ||
532 | } | ||
533 | |||
534 | /** | ||
535 | * Set sticky = false on all links | ||
536 | * | ||
537 | * @return bool true if the update is successful, false otherwise. | ||
538 | */ | ||
539 | public function updateMethodSetSticky() | ||
540 | { | ||
541 | foreach ($this->linkDB as $key => $link) { | ||
542 | if (isset($link['sticky'])) { | ||
543 | return true; | ||
544 | } | ||
545 | $link['sticky'] = false; | ||
546 | $this->linkDB[$key] = $link; | ||
547 | } | ||
548 | |||
549 | $this->linkDB->save($this->conf->get('resource.page_cache')); | ||
550 | |||
551 | return true; | ||
552 | } | ||
553 | } | ||