[TASK] Deprecate $GLOBALS['TYPO3_LOADED_EXT']
[Packages/TYPO3.CMS.git] / typo3 / sysext / lowlevel / Classes / Controller / ConfigurationController.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Lowlevel\Controller;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Backend\Configuration\SiteTcaConfiguration;
21 use TYPO3\CMS\Backend\Routing\Router;
22 use TYPO3\CMS\Backend\Template\ModuleTemplate;
23 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
24 use TYPO3\CMS\Core\Cache\CacheManager;
25 use TYPO3\CMS\Core\Http\HtmlResponse;
26 use TYPO3\CMS\Core\Http\MiddlewareStackResolver;
27 use TYPO3\CMS\Core\Localization\LanguageService;
28 use TYPO3\CMS\Core\Package\PackageManager;
29 use TYPO3\CMS\Core\Service\DependencyOrderingService;
30 use TYPO3\CMS\Core\Utility\ArrayUtility;
31 use TYPO3\CMS\Core\Utility\GeneralUtility;
32 use TYPO3\CMS\Fluid\View\StandaloneView;
33 use TYPO3\CMS\Lowlevel\Utility\ArrayBrowser;
34
35 /**
36 * View configuration arrays in the backend
37 */
38 class ConfigurationController
39 {
40 /**
41 * Available trees to render.
42 * * label is an LLL identifier
43 * * type is used to identify the data source type
44 * * globalKey (only for type=global) is the name of a global variable
45 *
46 * @var array
47 */
48 protected $treeSetup = [
49 'confVars' => [
50 'label' => 'typo3ConfVars',
51 'type' => 'global',
52 'globalKey' => 'TYPO3_CONF_VARS',
53 ],
54 'tca' => [
55 'label' => 'tca',
56 'type' => 'global',
57 'globalKey' => 'TCA',
58 ],
59 'tcaDescr' => [
60 'label' => 'tcaDescr',
61 'type' => 'global',
62 'globalKey' => 'TCA_DESCR',
63 ],
64 // @deprecated will be removed in v10
65 'loadedExt' => [
66 'label' => 'loadedExt',
67 'type' => 'global',
68 'globalKey' => 'TYPO3_LOADED_EXT',
69 ],
70 'services' => [
71 'label' => 't3services',
72 'key' => 'services',
73 'type' => 'global',
74 'globalKey' => 'T3_SERVICES',
75 ],
76 'tbeModules' => [
77 'label' => 'tbemodules',
78 'type' => 'global',
79 'globalKey' => 'TBE_MODULES',
80 ],
81 'tbeModulesExt' => [
82 'label' => 'tbemodulesext',
83 'type' => 'global',
84 'globalKey' => 'TBE_MODULES_EXT',
85 ],
86 'tbeStyles' => [
87 'label' => 'tbeStyles',
88 'type' => 'global',
89 'globalKey' => 'TBE_STYLES',
90 ],
91 'userSettings' => [
92 'label' => 'usersettings',
93 'type' => 'global',
94 'globalKey' => 'TYPO3_USER_SETTINGS',
95 ],
96 'pagesTypes' => [
97 'label' => 'pagesTypes',
98 'type' => 'global',
99 'globalKey' => 'PAGES_TYPES',
100 ],
101 'beUserUc' => [
102 'label' => 'beUser',
103 'type' => 'uc',
104 ],
105 'beUserTsConfig' => [
106 'label' => 'beUserTsConfig',
107 'type' => 'beUserTsConfig',
108 ],
109 'beRoutes' => [
110 'label' => 'routes',
111 'type' => 'routes',
112 ],
113 'httpMiddlewareStacks' => [
114 'label' => 'httpMiddlewareStacks',
115 'type' => 'httpMiddlewareStacks',
116 ],
117 'siteConfiguration' => [
118 'label' => 'siteConfiguration',
119 'type' => 'siteConfiguration',
120 ],
121 ];
122
123 /**
124 * Blind configurations which should not be visible to mortal admins
125 *
126 * @var array
127 */
128 protected $blindedConfigurationOptions = [
129 'TYPO3_CONF_VARS' => [
130 'DB' => [
131 'database' => '******',
132 'host' => '******',
133 'password' => '******',
134 'port' => '******',
135 'socket' => '******',
136 'username' => '******',
137 'Connections' => [
138 'Default' => [
139 'dbname' => '******',
140 'host' => '******',
141 'password' => '******',
142 'port' => '******',
143 'user' => '******',
144 'unix_socket' => '******',
145 ],
146 ],
147 ],
148 'SYS' => [
149 'encryptionKey' => '******'
150 ],
151 ],
152 ];
153
154 /**
155 * Main controller action determines get/post values, takes care of
156 * stored backend user settings for this module, determines tree
157 * and renders it.
158 *
159 * @param ServerRequestInterface $request the current request
160 * @return ResponseInterface the response with the content
161 * @throws \RuntimeException
162 */
163 public function mainAction(ServerRequestInterface $request): ResponseInterface
164 {
165 $backendUser = $this->getBackendUser();
166 $languageService = $this->getLanguageService();
167
168 $queryParams = $request->getQueryParams();
169 $postValues = $request->getParsedBody();
170
171 $moduleState = $backendUser->uc['moduleData']['system_config'] ?? [];
172
173 // Determine validated tree key and tree detail setup
174 $selectedTreeKey = $this->treeSetup[$queryParams['tree']] ? $queryParams['tree']
175 : ($this->treeSetup[$moduleState['tree']] ? $moduleState['tree'] : key($this->treeSetup));
176 $selectedTreeDetails = $this->treeSetup[$selectedTreeKey];
177 $moduleState['tree'] = $selectedTreeKey;
178
179 // Search string given or regex search enabled?
180 $searchString = (string)($postValues['searchString'] ? trim($postValues['searchString']) : '');
181 $moduleState['regexSearch'] = (bool)($postValues['regexSearch'] ?? $moduleState['regexSearch'] ?? false);
182
183 // Prepare main array
184 $sortKeysByName = true;
185 if ($selectedTreeDetails['type'] === 'global') {
186 $globalArrayKey = $selectedTreeDetails['globalKey'];
187 $renderArray = $GLOBALS[$globalArrayKey];
188
189 // Hook for Processing blindedConfigurationOptions
190 $blindedConfigurationOptions = $this->blindedConfigurationOptions;
191
192 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['modifyBlindedConfigurationOptions'] ?? [] as $classReference) {
193 $processingObject = GeneralUtility::makeInstance($classReference);
194 $blindedConfigurationOptions = $processingObject->modifyBlindedConfigurationOptions($blindedConfigurationOptions, $this);
195 }
196
197 if (isset($blindedConfigurationOptions[$globalArrayKey])) {
198 // Prepare blinding for all database connection types
199 foreach (array_keys($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']) as $connectionName) {
200 if ($connectionName !== 'Default') {
201 $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] =
202 $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
203 }
204 }
205 ArrayUtility::mergeRecursiveWithOverrule(
206 $renderArray,
207 ArrayUtility::intersectRecursive($blindedConfigurationOptions[$globalArrayKey], $renderArray)
208 );
209 }
210 } elseif ($selectedTreeDetails['type'] === 'beUserTsConfig') {
211 $renderArray = $backendUser->getTSConfig();
212 } elseif ($selectedTreeDetails['type'] === 'uc') {
213 $renderArray = $backendUser->uc;
214 } elseif ($selectedTreeDetails['type'] === 'routes') {
215 $router = GeneralUtility::makeInstance(Router::class);
216 $routes = $router->getRoutes();
217 $renderArray = [];
218 foreach ($routes as $identifier => $route) {
219 /** @var \TYPO3\CMS\Backend\Routing\Route $route */
220 $renderArray[$identifier] = [
221 'path' => $route->getPath(),
222 'options' => $route->getOptions()
223 ];
224 }
225 } elseif ($selectedTreeDetails['type'] === 'httpMiddlewareStacks') {
226 // Keep the order of the keys
227 $sortKeysByName = false;
228 $stackResolver = GeneralUtility::makeInstance(
229 MiddlewareStackResolver::class,
230 GeneralUtility::makeInstance(PackageManager::class),
231 GeneralUtility::makeInstance(DependencyOrderingService::class),
232 GeneralUtility::makeInstance(CacheManager::class)->getCache('cache_core')
233 );
234 $renderArray = [];
235 foreach (['frontend', 'backend'] as $stackName) {
236 // reversing the array allows the admin to read the stack from top to bottom
237 $renderArray[$stackName] = array_reverse($stackResolver->resolve($stackName));
238 }
239 } elseif ($selectedTreeDetails['type'] === 'siteConfiguration') {
240 $renderArray = GeneralUtility::makeInstance(SiteTcaConfiguration::class)->getTca();
241 } else {
242 throw new \RuntimeException('Unknown array type "' . $selectedTreeDetails['type'] . '"', 1507845662);
243 }
244 if ($sortKeysByName) {
245 ArrayUtility::naturalKeySortRecursive($renderArray);
246 }
247
248 // Prepare array renderer class, apply search and expand / collapse states
249 $arrayBrowser = GeneralUtility::makeInstance(ArrayBrowser::class);
250 $arrayBrowser->dontLinkVar = true;
251 $arrayBrowser->searchKeysToo = true;
252 $arrayBrowser->regexMode = $moduleState['regexSearch'];
253 $node = $queryParams['node'];
254 if ($searchString) {
255 $arrayBrowser->depthKeys = $arrayBrowser->getSearchKeys($renderArray, '', $searchString, []);
256 } elseif (is_array($node)) {
257 $newExpandCollapse = $arrayBrowser->depthKeys($node, $moduleState['node_' . $selectedTreeKey]);
258 $arrayBrowser->depthKeys = $newExpandCollapse;
259 $moduleState['node_' . $selectedTreeKey] = $newExpandCollapse;
260 } else {
261 $arrayBrowser->depthKeys = $moduleState['node_' . $selectedTreeKey] ?? [];
262 }
263
264 // Store new state
265 $backendUser->uc['moduleData']['system_config'] = $moduleState;
266 $backendUser->writeUC();
267
268 // Render main body
269 $view = GeneralUtility::makeInstance(StandaloneView::class);
270 $view->getRequest()->setControllerExtensionName('lowlevel');
271 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
272 'EXT:lowlevel/Resources/Private/Templates/Backend/Configuration.html'
273 ));
274 $view->assignMultiple([
275 'treeName' => $selectedTreeDetails['label'],
276 'searchString' => $searchString,
277 'regexSearch' => $moduleState['regexSearch'],
278 'tree' => $arrayBrowser->tree($renderArray, ''),
279 ]);
280
281 // Prepare module setup
282 $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
283 $moduleTemplate->setContent($view->render());
284 $moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lowlevel/ConfigurationView');
285
286 // Shortcut in doc header
287 $shortcutButton = $moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
288 $shortcutButton->setModuleName('system_config')
289 ->setDisplayName($languageService->sL(
290 'LLL:EXT:lowlevel/Resources/Private/Language/locallang.xlf:' . $selectedTreeDetails['label']
291 ))
292 ->setSetVariables(['tree']);
293 $moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortcutButton);
294
295 // Main drop down in doc header
296 $menu = $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
297 $menu->setIdentifier('tree');
298 foreach ($this->treeSetup as $treeKey => $treeDetails) {
299 $menuItem = $menu->makeMenuItem();
300 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
301 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
302 $menuItem->setHref((string)$uriBuilder->buildUriFromRoute('system_config', ['tree' => $treeKey]))
303 ->setTitle($languageService->sL(
304 'LLL:EXT:lowlevel/Resources/Private/Language/locallang.xlf:' . $treeDetails['label']
305 ));
306 if ($selectedTreeKey === $treeKey) {
307 $menuItem->setActive(true);
308 }
309 $menu->addMenuItem($menuItem);
310 }
311 $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
312
313 return new HtmlResponse($moduleTemplate->renderContent());
314 }
315
316 /**
317 * Returns the Backend User
318 * @return BackendUserAuthentication
319 */
320 protected function getBackendUser()
321 {
322 return $GLOBALS['BE_USER'];
323 }
324
325 /**
326 * @return LanguageService
327 */
328 protected function getLanguageService()
329 {
330 return $GLOBALS['LANG'];
331 }
332 }