Fetcher.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\App\AppStore\Fetcher;
  7. use GuzzleHttp\Exception\ConnectException;
  8. use OC\Files\AppData\Factory;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\Files\IAppData;
  12. use OCP\Files\NotFoundException;
  13. use OCP\Http\Client\IClientService;
  14. use OCP\IConfig;
  15. use OCP\Server;
  16. use OCP\ServerVersion;
  17. use OCP\Support\Subscription\IRegistry;
  18. use Psr\Log\LoggerInterface;
  19. abstract class Fetcher {
  20. public const INVALIDATE_AFTER_SECONDS = 3600;
  21. public const INVALIDATE_AFTER_SECONDS_UNSTABLE = 900;
  22. public const RETRY_AFTER_FAILURE_SECONDS = 300;
  23. public const APP_STORE_URL = 'https://apps.nextcloud.com/api/v1';
  24. /** @var IAppData */
  25. protected $appData;
  26. /** @var string */
  27. protected $fileName;
  28. /** @var string */
  29. protected $endpointName;
  30. /** @var ?string */
  31. protected $version = null;
  32. /** @var ?string */
  33. protected $channel = null;
  34. public function __construct(
  35. Factory $appDataFactory,
  36. protected IClientService $clientService,
  37. protected ITimeFactory $timeFactory,
  38. protected IConfig $config,
  39. protected LoggerInterface $logger,
  40. protected IRegistry $registry,
  41. ) {
  42. $this->appData = $appDataFactory->get('appstore');
  43. }
  44. /**
  45. * Fetches the response from the server
  46. *
  47. * @param string $ETag
  48. * @param string $content
  49. *
  50. * @return array
  51. */
  52. protected function fetch($ETag, $content) {
  53. $appstoreenabled = $this->config->getSystemValueBool('appstoreenabled', true);
  54. if ((int)$this->config->getAppValue('settings', 'appstore-fetcher-lastFailure', '0') > time() - self::RETRY_AFTER_FAILURE_SECONDS) {
  55. return [];
  56. }
  57. if (!$appstoreenabled) {
  58. return [];
  59. }
  60. $options = [
  61. 'timeout' => 60,
  62. ];
  63. if ($ETag !== '') {
  64. $options['headers'] = [
  65. 'If-None-Match' => $ETag,
  66. ];
  67. }
  68. if ($this->config->getSystemValueString('appstoreurl', self::APP_STORE_URL) === self::APP_STORE_URL) {
  69. // If we have a valid subscription key, send it to the appstore
  70. $subscriptionKey = $this->config->getAppValue('support', 'subscription_key');
  71. if ($this->registry->delegateHasValidSubscription() && $subscriptionKey) {
  72. $options['headers'] ??= [];
  73. $options['headers']['X-NC-Subscription-Key'] = $subscriptionKey;
  74. }
  75. }
  76. $client = $this->clientService->newClient();
  77. try {
  78. $response = $client->get($this->getEndpoint(), $options);
  79. } catch (ConnectException $e) {
  80. $this->config->setAppValue('settings', 'appstore-fetcher-lastFailure', (string)time());
  81. $this->logger->error('Failed to connect to the app store', ['exception' => $e]);
  82. return [];
  83. }
  84. $responseJson = [];
  85. if ($response->getStatusCode() === Http::STATUS_NOT_MODIFIED) {
  86. $responseJson['data'] = json_decode($content, true);
  87. } else {
  88. $responseJson['data'] = json_decode($response->getBody(), true);
  89. $ETag = $response->getHeader('ETag');
  90. }
  91. $this->config->deleteAppValue('settings', 'appstore-fetcher-lastFailure');
  92. $responseJson['timestamp'] = $this->timeFactory->getTime();
  93. $responseJson['ncversion'] = $this->getVersion();
  94. if ($ETag !== '') {
  95. $responseJson['ETag'] = $ETag;
  96. }
  97. return $responseJson;
  98. }
  99. /**
  100. * Returns the array with the entries on the appstore server
  101. *
  102. * @param bool [$allowUnstable] Allow unstable releases
  103. * @return array
  104. */
  105. public function get($allowUnstable = false) {
  106. $appstoreenabled = $this->config->getSystemValueBool('appstoreenabled', true);
  107. $internetavailable = $this->config->getSystemValueBool('has_internet_connection', true);
  108. $isDefaultAppStore = $this->config->getSystemValueString('appstoreurl', self::APP_STORE_URL) === self::APP_STORE_URL;
  109. if (!$appstoreenabled || (!$internetavailable && $isDefaultAppStore)) {
  110. $this->logger->info('AppStore is disabled or this instance has no Internet connection to access the default app store', ['app' => 'appstoreFetcher']);
  111. return [];
  112. }
  113. $rootFolder = $this->appData->getFolder('/');
  114. $ETag = '';
  115. $content = '';
  116. try {
  117. // File does already exists
  118. $file = $rootFolder->getFile($this->fileName);
  119. $jsonBlob = json_decode($file->getContent(), true);
  120. if (is_array($jsonBlob)) {
  121. // No caching when the version has been updated
  122. if (isset($jsonBlob['ncversion']) && $jsonBlob['ncversion'] === $this->getVersion()) {
  123. // If the timestamp is older than 3600 seconds request the files new
  124. $invalidateAfterSeconds = self::INVALIDATE_AFTER_SECONDS;
  125. if ($allowUnstable) {
  126. $invalidateAfterSeconds = self::INVALIDATE_AFTER_SECONDS_UNSTABLE;
  127. }
  128. if ((int)$jsonBlob['timestamp'] > ($this->timeFactory->getTime() - $invalidateAfterSeconds)) {
  129. return $jsonBlob['data'];
  130. }
  131. if (isset($jsonBlob['ETag'])) {
  132. $ETag = $jsonBlob['ETag'];
  133. $content = json_encode($jsonBlob['data']);
  134. }
  135. }
  136. }
  137. } catch (NotFoundException $e) {
  138. // File does not already exists
  139. $file = $rootFolder->newFile($this->fileName);
  140. }
  141. // Refresh the file content
  142. try {
  143. $responseJson = $this->fetch($ETag, $content, $allowUnstable);
  144. if (empty($responseJson) || empty($responseJson['data'])) {
  145. return [];
  146. }
  147. $file->putContent(json_encode($responseJson));
  148. return json_decode($file->getContent(), true)['data'];
  149. } catch (ConnectException $e) {
  150. $this->logger->warning('Could not connect to appstore: ' . $e->getMessage(), ['app' => 'appstoreFetcher']);
  151. return [];
  152. } catch (\Exception $e) {
  153. $this->logger->warning($e->getMessage(), [
  154. 'exception' => $e,
  155. 'app' => 'appstoreFetcher',
  156. ]);
  157. return [];
  158. }
  159. }
  160. /**
  161. * Get the currently Nextcloud version
  162. * @return string
  163. */
  164. protected function getVersion() {
  165. if ($this->version === null) {
  166. $this->version = $this->config->getSystemValueString('version', '0.0.0');
  167. }
  168. return $this->version;
  169. }
  170. /**
  171. * Set the current Nextcloud version
  172. * @param string $version
  173. */
  174. public function setVersion(string $version) {
  175. $this->version = $version;
  176. }
  177. /**
  178. * Get the currently Nextcloud update channel
  179. * @return string
  180. */
  181. protected function getChannel() {
  182. if ($this->channel === null) {
  183. $this->channel = Server::get(ServerVersion::class)->getChannel();
  184. }
  185. return $this->channel;
  186. }
  187. /**
  188. * Set the current Nextcloud update channel
  189. * @param string $channel
  190. */
  191. public function setChannel(string $channel) {
  192. $this->channel = $channel;
  193. }
  194. protected function getEndpoint(): string {
  195. return $this->config->getSystemValueString('appstoreurl', 'https://apps.nextcloud.com/api/v1') . '/' . $this->endpointName;
  196. }
  197. }