GlobalVariableProvider.php 4.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php

declare(strict_types=1);

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

namespace TYPO3\CMS\Lowlevel\ConfigurationModuleProvider;

use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Lowlevel\Controller\ConfigurationController;

class GlobalVariableProvider extends AbstractProvider
{
    /**
     * Blind configurations which should not be visible to mortal admins
     *
     * @var array
     */
    protected array $blindedConfigurationOptions = [
        'TYPO3_CONF_VARS' => [
33 34 35
            'BE' => [
                'installToolPassword' => '******'
            ],
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
            'DB' => [
                'database' => '******',
                'host' => '******',
                'password' => '******',
                'port' => '******',
                'socket' => '******',
                'username' => '******',
                'Connections' => [
                    'Default' => [
                        'dbname' => '******',
                        'host' => '******',
                        'password' => '******',
                        'port' => '******',
                        'user' => '******',
                        'unix_socket' => '******',
                    ],
                ],
            ],
54 55 56 57 58 59 60 61 62 63
            'HTTP' => [
                'cert' => '******',
                'ssl_key' => '******'
            ],
            'MAIL' => [
                'transport_smtp_encrypt' => '******',
                'transport_smtp_password' => '******',
                'transport_smtp_server' => '******',
                'transport_smtp_username' => '******',
            ],
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            'SYS' => [
                'encryptionKey' => '******'
            ],
        ],
    ];

    /**
     * The $GLOBALS key to be processed
     *
     * @var string
     */
    protected string $globalVariableKey;

    public function __invoke(array $attributes): self
    {
        parent::__invoke($attributes);

        if (!($attributes['globalVariableKey'] ?? false)) {
            throw new \RuntimeException('Attribute \'globalVariableKey\' must be set to use ' . __CLASS__, 1606478088);
        }

        $this->globalVariableKey = $attributes['globalVariableKey'];
        return $this;
    }

    public function getConfiguration(): array
    {
        $configurationArray = $GLOBALS[$this->globalVariableKey] ?? [];
        $blindedConfigurationOptions = $this->blindedConfigurationOptions;

        // Hook for Processing blindedConfigurationOptions
        foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][ConfigurationController::class]['modifyBlindedConfigurationOptions'] ?? [] as $classReference) {
            $processingObject = GeneralUtility::makeInstance($classReference);
            $blindedConfigurationOptions = $processingObject->modifyBlindedConfigurationOptions($blindedConfigurationOptions, $this);
        }

        if (isset($blindedConfigurationOptions[$this->globalVariableKey])) {
            // Prepare blinding for all database connection types
            foreach (array_keys($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']) as $connectionName) {
                if ($connectionName !== 'Default') {
                    $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] =
                        $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
                }
            }
            ArrayUtility::mergeRecursiveWithOverrule(
                $configurationArray,
                ArrayUtility::intersectRecursive($blindedConfigurationOptions[$this->globalVariableKey], $configurationArray)
            );
        }
        ArrayUtility::naturalKeySortRecursive($configurationArray);
        return $configurationArray;
    }
}