CheckSetupController.php 3.8 KB

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