ChangesCheck.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  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
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Updater;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\Http\Client\IResponse;
  30. use Psr\Log\LoggerInterface;
  31. class ChangesCheck {
  32. /** @var IClientService */
  33. protected $clientService;
  34. /** @var ChangesMapper */
  35. private $mapper;
  36. private LoggerInterface $logger;
  37. public const RESPONSE_NO_CONTENT = 0;
  38. public const RESPONSE_USE_CACHE = 1;
  39. public const RESPONSE_HAS_CONTENT = 2;
  40. public function __construct(IClientService $clientService, ChangesMapper $mapper, LoggerInterface $logger) {
  41. $this->clientService = $clientService;
  42. $this->mapper = $mapper;
  43. $this->logger = $logger;
  44. }
  45. /**
  46. * @throws DoesNotExistException
  47. */
  48. public function getChangesForVersion(string $version): array {
  49. $version = $this->normalizeVersion($version);
  50. $changesInfo = $this->mapper->getChanges($version);
  51. $changesData = json_decode($changesInfo->getData(), true);
  52. if (empty($changesData)) {
  53. throw new DoesNotExistException('Unable to decode changes info');
  54. }
  55. return $changesData;
  56. }
  57. /**
  58. * @throws \Exception
  59. */
  60. public function check(string $uri, string $version): array {
  61. try {
  62. $version = $this->normalizeVersion($version);
  63. $changesInfo = $this->mapper->getChanges($version);
  64. if ($changesInfo->getLastCheck() + 1800 > time()) {
  65. return json_decode($changesInfo->getData(), true);
  66. }
  67. } catch (DoesNotExistException $e) {
  68. $changesInfo = new Changes();
  69. }
  70. $response = $this->queryChangesServer($uri, $changesInfo);
  71. switch ($this->evaluateResponse($response)) {
  72. case self::RESPONSE_NO_CONTENT:
  73. return [];
  74. case self::RESPONSE_USE_CACHE:
  75. return json_decode($changesInfo->getData(), true);
  76. case self::RESPONSE_HAS_CONTENT:
  77. default:
  78. $data = $this->extractData($response->getBody());
  79. $changesInfo->setData(json_encode($data));
  80. $changesInfo->setEtag($response->getHeader('Etag'));
  81. $this->cacheResult($changesInfo, $version);
  82. return $data;
  83. }
  84. }
  85. protected function evaluateResponse(IResponse $response): int {
  86. if ($response->getStatusCode() === 304) {
  87. return self::RESPONSE_USE_CACHE;
  88. } elseif ($response->getStatusCode() === 404) {
  89. return self::RESPONSE_NO_CONTENT;
  90. } elseif ($response->getStatusCode() === 200) {
  91. return self::RESPONSE_HAS_CONTENT;
  92. }
  93. $this->logger->debug('Unexpected return code {code} from changelog server', [
  94. 'app' => 'core',
  95. 'code' => $response->getStatusCode(),
  96. ]);
  97. return self::RESPONSE_NO_CONTENT;
  98. }
  99. protected function cacheResult(Changes $entry, string $version) {
  100. if ($entry->getVersion() === $version) {
  101. $this->mapper->update($entry);
  102. } else {
  103. $entry->setVersion($version);
  104. $this->mapper->insert($entry);
  105. }
  106. }
  107. /**
  108. * @throws \Exception
  109. */
  110. protected function queryChangesServer(string $uri, Changes $entry): IResponse {
  111. $headers = [];
  112. if ($entry->getEtag() !== '') {
  113. $headers['If-None-Match'] = [$entry->getEtag()];
  114. }
  115. $entry->setLastCheck(time());
  116. $client = $this->clientService->newClient();
  117. return $client->get($uri, [
  118. 'headers' => $headers,
  119. ]);
  120. }
  121. protected function extractData($body):array {
  122. $data = [];
  123. if ($body) {
  124. if (\LIBXML_VERSION < 20900) {
  125. $loadEntities = libxml_disable_entity_loader(true);
  126. $xml = @simplexml_load_string($body);
  127. libxml_disable_entity_loader($loadEntities);
  128. } else {
  129. $xml = @simplexml_load_string($body);
  130. }
  131. if ($xml !== false) {
  132. $data['changelogURL'] = (string)$xml->changelog['href'];
  133. $data['whatsNew'] = [];
  134. foreach ($xml->whatsNew as $infoSet) {
  135. $data['whatsNew'][(string)$infoSet['lang']] = [
  136. 'regular' => (array)$infoSet->regular->item,
  137. 'admin' => (array)$infoSet->admin->item,
  138. ];
  139. }
  140. } else {
  141. libxml_clear_errors();
  142. }
  143. }
  144. return $data;
  145. }
  146. /**
  147. * returns a x.y.z form of the provided version. Extra numbers will be
  148. * omitted, missing ones added as zeros.
  149. */
  150. public function normalizeVersion(string $version): string {
  151. $versionNumbers = array_slice(explode('.', $version), 0, 3);
  152. $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input
  153. while (count($versionNumbers) < 3) {
  154. // changelog server expects x.y.z, pad 0 if it is too short
  155. $versionNumbers[] = 0;
  156. }
  157. return implode('.', $versionNumbers);
  158. }
  159. }