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