4c120bcefbbf93eea40778f7baf38fe4dc9488fc
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / Step / DatabaseConnect.php
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 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18
19 /**
20 * Database connect step:
21 * - Needs execution if database credentials are not set or fail to connect
22 * - Renders fields for database connection fields
23 * - Sets database credentials in LocalConfiguration
24 * - Loads / unloads ext:dbal and ext:adodb if requested
25 */
26 class DatabaseConnect extends AbstractStepAction {
27
28 /**
29 * Execute database step:
30 * - Load / unload dbal & adodb
31 * - Set database connect credentials in LocalConfiguration
32 *
33 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
34 */
35 public function execute() {
36 $result = array();
37
38 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
39 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
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 {
93 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
94 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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 {
106 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
107 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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 {
119 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
120 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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 {
132 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
133 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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 {
144 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
145 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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 {
157 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
158 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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.
171 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
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::class)->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
177 }
178 if (!$this->isConnectSuccessful()) {
179 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
180 $errorStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
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
191 /**
192 * Step needs to be executed if database connection is not successful.
193 *
194 * @throws \TYPO3\CMS\Install\Controller\Exception\RedirectException
195 * @return bool
196 */
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
211 /**
212 * Executes the step
213 *
214 * @return string Rendered content
215 */
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());
230 $this->setDbalInputFieldsToRender();
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
244 /**
245 * Render connect port and label
246 *
247 * @return int Configured or default port
248 */
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
277 /**
278 * Test connection with given credentials
279 *
280 * @return bool TRUE if connect was successful
281 */
282 protected function isConnectSuccessful() {
283 /** @var $databaseConnection \TYPO3\CMS\Core\Database\DatabaseConnection */
284 $databaseConnection = $this->objectManager->get(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
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
305 /**
306 * Check LocalConfiguration.php for required database settings:
307 * - 'host' is mandatory and must not be empty
308 * - 'port' OR 'socket' is mandatory, but may be empty
309 *
310 * @return bool TRUE if host is set
311 */
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
326 /**
327 * Check LocalConfiguration.php for required database settings:
328 * - 'host' is mandatory and must not be empty
329 * - 'port' OR 'socket' is mandatory, but may be empty
330 * - 'username' and 'password' are mandatory, but may be empty
331 *
332 * @return bool TRUE if required settings are present
333 */
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
345 /**
346 * Write DB settings to LocalConfiguration.php, using default values.
347 * With the switch from mysql to mysqli in 6.1, some mandatory settings were
348 * added. This method tries to add those settings in case of an upgrade, and
349 * pre-configures settings in case of a "new" install process.
350 *
351 * There are two different connection types:
352 * - Unix domain socket. This may be available if mysql is running on localhost
353 * - TCP/IP connection to some mysql system somewhere.
354 *
355 * Unix domain socket connections are quicker than TCP/IP, so it is
356 * tested if a unix domain socket connection to localhost is successful. If not,
357 * a default configuration for TCP/IP is used.
358 *
359 * @return void
360 */
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 || (string)$localConfigurationPathValuePairs['DB/host'] === ''
371 ) {
372 if ($this->isConnectionWithUnixDomainSocketPossible()) {
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
393 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
394 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
395 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
396 }
397
398 /**
399 * Test if a unix domain socket can be opened. This does not
400 * authenticate but only tests if a connect is successful.
401 *
402 * @return bool TRUE on success
403 */
404 protected function isConnectionWithUnixDomainSocketPossible() {
405 $result = FALSE;
406 // Use configured socket
407 $socket = (string)$this->getConfiguredSocket();
408 if ($socket === '') {
409 // If no configured socket, use default php socket
410 $defaultSocket = (string)ini_get('mysqli.default_socket');
411 if ($defaultSocket !== '') {
412 $socket = $defaultSocket;
413 }
414 }
415 if ($socket !== '') {
416 $socketOpenResult = @fsockopen('unix://' . $socket);
417 if ($socketOpenResult) {
418 fclose($socketOpenResult);
419 $result = TRUE;
420 }
421 }
422 return $result;
423 }
424
425 /**
426 * Render fields required for successful connect based on dbal driver selection.
427 * Hint: There is a code duplication in handle() and this method. This
428 * is done by intention to keep this code area easy to maintain and understand.
429 *
430 * @return void
431 */
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
463 /**
464 * Returns a list of database drivers that are available on current server.
465 *
466 * @return array
467 */
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
504 /**
505 * Returns a list of DBAL supported database drivers, with a
506 * user-friendly name and any PHP module dependency.
507 *
508 * @return array
509 */
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
536 /**
537 * Get selected dbal driver if any
538 *
539 * @return string Dbal driver or empty string if not yet selected
540 */
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
548 /**
549 * Adds dbal and adodb to list of loaded extensions
550 *
551 * @return \TYPO3\CMS\Install\Status\StatusInterface
552 */
553 protected function executeLoadDbalExtension() {
554 if (!ExtensionManagementUtility::isLoaded('adodb')) {
555 ExtensionManagementUtility::loadExtension('adodb');
556 }
557 if (!ExtensionManagementUtility::isLoaded('dbal')) {
558 ExtensionManagementUtility::loadExtension('dbal');
559 }
560 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
561 $warningStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\WarningStatus::class);
562 $warningStatus->setTitle('Loaded database abstraction layer');
563 return $warningStatus;
564 }
565
566 /**
567 * Remove dbal and adodb from list of loaded extensions
568 *
569 * @return \TYPO3\CMS\Install\Status\StatusInterface
570 */
571 protected function executeUnloadDbalExtension() {
572 if (ExtensionManagementUtility::isLoaded('adodb')) {
573 ExtensionManagementUtility::unloadExtension('adodb');
574 }
575 if (ExtensionManagementUtility::isLoaded('dbal')) {
576 ExtensionManagementUtility::unloadExtension('dbal');
577 }
578 // @TODO: Remove configuration from TYPO3_CONF_VARS['EXTCONF']['dbal']
579 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
580 $warningStatus = $this->objectManager->get(\TYPO3\CMS\Install\Status\WarningStatus::class);
581 $warningStatus->setTitle('Removed database abstraction layer');
582 return $warningStatus;
583 }
584
585 /**
586 * Returns configured username, if set
587 *
588 * @return string
589 */
590 protected function getConfiguredUsername() {
591 $username = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['username'] : '';
592 return $username;
593 }
594
595 /**
596 * Returns configured password, if set
597 *
598 * @return string
599 */
600 protected function getConfiguredPassword() {
601 $password = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['password'] : '';
602 return $password;
603 }
604
605 /**
606 * Returns configured host with port split off if given
607 *
608 * @return string
609 */
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
619 /**
620 * Returns configured port. Gets port from host value if port is not yet set.
621 *
622 * @return int
623 */
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 ($port === '' && substr_count($host, ':') === 1) {
628 $hostPortArray = explode(':', $host);
629 $port = $hostPortArray[1];
630 }
631 return (int)$port;
632 }
633
634 /**
635 * Returns configured socket, if set
636 *
637 * @return string|NULL
638 */
639 protected function getConfiguredSocket() {
640 $socket = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] : '';
641 return $socket;
642 }
643
644 }