AppSettingsController.php 20 KB

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