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