1
0

APIController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UpdateNotification\Controller;
  8. use OC\App\AppStore\Fetcher\AppFetcher;
  9. use OCA\UpdateNotification\Manager;
  10. use OCA\UpdateNotification\ResponseDefinitions;
  11. use OCP\App\AppPathNotFoundException;
  12. use OCP\App\IAppManager;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\IConfig;
  17. use OCP\IRequest;
  18. use OCP\IUserSession;
  19. use OCP\L10N\IFactory;
  20. /**
  21. * @psalm-import-type UpdateNotificationApp from ResponseDefinitions
  22. */
  23. class APIController extends OCSController {
  24. /** @var string */
  25. protected $language;
  26. /**
  27. * List of apps that were in the appstore but are now shipped and don't have
  28. * a compatible update available.
  29. *
  30. * @var array<string, int>
  31. */
  32. protected array $appsShippedInFutureVersion = [
  33. 'bruteforcesettings' => 25,
  34. 'suspicious_login' => 25,
  35. 'twofactor_totp' => 25,
  36. 'files_downloadlimit' => 29,
  37. 'twofactor_nextcloud_notification' => 30,
  38. 'app_api' => 30,
  39. ];
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. protected IConfig $config,
  44. protected IAppManager $appManager,
  45. protected AppFetcher $appFetcher,
  46. protected IFactory $l10nFactory,
  47. protected IUserSession $userSession,
  48. protected Manager $manager,
  49. ) {
  50. parent::__construct($appName, $request);
  51. }
  52. /**
  53. * List available updates for apps
  54. *
  55. * @param string $newVersion Server version to check updates for
  56. *
  57. * @return DataResponse<Http::STATUS_OK, array{missing: list<UpdateNotificationApp>, available: list<UpdateNotificationApp>}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{appstore_disabled: bool, already_on_latest?: bool}, array{}>
  58. *
  59. * 200: Apps returned
  60. * 404: New versions not found
  61. */
  62. public function getAppList(string $newVersion): DataResponse {
  63. if (!$this->config->getSystemValue('appstoreenabled', true)) {
  64. return new DataResponse([
  65. 'appstore_disabled' => true,
  66. ], Http::STATUS_NOT_FOUND);
  67. }
  68. // Get list of installed custom apps
  69. $installedApps = $this->appManager->getInstalledApps();
  70. $installedApps = array_filter($installedApps, function ($app) {
  71. try {
  72. $this->appManager->getAppPath($app);
  73. } catch (AppPathNotFoundException $e) {
  74. return false;
  75. }
  76. return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]);
  77. });
  78. if (empty($installedApps)) {
  79. return new DataResponse([
  80. 'missing' => [],
  81. 'available' => [],
  82. ]);
  83. }
  84. $this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
  85. // Apps available on the app store for that version
  86. $availableApps = array_map(static function (array $app) {
  87. return $app['id'];
  88. }, $this->appFetcher->get());
  89. if (empty($availableApps)) {
  90. return new DataResponse([
  91. 'appstore_disabled' => false,
  92. 'already_on_latest' => false,
  93. ], Http::STATUS_NOT_FOUND);
  94. }
  95. $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
  96. // Ignore apps that are deployed from git
  97. $installedApps = array_filter($installedApps, function (string $appId) {
  98. try {
  99. return !file_exists($this->appManager->getAppPath($appId) . '/.git');
  100. } catch (AppPathNotFoundException $e) {
  101. return true;
  102. }
  103. });
  104. $missing = array_diff($installedApps, $availableApps);
  105. $missing = array_map([$this, 'getAppDetails'], $missing);
  106. sort($missing);
  107. $available = array_intersect($installedApps, $availableApps);
  108. $available = array_map([$this, 'getAppDetails'], $available);
  109. sort($available);
  110. return new DataResponse([
  111. 'missing' => $missing,
  112. 'available' => $available,
  113. ]);
  114. }
  115. /**
  116. * Get translated app name
  117. *
  118. * @param string $appId
  119. * @return UpdateNotificationApp
  120. */
  121. protected function getAppDetails(string $appId): array {
  122. $app = $this->appManager->getAppInfo($appId, false, $this->language);
  123. /** @var ?string $name */
  124. $name = $app['name'];
  125. return [
  126. 'appId' => $appId,
  127. 'appName' => $name ?? $appId,
  128. ];
  129. }
  130. /**
  131. * Get changelog entry for an app
  132. *
  133. * @param string $appId App to search changelog entry for
  134. * @param string|null $version The version to search the changelog entry for (defaults to the latest installed)
  135. *
  136. * @return DataResponse<Http::STATUS_OK, array{appName: string, content: string, version: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
  137. *
  138. * 200: Changelog entry returned
  139. * 404: No changelog found
  140. */
  141. public function getAppChangelogEntry(string $appId, ?string $version = null): DataResponse {
  142. $version = $version ?? $this->appManager->getAppVersion($appId);
  143. $changes = $this->manager->getChangelog($appId, $version);
  144. if ($changes === null) {
  145. return new DataResponse([], Http::STATUS_NOT_FOUND);
  146. }
  147. // Remove version headline
  148. /** @var string[] */
  149. $changes = explode("\n", $changes, 2);
  150. $changes = trim(end($changes));
  151. // Get app info for localized app name
  152. $info = $this->appManager->getAppInfo($appId) ?? [];
  153. /** @var string */
  154. $appName = $info['name'] ?? $appId;
  155. return new DataResponse([
  156. 'appName' => $appName,
  157. 'content' => $changes,
  158. 'version' => $version,
  159. ]);
  160. }
  161. }