TYPO3 CMS  TYPO3_7-6
DatabaseSelect.php
Go to the documentation of this file.
1 <?php
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 
23 {
27  protected $databaseConnection = null;
28 
34  public function execute()
35  {
36  $result = [];
38  $postValues = $this->postValues['values'];
39  $localConfigurationPathValuePairs = [];
41  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
42  if ($postValues['type'] === 'new') {
43  $newDatabaseName = $postValues['new'];
44  if ($this->isValidDatabaseName($newDatabaseName)) {
45  $createDatabaseResult = $this->databaseConnection->admin_query('CREATE DATABASE ' . $newDatabaseName . ' CHARACTER SET utf8');
46  if ($createDatabaseResult) {
47  $localConfigurationPathValuePairs['DB/database'] = $newDatabaseName;
48  } else {
50  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
51  $errorStatus->setTitle('Unable to create database');
52  $errorStatus->setMessage(
53  'Database with name ' . $newDatabaseName . ' could not be created.' .
54  ' Either your database name contains a reserved keyword or your database' .
55  ' user does not have sufficient permissions to create it.' .
56  ' Please choose an existing (empty) database or contact administration.'
57  );
58  $result[] = $errorStatus;
59  }
60  } else {
62  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
63  $errorStatus->setTitle('Database name not valid');
64  $errorStatus->setMessage(
65  'Given database name must be shorter than fifty characters' .
66  ' and consist solely of basic latin letters (a-z), digits (0-9), dollar signs ($)' .
67  ' and underscores (_).'
68  );
69  $result[] = $errorStatus;
70  }
71  } elseif ($postValues['type'] === 'existing' && !empty($postValues['existing'])) {
72  // Only store database information when it's empty
73  $this->databaseConnection->setDatabaseName($postValues['existing']);
74  try {
75  $this->databaseConnection->sql_select_db();
76  $existingTables = $this->databaseConnection->admin_get_tables();
77  if (!empty($existingTables)) {
78  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
79  $errorStatus->setTitle('Selected database is not empty!');
80  $errorStatus->setMessage(
81  sprintf('Cannot use database "%s"', $postValues['existing'])
82  . ', because it has tables already. Please select a different database or choose to create one!'
83  );
84  $result[] = $errorStatus;
85  }
86  } catch (\RuntimeException $e) {
87  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
88  $errorStatus->setTitle('Could not connect to selected database!');
89  $errorStatus->setMessage(
90  sprintf('Could not connect to database "%s"', $postValues['existing'])
91  . '! Make sure it really exists and your database user has the permissions to select it!'
92  );
93  $result[] = $errorStatus;
94  }
95  $isInitialInstallation = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
96  if (!$isInitialInstallation || empty($result)) {
97  $localConfigurationPathValuePairs['DB/database'] = $postValues['existing'];
98  }
99  } else {
101  $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
102  $errorStatus->setTitle('No Database selected');
103  $errorStatus->setMessage('You must select a database.');
104  $result[] = $errorStatus;
105  }
106 
107  if (!empty($localConfigurationPathValuePairs)) {
108  $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
109  }
110 
111  return $result;
112  }
113 
120  public function needsExecution()
121  {
123  $result = true;
124  if ((string)$GLOBALS['TYPO3_CONF_VARS']['DB']['database'] !== '') {
125  $this->databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
126  try {
127  $selectResult = $this->databaseConnection->sql_select_db();
128  if ($selectResult === true) {
129  $result = false;
130  }
131  } catch (\RuntimeException $e) {
132  }
133  }
134  return $result;
135  }
136 
142  protected function executeAction()
143  {
145  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
146  $isInitialInstallationInProgress = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
147  $this->view->assign('databaseList', $this->getDatabaseList($isInitialInstallationInProgress));
148  $this->view->assign('isInitialInstallationInProgress', $isInitialInstallationInProgress);
149  $this->assignSteps();
150  return $this->view->render();
151  }
152 
159  protected function getDatabaseList($initialInstallation)
160  {
162  $databaseArray = $this->databaseConnection->admin_get_dbs();
163  // Remove mysql organizational tables from database list
164  $reservedDatabaseNames = ['mysql', 'information_schema', 'performance_schema'];
165  $allPossibleDatabases = array_diff($databaseArray, $reservedDatabaseNames);
166 
167  // If we are upgrading we show *all* databases the user has access to
168  if ($initialInstallation === false) {
169  return $allPossibleDatabases;
170  } else {
171  // In first installation we show all databases but disable not empty ones (with tables)
172  $databases = [];
173  foreach ($allPossibleDatabases as $database) {
174  $this->databaseConnection->setDatabaseName($database);
175  $this->databaseConnection->sql_select_db();
176  $existingTables = $this->databaseConnection->admin_get_tables();
177  $databases[] = [
178  'name' => $database,
179  'tables' => count($existingTables),
180  ];
181  }
182  return $databases;
183  }
184  }
185 
191  protected function initializeDatabaseConnection()
192  {
193  $this->databaseConnection = $this->objectManager->get(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
194  $this->databaseConnection->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
195  $this->databaseConnection->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
196  $this->databaseConnection->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
197  $this->databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
198  $this->databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
199  $this->databaseConnection->sql_pconnect();
200  }
201 
208  protected function isValidDatabaseName($databaseName)
209  {
210  return strlen($databaseName) <= 50 && preg_match('/^[a-zA-Z0-9\$_]*$/', $databaseName);
211  }
212 }
$database
Definition: server.php:40
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']