CheckSetupController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 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\AppFramework\Http;
  9. use OC\IntegrityCheck\Checker;
  10. use OCA\Settings\Settings\Admin\Overview;
  11. use OCP\AppFramework\Controller;
  12. use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
  13. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  15. use OCP\AppFramework\Http\Attribute\OpenAPI;
  16. use OCP\AppFramework\Http\DataDisplayResponse;
  17. use OCP\AppFramework\Http\DataResponse;
  18. use OCP\AppFramework\Http\RedirectResponse;
  19. use OCP\IConfig;
  20. use OCP\IL10N;
  21. use OCP\IRequest;
  22. use OCP\IURLGenerator;
  23. use OCP\SetupCheck\ISetupCheckManager;
  24. use Psr\Log\LoggerInterface;
  25. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  26. class CheckSetupController extends Controller {
  27. /** @var IConfig */
  28. private $config;
  29. /** @var IURLGenerator */
  30. private $urlGenerator;
  31. /** @var IL10N */
  32. private $l10n;
  33. /** @var Checker */
  34. private $checker;
  35. /** @var LoggerInterface */
  36. private $logger;
  37. private ISetupCheckManager $setupCheckManager;
  38. public function __construct($AppName,
  39. IRequest $request,
  40. IConfig $config,
  41. IURLGenerator $urlGenerator,
  42. IL10N $l10n,
  43. Checker $checker,
  44. LoggerInterface $logger,
  45. ISetupCheckManager $setupCheckManager,
  46. ) {
  47. parent::__construct($AppName, $request);
  48. $this->config = $config;
  49. $this->urlGenerator = $urlGenerator;
  50. $this->l10n = $l10n;
  51. $this->checker = $checker;
  52. $this->logger = $logger;
  53. $this->setupCheckManager = $setupCheckManager;
  54. }
  55. /**
  56. * @return DataResponse
  57. */
  58. #[NoCSRFRequired]
  59. #[NoAdminRequired]
  60. public function setupCheckManager(): DataResponse {
  61. return new DataResponse($this->setupCheckManager->runAll());
  62. }
  63. /**
  64. * @return RedirectResponse
  65. */
  66. #[NoCSRFRequired]
  67. #[AuthorizedAdminSetting(settings: Overview::class)]
  68. public function rescanFailedIntegrityCheck(): RedirectResponse {
  69. $this->checker->runInstanceVerification();
  70. return new RedirectResponse(
  71. $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'overview'])
  72. );
  73. }
  74. #[NoCSRFRequired]
  75. #[AuthorizedAdminSetting(settings: Overview::class)]
  76. public function getFailedIntegrityCheckFiles(): DataDisplayResponse {
  77. if (!$this->checker->isCodeCheckEnforced()) {
  78. return new DataDisplayResponse('Integrity checker has been disabled. Integrity cannot be verified.');
  79. }
  80. $completeResults = $this->checker->getResults();
  81. if ($completeResults === null) {
  82. return new DataDisplayResponse('Integrity checker has not been run. Integrity information not available.');
  83. }
  84. if (!empty($completeResults)) {
  85. $formattedTextResponse = 'Technical information
  86. =====================
  87. The following list covers which files have failed the integrity check. Please read
  88. the previous linked documentation to learn more about the errors and how to fix
  89. them.
  90. Results
  91. =======
  92. ';
  93. foreach ($completeResults as $context => $contextResult) {
  94. $formattedTextResponse .= "- $context\n";
  95. foreach ($contextResult as $category => $result) {
  96. $formattedTextResponse .= "\t- $category\n";
  97. if ($category !== 'EXCEPTION') {
  98. foreach ($result as $key => $results) {
  99. $formattedTextResponse .= "\t\t- $key\n";
  100. }
  101. } else {
  102. foreach ($result as $key => $results) {
  103. $formattedTextResponse .= "\t\t- $results\n";
  104. }
  105. }
  106. }
  107. }
  108. $formattedTextResponse .= '
  109. Raw output
  110. ==========
  111. ';
  112. $formattedTextResponse .= print_r($completeResults, true);
  113. } else {
  114. $formattedTextResponse = 'No errors have been found.';
  115. }
  116. return new DataDisplayResponse(
  117. $formattedTextResponse,
  118. Http::STATUS_OK,
  119. [
  120. 'Content-Type' => 'text/plain',
  121. ]
  122. );
  123. }
  124. /**
  125. * @return DataResponse
  126. */
  127. #[AuthorizedAdminSetting(settings: Overview::class)]
  128. public function check() {
  129. return new DataResponse(
  130. [
  131. 'generic' => $this->setupCheckManager->runAll(),
  132. ]
  133. );
  134. }
  135. }