SetupController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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\ILogger;
  35. class SetupController {
  36. private string $autoConfigFile;
  37. public function __construct(
  38. protected Setup $setupHelper,
  39. ) {
  40. $this->autoConfigFile = \OC::$configDir.'autoconfig.php';
  41. }
  42. public function run(array $post): void {
  43. // Check for autosetup:
  44. $post = $this->loadAutoConfig($post);
  45. $opts = $this->setupHelper->getSystemInfo();
  46. // convert 'abcpassword' to 'abcpass'
  47. if (isset($post['adminpassword'])) {
  48. $post['adminpass'] = $post['adminpassword'];
  49. }
  50. if (isset($post['dbpassword'])) {
  51. $post['dbpass'] = $post['dbpassword'];
  52. }
  53. if (!$this->setupHelper->canInstallFileExists()) {
  54. $this->displaySetupForbidden();
  55. return;
  56. }
  57. if (isset($post['install']) and $post['install'] == 'true') {
  58. // We have to launch the installation process :
  59. $e = $this->setupHelper->install($post);
  60. $errors = ['errors' => $e];
  61. if (count($e) > 0) {
  62. $options = array_merge($opts, $post, $errors);
  63. $this->display($options);
  64. } else {
  65. $this->finishSetup();
  66. }
  67. } else {
  68. $options = array_merge($opts, $post);
  69. $this->display($options);
  70. }
  71. }
  72. private function displaySetupForbidden() {
  73. \OC_Template::printGuestPage('', 'installation_forbidden');
  74. }
  75. public function display($post): void {
  76. $defaults = [
  77. 'adminlogin' => '',
  78. 'adminpass' => '',
  79. 'dbuser' => '',
  80. 'dbpass' => '',
  81. 'dbname' => '',
  82. 'dbtablespace' => '',
  83. 'dbhost' => 'localhost',
  84. 'dbtype' => '',
  85. ];
  86. $parameters = array_merge($defaults, $post);
  87. \OC_Template::printGuestPage('', 'installation', $parameters);
  88. }
  89. private function finishSetup() {
  90. if (file_exists($this->autoConfigFile)) {
  91. unlink($this->autoConfigFile);
  92. }
  93. \OC::$server->getIntegrityCodeChecker()->runInstanceVerification();
  94. if ($this->setupHelper->shouldRemoveCanInstallFile()) {
  95. \OC_Template::printGuestPage('', 'installation_incomplete');
  96. }
  97. header('Location: ' . \OC::$server->getURLGenerator()->getAbsoluteURL('index.php/core/apps/recommended'));
  98. exit();
  99. }
  100. public function loadAutoConfig(array $post): array {
  101. if (file_exists($this->autoConfigFile)) {
  102. \OCP\Util::writeLog('core', 'Autoconfig file found, setting up Nextcloud…', ILogger::INFO);
  103. $AUTOCONFIG = [];
  104. include $this->autoConfigFile;
  105. $post = array_merge($post, $AUTOCONFIG);
  106. }
  107. $dbIsSet = isset($post['dbtype']);
  108. $directoryIsSet = isset($post['directory']);
  109. $adminAccountIsSet = isset($post['adminlogin']);
  110. if ($dbIsSet and $directoryIsSet and $adminAccountIsSet) {
  111. $post['install'] = 'true';
  112. }
  113. $post['dbIsSet'] = $dbIsSet;
  114. $post['directoryIsSet'] = $directoryIsSet;
  115. return $post;
  116. }
  117. }