[TASK] Install tool: Refactor db connect configuration settings
[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->isConfigurationComplete() && !$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 * - 'username' and 'password' are mandatory, but may be empty
323 *
324 * @return boolean TRUE if required settings are present
325 */
326 protected function isConfigurationComplete() {
327 $configurationComplete = TRUE;
328 if (empty($GLOBALS['TYPO3_CONF_VARS']['DB']['host'])) {
329 $configurationComplete = FALSE;
330 }
331 if (
332 !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
333 && !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])
334 ) {
335 $configurationComplete = FALSE;
336 }
337 if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username'])) {
338 $configurationComplete = FALSE;
339 }
340 if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password'])) {
341 $configurationComplete = FALSE;
342 }
343 return $configurationComplete;
344 }
345
346 /**
347 * Write DB settings to LocalConfiguration.php, using default values.
348 * With the switch from mysql to mysqli in 6.1, some mandatory settings were
349 * added. This method tries to add those settings in case of an upgrade, and
350 * pre-configures settings in case of a "new" install process.
351 *
352 * There are two different connection types:
353 * - Unix domain socket. This may be available if mysql is running on localhost
354 * - TCP/IP connection to some mysql system somewhere.
355 *
356 * Unix domain socket connections are quicker than TCP/IP, so it is
357 * tested if a unix domain socket connection to localhost is successful. If not,
358 * a default configuration for TCP/IP is used.
359 *
360 * @return void
361 */
362 protected function useDefaultValuesForNotConfiguredOptions() {
363 $localConfigurationPathValuePairs = array();
364
365 // Mandatory settings will always be written to LocalConfiguration.php, username/password may be empty
366 $localConfigurationPathValuePairs['DB/username'] = $this->getConfiguredUsername();
367 $localConfigurationPathValuePairs['DB/password'] = $this->getConfiguredPassword();
368
369 $localConfigurationPathValuePairs['DB/host'] = $this->getConfiguredHost();
370
371 // If host is "local" either by upgrading or by first install, we try a socket
372 // connection first and use TCP/IP as fallback
373 if ($localConfigurationPathValuePairs['DB/host'] === 'localhost'
374 || $localConfigurationPathValuePairs['DB/host'] === '127.0.0.1'
375 || strlen($localConfigurationPathValuePairs['DB/host']) === 0
376 ) {
377 if ($this->isConnectionWithUnixDomainSocketPossible()) {
378 $localConfigurationPathValuePairs['DB/host'] = 'localhost';
379 $localConfigurationPathValuePairs['DB/socket'] = $this->getConfiguredSocket();
380 } else {
381 $localConfigurationPathValuePairs['DB/host'] = '127.0.0.1';
382 // Preserve port if already configured in LocalConfiguration.php
383 $port = $this->getConfiguredPort();
384 if ($port > 0) {
385 $localConfigurationPathValuePairs['DB/port'] = $port;
386 } else {
387 $localConfigurationPathValuePairs['DB/port'] = $this->getConfiguredOrDefaultPort();
388 }
389 }
390 }
391
392 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
393 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
394 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
395 }
396
397 /**
398 * Test if a unix domain socket can be opened. This does not
399 * authenticate but only tests if a connect is successful.
400 *
401 * @return boolean TRUE on success
402 */
403 protected function isConnectionWithUnixDomainSocketPossible() {
404 $result = FALSE;
405 // Use configured socket
406 $socket = $this->getConfiguredSocket();
407 if (!strlen($socket) > 0) {
408 // If no configured socket, use default php socket
409 $defaultSocket = ini_get('mysqli.default_socket');
410 if (strlen($defaultSocket) > 0) {
411 $socket = $defaultSocket;
412 }
413 }
414 if (strlen($socket) > 0) {
415 $socketOpenResult = @fsockopen('unix://' . $socket);
416 if ($socketOpenResult) {
417 fclose($socketOpenResult);
418 $result = TRUE;
419 }
420 }
421 return $result;
422 }
423
424 /**
425 * Render fields required for successful connect based on dbal driver selection.
426 * Hint: There is a code duplication in handle() and this method. This
427 * is done by intention to keep this code area easy to maintain and understand.
428 *
429 * @return void
430 */
431 protected function setDbalInputFieldsToRender() {
432 $driver = $this->getSelectedDbalDriver();
433 switch($driver) {
434 case 'mssql':
435 case 'odbc_mssql':
436 case 'postgres':
437 $this->view
438 ->assign('renderConnectDetailsUsername', TRUE)
439 ->assign('renderConnectDetailsPassword', TRUE)
440 ->assign('renderConnectDetailsHost', TRUE)
441 ->assign('renderConnectDetailsPort', TRUE)
442 ->assign('renderConnectDetailsDatabase', TRUE);
443 break;
444 case 'oci8':
445 $this->view
446 ->assign('renderConnectDetailsUsername', TRUE)
447 ->assign('renderConnectDetailsPassword', TRUE)
448 ->assign('renderConnectDetailsHost', TRUE)
449 ->assign('renderConnectDetailsPort', TRUE)
450 ->assign('renderConnectDetailsDatabase', TRUE)
451 ->assign('renderConnectDetailsOracleSidConnect', TRUE);
452 $type = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID'])
453 ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID']
454 : '';
455 if ($type === TRUE) {
456 $this->view->assign('oracleSidSelected', TRUE);
457 }
458 break;
459 }
460 }
461
462 /**
463 * Returns a list of database drivers that are available on current server.
464 *
465 * @return array
466 */
467 protected function getAvailableDbalDrivers() {
468 $supportedDrivers = $this->getSupportedDbalDrivers();
469 $availableDrivers = array();
470 $selectedDbalDriver = $this->getSelectedDbalDriver();
471 foreach ($supportedDrivers as $abstractionLayer => $drivers) {
472 foreach ($drivers as $driver => $info) {
473 if (isset($info['combine']) && $info['combine'] === 'OR') {
474 $isAvailable = FALSE;
475 } else {
476 $isAvailable = TRUE;
477 }
478 // Loop through each PHP module dependency to ensure it is loaded
479 foreach ($info['extensions'] as $extension) {
480 if (isset($info['combine']) && $info['combine'] === 'OR') {
481 $isAvailable |= extension_loaded($extension);
482 } else {
483 $isAvailable &= extension_loaded($extension);
484 }
485 }
486 if ($isAvailable) {
487 if (!isset($availableDrivers[$abstractionLayer])) {
488 $availableDrivers[$abstractionLayer] = array();
489 }
490 $availableDrivers[$abstractionLayer][$driver] = array();
491 $availableDrivers[$abstractionLayer][$driver]['driver'] = $driver;
492 $availableDrivers[$abstractionLayer][$driver]['label'] = $info['label'];
493 $availableDrivers[$abstractionLayer][$driver]['selected'] = FALSE;
494 if ($selectedDbalDriver === $driver) {
495 $availableDrivers[$abstractionLayer][$driver]['selected'] = TRUE;
496 }
497 }
498 }
499 }
500 return $availableDrivers;
501 }
502
503 /**
504 * Returns a list of DBAL supported database drivers, with a
505 * user-friendly name and any PHP module dependency.
506 *
507 * @return array
508 */
509 protected function getSupportedDbalDrivers() {
510 $supportedDrivers = array(
511 'Native' => array(
512 'mssql' => array(
513 'label' => 'Microsoft SQL Server',
514 'extensions' => array('mssql')
515 ),
516 'oci8' => array(
517 'label' => 'Oracle OCI8',
518 'extensions' => array('oci8')
519 ),
520 'postgres' => array(
521 'label' => 'PostgreSQL',
522 'extensions' => array('pgsql')
523 )
524 ),
525 'ODBC' => array(
526 'odbc_mssql' => array(
527 'label' => 'Microsoft SQL Server',
528 'extensions' => array('odbc', 'mssql')
529 )
530 )
531 );
532 return $supportedDrivers;
533 }
534
535 /**
536 * Get selected dbal driver if any
537 *
538 * @return string Dbal driver or empty string if not yet selected
539 */
540 protected function getSelectedDbalDriver() {
541 if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'])) {
542 return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'];
543 }
544 return '';
545 }
546
547 /**
548 * Adds dbal and adodb to list of loaded extensions
549 *
550 * @return \TYPO3\CMS\Install\Status\StatusInterface
551 */
552 protected function executeLoadDbalExtension() {
553 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')) {
554 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtension('adodb');
555 }
556 if (!\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')) {
557 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::loadExtension('dbal');
558 }
559 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
560 $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
561 $warningStatus->setTitle('Loaded database abstraction layer');
562 return $warningStatus;
563 }
564
565 /**
566 * Remove dbal and adodb from list of loaded extensions
567 *
568 * @return \TYPO3\CMS\Install\Status\StatusInterface
569 */
570 protected function executeUnloadDbalExtension() {
571 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')) {
572 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::unloadExtension('adodb');
573 }
574 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')) {
575 \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::unloadExtension('dbal');
576 }
577 // @TODO: Remove configuration from TYPO3_CONF_VARS['EXTCONF']['dbal']
578 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
579 $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
580 $warningStatus->setTitle('Removed database abstraction layer');
581 return $warningStatus;
582 }
583
584 /**
585 * Returns configured username, if set
586 *
587 * @return string
588 */
589 protected function getConfiguredUsername() {
590 $username = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['username'] : '';
591 return $username;
592 }
593
594 /**
595 * Returns configured password, if set
596 *
597 * @return string
598 */
599 protected function getConfiguredPassword() {
600 $password = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['password'] : '';
601 return $password;
602 }
603
604 /**
605 * Returns configured host with port split off if given
606 *
607 * @return string
608 */
609 protected function getConfiguredHost() {
610 $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
611 $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
612 if (strlen($port) < 1 && substr_count($host, ':') === 1) {
613 list($host) = explode(':', $host);
614 }
615 return $host;
616 }
617
618 /**
619 * Returns configured port. Gets port from host value if port is not yet set.
620 *
621 * @return integer
622 */
623 protected function getConfiguredPort() {
624 $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
625 $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
626 if (strlen($port) === 0 && substr_count($host, ':') === 1) {
627 $hostPortArray = explode(':', $host);
628 $port = $hostPortArray[1];
629 }
630 return (int)$port;
631 }
632
633 /**
634 * Returns configured socket, if set
635 *
636 * @return string|NULL
637 */
638 protected function getConfiguredSocket() {
639 $socket = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] : '';
640 return $socket;
641 }
642 }
643 ?>