e03a0e847e0da8f6b097e6098322708c7e48810a
[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\Routing\Router;
21 use TYPO3\CMS\Backend\Template\ModuleTemplate;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\Localization\LanguageService;
24 use TYPO3\CMS\Core\Utility\ArrayUtility;
25 use TYPO3\CMS\Core\Utility\GeneralUtility;
26 use TYPO3\CMS\Fluid\View\StandaloneView;
27 use TYPO3\CMS\Lowlevel\Utility\ArrayBrowser;
28
29 /**
30 * View configuration arrays in the backend
31 */
32 class ConfigurationController
33 {
34 /**
35 * Available trees to render.
36 * * label is an LLL identifier
37 * * type is used to identify the data source type
38 * * globalKey (only for type=global) is the name of a global variable
39 *
40 * @var array
41 */
42 protected $treeSetup = [
43 'confVars' => [
44 'label' => 'typo3ConfVars',
45 'type' => 'global',
46 'globalKey' => 'TYPO3_CONF_VARS',
47 ],
48 'tca' => [
49 'label' => 'tca',
50 'type' => 'global',
51 'globalKey' => 'TCA',
52 ],
53 'tcaDescr' => [
54 'label' => 'tcaDescr',
55 'type' => 'global',
56 'globalKey' => 'TCA_DESCR',
57 ],
58 'loadedExt' => [
59 'label' => 'loadedExt',
60 'type' => 'global',
61 'globalKey' => 'TYPO3_LOADED_EXT',
62 ],
63 'services' => [
64 'label' => 't3services',
65 'key' => 'services',
66 'type' => 'global',
67 'globalKey' => 'T3_SERVICES',
68 ],
69 'tbeModules' => [
70 'label' => 'tbemodules',
71 'type' => 'global',
72 'globalKey' => 'TBE_MODULES',
73 ],
74 'tbeModulesExt' => [
75 'label' => 'tbemodulesext',
76 'type' => 'global',
77 'globalKey' => 'TBE_MODULES_EXT',
78 ],
79 'tbeStyles' => [
80 'label' => 'tbeStyles',
81 'type' => 'global',
82 'globalKey' => 'TBE_STYLES',
83 ],
84 'userSettings' => [
85 'label' => 'usersettings',
86 'type' => 'global',
87 'globalKey' => 'TYPO3_USER_SETTINGS',
88 ],
89 'pagesTypes' => [
90 'label' => 'pagesTypes',
91 'type' => 'global',
92 'globalKey' => 'PAGES_TYPES',
93 ],
94 'beUserUc' => [
95 'label' => 'beUser',
96 'type' => 'uc',
97 ],
98 'beRoutes' => [
99 'label' => 'routes',
100 'type' => 'routes',
101 ],
102 ];
103
104 /**
105 * Blind configurations which should not be visible to mortal admins
106 *
107 * @var array
108 */
109 protected $blindedConfigurationOptions = [
110 'TYPO3_CONF_VARS' => [
111 'DB' => [
112 'database' => '******',
113 'host' => '******',
114 'password' => '******',
115 'port' => '******',
116 'socket' => '******',
117 'username' => '******',
118 'Connections' => [
119 'Default' => [
120 'dbname' => '******',
121 'host' => '******',
122 'password' => '******',
123 'port' => '******',
124 'user' => '******',
125 'unix_socket' => '******',
126 ],
127 ],
128 ],
129 'SYS' => [
130 'encryptionKey' => '******'
131 ],
132 ],
133 ];
134
135 /**
136 * Main controller action determines get/post values, takes care of
137 * stored backend user settings for this module, determines tree
138 * and renders it.
139 *
140 * @param ServerRequestInterface $request the current request
141 * @param ResponseInterface $response
142 * @return ResponseInterface the response with the content
143 * @throws \RuntimeException
144 */
145 public function mainAction(ServerRequestInterface $request, ResponseInterface $response)
146 {
147 $backendUser = $this->getBackendUser();
148 $languageService = $this->getLanguageService();
149
150 $queryParams = $request->getQueryParams();
151 $postValues = $request->getParsedBody();
152
153 $moduleState = $backendUser->uc['moduleData']['system_config'] ?? [];
154
155 // Determine validated tree key and tree detail setup
156 $selectedTreeKey = $this->treeSetup[$queryParams['tree']] ? $queryParams['tree']
157 : ($this->treeSetup[$moduleState['tree']] ? $moduleState['tree'] : key($this->treeSetup));
158 $selectedTreeDetails = $this->treeSetup[$selectedTreeKey];
159 $moduleState['tree'] = $selectedTreeKey;
160
161 // Search string given or regex search enabled?
162 $searchString = (string)($postValues['searchString'] ? trim($postValues['searchString']) : '');
163 $moduleState['regexSearch'] = (bool)($postValues['regexSearch'] ?? $moduleState['regexSearch'] ?? false);
164
165 // Prepare main array
166 if ($selectedTreeDetails['type'] === 'global') {
167 $globalArrayKey = $selectedTreeDetails['globalKey'];
168 $renderArray = $GLOBALS[$globalArrayKey];
169 $blindedConfigurationOptions = $this->blindedConfigurationOptions;
170 if (isset($blindedConfigurationOptions[$globalArrayKey])) {
171 // Prepare blinding for all database connection types
172 foreach (array_keys($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']) as $connectionName) {
173 if ($connectionName !== 'Default') {
174 $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] =
175 $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
176 }
177 }
178 ArrayUtility::mergeRecursiveWithOverrule(
179 $renderArray,
180 ArrayUtility::intersectRecursive($blindedConfigurationOptions[$globalArrayKey], $renderArray)
181 );
182 }
183 } elseif ($selectedTreeDetails['type'] === 'uc') {
184 $renderArray = $backendUser->uc;
185 } elseif ($selectedTreeDetails['type'] === 'routes') {
186 $router = GeneralUtility::makeInstance(Router::class);
187 $routes = $router->getRoutes();
188 $renderArray = [];
189 foreach ($routes as $identifier => $route) {
190 /** @var $route \TYPO3\CMS\Backend\Routing\Route */
191 $renderArray[$identifier] = [
192 'path' => $route->getPath(),
193 'options' => $route->getOptions()
194 ];
195 }
196 } else {
197 throw new \RuntimeException('Unknown array type "' . $selectedTreeDetails['type'] . '"', 1507845662);
198 }
199 ArrayUtility::naturalKeySortRecursive($renderArray);
200
201 // Prepare array renderer class, apply search and expand / collapse states
202 $arrayBrowser = GeneralUtility::makeInstance(ArrayBrowser::class);
203 $arrayBrowser->dontLinkVar = true;
204 $arrayBrowser->searchKeysToo = true;
205 $arrayBrowser->regexMode = $moduleState['regexSearch'];
206 $node = $queryParams['node'];
207 if ($searchString) {
208 $arrayBrowser->depthKeys = $arrayBrowser->getSearchKeys($renderArray, '', $searchString, []);
209 } elseif (is_array($node)) {
210 $newExpandCollapse = $arrayBrowser->depthKeys($node, $moduleState['node_' . $selectedTreeKey]);
211 $arrayBrowser->depthKeys = $newExpandCollapse;
212 $moduleState['node_' . $selectedTreeKey] = $newExpandCollapse;
213 } else {
214 $arrayBrowser->depthKeys = $moduleState['node_' . $selectedTreeKey] ?? [];
215 }
216
217 // Store new state
218 $backendUser->uc['moduleData']['system_config'] = $moduleState;
219 $backendUser->writeUC();
220
221 // Render main body
222 $view = GeneralUtility::makeInstance(StandaloneView::class);
223 $view->getRequest()->setControllerExtensionName('lowlevel');
224 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
225 'EXT:lowlevel/Resources/Private/Templates/Backend/Configuration.html'
226 ));
227 $view->assignMultiple([
228 'treeName' => $selectedTreeDetails['label'],
229 'searchString' => $searchString,
230 'regexSearch' => $moduleState['regexSearch'],
231 'tree' => $arrayBrowser->tree($renderArray, ''),
232 ]);
233
234 // Prepare module setup
235 $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
236 $moduleTemplate->setContent($view->render());
237 $moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lowlevel/ConfigurationView');
238
239 // Shortcut in doc header
240 $shortcutButton = $moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
241 $shortcutButton->setModuleName('system_config')
242 ->setDisplayName($languageService->sL(
243 'LLL:EXT:lowlevel/Resources/Private/Language/locallang.xlf:' . $selectedTreeDetails['label']
244 ))
245 ->setSetVariables(['tree']);
246 $moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortcutButton);
247
248 // Main drop down in doc header
249 $menu = $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
250 $menu->setIdentifier('tree');
251 foreach ($this->treeSetup as $treeKey => $treeDetails) {
252 $menuItem = $menu->makeMenuItem();
253 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
254 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
255 $menuItem->setHref((string)$uriBuilder->buildUriFromRoute('system_config', ['tree' => $treeKey]))
256 ->setTitle($languageService->sL(
257 'LLL:EXT:lowlevel/Resources/Private/Language/locallang.xlf:' . $treeDetails['label']
258 ));
259 if ($selectedTreeKey === $treeKey) {
260 $menuItem->setActive(true);
261 }
262 $menu->addMenuItem($menuItem);
263 }
264 $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
265
266 $response->getBody()->write($moduleTemplate->renderContent());
267 return $response;
268 }
269
270 /**
271 * Returns the Backend User
272 * @return BackendUserAuthentication
273 */
274 protected function getBackendUser()
275 {
276 return $GLOBALS['BE_USER'];
277 }
278
279 /**
280 * @return LanguageService
281 */
282 protected function getLanguageService()
283 {
284 return $GLOBALS['LANG'];
285 }
286 }