AppFetcher.php 6.4 KB

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