SetupController.php 4.3 KB

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