AppFetcher.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 (empty($response)) {
  51. return [];
  52. }
  53. $allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  54. $allowNightly = $allowUnstable || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  55. foreach ($response['data'] as $dataKey => $app) {
  56. $releases = [];
  57. // Filter all compatible releases
  58. foreach ($app['releases'] as $release) {
  59. // Exclude all nightly and pre-releases if required
  60. if (($allowNightly || $release['isNightly'] === false)
  61. && ($allowPreReleases || !str_contains($release['version'], '-'))) {
  62. // Exclude all versions not compatible with the current version
  63. try {
  64. $versionParser = new VersionParser();
  65. $serverVersion = $versionParser->getVersion($release['rawPlatformVersionSpec']);
  66. $ncVersion = $this->getVersion();
  67. $minServerVersion = $serverVersion->getMinimumVersion();
  68. $maxServerVersion = $serverVersion->getMaximumVersion();
  69. $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $minServerVersion, '>=');
  70. $maxFulfilled = $maxServerVersion !== '' &&
  71. $this->compareVersion->isCompatible($ncVersion, $maxServerVersion, '<=');
  72. $isPhpCompatible = true;
  73. if (($release['rawPhpVersionSpec'] ?? '*') !== '*') {
  74. $phpVersion = $versionParser->getVersion($release['rawPhpVersionSpec']);
  75. $minPhpVersion = $phpVersion->getMinimumVersion();
  76. $maxPhpVersion = $phpVersion->getMaximumVersion();
  77. $minPhpFulfilled = $minPhpVersion === '' || $this->compareVersion->isCompatible(
  78. PHP_VERSION,
  79. $minPhpVersion,
  80. '>='
  81. );
  82. $maxPhpFulfilled = $maxPhpVersion === '' || $this->compareVersion->isCompatible(
  83. PHP_VERSION,
  84. $maxPhpVersion,
  85. '<='
  86. );
  87. $isPhpCompatible = $minPhpFulfilled && $maxPhpFulfilled;
  88. }
  89. if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled) && $isPhpCompatible) {
  90. $releases[] = $release;
  91. }
  92. } catch (\InvalidArgumentException $e) {
  93. $this->logger->warning($e->getMessage(), [
  94. 'exception' => $e,
  95. ]);
  96. }
  97. }
  98. }
  99. if (empty($releases)) {
  100. // Remove apps that don't have a matching release
  101. $response['data'][$dataKey] = [];
  102. continue;
  103. }
  104. // Get the highest version
  105. $versions = [];
  106. foreach ($releases as $release) {
  107. $versions[] = $release['version'];
  108. }
  109. usort($versions, function ($version1, $version2) {
  110. return version_compare($version1, $version2);
  111. });
  112. $versions = array_reverse($versions);
  113. if (isset($versions[0])) {
  114. $highestVersion = $versions[0];
  115. foreach ($releases as $release) {
  116. if ((string)$release['version'] === (string)$highestVersion) {
  117. $response['data'][$dataKey]['releases'] = [$release];
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. $response['data'] = array_values(array_filter($response['data']));
  124. return $response;
  125. }
  126. /**
  127. * @param string $version
  128. * @param string $fileName
  129. * @param bool $ignoreMaxVersion
  130. */
  131. public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) {
  132. parent::setVersion($version);
  133. $this->fileName = $fileName;
  134. $this->ignoreMaxVersion = $ignoreMaxVersion;
  135. }
  136. public function get($allowUnstable = false) {
  137. $allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  138. $apps = parent::get($allowPreReleases);
  139. $allowList = $this->config->getSystemValue('appsallowlist');
  140. // If the admin specified a allow list, filter apps from the appstore
  141. if (is_array($allowList) && $this->registry->delegateHasValidSubscription()) {
  142. return array_filter($apps, function ($app) use ($allowList) {
  143. return in_array($app['id'], $allowList);
  144. });
  145. }
  146. return $apps;
  147. }
  148. }