Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\App\AppStore\Fetcher;
  27. use OC\App\AppStore\Version\VersionParser;
  28. use OC\App\CompareVersion;
  29. use OC\Files\AppData\Factory;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\Http\Client\IClientService;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use OCP\Util;
  35. class AppFetcher extends Fetcher {
  36. /** @var CompareVersion */
  37. private $compareVersion;
  38. /**
  39. * @param Factory $appDataFactory
  40. * @param IClientService $clientService
  41. * @param ITimeFactory $timeFactory
  42. * @param IConfig $config
  43. * @param CompareVersion $compareVersion
  44. * @param ILogger $logger
  45. */
  46. public function __construct(Factory $appDataFactory,
  47. IClientService $clientService,
  48. ITimeFactory $timeFactory,
  49. IConfig $config,
  50. CompareVersion $compareVersion,
  51. ILogger $logger) {
  52. parent::__construct(
  53. $appDataFactory,
  54. $clientService,
  55. $timeFactory,
  56. $config,
  57. $logger
  58. );
  59. $this->fileName = 'apps.json';
  60. $this->setEndpoint();
  61. $this->compareVersion = $compareVersion;
  62. }
  63. /**
  64. * Only returns the latest compatible app release in the releases array
  65. *
  66. * @param string $ETag
  67. * @param string $content
  68. *
  69. * @return array
  70. */
  71. protected function fetch($ETag, $content) {
  72. /** @var mixed[] $response */
  73. $response = parent::fetch($ETag, $content);
  74. foreach($response['data'] as $dataKey => $app) {
  75. $releases = [];
  76. // Filter all compatible releases
  77. foreach($app['releases'] as $release) {
  78. // Exclude all nightly and pre-releases
  79. if($release['isNightly'] === false
  80. && strpos($release['version'], '-') === false) {
  81. // Exclude all versions not compatible with the current version
  82. try {
  83. $versionParser = new VersionParser();
  84. $version = $versionParser->getVersion($release['rawPlatformVersionSpec']);
  85. $ncVersion = $this->getVersion();
  86. $min = $version->getMinimumVersion();
  87. $minFulfilled = $this->compareVersion->isCompatible($ncVersion, $min, '>=');
  88. if ($minFulfilled) {
  89. $releases[] = $release;
  90. }
  91. } catch (\InvalidArgumentException $e) {
  92. $this->logger->logException($e, ['app' => 'appstoreFetcher', 'level' => ILogger::WARN]);
  93. }
  94. }
  95. }
  96. // Get the highest version
  97. $versions = [];
  98. foreach($releases as $release) {
  99. $versions[] = $release['version'];
  100. }
  101. usort($versions, 'version_compare');
  102. $versions = array_reverse($versions);
  103. if(isset($versions[0])) {
  104. $highestVersion = $versions[0];
  105. foreach ($releases as $release) {
  106. if ((string)$release['version'] === (string)$highestVersion) {
  107. $response['data'][$dataKey]['releases'] = [$release];
  108. break;
  109. }
  110. }
  111. }
  112. }
  113. $response['data'] = array_values($response['data']);
  114. return $response;
  115. }
  116. private function setEndpoint() {
  117. $this->endpointUrl = 'https://apps.nextcloud.com/api/v1/apps.json';
  118. }
  119. /**
  120. * @param string $version
  121. * @param string $fileName
  122. */
  123. public function setVersion(string $version, string $fileName = 'apps.json') {
  124. parent::setVersion($version);
  125. $this->fileName = $fileName;
  126. $this->setEndpoint();
  127. }
  128. }