4c5b340b286992b12bebd90e65585a41828a71c6
[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 TYPO3\CMS\Core\Utility\GeneralUtility;
18
19 /**
20 * Content parsing for htmlArea RTE
21 */
22 class ParseHtmlController {
23
24 /**
25 * @var string
26 */
27 public $content;
28
29 /**
30 * @var array
31 */
32 public $modData;
33
34 /**
35 * document template object
36 *
37 * @var \TYPO3\CMS\Backend\Template\DocumentTemplate
38 */
39 public $doc;
40
41 /**
42 * @var string
43 */
44 public $extKey = 'rtehtmlarea';
45
46 /**
47 * @var string
48 */
49 public $prefixId = 'TYPO3HtmlParser';
50
51 /**
52 * @return void
53 */
54 public function init() {
55 $this->doc = GeneralUtility::makeInstance(\TYPO3\CMS\Backend\Template\DocumentTemplate::class);
56 $this->doc->backPath = $GLOBALS['BACK_PATH'];
57 $this->doc->JScode = '';
58 $this->modData = $GLOBALS['BE_USER']->getModuleData($GLOBALS['MCONF']['name'], 'ses');
59 if (GeneralUtility::_GP('OC_key')) {
60 $parts = explode('|', GeneralUtility::_GP('OC_key'));
61 $this->modData['openKeys'][$parts[1]] = $parts[0] == 'O' ? 1 : 0;
62 $GLOBALS['BE_USER']->pushModuleData($GLOBALS['MCONF']['name'], $this->modData);
63 }
64 }
65
66 /**
67 * Main function
68 *
69 * @return void
70 */
71 public function main() {
72 $this->content .= $this->main_parse_html($this->modData['openKeys']);
73 header('Content-Type: text/plain; charset=utf-8');
74 }
75
76 /**
77 * Print content
78 *
79 * @return void
80 */
81 public function printContent() {
82 echo $this->content;
83 }
84
85 /**
86 * Rich Text Editor (RTE) html parser
87 *
88 * @param array $openKeys Unused
89 * @return string
90 */
91 public function main_parse_html($openKeys) {
92 $editorNo = GeneralUtility::_GP('editorNo');
93 $html = GeneralUtility::_GP('content');
94 $RTEtsConfigParts = explode(':', GeneralUtility::_GP('RTEtsConfigParams'));
95 $RTEsetup = $GLOBALS['BE_USER']->getTSConfig('RTE', \TYPO3\CMS\Backend\Utility\BackendUtility::getPagesTSconfig($RTEtsConfigParts[5]));
96 $thisConfig = \TYPO3\CMS\Backend\Utility\BackendUtility::RTEsetup($RTEsetup['properties'], $RTEtsConfigParts[0], $RTEtsConfigParts[2], $RTEtsConfigParts[4]);
97 $HTMLParser = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Html\HtmlParser::class);
98 if (is_array($thisConfig['enableWordClean.'])) {
99 $HTMLparserConfig = $thisConfig['enableWordClean.']['HTMLparser.'];
100 if (is_array($HTMLparserConfig)) {
101 $this->keepSpanTagsWithId($HTMLparserConfig);
102 $HTMLparserConfig = $HTMLParser->HTMLparserConfig($HTMLparserConfig);
103 }
104 }
105 if (is_array($HTMLparserConfig)) {
106 $html = $HTMLParser->HTMLcleaner($html, $HTMLparserConfig[0], $HTMLparserConfig[1], $HTMLparserConfig[2], $HTMLparserConfig[3]);
107 }
108 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'])) {
109 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'] as $classRef) {
110 $hookObj = GeneralUtility::getUserObj($classRef);
111 if (method_exists($hookObj, 'cleanPastedContent_afterCleanWord')) {
112 $html = $hookObj->cleanPastedContent_afterCleanWord($html, $thisConfig);
113 }
114 }
115 }
116 return $html;
117 }
118
119 /**
120 * Modify incoming HTMLparser config in an attempt to keep span tags with id
121 * Such tags are used by the RTE in order to restore the cursor position when the cleaning operation is completed.
122 *
123 * @param array $HTMLparserConfig: incoming HTMLParser configuration (wil be modified)
124 * @return void
125 */
126 protected function keepSpanTagsWithId(&$HTMLparserConfig) {
127 // Allow span tag
128 if (isset($HTMLparserConfig['allowTags'])) {
129 if (!GeneralUtility::inList($HTMLparserConfig['allowTags'], 'span')) {
130 $HTMLparserConfig['allowTags'] .= ',span';
131 }
132 } else {
133 $HTMLparserConfig['allowTags'] = 'span';
134 }
135 // Allow attributes on span tags
136 if (isset($HTMLparserConfig['noAttrib']) && GeneralUtility::inList($HTMLparserConfig['noAttrib'], 'span')) {
137 $HTMLparserConfig['noAttrib'] = GeneralUtility::rmFromList('span', $HTMLparserConfig['noAttrib']);
138 }
139 // Do not remove span tags
140 if (isset($HTMLparserConfig['removeTags']) && GeneralUtility::inList($HTMLparserConfig['removeTags'], 'span')) {
141 $HTMLparserConfig['removeTags'] = GeneralUtility::rmFromList('span', $HTMLparserConfig['removeTags']);
142 }
143 // Review the tags array
144 if (is_array($HTMLparserConfig['tags.'])) {
145 // Allow span tag
146 if (isset($HTMLparserConfig['tags.']['span']) && !$HTMLparserConfig['tags.']['span']) {
147 $HTMLparserConfig['tags.']['span'] = 1;
148 }
149 if (is_array($HTMLparserConfig['tags.']['span.'])) {
150 if (isset($HTMLparserConfig['tags.']['span.']['allowedAttribs'])) {
151 if (!$HTMLparserConfig['tags.']['span.']['allowedAttribs']) {
152 $HTMLparserConfig['tags.']['span.']['allowedAttribs'] = 'id';
153 } elseif (!GeneralUtility::inList($HTMLparserConfig['tags.']['span.']['allowedAttribs'], 'id')) {
154 $HTMLparserConfig['tags.']['span.']['allowedAttribs'] .= ',id';
155 }
156 }
157 if (isset($HTMLparserConfig['tags.']['span.']['fixAttrib.']['id.']['unset'])) {
158 unset($HTMLparserConfig['tags.']['span.']['fixAttrib.']['id.']['unset']);
159 }
160 }
161 }
162 }
163
164 }