ChangesCheck.php 4.8 KB

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