1
0

APIController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UpdateNotification\Controller;
  26. use OC\App\AppStore\Fetcher\AppFetcher;
  27. use OCP\App\AppPathNotFoundException;
  28. use OCP\App\IAppManager;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCSController;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. use OCP\IUserSession;
  35. use OCP\L10N\IFactory;
  36. class APIController extends OCSController {
  37. /** @var IConfig */
  38. protected $config;
  39. /** @var IAppManager */
  40. protected $appManager;
  41. /** @var AppFetcher */
  42. protected $appFetcher;
  43. /** @var IFactory */
  44. protected $l10nFactory;
  45. /** @var IUserSession */
  46. protected $userSession;
  47. /** @var string */
  48. protected $language;
  49. /**
  50. * List of apps that were in the appstore but are now shipped and don't have
  51. * a compatible update available.
  52. *
  53. * @var array<string, int>
  54. */
  55. protected array $appsShippedInFutureVersion = [
  56. 'bruteforcesettings' => 25,
  57. 'suspicious_login' => 25,
  58. 'twofactor_totp' => 25,
  59. ];
  60. public function __construct(string $appName,
  61. IRequest $request,
  62. IConfig $config,
  63. IAppManager $appManager,
  64. AppFetcher $appFetcher,
  65. IFactory $l10nFactory,
  66. IUserSession $userSession) {
  67. parent::__construct($appName, $request);
  68. $this->config = $config;
  69. $this->appManager = $appManager;
  70. $this->appFetcher = $appFetcher;
  71. $this->l10nFactory = $l10nFactory;
  72. $this->userSession = $userSession;
  73. }
  74. /**
  75. * @param string $newVersion
  76. * @return DataResponse
  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. $missing = array_diff($installedApps, $availableApps);
  113. $missing = array_map([$this, 'getAppDetails'], $missing);
  114. sort($missing);
  115. $available = array_intersect($installedApps, $availableApps);
  116. $available = array_map([$this, 'getAppDetails'], $available);
  117. sort($available);
  118. return new DataResponse([
  119. 'missing' => $missing,
  120. 'available' => $available,
  121. ]);
  122. }
  123. /**
  124. * Get translated app name
  125. *
  126. * @param string $appId
  127. * @return string[]
  128. */
  129. protected function getAppDetails(string $appId): array {
  130. $app = $this->appManager->getAppInfo($appId, false, $this->language);
  131. return [
  132. 'appId' => $appId,
  133. 'appName' => $app['name'] ?? $appId,
  134. ];
  135. }
  136. }