1
0

SetupController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Damjan Georgievski <gdamjan@gmail.com>
  9. * @author ideaship <ideaship@users.noreply.github.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Core\Controller;
  33. use OC\Setup;
  34. use OCP\Util;
  35. use Psr\Log\LoggerInterface;
  36. class SetupController {
  37. private string $autoConfigFile;
  38. public function __construct(
  39. protected Setup $setupHelper,
  40. protected LoggerInterface $logger,
  41. ) {
  42. $this->autoConfigFile = \OC::$configDir.'autoconfig.php';
  43. }
  44. public function run(array $post): void {
  45. // Check for autosetup:
  46. $post = $this->loadAutoConfig($post);
  47. $opts = $this->setupHelper->getSystemInfo();
  48. // convert 'abcpassword' to 'abcpass'
  49. if (isset($post['adminpassword'])) {
  50. $post['adminpass'] = $post['adminpassword'];
  51. }
  52. if (isset($post['dbpassword'])) {
  53. $post['dbpass'] = $post['dbpassword'];
  54. }
  55. if (!$this->setupHelper->canInstallFileExists()) {
  56. $this->displaySetupForbidden();
  57. return;
  58. }
  59. if (isset($post['install']) and $post['install'] == 'true') {
  60. // We have to launch the installation process :
  61. $e = $this->setupHelper->install($post);
  62. $errors = ['errors' => $e];
  63. if (count($e) > 0) {
  64. $options = array_merge($opts, $post, $errors);
  65. $this->display($options);
  66. } else {
  67. $this->finishSetup();
  68. }
  69. } else {
  70. $options = array_merge($opts, $post);
  71. $this->display($options);
  72. }
  73. }
  74. private function displaySetupForbidden(): void {
  75. \OC_Template::printGuestPage('', 'installation_forbidden');
  76. }
  77. public function display($post): void {
  78. $defaults = [
  79. 'adminlogin' => '',
  80. 'adminpass' => '',
  81. 'dbuser' => '',
  82. 'dbpass' => '',
  83. 'dbname' => '',
  84. 'dbtablespace' => '',
  85. 'dbhost' => 'localhost',
  86. 'dbtype' => '',
  87. ];
  88. $parameters = array_merge($defaults, $post);
  89. Util::addStyle('server', null);
  90. // include common nextcloud webpack bundle
  91. Util::addScript('core', 'common');
  92. Util::addScript('core', 'main');
  93. Util::addTranslations('core');
  94. \OC_Template::printGuestPage('', 'installation', $parameters);
  95. }
  96. private function finishSetup(): void {
  97. if (file_exists($this->autoConfigFile)) {
  98. unlink($this->autoConfigFile);
  99. }
  100. \OC::$server->getIntegrityCodeChecker()->runInstanceVerification();
  101. if ($this->setupHelper->shouldRemoveCanInstallFile()) {
  102. \OC_Template::printGuestPage('', 'installation_incomplete');
  103. }
  104. header('Location: ' . \OC::$server->getURLGenerator()->getAbsoluteURL('index.php/core/apps/recommended'));
  105. exit();
  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 = array_merge($post, $AUTOCONFIG);
  113. }
  114. $dbIsSet = isset($post['dbtype']);
  115. $directoryIsSet = isset($post['directory']);
  116. $adminAccountIsSet = isset($post['adminlogin']);
  117. if ($dbIsSet and $directoryIsSet and $adminAccountIsSet) {
  118. $post['install'] = 'true';
  119. }
  120. $post['dbIsSet'] = $dbIsSet;
  121. $post['directoryIsSet'] = $directoryIsSet;
  122. return $post;
  123. }
  124. }