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