TYPO3  7.6
DatabaseSelect.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action\Step;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 
24 {
28  protected $databaseConnection = null;
29 
35  public function execute()
36  {
37  $result = array();
39  $postValues = $this->postValues['values'];
40  $localConfigurationPathValuePairs = array();
42  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
43  if ($postValues['type'] === 'new') {
44  $newDatabaseName = $postValues['new'];
45  if ($this->isValidDatabaseName($newDatabaseName)) {
46  $createDatabaseResult = $this->databaseConnection->admin_query('CREATE DATABASE ' . $newDatabaseName . ' CHARACTER SET utf8');
47  if ($createDatabaseResult) {
48  $localConfigurationPathValuePairs['DB/database'] = $newDatabaseName;
49  } else {
51  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
52  $errorStatus->setTitle('Unable to create database');
53  $errorStatus->setMessage(
54  'Database with name ' . $newDatabaseName . ' could not be created.' .
55  ' Either your database name contains a reserved keyword or your database' .
56  ' user does not have sufficient permissions to create it.' .
57  ' Please choose an existing (empty) database or contact administration.'
58  );
59  $result[] = $errorStatus;
60  }
61  } else {
63  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
64  $errorStatus->setTitle('Database name not valid');
65  $errorStatus->setMessage(
66  'Given database name must be shorter than fifty characters' .
67  ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)' .
68  ' and underscores (_).'
69  );
70  $result[] = $errorStatus;
71  }
72  } elseif ($postValues['type'] === 'existing' && !empty($postValues['existing'])) {
73  // Only store database information when it's empty
74  $this->databaseConnection->setDatabaseName($postValues['existing']);
75  $this->databaseConnection->sql_select_db();
76  $existingTables = $this->databaseConnection->admin_get_tables();
77  $isInitialInstallation = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
78  if (!$isInitialInstallation || empty($existingTables)) {
79  $localConfigurationPathValuePairs['DB/database'] = $postValues['existing'];
80  }
81  } else {
83  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
84  $errorStatus->setTitle('No Database selected');
85  $errorStatus->setMessage('You must select a database.');
86  $result[] = $errorStatus;
87  }
88 
89  if (!empty($localConfigurationPathValuePairs)) {
90  $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
91  }
92 
93  return $result;
94  }
95 
102  public function needsExecution()
103  {
105  $result = true;
106  if ((string)$GLOBALS['TYPO3_CONF_VARS']['DB']['database'] !== '') {
107  $this->databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
108  try {
109  $selectResult = $this->databaseConnection->sql_select_db();
110  if ($selectResult === true) {
111  $result = false;
112  }
113  } catch (\RuntimeException $e) {
114  }
115  }
116  return $result;
117  }
118 
124  protected function executeAction()
125  {
127  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
128  $isInitialInstallationInProgress = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
129  $this->view->assign('databaseList', $this->getDatabaseList($isInitialInstallationInProgress));
130  $this->view->assign('isInitialInstallationInProgress', $isInitialInstallationInProgress);
131  $this->assignSteps();
132  return $this->view->render();
133  }
134 
141  protected function getDatabaseList($initialInstallation)
142  {
144  $databaseArray = $this->databaseConnection->admin_get_dbs();
145  // Remove mysql organizational tables from database list
146  $reservedDatabaseNames = array('mysql', 'information_schema', 'performance_schema');
147  $allPossibleDatabases = array_diff($databaseArray, $reservedDatabaseNames);
148 
149  // If we are upgrading we show *all* databases the user has access to
150  if ($initialInstallation === false) {
151  return $allPossibleDatabases;
152  } else {
153  // In first installation we show all databases but disable not empty ones (with tables)
154  $databases = array();
155  foreach ($allPossibleDatabases as $database) {
156  $this->databaseConnection->setDatabaseName($database);
157  $this->databaseConnection->sql_select_db();
158  $existingTables = $this->databaseConnection->admin_get_tables();
159  $databases[] = array(
160  'name' => $database,
161  'tables' => count($existingTables),
162  );
163  }
164  return $databases;
165  }
166  }
167 
173  protected function initializeDatabaseConnection()
174  {
175  $this->databaseConnection = $this->objectManager->get(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
176  $this->databaseConnection->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
177  $this->databaseConnection->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
178  $this->databaseConnection->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
179  $this->databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
180  $this->databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
181  $this->databaseConnection->sql_pconnect();
182  }
183 
190  protected function isValidDatabaseName($databaseName)
191  {
192  return strlen($databaseName) <= 50 && preg_match('/^[a-zA-Z0-9\$_]*$/', $databaseName);
193  }
194 }