CheckSetupController.php 3.6 KB

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