AppFetcher.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Jakub Onderka <ahoj@jakubonderka.cz>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OC\App\AppStore\Fetcher;
  31. use OC\App\AppStore\Version\VersionParser;
  32. use OC\App\CompareVersion;
  33. use OC\Files\AppData\Factory;
  34. use OCP\AppFramework\Utility\ITimeFactory;
  35. use OCP\Http\Client\IClientService;
  36. use OCP\IConfig;
  37. use OCP\Support\Subscription\IRegistry;
  38. use Psr\Log\LoggerInterface;
  39. class AppFetcher extends Fetcher {
  40. /** @var bool */
  41. private $ignoreMaxVersion;
  42. public function __construct(Factory $appDataFactory,
  43. IClientService $clientService,
  44. ITimeFactory $timeFactory,
  45. IConfig $config,
  46. private CompareVersion $compareVersion,
  47. LoggerInterface $logger,
  48. protected IRegistry $registry,
  49. ) {
  50. parent::__construct(
  51. $appDataFactory,
  52. $clientService,
  53. $timeFactory,
  54. $config,
  55. $logger,
  56. $registry
  57. );
  58. $this->fileName = 'apps.json';
  59. $this->endpointName = 'apps.json';
  60. $this->ignoreMaxVersion = true;
  61. }
  62. /**
  63. * Only returns the latest compatible app release in the releases array
  64. *
  65. * @param string $ETag
  66. * @param string $content
  67. * @param bool [$allowUnstable] Allow unstable releases
  68. *
  69. * @return array
  70. */
  71. protected function fetch($ETag, $content, $allowUnstable = false) {
  72. /** @var mixed[] $response */
  73. $response = parent::fetch($ETag, $content);
  74. if (empty($response)) {
  75. return [];
  76. }
  77. $allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  78. $allowNightly = $allowUnstable || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  79. foreach ($response['data'] as $dataKey => $app) {
  80. $releases = [];
  81. // Filter all compatible releases
  82. foreach ($app['releases'] as $release) {
  83. // Exclude all nightly and pre-releases if required
  84. if (($allowNightly || $release['isNightly'] === false)
  85. && ($allowPreReleases || !str_contains($release['version'], '-'))) {
  86. // Exclude all versions not compatible with the current version
  87. try {
  88. $versionParser = new VersionParser();
  89. $serverVersion = $versionParser->getVersion($release['rawPlatformVersionSpec']);
  90. $ncVersion = $this->getVersion();
  91. $minServerVersion = $serverVersion->getMinimumVersion();
  92. $maxServerVersion = $serverVersion->getMaximumVersion();
  93. $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $minServerVersion, '>=');
  94. $maxFulfilled = $maxServerVersion !== '' &&
  95. $this->compareVersion->isCompatible($ncVersion, $maxServerVersion, '<=');
  96. $isPhpCompatible = true;
  97. if (($release['rawPhpVersionSpec'] ?? '*') !== '*') {
  98. $phpVersion = $versionParser->getVersion($release['rawPhpVersionSpec']);
  99. $minPhpVersion = $phpVersion->getMinimumVersion();
  100. $maxPhpVersion = $phpVersion->getMaximumVersion();
  101. $minPhpFulfilled = $minPhpVersion === '' || $this->compareVersion->isCompatible(
  102. PHP_VERSION,
  103. $minPhpVersion,
  104. '>='
  105. );
  106. $maxPhpFulfilled = $maxPhpVersion === '' || $this->compareVersion->isCompatible(
  107. PHP_VERSION,
  108. $maxPhpVersion,
  109. '<='
  110. );
  111. $isPhpCompatible = $minPhpFulfilled && $maxPhpFulfilled;
  112. }
  113. if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled) && $isPhpCompatible) {
  114. $releases[] = $release;
  115. }
  116. } catch (\InvalidArgumentException $e) {
  117. $this->logger->warning($e->getMessage(), [
  118. 'exception' => $e,
  119. ]);
  120. }
  121. }
  122. }
  123. if (empty($releases)) {
  124. // Remove apps that don't have a matching release
  125. $response['data'][$dataKey] = [];
  126. continue;
  127. }
  128. // Get the highest version
  129. $versions = [];
  130. foreach ($releases as $release) {
  131. $versions[] = $release['version'];
  132. }
  133. usort($versions, function ($version1, $version2) {
  134. return version_compare($version1, $version2);
  135. });
  136. $versions = array_reverse($versions);
  137. if (isset($versions[0])) {
  138. $highestVersion = $versions[0];
  139. foreach ($releases as $release) {
  140. if ((string)$release['version'] === (string)$highestVersion) {
  141. $response['data'][$dataKey]['releases'] = [$release];
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. $response['data'] = array_values(array_filter($response['data']));
  148. return $response;
  149. }
  150. /**
  151. * @param string $version
  152. * @param string $fileName
  153. * @param bool $ignoreMaxVersion
  154. */
  155. public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) {
  156. parent::setVersion($version);
  157. $this->fileName = $fileName;
  158. $this->ignoreMaxVersion = $ignoreMaxVersion;
  159. }
  160. public function get($allowUnstable = false) {
  161. $allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
  162. $apps = parent::get($allowPreReleases);
  163. $allowList = $this->config->getSystemValue('appsallowlist');
  164. // If the admin specified a allow list, filter apps from the appstore
  165. if (is_array($allowList) && $this->registry->delegateHasValidSubscription()) {
  166. return array_filter($apps, function ($app) use ($allowList) {
  167. return in_array($app['id'], $allowList);
  168. });
  169. }
  170. return $apps;
  171. }
  172. }