2016-10-27 11:41:15 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-10-27 11:41:15 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-10-27 11:41:15 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\App\AppStore\Fetcher;
|
|
|
|
|
|
2016-11-24 08:29:57 -05:00
|
|
|
use OC\App\AppStore\Version\VersionParser;
|
2018-03-29 03:44:38 -04:00
|
|
|
use OC\App\CompareVersion;
|
2017-05-10 03:56:38 -04:00
|
|
|
use OC\Files\AppData\Factory;
|
2016-10-27 11:41:15 -04:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
|
use OCP\IConfig;
|
2021-10-08 05:35:27 -04:00
|
|
|
use OCP\Support\Subscription\IRegistry;
|
2021-04-16 08:26:43 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-10-27 11:41:15 -04:00
|
|
|
|
|
|
|
|
class AppFetcher extends Fetcher {
|
2019-03-20 07:21:01 -04:00
|
|
|
/** @var bool */
|
|
|
|
|
private $ignoreMaxVersion;
|
|
|
|
|
|
2017-05-10 03:56:38 -04:00
|
|
|
public function __construct(
|
|
|
|
|
Factory $appDataFactory,
|
2016-10-27 11:41:15 -04:00
|
|
|
IClientService $clientService,
|
|
|
|
|
ITimeFactory $timeFactory,
|
2017-05-11 18:46:41 -04:00
|
|
|
IConfig $config,
|
2023-12-11 08:20:55 -05:00
|
|
|
private CompareVersion $compareVersion,
|
2021-10-08 05:35:27 -04:00
|
|
|
LoggerInterface $logger,
|
2023-07-18 04:35:20 -04:00
|
|
|
protected IRegistry $registry,
|
|
|
|
|
) {
|
2016-10-27 11:41:15 -04:00
|
|
|
parent::__construct(
|
2017-05-10 03:56:38 -04:00
|
|
|
$appDataFactory,
|
2016-10-27 11:41:15 -04:00
|
|
|
$clientService,
|
2016-12-15 16:04:03 -05:00
|
|
|
$timeFactory,
|
2017-05-11 18:46:41 -04:00
|
|
|
$config,
|
2021-10-14 05:57:28 -04:00
|
|
|
$logger,
|
|
|
|
|
$registry
|
2016-10-27 11:41:15 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->fileName = 'apps.json';
|
2020-01-09 11:29:00 -05:00
|
|
|
$this->endpointName = 'apps.json';
|
2019-03-20 07:21:01 -04:00
|
|
|
$this->ignoreMaxVersion = true;
|
2016-10-27 11:41:15 -04:00
|
|
|
}
|
2016-11-24 08:29:57 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Only returns the latest compatible app release in the releases array
|
|
|
|
|
*
|
2016-12-22 03:46:10 -05:00
|
|
|
* @param string $ETag
|
|
|
|
|
* @param string $content
|
2020-04-30 03:43:33 -04:00
|
|
|
* @param bool [$allowUnstable] Allow unstable releases
|
2016-12-22 03:46:10 -05:00
|
|
|
*
|
2016-11-24 08:29:57 -05:00
|
|
|
* @return array
|
|
|
|
|
*/
|
2020-04-30 03:43:33 -04:00
|
|
|
protected function fetch($ETag, $content, $allowUnstable = false) {
|
2016-12-15 16:04:03 -05:00
|
|
|
/** @var mixed[] $response */
|
2016-12-22 03:46:10 -05:00
|
|
|
$response = parent::fetch($ETag, $content);
|
2021-04-16 08:26:43 -04:00
|
|
|
|
2024-09-13 03:49:09 -04:00
|
|
|
if (!isset($response['data']) || $response['data'] === null) {
|
2024-09-09 13:50:11 -04:00
|
|
|
$this->logger->warning('Response from appstore is invalid, apps could not be retrieved. Try again later.', ['app' => 'appstoreFetcher']);
|
2021-03-10 10:11:14 -05:00
|
|
|
return [];
|
|
|
|
|
}
|
2016-11-24 08:29:57 -05:00
|
|
|
|
2022-03-10 02:30:50 -05:00
|
|
|
$allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
|
|
|
|
|
$allowNightly = $allowUnstable || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
|
2019-04-08 09:22:27 -04:00
|
|
|
|
2016-11-24 08:29:57 -05:00
|
|
|
foreach ($response['data'] as $dataKey => $app) {
|
|
|
|
|
$releases = [];
|
|
|
|
|
|
|
|
|
|
// Filter all compatible releases
|
|
|
|
|
foreach ($app['releases'] as $release) {
|
2019-04-08 09:22:27 -04:00
|
|
|
// Exclude all nightly and pre-releases if required
|
|
|
|
|
if (($allowNightly || $release['isNightly'] === false)
|
2023-05-15 07:47:19 -04:00
|
|
|
&& ($allowPreReleases || !str_contains($release['version'], '-'))) {
|
2016-11-24 08:29:57 -05:00
|
|
|
// Exclude all versions not compatible with the current version
|
2018-04-10 18:05:42 -04:00
|
|
|
try {
|
|
|
|
|
$versionParser = new VersionParser();
|
2020-11-27 10:44:55 -05:00
|
|
|
$serverVersion = $versionParser->getVersion($release['rawPlatformVersionSpec']);
|
2018-04-10 18:05:42 -04:00
|
|
|
$ncVersion = $this->getVersion();
|
2020-11-27 10:44:55 -05:00
|
|
|
$minServerVersion = $serverVersion->getMinimumVersion();
|
|
|
|
|
$maxServerVersion = $serverVersion->getMaximumVersion();
|
|
|
|
|
$minFulfilled = $this->compareVersion->isCompatible($ncVersion, $minServerVersion, '>=');
|
|
|
|
|
$maxFulfilled = $maxServerVersion !== ''
|
|
|
|
|
&& $this->compareVersion->isCompatible($ncVersion, $maxServerVersion, '<=');
|
|
|
|
|
$isPhpCompatible = true;
|
|
|
|
|
if (($release['rawPhpVersionSpec'] ?? '*') !== '*') {
|
|
|
|
|
$phpVersion = $versionParser->getVersion($release['rawPhpVersionSpec']);
|
|
|
|
|
$minPhpVersion = $phpVersion->getMinimumVersion();
|
|
|
|
|
$maxPhpVersion = $phpVersion->getMaximumVersion();
|
2021-01-26 07:48:06 -05:00
|
|
|
$minPhpFulfilled = $minPhpVersion === '' || $this->compareVersion->isCompatible(
|
2023-01-20 05:45:08 -05:00
|
|
|
PHP_VERSION,
|
|
|
|
|
$minPhpVersion,
|
|
|
|
|
'>='
|
|
|
|
|
);
|
2021-01-26 07:48:06 -05:00
|
|
|
$maxPhpFulfilled = $maxPhpVersion === '' || $this->compareVersion->isCompatible(
|
2023-01-20 05:45:08 -05:00
|
|
|
PHP_VERSION,
|
|
|
|
|
$maxPhpVersion,
|
|
|
|
|
'<='
|
|
|
|
|
);
|
2020-11-27 10:44:55 -05:00
|
|
|
|
|
|
|
|
$isPhpCompatible = $minPhpFulfilled && $maxPhpFulfilled;
|
|
|
|
|
}
|
|
|
|
|
if ($minFulfilled && ($this->ignoreMaxVersion || $maxFulfilled) && $isPhpCompatible) {
|
2018-04-10 18:05:42 -04:00
|
|
|
$releases[] = $release;
|
|
|
|
|
}
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
2021-04-16 08:26:43 -04:00
|
|
|
$this->logger->warning($e->getMessage(), [
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
]);
|
2016-11-24 08:29:57 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-20 07:15:52 -04:00
|
|
|
if (empty($releases)) {
|
|
|
|
|
// Remove apps that don't have a matching release
|
2019-08-20 08:47:46 -04:00
|
|
|
$response['data'][$dataKey] = [];
|
2019-03-20 07:15:52 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 08:29:57 -05:00
|
|
|
// Get the highest version
|
|
|
|
|
$versions = [];
|
|
|
|
|
foreach ($releases as $release) {
|
|
|
|
|
$versions[] = $release['version'];
|
|
|
|
|
}
|
2021-06-21 10:19:58 -04:00
|
|
|
usort($versions, function ($version1, $version2) {
|
|
|
|
|
return version_compare($version1, $version2);
|
|
|
|
|
});
|
2016-11-24 08:29:57 -05:00
|
|
|
$versions = array_reverse($versions);
|
|
|
|
|
if (isset($versions[0])) {
|
|
|
|
|
$highestVersion = $versions[0];
|
|
|
|
|
foreach ($releases as $release) {
|
|
|
|
|
if ((string)$release['version'] === (string)$highestVersion) {
|
|
|
|
|
$response['data'][$dataKey]['releases'] = [$release];
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 08:47:46 -04:00
|
|
|
$response['data'] = array_values(array_filter($response['data']));
|
2016-11-24 08:29:57 -05:00
|
|
|
return $response;
|
|
|
|
|
}
|
2017-05-02 04:08:16 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $version
|
2018-04-10 06:32:56 -04:00
|
|
|
* @param string $fileName
|
2019-03-20 07:21:01 -04:00
|
|
|
* @param bool $ignoreMaxVersion
|
2017-05-02 04:08:16 -04:00
|
|
|
*/
|
2019-03-20 07:21:01 -04:00
|
|
|
public function setVersion(string $version, string $fileName = 'apps.json', bool $ignoreMaxVersion = true) {
|
2017-05-02 04:08:16 -04:00
|
|
|
parent::setVersion($version);
|
2018-02-28 05:31:33 -05:00
|
|
|
$this->fileName = $fileName;
|
2019-03-20 07:21:01 -04:00
|
|
|
$this->ignoreMaxVersion = $ignoreMaxVersion;
|
2017-05-02 04:08:16 -04:00
|
|
|
}
|
2021-10-08 05:35:27 -04:00
|
|
|
|
2024-09-09 03:29:09 -04:00
|
|
|
public function get($allowUnstable = false): array {
|
2022-03-10 02:30:50 -05:00
|
|
|
$allowPreReleases = $allowUnstable || $this->getChannel() === 'beta' || $this->getChannel() === 'daily' || $this->getChannel() === 'git';
|
2022-03-10 02:29:41 -05:00
|
|
|
|
|
|
|
|
$apps = parent::get($allowPreReleases);
|
2024-07-16 05:42:35 -04:00
|
|
|
if (empty($apps)) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2021-10-13 12:14:48 -04:00
|
|
|
$allowList = $this->config->getSystemValue('appsallowlist');
|
2021-10-08 05:35:27 -04:00
|
|
|
|
2021-10-13 12:14:48 -04:00
|
|
|
// If the admin specified a allow list, filter apps from the appstore
|
|
|
|
|
if (is_array($allowList) && $this->registry->delegateHasValidSubscription()) {
|
|
|
|
|
return array_filter($apps, function ($app) use ($allowList) {
|
|
|
|
|
return in_array($app['id'], $allowList);
|
2021-10-08 05:35:27 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $apps;
|
|
|
|
|
}
|
2016-10-27 11:41:15 -04:00
|
|
|
}
|