[TASK] Remove closing PHP tags
[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 * 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 connect step:
31 * - Needs execution if database credentials are not set or fail to connect
32 * - Renders fields for database connection fields
33 * - Sets database credentials in LocalConfiguration
34 * - Loads / unloads ext:dbal and ext:adodb if requested
35 */
36 class DatabaseConnect extends Action\AbstractAction implements StepInterface {
37
38 /**
39 * Execute database step:
40 * - Load / unload dbal & adodb
41 * - Set database connect credentials in LocalConfiguration
42 *
43 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
44 */
45 public function execute() {
46 $result = array();
47
48 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
49 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
50
51 $postValues = $this->postValues['values'];
52 if (isset($postValues['loadDbal'])) {
53 $result[] = $this->executeLoadDbalExtension();
54 } elseif ($postValues['unloadDbal']) {
55 $result[] = $this->executeUnloadDbalExtension();
56 } elseif ($postValues['setDbalDriver']) {
57 $driver = $postValues['setDbalDriver'];
58 switch ($driver) {
59 case 'mssql':
60 case 'odbc_mssql':
61 $driverConfig = array(
62 'useNameQuote' => TRUE,
63 'quoteClob' => FALSE,
64 );
65 break;
66 case 'oci8':
67 $driverConfig = array(
68 'driverOptions' => array(
69 'connectSID' => '',
70 ),
71 );
72 break;
73 }
74 $config = array(
75 '_DEFAULT' => array(
76 'type' => 'adodb',
77 'config' => array(
78 'driver' => $driver,
79 )
80 )
81 );
82 if (isset($driverConfig)) {
83 $config['_DEFAULT']['config'] = array_merge($config['_DEFAULT']['config'], $driverConfig);
84 }
85 $configurationManager->setLocalConfigurationValueByPath('EXTCONF/dbal/handlerCfg', $config);
86 } else {
87 $localConfigurationPathValuePairs = array();
88
89 if ($this->isDbalEnabled()) {
90 $config = $configurationManager->getConfigurationValueByPath('EXTCONF/dbal/handlerCfg');
91 $driver = $config['_DEFAULT']['config']['driver'];
92 if ($driver === 'oci8') {
93 $configurationManager['_DEFAULT']['config']['driverOptions']['connectSID']
94 = $postValues['type'] === 'sid' ? TRUE : FALSE;
95 }
96 }
97
98 if (isset($postValues['username'])) {
99 $value = $postValues['username'];
100 if (strlen($value) <= 50) {
101 $localConfigurationPathValuePairs['DB/username'] = $value;
102 } else {
103 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
104 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
105 $errorStatus->setTitle('Database username not valid');
106 $errorStatus->setMessage('Given username must be shorter than fifty characters.');
107 $result[] = $errorStatus;
108 }
109 }
110
111 if (isset($postValues['password'])) {
112 $value = $postValues['password'];
113 if (strlen($value) <= 50) {
114 $localConfigurationPathValuePairs['DB/password'] = $value;
115 } else {
116 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
117 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
118 $errorStatus->setTitle('Database password not valid');
119 $errorStatus->setMessage('Given password must be shorter than fifty characters.');
120 $result[] = $errorStatus;
121 }
122 }
123
124 if (isset($postValues['host'])) {
125 $value = $postValues['host'];
126 if (preg_match('/^[a-zA-Z0-9_\\.-]+(:.+)?$/', $value) && strlen($value) <= 50) {
127 $localConfigurationPathValuePairs['DB/host'] = $value;
128 } else {
129 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
130 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
131 $errorStatus->setTitle('Database host not valid');
132 $errorStatus->setMessage('Given host is not alphanumeric (a-z, A-Z, 0-9 or _-.:) or longer than fifty characters.');
133 $result[] = $errorStatus;
134 }
135 }
136
137 if (isset($postValues['port']) && $postValues['host'] !== 'localhost') {
138 $value = $postValues['port'];
139 if (preg_match('/^[0-9]+(:.+)?$/', $value) && $value > 0 && $value <= 65535) {
140 $localConfigurationPathValuePairs['DB/port'] = (int)$value;
141 } else {
142 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
143 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
144 $errorStatus->setTitle('Database port not valid');
145 $errorStatus->setMessage('Given port is not numeric or within range 1 to 65535.');
146 $result[] = $errorStatus;
147 }
148 }
149
150 if (isset($postValues['socket']) && $postValues['socket'] !== '') {
151 if (@file_exists($postValues['socket'])) {
152 $localConfigurationPathValuePairs['DB/socket'] = $postValues['socket'];
153 } else {
154 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
155 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
156 $errorStatus->setTitle('Socket does not exist');
157 $errorStatus->setMessage('Given socket location does not exist on server.');
158 $result[] = $errorStatus;
159 }
160 }
161
162 if (isset($postValues['database'])) {
163 $value = $postValues['database'];
164 if (strlen($value) <= 50) {
165 $localConfigurationPathValuePairs['DB/database'] = $value;
166 } else {
167 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
168 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
169 $errorStatus->setTitle('Database name not valid');
170 $errorStatus->setMessage('Given database name must be shorter than fifty characters.');
171 $result[] = $errorStatus;
172 }
173 }
174
175 if (!empty($localConfigurationPathValuePairs)) {
176 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
177
178 // After setting new credentials, test again and create an error message if connect is not successful
179 // @TODO: This could be simplified, if isConnectSuccessful could be released from TYPO3_CONF_VARS
180 // and feeded with connect values directly in order to obsolete the bootstrap reload.
181 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
182 ->populateLocalConfiguration()
183 ->setCoreCacheToNullBackend();
184 if ($this->isDbalEnabled()) {
185 require(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('dbal') . 'ext_localconf.php');
186 $GLOBALS['typo3CacheManager']->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
187 }
188 if (!$this->isConnectSuccessful()) {
189 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
190 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
191 $errorStatus->setTitle('Database connect not successful');
192 $errorStatus->setMessage('Connecting the database with given settings failed. Please check.');
193 $result[] = $errorStatus;
194 }
195 }
196 }
197
198 return $result;
199 }
200
201 /**
202 * Step needs to be executed if database connection is not successful.
203 *
204 * @throws \TYPO3\CMS\Install\Controller\Exception\RedirectException
205 * @return boolean
206 */
207 public function needsExecution() {
208 if ($this->isConnectSuccessful() && $this->isConfigurationComplete()) {
209 return FALSE;
210 }
211 if (!$this->isHostConfigured() && !$this->isDbalEnabled()) {
212 $this->useDefaultValuesForNotConfiguredOptions();
213 throw new \TYPO3\CMS\Install\Controller\Exception\RedirectException(
214 'Wrote default settings to LocalConfiguration.php, redirect needed',
215 1377611168
216 );
217 }
218 return TRUE;
219 }
220
221 /**
222 * Render this step
223 *
224 * @return string
225 */
226 public function handle() {
227 $this->initializeHandle();
228
229 $isDbalEnabled = $this->isDbalEnabled();
230 $this->view
231 ->assign('isDbalEnabled', $isDbalEnabled)
232 ->assign('username', $this->getConfiguredUsername())
233 ->assign('password', $this->getConfiguredPassword())
234 ->assign('host', $this->getConfiguredHost())
235 ->assign('port', $this->getConfiguredOrDefaultPort())
236 ->assign('database', $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] ?: '')
237 ->assign('socket', $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] ?: '');
238
239 if ($isDbalEnabled) {
240 $this->view->assign('selectedDbalDriver', $this->getSelectedDbalDriver());
241 $this->view->assign('dbalDrivers', $this->getAvailableDbalDrivers());
242 $this->setDbalInputFieldsToRender();
243 } else {
244 $this->view
245 ->assign('renderConnectDetailsUsername', TRUE)
246 ->assign('renderConnectDetailsPassword', TRUE)
247 ->assign('renderConnectDetailsHost', TRUE)
248 ->assign('renderConnectDetailsPort', TRUE)
249 ->assign('renderConnectDetailsSocket', TRUE);
250 }
251
252 return $this->view->render();
253 }
254
255 /**
256 * Render connect port and label
257 *
258 * @return integer Configured or default port
259 */
260 protected function getConfiguredOrDefaultPort() {
261 $configuredPort = (int)$this->getConfiguredPort();
262 if (!$configuredPort) {
263 if ($this->isDbalEnabled()) {
264 $driver = $this->getSelectedDbalDriver();
265 switch ($driver) {
266 case 'postgres':
267 $port = 5432;
268 break;
269 case 'mssql':
270 case 'odbc_mssql':
271 $port = 1433;
272 break;
273 case 'oci8':
274 $port = 1521;
275 break;
276 default:
277 $port = 3306;
278 }
279 } else {
280 $port = 3306;
281 }
282 } else {
283 $port = $configuredPort;
284 }
285 return $port;
286 }
287
288 /**
289 * Test connection with given credentials
290 *
291 * @return boolean TRUE if connect was successful
292 */
293 protected function isConnectSuccessful() {
294 /** @var $databaseConnection \TYPO3\CMS\Core\Database\DatabaseConnection */
295 $databaseConnection = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
296
297 if ($this->isDbalEnabled()) {
298 // Set additional connect information based on dbal driver. postgres for example needs
299 // database name already for connect.
300 if (isset($GLOBALS['TYPO3_CONF_VARS']['DB']['database'])) {
301 $databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
302 }
303 }
304
305 $databaseConnection->setDatabaseUsername($this->getConfiguredUsername());
306 $databaseConnection->setDatabasePassword($this->getConfiguredPassword());
307 $databaseConnection->setDatabaseHost($this->getConfiguredHost());
308 $databaseConnection->setDatabasePort($this->getConfiguredPort());
309 $databaseConnection->setDatabaseSocket($this->getConfiguredSocket());
310
311 $result = FALSE;
312 if (@$databaseConnection->sql_pconnect()) {
313 $result = TRUE;
314 }
315 return $result;
316 }
317
318 /**
319 * Check LocalConfiguration.php for required database settings:
320 * - 'host' is mandatory and must not be empty
321 * - 'port' OR 'socket' is mandatory, but may be empty
322 *
323 * @return boolean TRUE if host is set
324 */
325 protected function isHostConfigured() {
326 $hostConfigured = TRUE;
327 if (empty($GLOBALS['TYPO3_CONF_VARS']['DB']['host'])) {
328 $hostConfigured = FALSE;
329 }
330 if (
331 !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
332 && !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])
333 ) {
334 $hostConfigured = FALSE;
335 }
336 return $hostConfigured;
337 }
338
339 /**
340 * Check LocalConfiguration.php for required database settings:
341 * - 'host' is mandatory and must not be empty
342 * - 'port' OR 'socket' is mandatory, but may be empty
343 * - 'username' and 'password' are mandatory, but may be empty
344 *
345 * @return boolean TRUE if required settings are present
346 */
347 protected function isConfigurationComplete() {
348 $configurationComplete = $this->isHostConfigured();
349 if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username'])) {
350 $configurationComplete = FALSE;
351 }
352 if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password'])) {
353 $configurationComplete = FALSE;
354 }
355 return $configurationComplete;
356 }
357
358 /**
359 * Write DB settings to LocalConfiguration.php, using default values.
360 * With the switch from mysql to mysqli in 6.1, some mandatory settings were
361 * added. This method tries to add those settings in case of an upgrade, and
362 * pre-configures settings in case of a "new" install process.
363 *
364 * There are two different connection types:
365 * - Unix domain socket. This may be available if mysql is running on localhost
366 * - TCP/IP connection to some mysql system somewhere.
367 *
368 * Unix domain socket connections are quicker than TCP/IP, so it is
369 * tested if a unix domain socket connection to localhost is successful. If not,
370 * a default configuration for TCP/IP is used.
371 *
372 * @return void
373 */
374 protected function useDefaultValuesForNotConfiguredOptions() {
375 $localConfigurationPathValuePairs = array();
376
377 $localConfigurationPathValuePairs['DB/host'] = $this->getConfiguredHost();
378
379 // If host is "local" either by upgrading or by first install, we try a socket
380 // connection first and use TCP/IP as fallback
381 if ($localConfigurationPathValuePairs['DB/host'] === 'localhost'
382 || $localConfigurationPathValuePairs['DB/host'] === '127.0.0.1'
383 || strlen($localConfigurationPathValuePairs['DB/host']) === 0
384 ) {
385 if ($this->isConnectionWithUnixDomainSocketPossible()) {
386 $localConfigurationPathValuePairs['DB/host'] = 'localhost';
387 $localConfigurationPathValuePairs['DB/socket'] = $this->getConfiguredSocket();
388 } else {
389 $localConfigurationPathValuePairs['DB/host'] = '127.0.0.1';
390 // Preserve port if already configured in LocalConfiguration.php
391 $port = $this->getConfiguredPort();
392 if ($port > 0) {
393 $localConfigurationPathValuePairs['DB/port'] = $port;
394 } else {
395 $localConfigurationPathValuePairs['DB/port'] = $this->getConfiguredOrDefaultPort();
396 }
397 }
398 }
399
400 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
401 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
402 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
403 }
404
405 /**
406 * Test if a unix domain socket can be opened. This does not
407 * authenticate but only tests if a connect is successful.
408 *
409 * @return boolean TRUE on success
410 */
411 protected function isConnectionWithUnixDomainSocketPossible() {
412 $result = FALSE;
413 // Use configured socket
414 $socket = $this->getConfiguredSocket();
415 if (!strlen($socket) > 0) {
416 // If no configured socket, use default php socket
417 $defaultSocket = ini_get('mysqli.default_socket');
418 if (strlen($defaultSocket) > 0) {
419 $socket = $defaultSocket;
420 }
421 }
422 if (strlen($socket) > 0) {
423 $socketOpenResult = @fsockopen('unix://' . $socket);
424 if ($socketOpenResult) {
425 fclose($socketOpenResult);
426 $result = TRUE;
427 }
428 }
429 return $result;
430 }
431
432 /**
433 * Render fields required for successful connect based on dbal driver selection.
434 * Hint: There is a code duplication in handle() and this method. This
435 * is done by intention to keep this code area easy to maintain and understand.
436 *
437 * @return void
438 */
439 protected function setDbalInputFieldsToRender() {
440 $driver = $this->getSelectedDbalDriver();
441 switch($driver) {
442 case 'mssql':
443 case 'odbc_mssql':
444 case 'postgres':
445 $this->view
446 ->assign('renderConnectDetailsUsername', TRUE)
447 ->assign('renderConnectDetailsPassword', TRUE)
448 ->assign('renderConnectDetailsHost', TRUE)
449 ->assign('renderConnectDetailsPort', TRUE)
450 ->assign('renderConnectDetailsDatabase', TRUE);
451 break;
452 case 'oci8':
453 $this->view
454 ->assign('renderConnectDetailsUsername', TRUE)
455 ->assign('renderConnectDetailsPassword', TRUE)
456 ->assign('renderConnectDetailsHost', TRUE)
457 ->assign('renderConnectDetailsPort', TRUE)
458 ->assign('renderConnectDetailsDatabase', TRUE)
459 ->assign('renderConnectDetailsOracleSidConnect', TRUE);
460 $type = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID'])
461 ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID']
462 : '';
463 if ($type === TRUE) {
464 $this->view->assign('oracleSidSelected', TRUE);
465 }
466 break;
467 }
468 }
469
470 /**
471 * Returns a list of database drivers that are available on current server.
472 *
473 * @return array
474 */
475 protected function getAvailableDbalDrivers() {
476 $supportedDrivers = $this->getSupportedDbalDrivers();
477 $availableDrivers = array();
478 $selectedDbalDriver = $this->getSelectedDbalDriver();
479 foreach ($supportedDrivers as $abstractionLayer => $drivers) {
480 foreach ($drivers as $driver => $info) {
481 if (isset($info['combine']) && $info['combine'] === 'OR') {
482 $isAvailable = FALSE;
483 } else {
484 $isAvailable = TRUE;
485 }
486 // Loop through each PHP module dependency to ensure it is loaded
487 foreach ($info['extensions'] as $extension) {
488 if (isset($info['combine']) && $info['combine'] === 'OR') {
489 $isAvailable |= extension_loaded($extension);
490 } else {
491 $isAvailable &= extension_loaded($extension);
492 }
493 }
494 if ($isAvailable) {
495 if (!isset($availableDrivers[$abstractionLayer])) {
496 $availableDrivers[$abstractionLayer] = array();
497 }
498 $availableDrivers[$abstractionLayer][$driver] = array();
499 $availableDrivers[$abstractionLayer][$driver]['driver'] = $driver;
500 $availableDrivers[$abstractionLayer][$driver]['label'] = $info['label'];
501 $availableDrivers[$abstractionLayer][$driver]['selected'] = FALSE;
502 if ($selectedDbalDriver === $driver) {
503 $availableDrivers[$abstractionLayer][$driver]['selected'] = TRUE;
504 }
505 }
506 }
507 }
508 return $availableDrivers;
509 }
510
511 /**
512 * Returns a list of DBAL supported database drivers, with a
513 * user-friendly name and any PHP module dependency.
514 *
515 * @return array
516 */
517 protected function getSupportedDbalDrivers() {
518 $supportedDrivers = array(
519 'Native' => array(
520 'mssql' => array(
521 'label' => 'Microsoft SQL Server',
522 'extensions' => array('mssql')
523 ),
524 'oci8' => array(
525 'label' => 'Oracle OCI8',
526 'extensions' => array('oci8')
527 ),
528 'postgres' => array(
529 'label' => 'PostgreSQL',
530 'extensions' => array('pgsql')
531 )
532 ),
533 'ODBC' => array(
534 'odbc_mssql' => array(
535 'label' => 'Microsoft SQL Server',
536 'extensions' => array('odbc', 'mssql')
537 )
538 )
539 );
540 return $supportedDrivers;
541 }
542
543 /**
544 * Get selected dbal driver if any
545 *
546 * @return string Dbal driver or empty string if not yet selected
547 */
548 protected function getSelectedDbalDriver() {
549 if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'])) {
550 return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'];
551 }
552 return '';
553 }
554
555 /**
556 * Adds dbal and adodb to list of loaded extensions
557 *
558 * @return \TYPO3\CMS\Install\Status\StatusInterface
559 */
560 protected function executeLoadDbalExtension() {
561 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')) {
562 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtension('adodb');
563 }
564 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')) {
565 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtension('dbal');
566 }
567 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
568 $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
569 $warningStatus->setTitle('Loaded database abstraction layer');
570 return $warningStatus;
571 }
572
573 /**
574 * Remove dbal and adodb from list of loaded extensions
575 *
576 * @return \TYPO3\CMS\Install\Status\StatusInterface
577 */
578 protected function executeUnloadDbalExtension() {
579 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')) {
580 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::unloadExtension('adodb');
581 }
582 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')) {
583 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::unloadExtension('dbal');
584 }
585 // @TODO: Remove configuration from TYPO3_CONF_VARS['EXTCONF']['dbal']
586 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
587 $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
588 $warningStatus->setTitle('Removed database abstraction layer');
589 return $warningStatus;
590 }
591
592 /**
593 * Returns configured username, if set
594 *
595 * @return string
596 */
597 protected function getConfiguredUsername() {
598 $username = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['username'] : '';
599 return $username;
600 }
601
602 /**
603 * Returns configured password, if set
604 *
605 * @return string
606 */
607 protected function getConfiguredPassword() {
608 $password = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['password'] : '';
609 return $password;
610 }
611
612 /**
613 * Returns configured host with port split off if given
614 *
615 * @return string
616 */
617 protected function getConfiguredHost() {
618 $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
619 $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
620 if (strlen($port) < 1 && substr_count($host, ':') === 1) {
621 list($host) = explode(':', $host);
622 }
623 return $host;
624 }
625
626 /**
627 * Returns configured port. Gets port from host value if port is not yet set.
628 *
629 * @return integer
630 */
631 protected function getConfiguredPort() {
632 $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
633 $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
634 if (strlen($port) === 0 && substr_count($host, ':') === 1) {
635 $hostPortArray = explode(':', $host);
636 $port = $hostPortArray[1];
637 }
638 return (int)$port;
639 }
640
641 /**
642 * Returns configured socket, if set
643 *
644 * @return string|NULL
645 */
646 protected function getConfiguredSocket() {
647 $socket = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] : '';
648 return $socket;
649 }
650 }