[BUGFIX] DatabaseConnection must be initialized
[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\Core\Utility\ExtensionManagementUtility;
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 AbstractStepAction {
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 $config['_DEFAULT']['config']['driverOptions']['connectSID'] = ($postValues['type'] === 'sid');
94 $localConfigurationPathValuePairs['EXTCONF/dbal/handlerCfg'] = $config;
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 fed with connect values directly in order to obsolete the bootstrap reload.
181 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
182 ->populateLocalConfiguration()
183 ->disableCoreAndClassesCache();
184 if ($this->isDbalEnabled()) {
185 require(ExtensionManagementUtility::extPath('dbal') . 'ext_localconf.php');
186 \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Cache\\CacheManager')->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 * Executes the step
223 *
224 * @return string Rendered content
225 */
226 protected function executeAction() {
227 $isDbalEnabled = $this->isDbalEnabled();
228 $this->view
229 ->assign('isDbalEnabled', $isDbalEnabled)
230 ->assign('username', $this->getConfiguredUsername())
231 ->assign('password', $this->getConfiguredPassword())
232 ->assign('host', $this->getConfiguredHost())
233 ->assign('port', $this->getConfiguredOrDefaultPort())
234 ->assign('database', $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] ?: '')
235 ->assign('socket', $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] ?: '');
236
237 if ($isDbalEnabled) {
238 $this->view->assign('selectedDbalDriver', $this->getSelectedDbalDriver());
239 $this->view->assign('dbalDrivers', $this->getAvailableDbalDrivers());
240 $this->setDbalInputFieldsToRender();
241 } else {
242 $this->view
243 ->assign('renderConnectDetailsUsername', TRUE)
244 ->assign('renderConnectDetailsPassword', TRUE)
245 ->assign('renderConnectDetailsHost', TRUE)
246 ->assign('renderConnectDetailsPort', TRUE)
247 ->assign('renderConnectDetailsSocket', TRUE);
248 }
249
250 return $this->view->render();
251 }
252
253 /**
254 * Render connect port and label
255 *
256 * @return integer Configured or default port
257 */
258 protected function getConfiguredOrDefaultPort() {
259 $configuredPort = (int)$this->getConfiguredPort();
260 if (!$configuredPort) {
261 if ($this->isDbalEnabled()) {
262 $driver = $this->getSelectedDbalDriver();
263 switch ($driver) {
264 case 'postgres':
265 $port = 5432;
266 break;
267 case 'mssql':
268 case 'odbc_mssql':
269 $port = 1433;
270 break;
271 case 'oci8':
272 $port = 1521;
273 break;
274 default:
275 $port = 3306;
276 }
277 } else {
278 $port = 3306;
279 }
280 } else {
281 $port = $configuredPort;
282 }
283 return $port;
284 }
285
286 /**
287 * Test connection with given credentials
288 *
289 * @return boolean TRUE if connect was successful
290 */
291 protected function isConnectSuccessful() {
292 /** @var $databaseConnection \TYPO3\CMS\Core\Database\DatabaseConnection */
293 $databaseConnection = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
294 $databaseConnection->initialize();
295
296 if ($this->isDbalEnabled()) {
297 // Set additional connect information based on dbal driver. postgres for example needs
298 // database name already for connect.
299 if (isset($GLOBALS['TYPO3_CONF_VARS']['DB']['database'])) {
300 $databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
301 }
302 }
303
304 $databaseConnection->setDatabaseUsername($this->getConfiguredUsername());
305 $databaseConnection->setDatabasePassword($this->getConfiguredPassword());
306 $databaseConnection->setDatabaseHost($this->getConfiguredHost());
307 $databaseConnection->setDatabasePort($this->getConfiguredPort());
308 $databaseConnection->setDatabaseSocket($this->getConfiguredSocket());
309
310 $result = FALSE;
311 if (@$databaseConnection->sql_pconnect()) {
312 $result = TRUE;
313 }
314 return $result;
315 }
316
317 /**
318 * Check LocalConfiguration.php for required database settings:
319 * - 'host' is mandatory and must not be empty
320 * - 'port' OR 'socket' is mandatory, but may be empty
321 *
322 * @return boolean TRUE if host is set
323 */
324 protected function isHostConfigured() {
325 $hostConfigured = TRUE;
326 if (empty($GLOBALS['TYPO3_CONF_VARS']['DB']['host'])) {
327 $hostConfigured = FALSE;
328 }
329 if (
330 !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
331 && !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])
332 ) {
333 $hostConfigured = FALSE;
334 }
335 return $hostConfigured;
336 }
337
338 /**
339 * Check LocalConfiguration.php for required database settings:
340 * - 'host' is mandatory and must not be empty
341 * - 'port' OR 'socket' is mandatory, but may be empty
342 * - 'username' and 'password' are mandatory, but may be empty
343 *
344 * @return boolean TRUE if required settings are present
345 */
346 protected function isConfigurationComplete() {
347 $configurationComplete = $this->isHostConfigured();
348 if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username'])) {
349 $configurationComplete = FALSE;
350 }
351 if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password'])) {
352 $configurationComplete = FALSE;
353 }
354 return $configurationComplete;
355 }
356
357 /**
358 * Write DB settings to LocalConfiguration.php, using default values.
359 * With the switch from mysql to mysqli in 6.1, some mandatory settings were
360 * added. This method tries to add those settings in case of an upgrade, and
361 * pre-configures settings in case of a "new" install process.
362 *
363 * There are two different connection types:
364 * - Unix domain socket. This may be available if mysql is running on localhost
365 * - TCP/IP connection to some mysql system somewhere.
366 *
367 * Unix domain socket connections are quicker than TCP/IP, so it is
368 * tested if a unix domain socket connection to localhost is successful. If not,
369 * a default configuration for TCP/IP is used.
370 *
371 * @return void
372 */
373 protected function useDefaultValuesForNotConfiguredOptions() {
374 $localConfigurationPathValuePairs = array();
375
376 $localConfigurationPathValuePairs['DB/host'] = $this->getConfiguredHost();
377
378 // If host is "local" either by upgrading or by first install, we try a socket
379 // connection first and use TCP/IP as fallback
380 if ($localConfigurationPathValuePairs['DB/host'] === 'localhost'
381 || \TYPO3\CMS\Core\Utility\GeneralUtility::cmpIP($localConfigurationPathValuePairs['DB/host'], '127.*.*.*')
382 || strlen($localConfigurationPathValuePairs['DB/host']) === 0
383 ) {
384 if ($this->isConnectionWithUnixDomainSocketPossible()) {
385 $localConfigurationPathValuePairs['DB/host'] = 'localhost';
386 $localConfigurationPathValuePairs['DB/socket'] = $this->getConfiguredSocket();
387 } else {
388 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::isFirstPartOfStr($localConfigurationPathValuePairs['DB/host'], '127.')) {
389 $localConfigurationPathValuePairs['DB/host'] = '127.0.0.1';
390 }
391 }
392 }
393
394 if (!isset($localConfigurationPathValuePairs['DB/socket'])) {
395 // Make sure a default port is set if not configured yet
396 // This is independent from any host configuration
397 $port = $this->getConfiguredPort();
398 if ($port > 0) {
399 $localConfigurationPathValuePairs['DB/port'] = $port;
400 } else {
401 $localConfigurationPathValuePairs['DB/port'] = $this->getConfiguredOrDefaultPort();
402 }
403 }
404
405 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
406 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
407 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
408 }
409
410 /**
411 * Test if a unix domain socket can be opened. This does not
412 * authenticate but only tests if a connect is successful.
413 *
414 * @return boolean TRUE on success
415 */
416 protected function isConnectionWithUnixDomainSocketPossible() {
417 $result = FALSE;
418 // Use configured socket
419 $socket = $this->getConfiguredSocket();
420 if (!strlen($socket) > 0) {
421 // If no configured socket, use default php socket
422 $defaultSocket = ini_get('mysqli.default_socket');
423 if (strlen($defaultSocket) > 0) {
424 $socket = $defaultSocket;
425 }
426 }
427 if (strlen($socket) > 0) {
428 $socketOpenResult = @fsockopen('unix://' . $socket);
429 if ($socketOpenResult) {
430 fclose($socketOpenResult);
431 $result = TRUE;
432 }
433 }
434 return $result;
435 }
436
437 /**
438 * Render fields required for successful connect based on dbal driver selection.
439 * Hint: There is a code duplication in handle() and this method. This
440 * is done by intention to keep this code area easy to maintain and understand.
441 *
442 * @return void
443 */
444 protected function setDbalInputFieldsToRender() {
445 $driver = $this->getSelectedDbalDriver();
446 switch($driver) {
447 case 'mssql':
448 case 'odbc_mssql':
449 case 'postgres':
450 $this->view
451 ->assign('renderConnectDetailsUsername', TRUE)
452 ->assign('renderConnectDetailsPassword', TRUE)
453 ->assign('renderConnectDetailsHost', TRUE)
454 ->assign('renderConnectDetailsPort', TRUE)
455 ->assign('renderConnectDetailsDatabase', TRUE);
456 break;
457 case 'oci8':
458 $this->view
459 ->assign('renderConnectDetailsUsername', TRUE)
460 ->assign('renderConnectDetailsPassword', TRUE)
461 ->assign('renderConnectDetailsHost', TRUE)
462 ->assign('renderConnectDetailsPort', TRUE)
463 ->assign('renderConnectDetailsDatabase', TRUE)
464 ->assign('renderConnectDetailsOracleSidConnect', TRUE);
465 $type = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID'])
466 ? $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driverOptions']['connectSID']
467 : '';
468 if ($type === TRUE) {
469 $this->view->assign('oracleSidSelected', TRUE);
470 }
471 break;
472 }
473 }
474
475 /**
476 * Returns a list of database drivers that are available on current server.
477 *
478 * @return array
479 */
480 protected function getAvailableDbalDrivers() {
481 $supportedDrivers = $this->getSupportedDbalDrivers();
482 $availableDrivers = array();
483 $selectedDbalDriver = $this->getSelectedDbalDriver();
484 foreach ($supportedDrivers as $abstractionLayer => $drivers) {
485 foreach ($drivers as $driver => $info) {
486 if (isset($info['combine']) && $info['combine'] === 'OR') {
487 $isAvailable = FALSE;
488 } else {
489 $isAvailable = TRUE;
490 }
491 // Loop through each PHP module dependency to ensure it is loaded
492 foreach ($info['extensions'] as $extension) {
493 if (isset($info['combine']) && $info['combine'] === 'OR') {
494 $isAvailable |= extension_loaded($extension);
495 } else {
496 $isAvailable &= extension_loaded($extension);
497 }
498 }
499 if ($isAvailable) {
500 if (!isset($availableDrivers[$abstractionLayer])) {
501 $availableDrivers[$abstractionLayer] = array();
502 }
503 $availableDrivers[$abstractionLayer][$driver] = array();
504 $availableDrivers[$abstractionLayer][$driver]['driver'] = $driver;
505 $availableDrivers[$abstractionLayer][$driver]['label'] = $info['label'];
506 $availableDrivers[$abstractionLayer][$driver]['selected'] = FALSE;
507 if ($selectedDbalDriver === $driver) {
508 $availableDrivers[$abstractionLayer][$driver]['selected'] = TRUE;
509 }
510 }
511 }
512 }
513 return $availableDrivers;
514 }
515
516 /**
517 * Returns a list of DBAL supported database drivers, with a
518 * user-friendly name and any PHP module dependency.
519 *
520 * @return array
521 */
522 protected function getSupportedDbalDrivers() {
523 $supportedDrivers = array(
524 'Native' => array(
525 'mssql' => array(
526 'label' => 'Microsoft SQL Server',
527 'extensions' => array('mssql')
528 ),
529 'oci8' => array(
530 'label' => 'Oracle OCI8',
531 'extensions' => array('oci8')
532 ),
533 'postgres' => array(
534 'label' => 'PostgreSQL',
535 'extensions' => array('pgsql')
536 )
537 ),
538 'ODBC' => array(
539 'odbc_mssql' => array(
540 'label' => 'Microsoft SQL Server',
541 'extensions' => array('odbc', 'mssql')
542 )
543 )
544 );
545 return $supportedDrivers;
546 }
547
548 /**
549 * Get selected dbal driver if any
550 *
551 * @return string Dbal driver or empty string if not yet selected
552 */
553 protected function getSelectedDbalDriver() {
554 if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'])) {
555 return $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dbal']['handlerCfg']['_DEFAULT']['config']['driver'];
556 }
557 return '';
558 }
559
560 /**
561 * Adds dbal and adodb to list of loaded extensions
562 *
563 * @return \TYPO3\CMS\Install\Status\StatusInterface
564 */
565 protected function executeLoadDbalExtension() {
566 if (!ExtensionManagementUtility::isLoaded('adodb')) {
567 ExtensionManagementUtility::loadExtension('adodb');
568 }
569 if (!ExtensionManagementUtility::isLoaded('dbal')) {
570 ExtensionManagementUtility::loadExtension('dbal');
571 }
572 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
573 $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
574 $warningStatus->setTitle('Loaded database abstraction layer');
575 return $warningStatus;
576 }
577
578 /**
579 * Remove dbal and adodb from list of loaded extensions
580 *
581 * @return \TYPO3\CMS\Install\Status\StatusInterface
582 */
583 protected function executeUnloadDbalExtension() {
584 if (ExtensionManagementUtility::isLoaded('adodb')) {
585 ExtensionManagementUtility::unloadExtension('adodb');
586 }
587 if (ExtensionManagementUtility::isLoaded('dbal')) {
588 ExtensionManagementUtility::unloadExtension('dbal');
589 }
590 // @TODO: Remove configuration from TYPO3_CONF_VARS['EXTCONF']['dbal']
591 /** @var $errorStatus \TYPO3\CMS\Install\Status\WarningStatus */
592 $warningStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\WarningStatus');
593 $warningStatus->setTitle('Removed database abstraction layer');
594 return $warningStatus;
595 }
596
597 /**
598 * Returns configured username, if set
599 *
600 * @return string
601 */
602 protected function getConfiguredUsername() {
603 $username = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['username']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['username'] : '';
604 return $username;
605 }
606
607 /**
608 * Returns configured password, if set
609 *
610 * @return string
611 */
612 protected function getConfiguredPassword() {
613 $password = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['password']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['password'] : '';
614 return $password;
615 }
616
617 /**
618 * Returns configured host with port split off if given
619 *
620 * @return string
621 */
622 protected function getConfiguredHost() {
623 $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
624 $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
625 if (strlen($port) < 1 && substr_count($host, ':') === 1) {
626 list($host) = explode(':', $host);
627 }
628 return $host;
629 }
630
631 /**
632 * Returns configured port. Gets port from host value if port is not yet set.
633 *
634 * @return integer
635 */
636 protected function getConfiguredPort() {
637 $host = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] : '';
638 $port = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['port'] : '';
639 if (strlen($port) === 0 && substr_count($host, ':') === 1) {
640 $hostPortArray = explode(':', $host);
641 $port = $hostPortArray[1];
642 }
643 return (int)$port;
644 }
645
646 /**
647 * Returns configured socket, if set
648 *
649 * @return string|NULL
650 */
651 protected function getConfiguredSocket() {
652 $socket = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] : '';
653 return $socket;
654 }
655 }