UpdateChecker.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification;
  8. use OC\Updater\ChangesCheck;
  9. use OC\Updater\VersionCheck;
  10. class UpdateChecker {
  11. /** @var VersionCheck */
  12. private $updater;
  13. /** @var ChangesCheck */
  14. private $changesCheck;
  15. /**
  16. * @param VersionCheck $updater
  17. */
  18. public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) {
  19. $this->updater = $updater;
  20. $this->changesCheck = $changesCheck;
  21. }
  22. /**
  23. * @return array
  24. */
  25. public function getUpdateState(): array {
  26. $data = $this->updater->check();
  27. $result = [];
  28. if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) {
  29. $result['updateAvailable'] = true;
  30. $result['updateVersion'] = $data['version'];
  31. $result['updateVersionString'] = $data['versionstring'];
  32. $result['updaterEnabled'] = $data['autoupdater'] === '1';
  33. $result['versionIsEol'] = $data['eol'] === '1';
  34. if (strpos($data['web'], 'https://') === 0) {
  35. $result['updateLink'] = $data['web'];
  36. }
  37. if (strpos($data['url'], 'https://') === 0) {
  38. $result['downloadLink'] = $data['url'];
  39. }
  40. if (strpos($data['changes'], 'https://') === 0) {
  41. try {
  42. $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']);
  43. } catch (\Exception $e) {
  44. // no info, not a problem
  45. }
  46. }
  47. return $result;
  48. }
  49. return [];
  50. }
  51. /**
  52. * @param array $data
  53. */
  54. public function populateJavaScriptVariables(array $data) {
  55. $data['array']['oc_updateState'] = json_encode([
  56. 'updateAvailable' => true,
  57. 'updateVersion' => $this->getUpdateState()['updateVersionString'],
  58. 'updateLink' => $this->getUpdateState()['updateLink'] ?? '',
  59. ]);
  60. }
  61. }