[TASK] Create own response instance in controller actions
[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\Cache\Backend\NullBackend;
24 use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
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 'loadedExt' => [
65 'label' => 'loadedExt',
66 'type' => 'global',
67 'globalKey' => 'TYPO3_LOADED_EXT',
68 ],
69 'services' => [
70 'label' => 't3services',
71 'key' => 'services',
72 'type' => 'global',
73 'globalKey' => 'T3_SERVICES',
74 ],
75 'tbeModules' => [
76 'label' => 'tbemodules',
77 'type' => 'global',
78 'globalKey' => 'TBE_MODULES',
79 ],
80 'tbeModulesExt' => [
81 'label' => 'tbemodulesext',
82 'type' => 'global',
83 'globalKey' => 'TBE_MODULES_EXT',
84 ],
85 'tbeStyles' => [
86 'label' => 'tbeStyles',
87 'type' => 'global',
88 'globalKey' => 'TBE_STYLES',
89 ],
90 'userSettings' => [
91 'label' => 'usersettings',
92 'type' => 'global',
93 'globalKey' => 'TYPO3_USER_SETTINGS',
94 ],
95 'pagesTypes' => [
96 'label' => 'pagesTypes',
97 'type' => 'global',
98 'globalKey' => 'PAGES_TYPES',
99 ],
100 'beUserUc' => [
101 'label' => 'beUser',
102 'type' => 'uc',
103 ],
104 'beRoutes' => [
105 'label' => 'routes',
106 'type' => 'routes',
107 ],
108 'httpMiddlewareStacks' => [
109 'label' => 'httpMiddlewareStacks',
110 'type' => 'httpMiddlewareStacks',
111 ],
112 ];
113
114 /**
115 * Blind configurations which should not be visible to mortal admins
116 *
117 * @var array
118 */
119 protected $blindedConfigurationOptions = [
120 'TYPO3_CONF_VARS' => [
121 'DB' => [
122 'database' => '******',
123 'host' => '******',
124 'password' => '******',
125 'port' => '******',
126 'socket' => '******',
127 'username' => '******',
128 'Connections' => [
129 'Default' => [
130 'dbname' => '******',
131 'host' => '******',
132 'password' => '******',
133 'port' => '******',
134 'user' => '******',
135 'unix_socket' => '******',
136 ],
137 ],
138 ],
139 'SYS' => [
140 'encryptionKey' => '******'
141 ],
142 ],
143 ];
144
145 /**
146 * Main controller action determines get/post values, takes care of
147 * stored backend user settings for this module, determines tree
148 * and renders it.
149 *
150 * @param ServerRequestInterface $request the current request
151 * @return ResponseInterface the response with the content
152 * @throws \RuntimeException
153 */
154 public function mainAction(ServerRequestInterface $request): ResponseInterface
155 {
156 $backendUser = $this->getBackendUser();
157 $languageService = $this->getLanguageService();
158
159 $queryParams = $request->getQueryParams();
160 $postValues = $request->getParsedBody();
161
162 $moduleState = $backendUser->uc['moduleData']['system_config'] ?? [];
163
164 // Determine validated tree key and tree detail setup
165 $selectedTreeKey = $this->treeSetup[$queryParams['tree']] ? $queryParams['tree']
166 : ($this->treeSetup[$moduleState['tree']] ? $moduleState['tree'] : key($this->treeSetup));
167 $selectedTreeDetails = $this->treeSetup[$selectedTreeKey];
168 $moduleState['tree'] = $selectedTreeKey;
169
170 // Search string given or regex search enabled?
171 $searchString = (string)($postValues['searchString'] ? trim($postValues['searchString']) : '');
172 $moduleState['regexSearch'] = (bool)($postValues['regexSearch'] ?? $moduleState['regexSearch'] ?? false);
173
174 // Prepare main array
175 $sortKeysByName = true;
176 if ($selectedTreeDetails['type'] === 'global') {
177 $globalArrayKey = $selectedTreeDetails['globalKey'];
178 $renderArray = $GLOBALS[$globalArrayKey];
179
180 // Hook for Processing blindedConfigurationOptions
181 $blindedConfigurationOptions = $this->blindedConfigurationOptions;
182
183 foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::class]['modifyBlindedConfigurationOptions'] ?? [] as $classReference) {
184 $processingObject = GeneralUtility::makeInstance($classReference);
185 $blindedConfigurationOptions = $processingObject->modifyBlindedConfigurationOptions($blindedConfigurationOptions, $this);
186 }
187
188 if (isset($blindedConfigurationOptions[$globalArrayKey])) {
189 // Prepare blinding for all database connection types
190 foreach (array_keys($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']) as $connectionName) {
191 if ($connectionName !== 'Default') {
192 $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections'][$connectionName] =
193 $blindedConfigurationOptions['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
194 }
195 }
196 ArrayUtility::mergeRecursiveWithOverrule(
197 $renderArray,
198 ArrayUtility::intersectRecursive($blindedConfigurationOptions[$globalArrayKey], $renderArray)
199 );
200 }
201 } elseif ($selectedTreeDetails['type'] === 'uc') {
202 $renderArray = $backendUser->uc;
203 } elseif ($selectedTreeDetails['type'] === 'routes') {
204 $router = GeneralUtility::makeInstance(Router::class);
205 $routes = $router->getRoutes();
206 $renderArray = [];
207 foreach ($routes as $identifier => $route) {
208 /** @var $route \TYPO3\CMS\Backend\Routing\Route */
209 $renderArray[$identifier] = [
210 'path' => $route->getPath(),
211 'options' => $route->getOptions()
212 ];
213 }
214 } elseif ($selectedTreeDetails['type'] === 'httpMiddlewareStacks') {
215 // Keep the order of the keys
216 $sortKeysByName = false;
217 // Fake a PHP frontend with a null backend to avoid PHP Opcache conflicts
218 // When using >requireOnce() multiple times in one request
219 $cache = GeneralUtility::makeInstance(
220 PhpFrontend::class,
221 'middleware',
222 GeneralUtility::makeInstance(NullBackend::class, 'Production')
223 );
224 $stackResolver = GeneralUtility::makeInstance(
225 MiddlewareStackResolver::class,
226 GeneralUtility::makeInstance(PackageManager::class),
227 GeneralUtility::makeInstance(DependencyOrderingService::class),
228 $cache
229 );
230 $renderArray = [];
231 foreach (['frontend', 'backend'] as $stackName) {
232 // reversing the array allows the admin to read the stack from top to bottom
233 $renderArray[$stackName] = array_reverse($stackResolver->resolve($stackName));
234 }
235 } else {
236 throw new \RuntimeException('Unknown array type "' . $selectedTreeDetails['type'] . '"', 1507845662);
237 }
238 if ($sortKeysByName) {
239 ArrayUtility::naturalKeySortRecursive($renderArray);
240 }
241
242 // Prepare array renderer class, apply search and expand / collapse states
243 $arrayBrowser = GeneralUtility::makeInstance(ArrayBrowser::class);
244 $arrayBrowser->dontLinkVar = true;
245 $arrayBrowser->searchKeysToo = true;
246 $arrayBrowser->regexMode = $moduleState['regexSearch'];
247 $node = $queryParams['node'];
248 if ($searchString) {
249 $arrayBrowser->depthKeys = $arrayBrowser->getSearchKeys($renderArray, '', $searchString, []);
250 } elseif (is_array($node)) {
251 $newExpandCollapse = $arrayBrowser->depthKeys($node, $moduleState['node_' . $selectedTreeKey]);
252 $arrayBrowser->depthKeys = $newExpandCollapse;
253 $moduleState['node_' . $selectedTreeKey] = $newExpandCollapse;
254 } else {
255 $arrayBrowser->depthKeys = $moduleState['node_' . $selectedTreeKey] ?? [];
256 }
257
258 // Store new state
259 $backendUser->uc['moduleData']['system_config'] = $moduleState;
260 $backendUser->writeUC();
261
262 // Render main body
263 $view = GeneralUtility::makeInstance(StandaloneView::class);
264 $view->getRequest()->setControllerExtensionName('lowlevel');
265 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
266 'EXT:lowlevel/Resources/Private/Templates/Backend/Configuration.html'
267 ));
268 $view->assignMultiple([
269 'treeName' => $selectedTreeDetails['label'],
270 'searchString' => $searchString,
271 'regexSearch' => $moduleState['regexSearch'],
272 'tree' => $arrayBrowser->tree($renderArray, ''),
273 ]);
274
275 // Prepare module setup
276 $moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
277 $moduleTemplate->setContent($view->render());
278 $moduleTemplate->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Lowlevel/ConfigurationView');
279
280 // Shortcut in doc header
281 $shortcutButton = $moduleTemplate->getDocHeaderComponent()->getButtonBar()->makeShortcutButton();
282 $shortcutButton->setModuleName('system_config')
283 ->setDisplayName($languageService->sL(
284 'LLL:EXT:lowlevel/Resources/Private/Language/locallang.xlf:' . $selectedTreeDetails['label']
285 ))
286 ->setSetVariables(['tree']);
287 $moduleTemplate->getDocHeaderComponent()->getButtonBar()->addButton($shortcutButton);
288
289 // Main drop down in doc header
290 $menu = $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
291 $menu->setIdentifier('tree');
292 foreach ($this->treeSetup as $treeKey => $treeDetails) {
293 $menuItem = $menu->makeMenuItem();
294 /** @var \TYPO3\CMS\Backend\Routing\UriBuilder $uriBuilder */
295 $uriBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Routing\UriBuilder::class);
296 $menuItem->setHref((string)$uriBuilder->buildUriFromRoute('system_config', ['tree' => $treeKey]))
297 ->setTitle($languageService->sL(
298 'LLL:EXT:lowlevel/Resources/Private/Language/locallang.xlf:' . $treeDetails['label']
299 ));
300 if ($selectedTreeKey === $treeKey) {
301 $menuItem->setActive(true);
302 }
303 $menu->addMenuItem($menuItem);
304 }
305 $moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
306
307 return new HtmlResponse($moduleTemplate->renderContent());
308 }
309
310 /**
311 * Returns the Backend User
312 * @return BackendUserAuthentication
313 */
314 protected function getBackendUser()
315 {
316 return $GLOBALS['BE_USER'];
317 }
318
319 /**
320 * @return LanguageService
321 */
322 protected function getLanguageService()
323 {
324 return $GLOBALS['LANG'];
325 }
326 }