AppFetcher.php 5.3 KB

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