[!!!][TASK] Harden \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
[Packages/TYPO3.CMS.git] / typo3 / sysext / tstemplate / Classes / Controller / TypoScriptTemplateConstantEditorModuleFunctionController.php
1 <?php
2 namespace TYPO3\CMS\Tstemplate\Controller;
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 Psr\Http\Message\ServerRequestInterface;
18 use TYPO3\CMS\Backend\Utility\BackendUtility;
19 use TYPO3\CMS\Core\DataHandling\DataHandler;
20 use TYPO3\CMS\Core\Localization\LanguageService;
21 use TYPO3\CMS\Core\Page\PageRenderer;
22 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Core\Utility\RootlineUtility;
25 use TYPO3\CMS\Fluid\View\StandaloneView;
26
27 /**
28 * TypoScript Constant editor
29 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
30 */
31 class TypoScriptTemplateConstantEditorModuleFunctionController
32 {
33
34 /**
35 * @var TypoScriptTemplateModuleController
36 */
37 protected $pObj;
38
39 /**
40 * The currently selected sys_template record
41 * @var array
42 */
43 protected $templateRow;
44
45 /**
46 * @var ExtendedTemplateService
47 */
48 protected $templateService;
49
50 /**
51 * @var array
52 */
53 protected $constants;
54
55 /**
56 * @var int GET/POST var 'id'
57 */
58 protected $id;
59
60 /**
61 * @var ServerRequestInterface
62 */
63 protected $request;
64
65 /**
66 * Init, called from parent object
67 *
68 * @param TypoScriptTemplateModuleController $pObj A reference to the parent (calling) object
69 * @param ServerRequestInterface $request
70 */
71 public function init($pObj, ServerRequestInterface $request)
72 {
73 $this->pObj = $pObj;
74 $this->request = $request;
75 $this->id = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
76 }
77
78 /**
79 * Initialize editor
80 *
81 * Initializes the module.
82 * Done in this function because we may need to re-initialize if data is submitted!
83 *
84 * @param int $pageId
85 * @param int $template_uid
86 * @return bool
87 */
88 protected function initialize_editor($pageId, $template_uid = 0)
89 {
90 $this->templateService = GeneralUtility::makeInstance(ExtendedTemplateService::class);
91
92 // Get the row of the first VISIBLE template of the page. whereclause like the frontend.
93 $this->templateRow = $this->templateService->ext_getFirstTemplate($pageId, $template_uid);
94 // IF there was a template...
95 if (is_array($this->templateRow)) {
96 // Gets the rootLine
97 $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId);
98 $rootLine = $rootlineUtility->get();
99 // This generates the constants/config + hierarchy info for the template.
100 $this->templateService->runThroughTemplates($rootLine, $template_uid);
101 // The editable constants are returned in an array.
102 $this->constants = $this->templateService->generateConfig_constants();
103 // The returned constants are sorted in categories, that goes into the $tmpl->categories array
104 $this->templateService->ext_categorizeEditableConstants($this->constants);
105 // This array will contain key=[expanded constant name], value=line number in template.
106 $this->templateService->ext_regObjectPositions($this->templateRow['constants']);
107 return true;
108 }
109 return false;
110 }
111
112 /**
113 * Main, called from parent object
114 *
115 * @return string
116 */
117 public function main()
118 {
119 $assigns = [];
120 $assigns['LLPrefix'] = 'LLL:EXT:tstemplate/Resources/Private/Language/locallang_ceditor.xlf:';
121 // Create extension template
122 $this->pObj->createTemplate($this->id);
123 // Checking for more than one template an if, set a menu...
124 $manyTemplatesMenu = $this->pObj->templateMenu($this->request);
125 $template_uid = 0;
126 if ($manyTemplatesMenu) {
127 $template_uid = $this->pObj->MOD_SETTINGS['templatesOnPage'];
128 }
129
130 // initialize
131 $existTemplate = $this->initialize_editor($this->id, $template_uid);
132 if ($existTemplate) {
133 $assigns['siteTitle'] = trim($this->templateRow['sitetitle']);
134 $assigns['templateRecord'] = $this->templateRow;
135 if ($manyTemplatesMenu) {
136 $assigns['manyTemplatesMenu'] = $manyTemplatesMenu;
137 }
138
139 $saveId = $this->templateRow['_ORIG_uid'] ?: $this->templateRow['uid'];
140 // Update template ?
141 if ($this->request->getParsedBody()['_savedok'] ?? false) {
142 $this->templateService->changed = 0;
143 $this->templateService->ext_procesInput($this->request->getParsedBody(), [], $this->constants, $this->templateRow);
144 if ($this->templateService->changed) {
145 // Set the data to be saved
146 $recData = [];
147 $recData['sys_template'][$saveId]['constants'] = implode($this->templateService->raw, LF);
148 // Create new tce-object
149 $tce = GeneralUtility::makeInstance(DataHandler::class);
150 $tce->start($recData, []);
151 $tce->process_datamap();
152 // Clear the cache (note: currently only admin-users can clear the cache in tce_main.php)
153 $tce->clear_cacheCmd('all');
154 // re-read the template ...
155 // re-read the constants as they have changed
156 $this->initialize_editor($this->id, $template_uid);
157 }
158 }
159 // Resetting the menu (start). I wonder if this in any way is a violation of the menu-system. Haven't checked. But need to do it here, because the menu is dependent on the categories available.
160 $this->pObj->MOD_MENU['constant_editor_cat'] = $this->templateService->ext_getCategoryLabelArray();
161 $this->pObj->MOD_SETTINGS = BackendUtility::getModuleData($this->pObj->MOD_MENU, $this->request->getParsedBody()['SET'] ?? $this->request->getQueryParams()['SET'] ?? [], 'web_ts');
162 // Resetting the menu (stop)
163 $assigns['title'] = $this->pObj->linkWrapTemplateTitle($this->templateRow['title'], 'constants');
164 if (!empty($this->pObj->MOD_MENU['constant_editor_cat'])) {
165 $assigns['constantsMenu'] = BackendUtility::getDropdownMenu($this->id, 'SET[constant_editor_cat]', $this->pObj->MOD_SETTINGS['constant_editor_cat'], $this->pObj->MOD_MENU['constant_editor_cat']);
166 }
167 // Category and constant editor config:
168 $category = $this->pObj->MOD_SETTINGS['constant_editor_cat'];
169
170 $printFields = trim($this->templateService->ext_printFields($this->constants, $category));
171 foreach ($this->templateService->getInlineJavaScript() as $name => $inlineJavaScript) {
172 $this->getPageRenderer()->addJsInlineCode($name, $inlineJavaScript);
173 }
174
175 if ($printFields) {
176 $assigns['printFields'] = $printFields;
177 }
178 // Rendering of the output via fluid
179 $view = GeneralUtility::makeInstance(StandaloneView::class);
180 $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
181 'EXT:tstemplate/Resources/Private/Templates/ConstantEditor.html'
182 ));
183 $view->assignMultiple($assigns);
184 $theOutput = $view->render();
185 } else {
186 $theOutput = $this->pObj->noTemplate(1);
187 }
188 return $theOutput;
189 }
190
191 /**
192 * @return LanguageService
193 */
194 protected function getLanguageService(): LanguageService
195 {
196 return $GLOBALS['LANG'];
197 }
198
199 /**
200 * @return PageRenderer
201 */
202 protected function getPageRenderer(): PageRenderer
203 {
204 return GeneralUtility::makeInstance(PageRenderer::class);
205 }
206 }