APIController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\UpdateNotification\Controller;
  27. use OC\App\AppStore\Fetcher\AppFetcher;
  28. use OCA\UpdateNotification\Manager;
  29. use OCA\UpdateNotification\ResponseDefinitions;
  30. use OCP\App\AppPathNotFoundException;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\IConfig;
  36. use OCP\IRequest;
  37. use OCP\IUserSession;
  38. use OCP\L10N\IFactory;
  39. /**
  40. * @psalm-import-type UpdateNotificationApp from ResponseDefinitions
  41. */
  42. class APIController extends OCSController {
  43. /** @var string */
  44. protected $language;
  45. /**
  46. * List of apps that were in the appstore but are now shipped and don't have
  47. * a compatible update available.
  48. *
  49. * @var array<string, int>
  50. */
  51. protected array $appsShippedInFutureVersion = [
  52. 'bruteforcesettings' => 25,
  53. 'suspicious_login' => 25,
  54. 'twofactor_totp' => 25,
  55. 'files_downloadlimit' => 29,
  56. 'twofactor_nextcloud_notification' => 30,
  57. 'app_api' => 30,
  58. ];
  59. public function __construct(
  60. string $appName,
  61. IRequest $request,
  62. protected IConfig $config,
  63. protected IAppManager $appManager,
  64. protected AppFetcher $appFetcher,
  65. protected IFactory $l10nFactory,
  66. protected IUserSession $userSession,
  67. protected Manager $manager,
  68. ) {
  69. parent::__construct($appName, $request);
  70. }
  71. /**
  72. * List available updates for apps
  73. *
  74. * @param string $newVersion Server version to check updates for
  75. *
  76. * @return DataResponse<Http::STATUS_OK, array{missing: UpdateNotificationApp[], available: UpdateNotificationApp[]}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{appstore_disabled: bool, already_on_latest?: bool}, array{}>
  77. *
  78. * 200: Apps returned
  79. * 404: New versions not found
  80. */
  81. public function getAppList(string $newVersion): DataResponse {
  82. if (!$this->config->getSystemValue('appstoreenabled', true)) {
  83. return new DataResponse([
  84. 'appstore_disabled' => true,
  85. ], Http::STATUS_NOT_FOUND);
  86. }
  87. // Get list of installed custom apps
  88. $installedApps = $this->appManager->getInstalledApps();
  89. $installedApps = array_filter($installedApps, function ($app) {
  90. try {
  91. $this->appManager->getAppPath($app);
  92. } catch (AppPathNotFoundException $e) {
  93. return false;
  94. }
  95. return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]);
  96. });
  97. if (empty($installedApps)) {
  98. return new DataResponse([
  99. 'missing' => [],
  100. 'available' => [],
  101. ]);
  102. }
  103. $this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
  104. // Apps available on the app store for that version
  105. $availableApps = array_map(static function (array $app) {
  106. return $app['id'];
  107. }, $this->appFetcher->get());
  108. if (empty($availableApps)) {
  109. return new DataResponse([
  110. 'appstore_disabled' => false,
  111. 'already_on_latest' => false,
  112. ], Http::STATUS_NOT_FOUND);
  113. }
  114. $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
  115. // Ignore apps that are deployed from git
  116. $installedApps = array_filter($installedApps, function (string $appId) {
  117. try {
  118. return !file_exists($this->appManager->getAppPath($appId) . '/.git');
  119. } catch (AppPathNotFoundException $e) {
  120. return true;
  121. }
  122. });
  123. $missing = array_diff($installedApps, $availableApps);
  124. $missing = array_map([$this, 'getAppDetails'], $missing);
  125. sort($missing);
  126. $available = array_intersect($installedApps, $availableApps);
  127. $available = array_map([$this, 'getAppDetails'], $available);
  128. sort($available);
  129. return new DataResponse([
  130. 'missing' => $missing,
  131. 'available' => $available,
  132. ]);
  133. }
  134. /**
  135. * Get translated app name
  136. *
  137. * @param string $appId
  138. * @return UpdateNotificationApp
  139. */
  140. protected function getAppDetails(string $appId): array {
  141. $app = $this->appManager->getAppInfo($appId, false, $this->language);
  142. /** @var ?string $name */
  143. $name = $app['name'];
  144. return [
  145. 'appId' => $appId,
  146. 'appName' => $name ?? $appId,
  147. ];
  148. }
  149. /**
  150. * Get changelog entry for an app
  151. *
  152. * @param string $appId App to search changelog entry for
  153. * @param string|null $version The version to search the changelog entry for (defaults to the latest installed)
  154. *
  155. * @return DataResponse<Http::STATUS_OK, array{appName: string, content: string, version: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
  156. *
  157. * 200: Changelog entry returned
  158. * 404: No changelog found
  159. */
  160. public function getAppChangelogEntry(string $appId, ?string $version = null): DataResponse {
  161. $version = $version ?? $this->appManager->getAppVersion($appId);
  162. $changes = $this->manager->getChangelog($appId, $version);
  163. if ($changes === null) {
  164. return new DataResponse([], Http::STATUS_NOT_FOUND);
  165. }
  166. // Remove version headline
  167. /** @var string[] */
  168. $changes = explode("\n", $changes, 2);
  169. $changes = trim(end($changes));
  170. // Get app info for localized app name
  171. $info = $this->appManager->getAppInfo($appId) ?? [];
  172. /** @var string */
  173. $appName = $info['name'] ?? $appId;
  174. return new DataResponse([
  175. 'appName' => $appName,
  176. 'content' => $changes,
  177. 'version' => $version,
  178. ]);
  179. }
  180. }