UpdateChecker.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\UpdateNotification;
  27. use OC\Updater\ChangesCheck;
  28. use OC\Updater\VersionCheck;
  29. class UpdateChecker {
  30. /** @var VersionCheck */
  31. private $updater;
  32. /** @var ChangesCheck */
  33. private $changesCheck;
  34. /**
  35. * @param VersionCheck $updater
  36. */
  37. public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) {
  38. $this->updater = $updater;
  39. $this->changesCheck = $changesCheck;
  40. }
  41. /**
  42. * @return array
  43. */
  44. public function getUpdateState(): array {
  45. $data = $this->updater->check();
  46. $result = [];
  47. if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) {
  48. $result['updateAvailable'] = true;
  49. $result['updateVersion'] = $data['version'];
  50. $result['updateVersionString'] = $data['versionstring'];
  51. $result['updaterEnabled'] = $data['autoupdater'] === '1';
  52. $result['versionIsEol'] = $data['eol'] === '1';
  53. if (strpos($data['web'], 'https://') === 0) {
  54. $result['updateLink'] = $data['web'];
  55. }
  56. if (strpos($data['url'], 'https://') === 0) {
  57. $result['downloadLink'] = $data['url'];
  58. }
  59. if (strpos($data['changes'], 'https://') === 0) {
  60. try {
  61. $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']);
  62. } catch (\Exception $e) {
  63. // no info, not a problem
  64. }
  65. }
  66. return $result;
  67. }
  68. return [];
  69. }
  70. /**
  71. * @param array $data
  72. */
  73. public function populateJavaScriptVariables(array $data) {
  74. $data['array']['oc_updateState'] = json_encode([
  75. 'updateAvailable' => true,
  76. 'updateVersion' => $this->getUpdateState()['updateVersionString'],
  77. 'updateLink' => $this->getUpdateState()['updateLink'] ?? '',
  78. ]);
  79. }
  80. }