TYPO3 CMS  TYPO3_6-2
DatabaseConnect.php
Go to the documentation of this file.
1 <?php
3 
18 
27 
35  public function execute() {
36  $result = array();
37 
39  $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
40 
41  $postValues = $this->postValues['values'];
42  if (isset($postValues['loadDbal'])) {
43  $result[] = $this->executeLoadDbalExtension();
44  } elseif ($postValues['unloadDbal']) {
45  $result[] = $this->executeUnloadDbalExtension();
46  } elseif ($postValues['setDbalDriver']) {
47  $driver = $postValues['setDbalDriver'];
48  switch ($driver) {
49  case 'mssql':
50  case 'odbc_mssql':
51  $driverConfig = array(
52  'useNameQuote' => TRUE,
53  'quoteClob' => FALSE,
54  );
55  break;
56  case 'oci8':
57  $driverConfig = array(
58  'driverOptions' => array(
59  'connectSID' => '',
60  ),
61  );
62  break;
63  }
64  $config = array(
65  '_DEFAULT' => array(
66  'type' => 'adodb',
67  'config' => array(
68  'driver' => $driver,
69  )
70  )
71  );
72  if (isset($driverConfig)) {
73  $config['_DEFAULT']['config'] = array_merge($config['_DEFAULT']['config'], $driverConfig);
74  }
75  $configurationManager->setLocalConfigurationValueByPath('EXTCONF/dbal/handlerCfg', $config);
76  } else {
77  $localConfigurationPathValuePairs = array();
78 
79  if ($this->isDbalEnabled()) {
80  $config = $configurationManager->getConfigurationValueByPath('EXTCONF/dbal/handlerCfg');
81  $driver = $config['_DEFAULT']['config']['driver'];
82  if ($driver === 'oci8') {
83  $config['_DEFAULT']['config']['driverOptions']['connectSID'] = ($postValues['type'] === 'sid');
84  $localConfigurationPathValuePairs['EXTCONF/dbal/handlerCfg'] = $config;
85  }
86  }
87 
88  if (isset($postValues['username'])) {
89  $value = $postValues['username'];
90  if (strlen($value) <= 50) {
91  $localConfigurationPathValuePairs['DB/username'] = $value;
92  } else {
94  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
95  $errorStatus->setTitle('Database username not valid');
96  $errorStatus->setMessage('Given username must be shorter than fifty characters.');
97  $result[] = $errorStatus;
98  }
99  }
100 
101  if (isset($postValues['password'])) {
102  $value = $postValues['password'];
103  if (strlen($value) <= 50) {
104  $localConfigurationPathValuePairs['DB/password'] = $value;
105  } else {
107  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
108  $errorStatus->setTitle('Database password not valid');
109  $errorStatus->setMessage('Given password must be shorter than fifty characters.');
110  $result[] = $errorStatus;
111  }
112  }
113 
114  if (isset($postValues['host'])) {
115  $value = $postValues['host'];
116  if (preg_match('/^[a-zA-Z0-9_\\.-]+(:.+)?$/', $value) && strlen($value) <= 50) {
117  $localConfigurationPathValuePairs['DB/host'] = $value;
118  } else {
120  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
121  $errorStatus->setTitle('Database host not valid');
122  $errorStatus->setMessage('Given host is not alphanumeric (a-z, A-Z, 0-9 or _-.:) or longer than fifty characters.');
123  $result[] = $errorStatus;
124  }
125  }
126 
127  if (isset($postValues['port']) && $postValues['host'] !== 'localhost') {
128  $value = $postValues['port'];
129  if (preg_match('/^[0-9]+(:.+)?$/', $value) && $value > 0 && $value <= 65535) {
130  $localConfigurationPathValuePairs['DB/port'] = (int)$value;
131  } else {
133  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
134  $errorStatus->setTitle('Database port not valid');
135  $errorStatus->setMessage('Given port is not numeric or within range 1 to 65535.');
136  $result[] = $errorStatus;
137  }
138  }
139 
140  if (isset($postValues['socket']) && $postValues['socket'] !== '') {
141  if (@file_exists($postValues['socket'])) {
142  $localConfigurationPathValuePairs['DB/socket'] = $postValues['socket'];
143  } else {
145  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
146  $errorStatus->setTitle('Socket does not exist');
147  $errorStatus->setMessage('Given socket location does not exist on server.');
148  $result[] = $errorStatus;
149  }
150  }
151 
152  if (isset($postValues['database'])) {
153  $value = $postValues['database'];
154  if (strlen($value) <= 50) {
155  $localConfigurationPathValuePairs['DB/database'] = $value;
156  } else {
158  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
159  $errorStatus->setTitle('Database name not valid');
160  $errorStatus->setMessage('Given database name must be shorter than fifty characters.');
161  $result[] = $errorStatus;
162  }
163  }
164 
165  if (!empty($localConfigurationPathValuePairs)) {
166  $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
167 
168  // After setting new credentials, test again and create an error message if connect is not successful
169  // @TODO: This could be simplified, if isConnectSuccessful could be released from TYPO3_CONF_VARS
170  // and fed with connect values directly in order to obsolete the bootstrap reload.
172  ->populateLocalConfiguration()
173  ->disableCoreAndClassesCache();
174  if ($this->isDbalEnabled()) {
175  require(ExtensionManagementUtility::extPath('dbal') . 'ext_localconf.php');
176  \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
177  }
178  if (!$this->isConnectSuccessful()) {
180  $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
181  $errorStatus->setTitle('Database connect not successful');
182  $errorStatus->setMessage('Connecting to the database with given settings failed. Please check.');
183  $result[] = $errorStatus;
184  }
185  }
186  }
187 
188  return $result;
189  }
190 
197  public function needsExecution() {
198  if ($this->isConnectSuccessful() && $this->isConfigurationComplete()) {
199  return FALSE;
200  }
201  if (!$this->isHostConfigured() && !$this->isDbalEnabled()) {
202  $this->useDefaultValuesForNotConfiguredOptions();
203  throw new \TYPO3\CMS\Install\Controller\Exception\RedirectException(
204  'Wrote default settings to LocalConfiguration.php, redirect needed',
205  1377611168
206  );
207  }
208  return TRUE;
209  }
210 
216  protected function executeAction() {
217  $isDbalEnabled = $this->isDbalEnabled();
218  $this->view
219  ->assign('isDbalEnabled', $isDbalEnabled)
220  ->assign('username', $this->getConfiguredUsername())
221  ->assign('password', $this->getConfiguredPassword())
222  ->assign('host', $this->getConfiguredHost())
223  ->assign('port', $this->getConfiguredOrDefaultPort())
224  ->assign('database', $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] ?: '')
225  ->assign('socket', $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] ?: '');
226 
227  if ($isDbalEnabled) {
228  $this->view->assign('selectedDbalDriver', $this->getSelectedDbalDriver());
229  $this->view->assign('dbalDrivers', $this->getAvailableDbalDrivers());
231  } else {
232  $this->view
233  ->assign('renderConnectDetailsUsername', TRUE)
234  ->assign('renderConnectDetailsPassword', TRUE)
235  ->assign('renderConnectDetailsHost', TRUE)
236  ->assign('renderConnectDetailsPort', TRUE)
237  ->assign('renderConnectDetailsSocket', TRUE);
238  }
239  $this->assignSteps();
240 
241  return $this->view->render();
242  }
243 
249  protected function getConfiguredOrDefaultPort() {
250  $configuredPort = (int)$this->getConfiguredPort();
251  if (!$configuredPort) {
252  if ($this->isDbalEnabled()) {
253  $driver = $this->getSelectedDbalDriver();
254  switch ($driver) {
255  case 'postgres':
256  $port = 5432;
257  break;
258  case 'mssql':
259  case 'odbc_mssql':
260  $port = 1433;
261  break;
262  case 'oci8':
263  $port = 1521;
264  break;
265  default:
266  $port = 3306;
267  }
268  } else {
269  $port = 3306;
270  }
271  } else {
272  $port = $configuredPort;
273  }
274  return $port;
275  }
276 
282  protected function isConnectSuccessful() {
284  $databaseConnection = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
285 
286  if ($this->isDbalEnabled()) {
287  // Set additional connect information based on dbal driver. postgres for example needs
288  // database name already for connect.
289  if (isset($GLOBALS['TYPO3_CONF_VARS']['DB']['database'])) {
290  $databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
291  }
292  }
293 
294  $databaseConnection->setDatabaseUsername($this->getConfiguredUsername());
295  $databaseConnection->setDatabasePassword($this->getConfiguredPassword());
296  $databaseConnection->setDatabaseHost($this->getConfiguredHost());
297  $databaseConnection->setDatabasePort($this->getConfiguredPort());
298  $databaseConnection->setDatabaseSocket($this->getConfiguredSocket());
299 
300  $databaseConnection->initialize();
301 
302  return (bool)@$databaseConnection->sql_pconnect();
303  }
304 
312  protected function isHostConfigured() {
313  $hostConfigured = TRUE;
314  if (empty($GLOBALS['TYPO3_CONF_VARS']['DB']['host'])) {
315  $hostConfigured = FALSE;
316  }
317  if (
318  !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
319  && !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])
320  ) {
321  $hostConfigured = FALSE;
322  }
323  return $hostConfigured;
324  }
325 
334  protected function isConfigurationComplete() {
335  $configurationComplete = $this->isHostConfigured();
336  if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username'])) {
337  $configurationComplete = FALSE;
338  }
339  if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password'])) {
340  $configurationComplete = FALSE;
341  }
342  return $configurationComplete;
343  }
344 
361  protected function useDefaultValuesForNotConfiguredOptions() {
362  $localConfigurationPathValuePairs = array();
363 
364  $localConfigurationPathValuePairs['DB/host'] = $this->getConfiguredHost();
365 
366  // If host is "local" either by upgrading or by first install, we try a socket
367  // connection first and use TCP/IP as fallback
368  if ($localConfigurationPathValuePairs['DB/host'] === 'localhost'
369  || \TYPO3\CMS\Core\Utility\GeneralUtility::cmpIP($localConfigurationPathValuePairs['DB/host'], '127.*.*.*')
370  || strlen($localConfigurationPathValuePairs['DB/host']) === 0
371  ) {
373  $localConfigurationPathValuePairs['DB/host'] = 'localhost';
374  $localConfigurationPathValuePairs['DB/socket'] = $this->getConfiguredSocket();
375  } else {
376  if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($localConfigurationPathValuePairs['DB/host'], '127.')) {
377  $localConfigurationPathValuePairs['DB/host'] = '127.0.0.1';
378  }
379  }
380  }
381 
382  if (!isset($localConfigurationPathValuePairs['DB/socket'])) {
383  // Make sure a default port is set if not configured yet
384  // This is independent from any host configuration
385  $port = $this->getConfiguredPort();
386  if ($port > 0) {
387  $localConfigurationPathValuePairs['DB/port'] = $port;
388  } else {
389  $localConfigurationPathValuePairs['DB/port'] = $this->getConfiguredOrDefaultPort();
390  }
391  }
392 
394  $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
395  $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
396  }
397 
405  $result = FALSE;
406  // Use configured socket
407  $socket = $this->getConfiguredSocket();
408  if (!strlen($socket) > 0) {
409  // If no configured socket, use default php socket
410  $defaultSocket = ini_get('mysqli.default_socket');
411  if (strlen($defaultSocket) > 0) {
412  $socket = $defaultSocket;
413  }
414  }
415  if (strlen($socket) > 0) {
416  $socketOpenResult = @fsockopen('unix://' . $socket);
417  if ($socketOpenResult) {
418  fclose($socketOpenResult);
419  $result = TRUE;
420  }
421  }
422  return $result;
423  }
424 
432  protected function setDbalInputFieldsToRender() {
433  $driver = $this->getSelectedDbalDriver();
434  switch($driver) {
435  case 'mssql':
436  case 'odbc_mssql':
437  case 'postgres':
438  $this->view
439  ->assign('renderConnectDetailsUsername', TRUE)
440  ->assign('renderConnectDetailsPassword', TRUE)
441  ->assign('renderConnectDetailsHost', TRUE)
442  ->assign('renderConnectDetailsPort', TRUE)
443  ->assign('renderConnectDetailsDatabase', TRUE);
444  break;
445  case 'oci8':
446  $this->view
447  ->assign('renderConnectDetailsUsername', TRUE)
448  ->assign('renderConnectDetailsPassword', TRUE)
449  ->assign('renderConnectDetailsHost', TRUE)
450  ->assign('renderConnectDetailsPort', TRUE)
451  ->assign('renderConnectDetailsDatabase', TRUE)
452  ->assign('renderConnectDetailsOracleSidConnect', TRUE);
453  $type = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID'])
454  ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID']
455  : '';
456  if ($type === TRUE) {
457  $this->view->assign('oracleSidSelected', TRUE);
458  }
459  break;
460  }
461  }
462 
468  protected function getAvailableDbalDrivers() {
469  $supportedDrivers = $this->getSupportedDbalDrivers();
470  $availableDrivers = array();
471  $selectedDbalDriver = $this->getSelectedDbalDriver();
472  foreach ($supportedDrivers as $abstractionLayer => $drivers) {
473  foreach ($drivers as $driver => $info) {
474  if (isset($info['combine']) && $info['combine'] === 'OR') {
475  $isAvailable = FALSE;
476  } else {
477  $isAvailable = TRUE;
478  }
479  // Loop through each PHP module dependency to ensure it is loaded
480  foreach ($info['extensions'] as $extension) {
481  if (isset($info['combine']) && $info['combine'] === 'OR') {
482  $isAvailable |= extension_loaded($extension);
483  } else {
484  $isAvailable &= extension_loaded($extension);
485  }
486  }
487  if ($isAvailable) {
488  if (!isset($availableDrivers[$abstractionLayer])) {
489  $availableDrivers[$abstractionLayer] = array();
490  }
491  $availableDrivers[$abstractionLayer][$driver] = array();
492  $availableDrivers[$abstractionLayer][$driver]['driver'] = $driver;
493  $availableDrivers[$abstractionLayer][$driver]['label'] = $info['label'];
494  $availableDrivers[$abstractionLayer][$driver]['selected'] = FALSE;
495  if ($selectedDbalDriver === $driver) {
496  $availableDrivers[$abstractionLayer][$driver]['selected'] = TRUE;
497  }
498  }
499  }
500  }
501  return $availableDrivers;
502  }
503 
510  protected function getSupportedDbalDrivers() {
511  $supportedDrivers = array(
512  'Native' => array(
513  'mssql' => array(
514  'label' => 'Microsoft SQL Server',
515  'extensions' => array('mssql')
516  ),
517  'oci8' => array(
518  'label' => 'Oracle OCI8',
519  'extensions' => array('oci8')
520  ),
521  'postgres' => array(
522  'label' => 'PostgreSQL',
523  'extensions' => array('pgsql')
524  )
525  ),
526  'ODBC' => array(
527  'odbc_mssql' => array(
528  'label' => 'Microsoft SQL Server',
529  'extensions' => array('odbc', 'mssql')
530  )
531  )
532  );
533  return $supportedDrivers;
534  }
535 
541  protected function getSelectedDbalDriver() {
542  if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'])) {
543  return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'];
544  }
545  return '';
546  }
547 
553  protected function executeLoadDbalExtension() {
554  if (!ExtensionManagementUtility::isLoaded('adodb')) {
556  }
559  }
561  $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
562  $warningStatus->setTitle('Loaded database abstraction layer');
563  return $warningStatus;
564  }
565 
571  protected function executeUnloadDbalExtension() {
574  }
577  }
578  // @TODO: Remove configuration from TYPO3_CONF_VARS['EXTCONF']['dbal']
580  $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
581  $warningStatus->setTitle('Removed database abstraction layer');
582  return $warningStatus;
583  }
584 
590  protected function getConfiguredUsername() {
591  $username = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['username'] : '';
592  return $username;
593  }
594 
600  protected function getConfiguredPassword() {
601  $password = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['password'] : '';
602  return $password;
603  }
604 
610  protected function getConfiguredHost() {
611  $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
612  $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
613  if (strlen($port) < 1 && substr_count($host, ':') === 1) {
614  list($host) = explode(':', $host);
615  }
616  return $host;
617  }
618 
624  protected function getConfiguredPort() {
625  $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
626  $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
627  if (strlen($port) === 0 && substr_count($host, ':') === 1) {
628  $hostPortArray = explode(':', $host);
629  $port = $hostPortArray[1];
630  }
631  return (int)$port;
632  }
633 
639  protected function getConfiguredSocket() {
640  $socket = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] : '';
641  return $socket;
642  }
643 }
$driver
Definition: server.php:34
static isFirstPartOfStr($str, $partStr)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
$host
Definition: server.php:35
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]