AppFetcher.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 OC\App\AppStore\Version\VersionParser;
  8. use OC\App\CompareVersion;
  9. use OC\Files\AppData\Factory;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\Http\Client\IClientService;
  12. use OCP\IConfig;
  13. use OCP\Support\Subscription\IRegistry;
  14. use Psr\Log\LoggerInterface;
  15. class AppFetcher extends Fetcher {
  16. /** @var bool */
  17. private $ignoreMaxVersion;
  18. public function __construct(
  19. Factory $appDataFactory,
  20. IClientService $clientService,
  21. ITimeFactory $timeFactory,
  22. IConfig $config,
  23. private CompareVersion $compareVersion,
  24. LoggerInterface $logger,
  25. protected IRegistry $registry,
  26. ) {
  27. parent::__construct(
  28. $appDataFactory,
  29. $clientService,
  30. $timeFactory,
  31. $config,
  32. $logger,
  33. $registry
  34. );
  35. $this->fileName = 'apps.json';
  36. $this->endpointName = 'apps.json';
  37. $this->ignoreMaxVersion = true;
  38. }
  39. /**
  40. * Only returns the latest compatible app release in the releases array
  41. *
  42. * @param string $ETag
  43. * @param string $content
  44. * @param bool [$allowUnstable] Allow unstable releases
  45. *
  46. * @return array
  47. */
  48. protected function fetch($ETag, $content, $allowUnstable = false) {
  49. /** @var mixed[] $response */
  50. $response = parent::fetch($ETag, $content);
  51. if (!isset($response['data']) || $response['data'] === null) {
  52. $this->logger->warning('Response from appstore is invalid, apps could not be retrieved. Try again later.', ['app' => 'appstoreFetcher']);
  53. return [];
  54. }
  55. $allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  56. $allowNightly = $allowUnstable || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  57. foreach ($response['data'] as $dataKey => $app) {
  58. $releases = [];
  59. // Filter all compatible releases
  60. foreach ($app['releases'] as $release) {
  61. // Exclude all nightly and pre-releases if required
  62. if (($allowNightly || $release['isNightly'] === false)
  63. && ($allowPreReleases || !str_contains($release['version'], '-'))) {
  64. // Exclude all versions not compatible with the current version
  65. try {
  66. $versionParser = new VersionParser();
  67. $serverVersion = $versionParser->getVersion($release['rawPlatformVersionSpec']);
  68. $ncVersion = $this->getVersion();
  69. $minServerVersion = $serverVersion->getMinimumVersion();
  70. $maxServerVersion = $serverVersion->getMaximumVersion();
  71. $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $minServerVersion, '>=');
  72. $maxFulfilled = $maxServerVersion !== '' &&
  73. $this->compareVersion->isCompatible($ncVersion, $maxServerVersion, '<=');
  74. $isPhpCompatible = true;
  75. if (($release['rawPhpVersionSpec'] ?? '*') !== '*') {
  76. $phpVersion = $versionParser->getVersion($release['rawPhpVersionSpec']);
  77. $minPhpVersion = $phpVersion->getMinimumVersion();
  78. $maxPhpVersion = $phpVersion->getMaximumVersion();
  79. $minPhpFulfilled = $minPhpVersion === '' || $this->compareVersion->isCompatible(
  80. PHP_VERSION,
  81. $minPhpVersion,
  82. '>='
  83. );
  84. $maxPhpFulfilled = $maxPhpVersion === '' || $this->compareVersion->isCompatible(
  85. PHP_VERSION,
  86. $maxPhpVersion,
  87. '<='
  88. );
  89. $isPhpCompatible = $minPhpFulfilled && $maxPhpFulfilled;
  90. }
  91. if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled) && $isPhpCompatible) {
  92. $releases[] = $release;
  93. }
  94. } catch (\InvalidArgumentException $e) {
  95. $this->logger->warning($e->getMessage(), [
  96. 'exception' => $e,
  97. ]);
  98. }
  99. }
  100. }
  101. if (empty($releases)) {
  102. // Remove apps that don't have a matching release
  103. $response['data'][$dataKey] = [];
  104. continue;
  105. }
  106. // Get the highest version
  107. $versions = [];
  108. foreach ($releases as $release) {
  109. $versions[] = $release['version'];
  110. }
  111. usort($versions, function ($version1, $version2) {
  112. return version_compare($version1, $version2);
  113. });
  114. $versions = array_reverse($versions);
  115. if (isset($versions[0])) {
  116. $highestVersion = $versions[0];
  117. foreach ($releases as $release) {
  118. if ((string)$release['version'] === (string)$highestVersion) {
  119. $response['data'][$dataKey]['releases'] = [$release];
  120. break;
  121. }
  122. }
  123. }
  124. }
  125. $response['data'] = array_values(array_filter($response['data']));
  126. return $response;
  127. }
  128. /**
  129. * @param string $version
  130. * @param string $fileName
  131. * @param bool $ignoreMaxVersion
  132. */
  133. public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) {
  134. parent::setVersion($version);
  135. $this->fileName = $fileName;
  136. $this->ignoreMaxVersion = $ignoreMaxVersion;
  137. }
  138. public function get($allowUnstable = false): array {
  139. $allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  140. $apps = parent::get($allowPreReleases);
  141. if (empty($apps)) {
  142. return [];
  143. }
  144. $allowList = $this->config->getSystemValue('appsallowlist');
  145. // If the admin specified a allow list, filter apps from the appstore
  146. if (is_array($allowList) && $this->registry->delegateHasValidSubscription()) {
  147. return array_filter($apps, function ($app) use ($allowList) {
  148. return in_array($app['id'], $allowList);
  149. });
  150. }
  151. return $apps;
  152. }
  153. }