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