APIController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ];
  56. public function __construct(
  57. string $appName,
  58. IRequest $request,
  59. protected IConfig $config,
  60. protected IAppManager $appManager,
  61. protected AppFetcher $appFetcher,
  62. protected IFactory $l10nFactory,
  63. protected IUserSession $userSession,
  64. protected Manager $manager,
  65. ) {
  66. parent::__construct($appName, $request);
  67. }
  68. /**
  69. * List available updates for apps
  70. *
  71. * @param string $newVersion Server version to check updates for
  72. *
  73. * @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{}>
  74. *
  75. * 200: Apps returned
  76. * 404: New versions not found
  77. */
  78. public function getAppList(string $newVersion): DataResponse {
  79. if (!$this->config->getSystemValue('appstoreenabled', true)) {
  80. return new DataResponse([
  81. 'appstore_disabled' => true,
  82. ], Http::STATUS_NOT_FOUND);
  83. }
  84. // Get list of installed custom apps
  85. $installedApps = $this->appManager->getInstalledApps();
  86. $installedApps = array_filter($installedApps, function ($app) {
  87. try {
  88. $this->appManager->getAppPath($app);
  89. } catch (AppPathNotFoundException $e) {
  90. return false;
  91. }
  92. return !$this->appManager->isShipped($app) && !isset($this->appsShippedInFutureVersion[$app]);
  93. });
  94. if (empty($installedApps)) {
  95. return new DataResponse([
  96. 'missing' => [],
  97. 'available' => [],
  98. ]);
  99. }
  100. $this->appFetcher->setVersion($newVersion, 'future-apps.json', false);
  101. // Apps available on the app store for that version
  102. $availableApps = array_map(static function (array $app) {
  103. return $app['id'];
  104. }, $this->appFetcher->get());
  105. if (empty($availableApps)) {
  106. return new DataResponse([
  107. 'appstore_disabled' => false,
  108. 'already_on_latest' => false,
  109. ], Http::STATUS_NOT_FOUND);
  110. }
  111. $this->language = $this->l10nFactory->getUserLanguage($this->userSession->getUser());
  112. // Ignore apps that are deployed from git
  113. $installedApps = array_filter($installedApps, function (string $appId) {
  114. try {
  115. return !file_exists($this->appManager->getAppPath($appId) . '/.git');
  116. } catch (AppPathNotFoundException $e) {
  117. return true;
  118. }
  119. });
  120. $missing = array_diff($installedApps, $availableApps);
  121. $missing = array_map([$this, 'getAppDetails'], $missing);
  122. sort($missing);
  123. $available = array_intersect($installedApps, $availableApps);
  124. $available = array_map([$this, 'getAppDetails'], $available);
  125. sort($available);
  126. return new DataResponse([
  127. 'missing' => $missing,
  128. 'available' => $available,
  129. ]);
  130. }
  131. /**
  132. * Get translated app name
  133. *
  134. * @param string $appId
  135. * @return UpdateNotificationApp
  136. */
  137. protected function getAppDetails(string $appId): array {
  138. $app = $this->appManager->getAppInfo($appId, false, $this->language);
  139. /** @var ?string $name */
  140. $name = $app['name'];
  141. return [
  142. 'appId' => $appId,
  143. 'appName' => $name ?? $appId,
  144. ];
  145. }
  146. /**
  147. * Get changelog entry for an app
  148. *
  149. * @param string $appId App to search changelog entry for
  150. * @param string|null $version The version to search the changelog entry for (defaults to the latest installed)
  151. *
  152. * @return DataResponse<Http::STATUS_OK, array{appName: string, content: string, version: string}, array{}>|DataResponse<Http::STATUS_NOT_FOUND, array{}, array{}>
  153. *
  154. * 200: Changelog entry returned
  155. * 404: No changelog found
  156. */
  157. public function getAppChangelogEntry(string $appId, ?string $version = null): DataResponse {
  158. $version = $version ?? $this->appManager->getAppVersion($appId);
  159. $changes = $this->manager->getChangelog($appId, $version);
  160. if ($changes === null) {
  161. return new DataResponse([], Http::STATUS_NOT_FOUND);
  162. }
  163. // Remove version headline
  164. /** @var string[] */
  165. $changes = explode("\n", $changes, 2);
  166. $changes = trim(end($changes));
  167. // Get app info for localized app name
  168. $info = $this->appManager->getAppInfo($appId) ?? [];
  169. /** @var string */
  170. $appName = $info['name'] ?? $appId;
  171. return new DataResponse([
  172. 'appName' => $appName,
  173. 'content' => $changes,
  174. 'version' => $version,
  175. ]);
  176. }
  177. }