[!!!][TASK] Removed deprecated code from EXT:rtehtmlarea
[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 * Rich Text Editor (RTE) html parser
102 *
103 * @param array $openKeys Unused
104 * @return string
105 */
106 public function main_parse_html($openKeys)
107 {
108 $editorNo = GeneralUtility::_GP('editorNo');
109 $html = GeneralUtility::_GP('content');
110 $RTEtsConfigParts = explode(':', GeneralUtility::_GP('RTEtsConfigParams'));
111 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
112 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
113 $HTMLParser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Html\HtmlParser::class);
114 if (is_array($thisConfig['enableWordClean.'])) {
115 $HTMLparserConfig = $thisConfig['enableWordClean.']['HTMLparser.'];
116 if (is_array($HTMLparserConfig)) {
117 $this->keepSpanTagsWithId($HTMLparserConfig);
118 $HTMLparserConfig = $HTMLParser->HTMLparserConfig($HTMLparserConfig);
119 }
120 }
121 if (is_array($HTMLparserConfig)) {
122 $html = $HTMLParser->HTMLcleaner($html, $HTMLparserConfig[0], $HTMLparserConfig[1], $HTMLparserConfig[2], $HTMLparserConfig[3]);
123 }
124 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'])) {
125 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'] as $classRef) {
126 $hookObj = GeneralUtility::getUserObj($classRef);
127 if (method_exists($hookObj, 'cleanPastedContent_afterCleanWord')) {
128 $html = $hookObj->cleanPastedContent_afterCleanWord($html, $thisConfig);
129 }
130 }
131 }
132 return $html;
133 }
134
135 /**
136 * Modify incoming HTMLparser config in an attempt to keep span tags with id
137 * Such tags are used by the RTE in order to restore the cursor position when the cleaning operation is completed.
138 *
139 * @param array $HTMLparserConfig: incoming HTMLParser configuration (wil be modified)
140 * @return void
141 */
142 protected function keepSpanTagsWithId(&$HTMLparserConfig)
143 {
144 // Allow span tag
145 if (isset($HTMLparserConfig['allowTags'])) {
146 if (!GeneralUtility::inList($HTMLparserConfig['allowTags'], 'span')) {
147 $HTMLparserConfig['allowTags'] .= ',span';
148 }
149 } else {
150 $HTMLparserConfig['allowTags'] = 'span';
151 }
152 // Allow attributes on span tags
153 if (isset($HTMLparserConfig['noAttrib']) && GeneralUtility::inList($HTMLparserConfig['noAttrib'], 'span')) {
154 $HTMLparserConfig['noAttrib'] = GeneralUtility::rmFromList('span', $HTMLparserConfig['noAttrib']);
155 }
156 // Do not remove span tags
157 if (isset($HTMLparserConfig['removeTags']) && GeneralUtility::inList($HTMLparserConfig['removeTags'], 'span')) {
158 $HTMLparserConfig['removeTags'] = GeneralUtility::rmFromList('span', $HTMLparserConfig['removeTags']);
159 }
160 // Review the tags array
161 if (is_array($HTMLparserConfig['tags.'])) {
162 // Allow span tag
163 if (isset($HTMLparserConfig['tags.']['span']) && !$HTMLparserConfig['tags.']['span']) {
164 $HTMLparserConfig['tags.']['span'] = 1;
165 }
166 if (is_array($HTMLparserConfig['tags.']['span.'])) {
167 if (isset($HTMLparserConfig['tags.']['span.']['allowedAttribs'])) {
168 if (!$HTMLparserConfig['tags.']['span.']['allowedAttribs']) {
169 $HTMLparserConfig['tags.']['span.']['allowedAttribs'] = 'id';
170 } elseif (!GeneralUtility::inList($HTMLparserConfig['tags.']['span.']['allowedAttribs'], 'id')) {
171 $HTMLparserConfig['tags.']['span.']['allowedAttribs'] .= ',id';
172 }
173 }
174 if (isset($HTMLparserConfig['tags.']['span.']['fixAttrib.']['id.']['unset'])) {
175 unset($HTMLparserConfig['tags.']['span.']['fixAttrib.']['id.']['unset']);
176 }
177 }
178 }
179 }
180 }