ChangesCheck.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Updater;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\Http\Client\IClientService;
  10. use OCP\Http\Client\IResponse;
  11. use Psr\Log\LoggerInterface;
  12. class ChangesCheck {
  13. /** @var IClientService */
  14. protected $clientService;
  15. /** @var ChangesMapper */
  16. private $mapper;
  17. private LoggerInterface $logger;
  18. public const RESPONSE_NO_CONTENT = 0;
  19. public const RESPONSE_USE_CACHE = 1;
  20. public const RESPONSE_HAS_CONTENT = 2;
  21. public function __construct(IClientService $clientService, ChangesMapper $mapper, LoggerInterface $logger) {
  22. $this->clientService = $clientService;
  23. $this->mapper = $mapper;
  24. $this->logger = $logger;
  25. }
  26. /**
  27. * @throws DoesNotExistException
  28. * @return array{changelogURL: string, whatsNew: array<string, array{admin: list<string>, regular: list<string>}>}
  29. */
  30. public function getChangesForVersion(string $version): array {
  31. $version = $this->normalizeVersion($version);
  32. $changesInfo = $this->mapper->getChanges($version);
  33. $changesData = json_decode($changesInfo->getData(), true);
  34. if (empty($changesData)) {
  35. throw new DoesNotExistException('Unable to decode changes info');
  36. }
  37. return $changesData;
  38. }
  39. /**
  40. * @throws \Exception
  41. */
  42. public function check(string $uri, string $version): array {
  43. try {
  44. $version = $this->normalizeVersion($version);
  45. $changesInfo = $this->mapper->getChanges($version);
  46. if ($changesInfo->getLastCheck() + 1800 > time()) {
  47. return json_decode($changesInfo->getData(), true);
  48. }
  49. } catch (DoesNotExistException $e) {
  50. $changesInfo = new Changes();
  51. }
  52. $response = $this->queryChangesServer($uri, $changesInfo);
  53. switch ($this->evaluateResponse($response)) {
  54. case self::RESPONSE_NO_CONTENT:
  55. return [];
  56. case self::RESPONSE_USE_CACHE:
  57. return json_decode($changesInfo->getData(), true);
  58. case self::RESPONSE_HAS_CONTENT:
  59. default:
  60. $data = $this->extractData($response->getBody());
  61. $changesInfo->setData(json_encode($data));
  62. $changesInfo->setEtag($response->getHeader('Etag'));
  63. $this->cacheResult($changesInfo, $version);
  64. return $data;
  65. }
  66. }
  67. protected function evaluateResponse(IResponse $response): int {
  68. if ($response->getStatusCode() === 304) {
  69. return self::RESPONSE_USE_CACHE;
  70. } elseif ($response->getStatusCode() === 404) {
  71. return self::RESPONSE_NO_CONTENT;
  72. } elseif ($response->getStatusCode() === 200) {
  73. return self::RESPONSE_HAS_CONTENT;
  74. }
  75. $this->logger->debug('Unexpected return code {code} from changelog server', [
  76. 'app' => 'core',
  77. 'code' => $response->getStatusCode(),
  78. ]);
  79. return self::RESPONSE_NO_CONTENT;
  80. }
  81. protected function cacheResult(Changes $entry, string $version) {
  82. if ($entry->getVersion() === $version) {
  83. $this->mapper->update($entry);
  84. } else {
  85. $entry->setVersion($version);
  86. $this->mapper->insert($entry);
  87. }
  88. }
  89. /**
  90. * @throws \Exception
  91. */
  92. protected function queryChangesServer(string $uri, Changes $entry): IResponse {
  93. $headers = [];
  94. if ($entry->getEtag() !== '') {
  95. $headers['If-None-Match'] = [$entry->getEtag()];
  96. }
  97. $entry->setLastCheck(time());
  98. $client = $this->clientService->newClient();
  99. return $client->get($uri, [
  100. 'headers' => $headers,
  101. ]);
  102. }
  103. protected function extractData($body):array {
  104. $data = [];
  105. if ($body) {
  106. if (\LIBXML_VERSION < 20900) {
  107. $loadEntities = libxml_disable_entity_loader(true);
  108. $xml = @simplexml_load_string($body);
  109. libxml_disable_entity_loader($loadEntities);
  110. } else {
  111. $xml = @simplexml_load_string($body);
  112. }
  113. if ($xml !== false) {
  114. $data['changelogURL'] = (string)$xml->changelog['href'];
  115. $data['whatsNew'] = [];
  116. foreach ($xml->whatsNew as $infoSet) {
  117. $data['whatsNew'][(string)$infoSet['lang']] = [
  118. 'regular' => (array)$infoSet->regular->item,
  119. 'admin' => (array)$infoSet->admin->item,
  120. ];
  121. }
  122. } else {
  123. libxml_clear_errors();
  124. }
  125. }
  126. return $data;
  127. }
  128. /**
  129. * returns a x.y.z form of the provided version. Extra numbers will be
  130. * omitted, missing ones added as zeros.
  131. */
  132. public function normalizeVersion(string $version): string {
  133. $versionNumbers = array_slice(explode('.', $version), 0, 3);
  134. $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input
  135. while (count($versionNumbers) < 3) {
  136. // changelog server expects x.y.z, pad 0 if it is too short
  137. $versionNumbers[] = 0;
  138. }
  139. return implode('.', $versionNumbers);
  140. }
  141. }