123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
- declare(strict_types=1);
- namespace OCA\UpdateNotification\Controller;
- use OC\App\AppStore\Fetcher\AppFetcher;
- use OCA\UpdateNotification\ResponseDefinitions;
- use OCP\App\AppPathNotFoundException;
- use OCP\App\IAppManager;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\OCSController;
- use OCP\IConfig;
- use OCP\IRequest;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- class APIController extends OCSController {
-
- protected $config;
-
- protected $appManager;
-
- protected $appFetcher;
-
- protected $l10nFactory;
-
- protected $userSession;
-
- protected $language;
-
- protected array $appsShippedInFutureVersion = [
- 'bruteforcesettings' => 25,
- 'suspicious_login' => 25,
- 'twofactor_totp' => 25,
- ];
- public function __construct(string $appName,
- IRequest $request,
- IConfig $config,
- IAppManager $appManager,
- AppFetcher $appFetcher,
- IFactory $l10nFactory,
- IUserSession $userSession) {
- parent::__construct($appName, $request);
- $this->config = $config;
- $this->appManager = $appManager;
- $this->appFetcher = $appFetcher;
- $this->l10nFactory = $l10nFactory;
- $this->userSession = $userSession;
- }
-
- public function getAppList(string $newVersion): DataResponse {
- if (!$this->config->getSystemValue('appstoreenabled', true)) {
- return new DataResponse([
- 'appstore_disabled' => true,
- ], Http::STATUS_NOT_FOUND);
- }
-
- $installedApps = $this->appManager->getInstalledApps();
- $installedApps = array_filter($installedApps, function ($app) {
- try {
- $this->appManager->getAppPath($app);
- } catch (AppPathNotFoundException $e) {
- return false;
- }
- return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]);
- });
- if (empty($installedApps)) {
- return new DataResponse([
- 'missing' => [],
- 'available' => [],
- ]);
- }
- $this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
-
- $availableApps = array_map(static function (array $app) {
- return $app['id'];
- }, $this->appFetcher->get());
- if (empty($availableApps)) {
- return new DataResponse([
- 'appstore_disabled' => false,
- 'already_on_latest' => false,
- ], Http::STATUS_NOT_FOUND);
- }
- $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
-
- $installedApps = array_filter($installedApps, function(string $appId) {
- try {
- return !file_exists($this->appManager->getAppPath($appId) . '/.git');
- } catch (AppPathNotFoundException $e) {
- return true;
- }
- });
- $missing = array_diff($installedApps, $availableApps);
- $missing = array_map([$this, 'getAppDetails'], $missing);
- sort($missing);
- $available = array_intersect($installedApps, $availableApps);
- $available = array_map([$this, 'getAppDetails'], $available);
- sort($available);
- return new DataResponse([
- 'missing' => $missing,
- 'available' => $available,
- ]);
- }
-
- protected function getAppDetails(string $appId): array {
- $app = $this->appManager->getAppInfo($appId, false, $this->language);
-
- $name = $app['name'];
- return [
- 'appId' => $appId,
- 'appName' => $name ?? $appId,
- ];
- }
- }
|