1
0

UpdateChecker.php 2.1 KB

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