[BUGFIX] Support mysql socket connection
[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->initialize();
118 $this->view->assign('databaseList', $this->getDatabaseList());
119 return $this->view->render();
120 }
121
122 /**
123 * Returns list of available databases (with access-check based on username/password)
124 *
125 * @return array List of available databases
126 */
127 protected function getDatabaseList() {
128 $this->initializeDatabaseConnection();
129 $databaseArray = $this->databaseConnection->admin_get_dbs();
130 // Remove mysql organizational tables from database list
131 $reservedDatabaseNames = array('mysql', 'information_schema', 'performance_schema');
132 $allPossibleDatabases = array_diff($databaseArray, $reservedDatabaseNames);
133 $databasesWithoutTables = array();
134 foreach ($allPossibleDatabases as $database) {
135 $this->databaseConnection->setDatabaseName($database);
136 $this->databaseConnection->sql_select_db();
137 $existingTables = $this->databaseConnection->admin_get_tables();
138 if (count($existingTables) === 0) {
139 $databasesWithoutTables[] = $database;
140 }
141 }
142 return $databasesWithoutTables;
143 }
144
145 /**
146 * Initialize database connection
147 *
148 * @return void
149 */
150 protected function initializeDatabaseConnection() {
151 $this->databaseConnection = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
152 $this->databaseConnection->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
153 $this->databaseConnection->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
154 $this->databaseConnection->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
155 $this->databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
156 $this->databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
157 $this->databaseConnection->sql_pconnect();
158 }
159 }
160 ?>