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