Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / mod6 / class.tx_rtehtmlarea_parse_html.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005-2011 Stanislas Rolland <typo3(arobas)sjbr.ca>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Content parsing for htmlArea RTE
29 *
30 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
31 *
32 * $Id$ *
33 */
34
35 class tx_rtehtmlarea_parse_html {
36 var $content;
37 var $modData;
38
39 /**
40 * document template object
41 *
42 * @var template
43 */
44 var $doc;
45 var $extKey = 'rtehtmlarea';
46 var $prefixId = 'TYPO3HtmlParser';
47
48 /**
49 * @return [type] ...
50 */
51 function init() {
52 global $BE_USER,$BACK_PATH,$MCONF;
53
54 $this->doc = t3lib_div::makeInstance('template');
55 $this->doc->backPath = $BACK_PATH;
56 $this->doc->JScode='';
57
58 $this->modData = $BE_USER->getModuleData($MCONF['name'],'ses');
59 if (t3lib_div::_GP('OC_key')) {
60 $parts = explode('|',t3lib_div::_GP('OC_key'));
61 $this->modData['openKeys'][$parts[1]] = $parts[0]=='O' ? 1 : 0;
62 $BE_USER->pushModuleData($MCONF['name'],$this->modData);
63 }
64 }
65
66 /**
67 * [Describe function...]
68 *
69 * @return [type] ...
70 */
71 function main() {
72 global $LANG;
73
74 $this->content .= $this->main_parse_html($this->modData['openKeys']);
75
76 // if no HTTP input conversion is configured, the input was uft-8 (urlencoded).
77 $fromCharSet = 'utf-8';
78 // if conversion was done, the input is encoded in mbstring.internal_encoding
79 if (in_array('mbstring', get_loaded_extensions()) && ini_get('mbstring.encoding_translation')) {
80 $fromCharSet = strToLower(ini_get('mbstring.internal_encoding'));
81 }
82
83 $clientInfo = t3lib_div::clientInfo();
84 // the charset of the content element, possibly overidden by forceCharset
85 $toCharSet = t3lib_div::_GP('charset')?t3lib_div::_GP('charset'):'iso-8859-1';
86 // IE wants it back in utf-8
87 if ( $clientInfo['BROWSER']= 'msie') {
88 $toCharSet = 'utf-8';
89 } elseif ($clientInfo['SYSTEM'] = 'win') {
90 // if the client is windows the input may contain windows-1252 characters;
91 if (strToLower($toCharSet) == 'iso-8859-1') {
92 $toCharSet = 'Windows-1252';
93 }
94 }
95 // convert to requested charset
96 $this->content = $LANG->csConvObj->conv($this->content, $fromCharSet, $toCharSet);
97 header('Content-Type: text/plain; charset='.$toCharSet);
98 }
99
100 /**
101 * [Describe function...]
102 *
103 * @return [type] ...
104 */
105 function printContent() {
106 echo $this->content;
107 }
108
109 /**
110 * Rich Text Editor (RTE) html parser
111 *
112 * @param [type] $openKeys: ...
113 * @return [type] ...
114 */
115 function main_parse_html($openKeys) {
116 global $BE_USER, $TYPO3_CONF_VARS;
117
118 $editorNo = t3lib_div::_GP('editorNo');
119 $html = t3lib_div::_GP('content');
120
121 $RTEtsConfigParts = explode(':',t3lib_div::_GP('RTEtsConfigParams'));
122 $RTEsetup = $BE_USER->getTSConfig('RTE',t3lib_BEfunc::getPagesTSconfig($RTEtsConfigParts[5]));
123 $thisConfig = t3lib_BEfunc::RTEsetup($RTEsetup['properties'],$RTEtsConfigParts[0],$RTEtsConfigParts[2],$RTEtsConfigParts[4]);
124
125 $HTMLParser = t3lib_div::makeInstance('t3lib_parsehtml');
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
137 if (is_array ($TYPO3_CONF_VARS['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'])) {
138 foreach ($TYPO3_CONF_VARS['EXTCONF'][$this->extKey][$this->prefixId]['cleanPastedContent'] as $classRef) {
139 $hookObj = t3lib_div::getUserObj($classRef);
140 if (method_exists($hookObj, 'cleanPastedContent_afterCleanWord')) {
141 $html = $hookObj->cleanPastedContent_afterCleanWord($html, $thisConfig);
142 }
143 }
144 }
145 return $html;
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 (!t3lib_div::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']) && t3lib_div::inList($HTMLparserConfig['noAttrib'], 'span')) {
165 $HTMLparserConfig['noAttrib'] = t3lib_div::rmFromList('span', $HTMLparserConfig['noAttrib']);
166 }
167 // Do not remove span tags
168 if (isset($HTMLparserConfig['removeTags']) && t3lib_div::inList($HTMLparserConfig['removeTags'], 'span')) {
169 $HTMLparserConfig['removeTags'] = t3lib_div::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 } else if (!t3lib_div::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 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/mod6/class.tx_rtehtmlarea_parse_html.php'])) {
193 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/rtehtmlarea/mod6/class.tx_rtehtmlarea_parse_html.php']);
194 }
195 ?>