AppSettingsController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Settings\Controller;
  8. use OC\App\AppStore\Bundles\BundleFetcher;
  9. use OC\App\AppStore\Fetcher\AppDiscoverFetcher;
  10. use OC\App\AppStore\Fetcher\AppFetcher;
  11. use OC\App\AppStore\Fetcher\CategoryFetcher;
  12. use OC\App\AppStore\Version\VersionParser;
  13. use OC\App\DependencyAnalyzer;
  14. use OC\App\Platform;
  15. use OC\Installer;
  16. use OCP\App\AppPathNotFoundException;
  17. use OCP\App\IAppManager;
  18. use OCP\AppFramework\Controller;
  19. use OCP\AppFramework\Http;
  20. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  21. use OCP\AppFramework\Http\Attribute\OpenAPI;
  22. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  23. use OCP\AppFramework\Http\Attribute\PublicPage;
  24. use OCP\AppFramework\Http\ContentSecurityPolicy;
  25. use OCP\AppFramework\Http\FileDisplayResponse;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. use OCP\AppFramework\Http\NotFoundResponse;
  28. use OCP\AppFramework\Http\Response;
  29. use OCP\AppFramework\Http\TemplateResponse;
  30. use OCP\AppFramework\Services\IInitialState;
  31. use OCP\Files\AppData\IAppDataFactory;
  32. use OCP\Files\IAppData;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\NotPermittedException;
  35. use OCP\Files\SimpleFS\ISimpleFile;
  36. use OCP\Files\SimpleFS\ISimpleFolder;
  37. use OCP\Http\Client\IClientService;
  38. use OCP\IConfig;
  39. use OCP\IGroup;
  40. use OCP\IL10N;
  41. use OCP\INavigationManager;
  42. use OCP\IRequest;
  43. use OCP\IURLGenerator;
  44. use OCP\L10N\IFactory;
  45. use OCP\Server;
  46. use OCP\Util;
  47. use Psr\Log\LoggerInterface;
  48. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  49. class AppSettingsController extends Controller {
  50. /** @var array */
  51. private $allApps = [];
  52. private IAppData $appData;
  53. public function __construct(
  54. string $appName,
  55. IRequest $request,
  56. IAppDataFactory $appDataFactory,
  57. private IL10N $l10n,
  58. private IConfig $config,
  59. private INavigationManager $navigationManager,
  60. private IAppManager $appManager,
  61. private CategoryFetcher $categoryFetcher,
  62. private AppFetcher $appFetcher,
  63. private IFactory $l10nFactory,
  64. private BundleFetcher $bundleFetcher,
  65. private Installer $installer,
  66. private IURLGenerator $urlGenerator,
  67. private LoggerInterface $logger,
  68. private IInitialState $initialState,
  69. private AppDiscoverFetcher $discoverFetcher,
  70. private IClientService $clientService,
  71. ) {
  72. parent::__construct($appName, $request);
  73. $this->appData = $appDataFactory->get('appstore');
  74. }
  75. /**
  76. * @psalm-suppress UndefinedClass AppAPI is shipped since 30.0.1
  77. *
  78. * @return TemplateResponse
  79. */
  80. #[NoCSRFRequired]
  81. public function viewApps(): TemplateResponse {
  82. $this->navigationManager->setActiveEntry('core_apps');
  83. $this->initialState->provideInitialState('appstoreEnabled', $this->config->getSystemValueBool('appstoreenabled', true));
  84. $this->initialState->provideInitialState('appstoreBundles', $this->getBundles());
  85. $this->initialState->provideInitialState('appstoreDeveloperDocs', $this->urlGenerator->linkToDocs('developer-manual'));
  86. $this->initialState->provideInitialState('appstoreUpdateCount', count($this->getAppsWithUpdates()));
  87. if ($this->appManager->isInstalled('app_api')) {
  88. try {
  89. Server::get(\OCA\AppAPI\Service\ExAppsPageService::class)->provideAppApiState($this->initialState);
  90. } catch (\Psr\Container\NotFoundExceptionInterface|\Psr\Container\ContainerExceptionInterface $e) {
  91. }
  92. }
  93. $policy = new ContentSecurityPolicy();
  94. $policy->addAllowedImageDomain('https://usercontent.apps.nextcloud.com');
  95. $templateResponse = new TemplateResponse('settings', 'settings/empty', ['pageTitle' => $this->l10n->t('Settings')]);
  96. $templateResponse->setContentSecurityPolicy($policy);
  97. Util::addStyle('settings', 'settings');
  98. Util::addScript('settings', 'vue-settings-apps-users-management');
  99. return $templateResponse;
  100. }
  101. /**
  102. * Get all active entries for the app discover section
  103. */
  104. #[NoCSRFRequired]
  105. public function getAppDiscoverJSON(): JSONResponse {
  106. $data = $this->discoverFetcher->get(true);
  107. return new JSONResponse(array_values($data));
  108. }
  109. /**
  110. * Get a image for the app discover section - this is proxied for privacy and CSP reasons
  111. *
  112. * @param string $image
  113. * @throws \Exception
  114. */
  115. #[PublicPage]
  116. #[NoCSRFRequired]
  117. public function getAppDiscoverMedia(string $fileName): Response {
  118. $etag = $this->discoverFetcher->getETag() ?? date('Y-m');
  119. $folder = null;
  120. try {
  121. $folder = $this->appData->getFolder('app-discover-cache');
  122. $this->cleanUpImageCache($folder, $etag);
  123. } catch (\Throwable $e) {
  124. $folder = $this->appData->newFolder('app-discover-cache');
  125. }
  126. // Get the current cache folder
  127. try {
  128. $folder = $folder->getFolder($etag);
  129. } catch (NotFoundException $e) {
  130. $folder = $folder->newFolder($etag);
  131. }
  132. $info = pathinfo($fileName);
  133. $hashName = md5($fileName);
  134. $allFiles = $folder->getDirectoryListing();
  135. // Try to find the file
  136. $file = array_filter($allFiles, function (ISimpleFile $file) use ($hashName) {
  137. return str_starts_with($file->getName(), $hashName);
  138. });
  139. // Get the first entry
  140. $file = reset($file);
  141. // If not found request from Web
  142. if ($file === false) {
  143. try {
  144. $client = $this->clientService->newClient();
  145. $fileResponse = $client->get($fileName);
  146. $contentType = $fileResponse->getHeader('Content-Type');
  147. $extension = $info['extension'] ?? '';
  148. $file = $folder->newFile($hashName . '.' . base64_encode($contentType) . '.' . $extension, $fileResponse->getBody());
  149. } catch (\Throwable $e) {
  150. $this->logger->warning('Could not load media file for app discover section', ['media_src' => $fileName, 'exception' => $e]);
  151. return new NotFoundResponse();
  152. }
  153. } else {
  154. // File was found so we can get the content type from the file name
  155. $contentType = base64_decode(explode('.', $file->getName())[1] ?? '');
  156. }
  157. $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $contentType]);
  158. // cache for 7 days
  159. $response->cacheFor(604800, false, true);
  160. return $response;
  161. }
  162. /**
  163. * Remove orphaned folders from the image cache that do not match the current etag
  164. * @param ISimpleFolder $folder The folder to clear
  165. * @param string $etag The etag (directory name) to keep
  166. */
  167. private function cleanUpImageCache(ISimpleFolder $folder, string $etag): void {
  168. // Cleanup old cache folders
  169. $allFiles = $folder->getDirectoryListing();
  170. foreach ($allFiles as $dir) {
  171. try {
  172. if ($dir->getName() !== $etag) {
  173. $dir->delete();
  174. }
  175. } catch (NotPermittedException $e) {
  176. // ignore folder for now
  177. }
  178. }
  179. }
  180. private function getAppsWithUpdates() {
  181. $appClass = new \OC_App();
  182. $apps = $appClass->listAllApps();
  183. foreach ($apps as $key => $app) {
  184. $newVersion = $this->installer->isUpdateAvailable($app['id']);
  185. if ($newVersion === false) {
  186. unset($apps[$key]);
  187. }
  188. }
  189. return $apps;
  190. }
  191. private function getBundles() {
  192. $result = [];
  193. $bundles = $this->bundleFetcher->getBundles();
  194. foreach ($bundles as $bundle) {
  195. $result[] = [
  196. 'name' => $bundle->getName(),
  197. 'id' => $bundle->getIdentifier(),
  198. 'appIdentifiers' => $bundle->getAppIdentifiers()
  199. ];
  200. }
  201. return $result;
  202. }
  203. /**
  204. * Get all available categories
  205. *
  206. * @return JSONResponse
  207. */
  208. public function listCategories(): JSONResponse {
  209. return new JSONResponse($this->getAllCategories());
  210. }
  211. private function getAllCategories() {
  212. $currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
  213. $categories = $this->categoryFetcher->get();
  214. return array_map(fn ($category) => [
  215. 'id' => $category['id'],
  216. 'displayName' => $category['translations'][$currentLanguage]['name'] ?? $category['translations']['en']['name'],
  217. ], $categories);
  218. }
  219. /**
  220. * Convert URL to proxied URL so CSP is no problem
  221. */
  222. private function createProxyPreviewUrl(string $url): string {
  223. if ($url === '') {
  224. return '';
  225. }
  226. return 'https://usercontent.apps.nextcloud.com/' . base64_encode($url);
  227. }
  228. private function fetchApps() {
  229. $appClass = new \OC_App();
  230. $apps = $appClass->listAllApps();
  231. foreach ($apps as $app) {
  232. $app['installed'] = true;
  233. if (isset($app['screenshot'][0])) {
  234. $appScreenshot = $app['screenshot'][0] ?? null;
  235. if (is_array($appScreenshot)) {
  236. // Screenshot with thumbnail
  237. $appScreenshot = $appScreenshot['@value'];
  238. }
  239. $app['screenshot'] = $this->createProxyPreviewUrl($appScreenshot);
  240. }
  241. $this->allApps[$app['id']] = $app;
  242. }
  243. $apps = $this->getAppsForCategory('');
  244. $supportedApps = $appClass->getSupportedApps();
  245. foreach ($apps as $app) {
  246. $app['appstore'] = true;
  247. if (!array_key_exists($app['id'], $this->allApps)) {
  248. $this->allApps[$app['id']] = $app;
  249. } else {
  250. $this->allApps[$app['id']] = array_merge($app, $this->allApps[$app['id']]);
  251. }
  252. if (in_array($app['id'], $supportedApps)) {
  253. $this->allApps[$app['id']]['level'] = \OC_App::supportedApp;
  254. }
  255. }
  256. // add bundle information
  257. $bundles = $this->bundleFetcher->getBundles();
  258. foreach ($bundles as $bundle) {
  259. foreach ($bundle->getAppIdentifiers() as $identifier) {
  260. foreach ($this->allApps as &$app) {
  261. if ($app['id'] === $identifier) {
  262. $app['bundleIds'][] = $bundle->getIdentifier();
  263. continue;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. private function getAllApps() {
  270. return $this->allApps;
  271. }
  272. /**
  273. * Get all available apps in a category
  274. *
  275. * @return JSONResponse
  276. * @throws \Exception
  277. */
  278. public function listApps(): JSONResponse {
  279. $this->fetchApps();
  280. $apps = $this->getAllApps();
  281. $dependencyAnalyzer = new DependencyAnalyzer(new Platform($this->config), $this->l10n);
  282. $ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []);
  283. if (!is_array($ignoreMaxApps)) {
  284. $this->logger->warning('The value given for app_install_overwrite is not an array. Ignoring...');
  285. $ignoreMaxApps = [];
  286. }
  287. // Extend existing app details
  288. $apps = array_map(function (array $appData) use ($dependencyAnalyzer, $ignoreMaxApps) {
  289. if (isset($appData['appstoreData'])) {
  290. $appstoreData = $appData['appstoreData'];
  291. $appData['screenshot'] = $this->createProxyPreviewUrl($appstoreData['screenshots'][0]['url'] ?? '');
  292. $appData['category'] = $appstoreData['categories'];
  293. $appData['releases'] = $appstoreData['releases'];
  294. }
  295. $newVersion = $this->installer->isUpdateAvailable($appData['id']);
  296. if ($newVersion) {
  297. $appData['update'] = $newVersion;
  298. }
  299. // fix groups to be an array
  300. $groups = [];
  301. if (is_string($appData['groups'])) {
  302. $groups = json_decode($appData['groups']);
  303. // ensure 'groups' is an array
  304. if (!is_array($groups)) {
  305. $groups = [$groups];
  306. }
  307. }
  308. $appData['groups'] = $groups;
  309. $appData['canUnInstall'] = !$appData['active'] && $appData['removable'];
  310. // fix licence vs license
  311. if (isset($appData['license']) && !isset($appData['licence'])) {
  312. $appData['licence'] = $appData['license'];
  313. }
  314. $ignoreMax = in_array($appData['id'], $ignoreMaxApps);
  315. // analyse dependencies
  316. $missing = $dependencyAnalyzer->analyze($appData, $ignoreMax);
  317. $appData['canInstall'] = empty($missing);
  318. $appData['missingDependencies'] = $missing;
  319. $appData['missingMinOwnCloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['min-version']);
  320. $appData['missingMaxOwnCloudVersion'] = !isset($appData['dependencies']['nextcloud']['@attributes']['max-version']);
  321. $appData['isCompatible'] = $dependencyAnalyzer->isMarkedCompatible($appData);
  322. return $appData;
  323. }, $apps);
  324. usort($apps, [$this, 'sortApps']);
  325. return new JSONResponse(['apps' => $apps, 'status' => 'success']);
  326. }
  327. /**
  328. * Get all apps for a category from the app store
  329. *
  330. * @param string $requestedCategory
  331. * @return array
  332. * @throws \Exception
  333. */
  334. private function getAppsForCategory($requestedCategory = ''): array {
  335. $versionParser = new VersionParser();
  336. $formattedApps = [];
  337. $apps = $this->appFetcher->get();
  338. foreach ($apps as $app) {
  339. // Skip all apps not in the requested category
  340. if ($requestedCategory !== '') {
  341. $isInCategory = false;
  342. foreach ($app['categories'] as $category) {
  343. if ($category === $requestedCategory) {
  344. $isInCategory = true;
  345. }
  346. }
  347. if (!$isInCategory) {
  348. continue;
  349. }
  350. }
  351. if (!isset($app['releases'][0]['rawPlatformVersionSpec'])) {
  352. continue;
  353. }
  354. $nextCloudVersion = $versionParser->getVersion($app['releases'][0]['rawPlatformVersionSpec']);
  355. $nextCloudVersionDependencies = [];
  356. if ($nextCloudVersion->getMinimumVersion() !== '') {
  357. $nextCloudVersionDependencies['nextcloud']['@attributes']['min-version'] = $nextCloudVersion->getMinimumVersion();
  358. }
  359. if ($nextCloudVersion->getMaximumVersion() !== '') {
  360. $nextCloudVersionDependencies['nextcloud']['@attributes']['max-version'] = $nextCloudVersion->getMaximumVersion();
  361. }
  362. $phpVersion = $versionParser->getVersion($app['releases'][0]['rawPhpVersionSpec']);
  363. try {
  364. $this->appManager->getAppPath($app['id']);
  365. $existsLocally = true;
  366. } catch (AppPathNotFoundException) {
  367. $existsLocally = false;
  368. }
  369. $phpDependencies = [];
  370. if ($phpVersion->getMinimumVersion() !== '') {
  371. $phpDependencies['php']['@attributes']['min-version'] = $phpVersion->getMinimumVersion();
  372. }
  373. if ($phpVersion->getMaximumVersion() !== '') {
  374. $phpDependencies['php']['@attributes']['max-version'] = $phpVersion->getMaximumVersion();
  375. }
  376. if (isset($app['releases'][0]['minIntSize'])) {
  377. $phpDependencies['php']['@attributes']['min-int-size'] = $app['releases'][0]['minIntSize'];
  378. }
  379. $authors = '';
  380. foreach ($app['authors'] as $key => $author) {
  381. $authors .= $author['name'];
  382. if ($key !== count($app['authors']) - 1) {
  383. $authors .= ', ';
  384. }
  385. }
  386. $currentLanguage = substr($this->l10nFactory->findLanguage(), 0, 2);
  387. $enabledValue = $this->config->getAppValue($app['id'], 'enabled', 'no');
  388. $groups = null;
  389. if ($enabledValue !== 'no' && $enabledValue !== 'yes') {
  390. $groups = $enabledValue;
  391. }
  392. $currentVersion = '';
  393. if ($this->appManager->isInstalled($app['id'])) {
  394. $currentVersion = $this->appManager->getAppVersion($app['id']);
  395. } else {
  396. $currentVersion = $app['releases'][0]['version'];
  397. }
  398. $formattedApps[] = [
  399. 'id' => $app['id'],
  400. 'app_api' => false,
  401. 'name' => $app['translations'][$currentLanguage]['name'] ?? $app['translations']['en']['name'],
  402. 'description' => $app['translations'][$currentLanguage]['description'] ?? $app['translations']['en']['description'],
  403. 'summary' => $app['translations'][$currentLanguage]['summary'] ?? $app['translations']['en']['summary'],
  404. 'license' => $app['releases'][0]['licenses'],
  405. 'author' => $authors,
  406. 'shipped' => $this->appManager->isShipped($app['id']),
  407. 'version' => $currentVersion,
  408. 'default_enable' => '',
  409. 'types' => [],
  410. 'documentation' => [
  411. 'admin' => $app['adminDocs'],
  412. 'user' => $app['userDocs'],
  413. 'developer' => $app['developerDocs']
  414. ],
  415. 'website' => $app['website'],
  416. 'bugs' => $app['issueTracker'],
  417. 'detailpage' => $app['website'],
  418. 'dependencies' => array_merge(
  419. $nextCloudVersionDependencies,
  420. $phpDependencies
  421. ),
  422. 'level' => ($app['isFeatured'] === true) ? 200 : 100,
  423. 'missingMaxOwnCloudVersion' => false,
  424. 'missingMinOwnCloudVersion' => false,
  425. 'canInstall' => true,
  426. 'screenshot' => isset($app['screenshots'][0]['url']) ? 'https://usercontent.apps.nextcloud.com/' . base64_encode($app['screenshots'][0]['url']) : '',
  427. 'score' => $app['ratingOverall'],
  428. 'ratingNumOverall' => $app['ratingNumOverall'],
  429. 'ratingNumThresholdReached' => $app['ratingNumOverall'] > 5,
  430. 'removable' => $existsLocally,
  431. 'active' => $this->appManager->isEnabledForUser($app['id']),
  432. 'needsDownload' => !$existsLocally,
  433. 'groups' => $groups,
  434. 'fromAppStore' => true,
  435. 'appstoreData' => $app,
  436. ];
  437. }
  438. return $formattedApps;
  439. }
  440. /**
  441. * @param string $appId
  442. * @param array $groups
  443. * @return JSONResponse
  444. */
  445. #[PasswordConfirmationRequired]
  446. public function enableApp(string $appId, array $groups = []): JSONResponse {
  447. return $this->enableApps([$appId], $groups);
  448. }
  449. /**
  450. * Enable one or more apps
  451. *
  452. * apps will be enabled for specific groups only if $groups is defined
  453. *
  454. * @param array $appIds
  455. * @param array $groups
  456. * @return JSONResponse
  457. */
  458. #[PasswordConfirmationRequired]
  459. public function enableApps(array $appIds, array $groups = []): JSONResponse {
  460. try {
  461. $updateRequired = false;
  462. foreach ($appIds as $appId) {
  463. $appId = $this->appManager->cleanAppId($appId);
  464. // Check if app is already downloaded
  465. /** @var Installer $installer */
  466. $installer = \OC::$server->get(Installer::class);
  467. $isDownloaded = $installer->isDownloaded($appId);
  468. if (!$isDownloaded) {
  469. $installer->downloadApp($appId);
  470. }
  471. $installer->installApp($appId);
  472. if (count($groups) > 0) {
  473. $this->appManager->enableAppForGroups($appId, $this->getGroupList($groups));
  474. } else {
  475. $this->appManager->enableApp($appId);
  476. }
  477. if (\OC_App::shouldUpgrade($appId)) {
  478. $updateRequired = true;
  479. }
  480. }
  481. return new JSONResponse(['data' => ['update_required' => $updateRequired]]);
  482. } catch (\Throwable $e) {
  483. $this->logger->error('could not enable apps', ['exception' => $e]);
  484. return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR);
  485. }
  486. }
  487. private function getGroupList(array $groups) {
  488. $groupManager = \OC::$server->getGroupManager();
  489. $groupsList = [];
  490. foreach ($groups as $group) {
  491. $groupItem = $groupManager->get($group);
  492. if ($groupItem instanceof IGroup) {
  493. $groupsList[] = $groupManager->get($group);
  494. }
  495. }
  496. return $groupsList;
  497. }
  498. /**
  499. * @param string $appId
  500. * @return JSONResponse
  501. */
  502. #[PasswordConfirmationRequired]
  503. public function disableApp(string $appId): JSONResponse {
  504. return $this->disableApps([$appId]);
  505. }
  506. /**
  507. * @param array $appIds
  508. * @return JSONResponse
  509. */
  510. #[PasswordConfirmationRequired]
  511. public function disableApps(array $appIds): JSONResponse {
  512. try {
  513. foreach ($appIds as $appId) {
  514. $appId = $this->appManager->cleanAppId($appId);
  515. $this->appManager->disableApp($appId);
  516. }
  517. return new JSONResponse([]);
  518. } catch (\Exception $e) {
  519. $this->logger->error('could not disable app', ['exception' => $e]);
  520. return new JSONResponse(['data' => ['message' => $e->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR);
  521. }
  522. }
  523. /**
  524. * @param string $appId
  525. * @return JSONResponse
  526. */
  527. #[PasswordConfirmationRequired]
  528. public function uninstallApp(string $appId): JSONResponse {
  529. $appId = $this->appManager->cleanAppId($appId);
  530. $result = $this->installer->removeApp($appId);
  531. if ($result !== false) {
  532. $this->appManager->clearAppsCache();
  533. return new JSONResponse(['data' => ['appid' => $appId]]);
  534. }
  535. return new JSONResponse(['data' => ['message' => $this->l10n->t('Could not remove app.')]], Http::STATUS_INTERNAL_SERVER_ERROR);
  536. }
  537. /**
  538. * @param string $appId
  539. * @return JSONResponse
  540. */
  541. public function updateApp(string $appId): JSONResponse {
  542. $appId = $this->appManager->cleanAppId($appId);
  543. $this->config->setSystemValue('maintenance', true);
  544. try {
  545. $result = $this->installer->updateAppstoreApp($appId);
  546. $this->config->setSystemValue('maintenance', false);
  547. } catch (\Exception $ex) {
  548. $this->config->setSystemValue('maintenance', false);
  549. return new JSONResponse(['data' => ['message' => $ex->getMessage()]], Http::STATUS_INTERNAL_SERVER_ERROR);
  550. }
  551. if ($result !== false) {
  552. return new JSONResponse(['data' => ['appid' => $appId]]);
  553. }
  554. return new JSONResponse(['data' => ['message' => $this->l10n->t('Could not update app.')]], Http::STATUS_INTERNAL_SERVER_ERROR);
  555. }
  556. private function sortApps($a, $b) {
  557. $a = (string)$a['name'];
  558. $b = (string)$b['name'];
  559. if ($a === $b) {
  560. return 0;
  561. }
  562. return ($a < $b) ? -1 : 1;
  563. }
  564. public function force(string $appId): JSONResponse {
  565. $appId = $this->appManager->cleanAppId($appId);
  566. $this->appManager->ignoreNextcloudRequirementForApp($appId);
  567. return new JSONResponse();
  568. }
  569. }