You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.1 KiB

  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\IntegrityCheck\Checker;
  9. use OC\Setup;
  10. use OCP\IInitialStateService;
  11. use OCP\IURLGenerator;
  12. use OCP\Server;
  13. use OCP\ServerVersion;
  14. use OCP\Template\ITemplateManager;
  15. use OCP\Util;
  16. use Psr\Log\LoggerInterface;
  17. class SetupController {
  18. private string $autoConfigFile;
  19. public function __construct(
  20. protected Setup $setupHelper,
  21. protected LoggerInterface $logger,
  22. protected ITemplateManager $templateManager,
  23. protected IInitialStateService $initialStateService,
  24. protected IURLGenerator $urlGenerator,
  25. protected ServerVersion $serverVersion,
  26. ) {
  27. $this->autoConfigFile = \OC::$configDir . 'autoconfig.php';
  28. }
  29. public function run(array $post): void {
  30. // Check for autosetup:
  31. $post = $this->loadAutoConfig($post);
  32. $opts = $this->setupHelper->getSystemInfo();
  33. // convert 'abcpassword' to 'abcpass'
  34. if (isset($post['adminpassword'])) {
  35. $post['adminpass'] = $post['adminpassword'];
  36. }
  37. if (isset($post['dbpassword'])) {
  38. $post['dbpass'] = $post['dbpassword'];
  39. }
  40. if (!$this->setupHelper->canInstallFileExists()) {
  41. $this->displaySetupForbidden();
  42. return;
  43. }
  44. if (isset($post['install']) && $post['install'] == 'true') {
  45. // We have to launch the installation process :
  46. $e = $this->setupHelper->install($post);
  47. $errors = ['errors' => $e];
  48. if (count($e) > 0) {
  49. $options = array_merge($opts, $post, $errors);
  50. $this->display($options);
  51. } else {
  52. $this->finishSetup();
  53. }
  54. } else {
  55. $options = array_merge($opts, $post);
  56. $this->display($options);
  57. }
  58. }
  59. private function displaySetupForbidden(): void {
  60. $this->templateManager->printGuestPage('', 'installation_forbidden');
  61. }
  62. public function display(array $post): void {
  63. $defaults = [
  64. 'adminlogin' => '',
  65. 'adminpass' => '',
  66. 'dbuser' => '',
  67. 'dbpass' => '',
  68. 'dbname' => '',
  69. 'dbtablespace' => '',
  70. 'dbhost' => 'localhost',
  71. 'dbtype' => '',
  72. 'hasAutoconfig' => false,
  73. 'serverRoot' => \OC::$SERVERROOT,
  74. 'version' => implode('.', $this->serverVersion->getVersion()),
  75. 'versionstring' => $this->serverVersion->getVersionString(),
  76. ];
  77. $parameters = array_merge($defaults, $post);
  78. Util::addStyle('server', null);
  79. // include common nextcloud webpack bundle
  80. Util::addScript('core', 'common');
  81. Util::addScript('core', 'main');
  82. Util::addScript('core', 'install');
  83. Util::addTranslations('core');
  84. $this->initialStateService->provideInitialState('core', 'config', $parameters);
  85. $this->initialStateService->provideInitialState('core', 'data', false);
  86. $this->initialStateService->provideInitialState('core', 'links', [
  87. 'adminInstall' => $this->urlGenerator->linkToDocs('admin-install'),
  88. 'adminSourceInstall' => $this->urlGenerator->linkToDocs('admin-source_install'),
  89. 'adminDBConfiguration' => $this->urlGenerator->linkToDocs('admin-db-configuration'),
  90. ]);
  91. $this->templateManager->printGuestPage('', 'installation');
  92. }
  93. private function finishSetup(): void {
  94. if (file_exists($this->autoConfigFile)) {
  95. unlink($this->autoConfigFile);
  96. }
  97. Server::get(Checker::class)->runInstanceVerification();
  98. if ($this->setupHelper->shouldRemoveCanInstallFile()) {
  99. $this->templateManager->printGuestPage('', 'installation_incomplete');
  100. }
  101. header('Location: ' . Server::get(IURLGenerator::class)->getAbsoluteURL('index.php/core/apps/recommended'));
  102. exit();
  103. }
  104. /**
  105. * @psalm-taint-escape file we trust file path given in POST for setup
  106. */
  107. public function loadAutoConfig(array $post): array {
  108. if (file_exists($this->autoConfigFile)) {
  109. $this->logger->info('Autoconfig file found, setting up Nextcloud…');
  110. $AUTOCONFIG = [];
  111. include $this->autoConfigFile;
  112. $post['hasAutoconfig'] = count($AUTOCONFIG) > 0;
  113. $post = array_merge($post, $AUTOCONFIG);
  114. }
  115. $dbIsSet = isset($post['dbtype']);
  116. $directoryIsSet = isset($post['directory']);
  117. $adminAccountIsSet = isset($post['adminlogin']);
  118. if ($dbIsSet && $directoryIsSet && $adminAccountIsSet) {
  119. $post['install'] = 'true';
  120. }
  121. return $post;
  122. }
  123. }