793ec21719bc8d398c4ab5e78cfe82087cb0a249
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / Step / DatabaseSelect.php
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action\Step;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Install\Controller\Action;
28
29 /**
30 * Database select step.
31 * This step is only rendered if database is mysql. With dbal,
32 * database name is submitted by previous step already.
33 */
34 class DatabaseSelect extends Action\AbstractAction implements StepInterface {
35
36 /**
37 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
38 */
39 protected $databaseConnection = NULL;
40
41 /**
42 * Create database if needed, save selected db name in configuration
43 *
44 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
45 */
46 public function execute() {
47 $result = array();
48 $this->initializeDatabaseConnection();
49 $postValues = $this->postValues['values'];
50 $localConfigurationPathValuePairs = array();
51 if ($postValues['type'] === 'new') {
52 $newDatabaseName = $postValues['new'];
53 if (strlen($newDatabaseName) <= 50) {
54 $createDatabaseResult = $this->databaseConnection->admin_query('CREATE DATABASE ' . $newDatabaseName . ' CHARACTER SET utf8');
55 if ($createDatabaseResult) {
56 $localConfigurationPathValuePairs['DB/database'] = $newDatabaseName;
57 } else {
58 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
59 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
60 $errorStatus->setTitle('Unable to create database');
61 $errorStatus->setMessage(
62 'Database with name ' . $newDatabaseName . ' could not be created.' .
63 ' Either your database name contains special chars (only alphanumeric characters are allowed)' .
64 ' or your database user probably has no sufficient permissions to create it.' .
65 ' Please choose an existing (empty) database or contact administration.'
66 );
67 $result[] = $errorStatus;
68 }
69 } else {
70 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
71 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
72 $errorStatus->setTitle('Database name not valid');
73 $errorStatus->setMessage('Given database name must be shorter than fifty characters.');
74 $result[] = $errorStatus;
75 }
76 } elseif ($postValues['type'] === 'existing') {
77 $localConfigurationPathValuePairs['DB/database'] = $postValues['existing'];
78 }
79
80 if (!empty($localConfigurationPathValuePairs)) {
81 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
82 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
83 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
84 }
85
86 return $result;
87 }
88
89 /**
90 * Step needs to be executed if database is not set or can
91 * not be selected.
92 *
93 * @return boolean
94 */
95 public function needsExecution() {
96 $this->initializeDatabaseConnection();
97 $result = TRUE;
98 if (strlen($GLOBALS['TYPO3_CONF_VARS']['DB']['database']) > 0) {
99 $this->databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
100 try {
101 $selectResult = $this->databaseConnection->sql_select_db();
102 if ($selectResult === TRUE) {
103 $result = FALSE;
104 }
105 } catch (\RuntimeException $e) {
106 }
107 }
108 return $result;
109 }
110
111 /**
112 * Render this step
113 *
114 * @return string
115 */
116 public function handle() {
117 $this->initializeHandle();
118 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
119 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
120 $isInitialInstallationInProgress = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
121 $this->view->assign('databaseList', $this->getDatabaseList($isInitialInstallationInProgress));
122 $this->view->assign('isInitialInstallationInProgress', $isInitialInstallationInProgress);
123 return $this->view->render();
124 }
125
126 /**
127 * Returns list of available databases (with access-check based on username/password)
128 *
129 * @param boolean $initialInstallation TRUE if first installation is in progress, FALSE if upgrading or usual access
130 * @return array List of available databases
131 */
132 protected function getDatabaseList($initialInstallation) {
133 $this->initializeDatabaseConnection();
134 $databaseArray = $this->databaseConnection->admin_get_dbs();
135 // Remove mysql organizational tables from database list
136 $reservedDatabaseNames = array('mysql', 'information_schema', 'performance_schema');
137 $allPossibleDatabases = array_diff($databaseArray, $reservedDatabaseNames);
138
139 // If we are upgrading we show *all* databases the user has access to
140 if ($initialInstallation === FALSE) {
141 return $allPossibleDatabases;
142 } else {
143 // In first installation we only show empty databases (no tables)
144 $databasesWithoutTables = array();
145 foreach ($allPossibleDatabases as $database) {
146 $this->databaseConnection->setDatabaseName($database);
147 $this->databaseConnection->sql_select_db();
148 $existingTables = $this->databaseConnection->admin_get_tables();
149 if (count($existingTables) === 0) {
150 $databasesWithoutTables[] = $database;
151 }
152 }
153 return $databasesWithoutTables;
154 }
155 }
156
157 /**
158 * Initialize database connection
159 *
160 * @return void
161 */
162 protected function initializeDatabaseConnection() {
163 $this->databaseConnection = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
164 $this->databaseConnection->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
165 $this->databaseConnection->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
166 $this->databaseConnection->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
167 $this->databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
168 $this->databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
169 $this->databaseConnection->sql_pconnect();
170 }
171 }