[TASK] Add backend routes for wizards
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / ParseHtmlController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\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 Psr\Http\Message\ResponseInterface;
19 use TYPO3\CMS\Core\Http\Response;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Content parsing for htmlArea RTE
24 */
25 class ParseHtmlController implements \TYPO3\CMS\Core\Http\ControllerInterface {
26
27 /**
28 * @var string
29 */
30 public $content;
31
32 /**
33 * @var array
34 */
35 public $modData;
36
37 /**
38 * document template object
39 *
40 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
41 */
42 public $doc;
43
44 /**
45 * @var string
46 */
47 public $extKey = 'rtehtmlarea';
48
49 /**
50 * @var string
51 */
52 public $prefixId = 'TYPO3HtmlParser';
53
54 /**
55 * Initialize
56 */
57 public function __construct() {
58 $this->init();
59 }
60
61 /**
62 * @return void
63 */
64 public function init() {
65 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
66 $this->doc->JScode = '';
67 $this->modData = $GLOBALS['BE_USER']->getModuleData($GLOBALS['MCONF']['name'], 'ses');
68 if (GeneralUtility::_GP('OC_key')) {
69 $parts = explode('|', GeneralUtility::_GP('OC_key'));
70 $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
71 $GLOBALS['BE_USER']->pushModuleData($GLOBALS['MCONF']['name'], $this->modData);
72 }
73 }
74
75 /**
76 * Injects the request object for the current request or subrequest
77 * As this controller goes only through the main_parse_html() method, it is rather simple for now
78 *
79 * @param ServerRequestInterface $request
80 * @return ResponseInterface $response
81 */
82 public function processRequest(ServerRequestInterface $request) {
83 $this->content .= $this->main_parse_html($this->modData['openKeys']);
84
85 /** @var Response $response */
86 $response = GeneralUtility::makeInstance(Response::class);
87 $response->getBody()->write($this->content);
88 $response = $response->withHeader('Content-Type', 'text/plain; charset=utf-8');
89 return $response;
90 }
91
92 /**
93 * Main function
94 *
95 * @return void
96 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use main_parse_html() instead
97 */
98 public function main() {
99 GeneralUtility::logDeprecatedFunction();
100 $this->content .= $this->main_parse_html($this->modData['openKeys']);
101 header('Content-Type: text/plain; charset=utf-8');
102 }
103
104 /**
105 * Print content
106 *
107 * @return void
108 * @deprecated since TYPO3 CMS 7, will be removed in TYPO3 CMS 8, use processRequest() instead
109 */
110 public function printContent() {
111 GeneralUtility::logDeprecatedFunction();
112 echo $this->content;
113 }
114
115 /**
116 * Rich Text Editor (RTE) html parser
117 *
118 * @param array $openKeys Unused
119 * @return string
120 */
121 public function main_parse_html($openKeys) {
122 $editorNo = GeneralUtility::_GP('editorNo');
123 $html = GeneralUtility::_GP('content');
124 $RTEtsConfigParts = explode(':', GeneralUtility::_GP('RTEtsConfigParams'));
125 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
126 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
127 $HTMLParser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Html\HtmlParser::class);
128 if (is_array($thisConfig['enableWordClean.'])) {
129 $HTMLparserConfig = $thisConfig['enableWordClean.']['HTMLparser.'];
130 if (is_array($HTMLparserConfig)) {
131 $this->keepSpanTagsWithId($HTMLparserConfig);
132 $HTMLparserConfig = $HTMLParser->HTMLparserConfig($HTMLparserConfig);
133 }
134 }
135 if (is_array($HTMLparserConfig)) {
136 $html = $HTMLParser->HTMLcleaner($html, $HTMLparserConfig[0], $HTMLparserConfig[1], $HTMLparserConfig[2], $HTMLparserConfig[3]);
137 }
138 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'])) {
139 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'] as $classRef) {
140 $hookObj = GeneralUtility::getUserObj($classRef);
141 if (method_exists($hookObj, 'cleanPastedContent_afterCleanWord')) {
142 $html = $hookObj->cleanPastedContent_afterCleanWord($html, $thisConfig);
143 }
144 }
145 }
146 return $html;
147 }
148
149 /**
150 * Modify incoming HTMLparser config in an attempt to keep span tags with id
151 * Such tags are used by the RTE in order to restore the cursor position when the cleaning operation is completed.
152 *
153 * @param array $HTMLparserConfig: incoming HTMLParser configuration (wil be modified)
154 * @return void
155 */
156 protected function keepSpanTagsWithId(&$HTMLparserConfig) {
157 // Allow span tag
158 if (isset($HTMLparserConfig['allowTags'])) {
159 if (!GeneralUtility::inList($HTMLparserConfig['allowTags'], 'span')) {
160 $HTMLparserConfig['allowTags'] .= ',span';
161 }
162 } else {
163 $HTMLparserConfig['allowTags'] = 'span';
164 }
165 // Allow attributes on span tags
166 if (isset($HTMLparserConfig['noAttrib']) && GeneralUtility::inList($HTMLparserConfig['noAttrib'], 'span')) {
167 $HTMLparserConfig['noAttrib'] = GeneralUtility::rmFromList('span', $HTMLparserConfig['noAttrib']);
168 }
169 // Do not remove span tags
170 if (isset($HTMLparserConfig['removeTags']) && GeneralUtility::inList($HTMLparserConfig['removeTags'], 'span')) {
171 $HTMLparserConfig['removeTags'] = GeneralUtility::rmFromList('span', $HTMLparserConfig['removeTags']);
172 }
173 // Review the tags array
174 if (is_array($HTMLparserConfig['tags.'])) {
175 // Allow span tag
176 if (isset($HTMLparserConfig['tags.']['span']) && !$HTMLparserConfig['tags.']['span']) {
177 $HTMLparserConfig['tags.']['span'] = 1;
178 }
179 if (is_array($HTMLparserConfig['tags.']['span.'])) {
180 if (isset($HTMLparserConfig['tags.']['span.']['allowedAttribs'])) {
181 if (!$HTMLparserConfig['tags.']['span.']['allowedAttribs']) {
182 $HTMLparserConfig['tags.']['span.']['allowedAttribs'] = 'id';
183 } elseif (!GeneralUtility::inList($HTMLparserConfig['tags.']['span.']['allowedAttribs'], 'id')) {
184 $HTMLparserConfig['tags.']['span.']['allowedAttribs'] .= ',id';
185 }
186 }
187 if (isset($HTMLparserConfig['tags.']['span.']['fixAttrib.']['id.']['unset'])) {
188 unset($HTMLparserConfig['tags.']['span.']['fixAttrib.']['id.']['unset']);
189 }
190 }
191 }
192 }
193
194 }