Initial revision
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_parsehtml.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skårhøj (kasper@typo3.com)
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 * Contains class with functions for parsing HTML code.
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasper@typo3.com>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 99: class t3lib_parsehtml
42 * 116: function getSubpart($content, $marker)
43 * 144: function substituteSubpart($content,$marker,$subpartContent,$recursive=1,$keepMarker=0)
44 * 209: function splitIntoBlock($tag,$content,$eliminateExtraEndTags=0)
45 * 268: function splitTags($tag,$content)
46 * 302: function getAllParts($parts,$tag_parts=1,$include_tag=1)
47 * 321: function removeFirstAndLastTag($str)
48 * 340: function getFirstTag($str)
49 * 354: function getFirstTagName($str)
50 * 372: function checkTagTypeCounts($content,$blockTags='a,b,blockquote,body,div,em,font,form,h1,h2,h3,h4,h5,h6,i,li,map,ol,option,p,pre,select,span,strong,table,td,textarea,tr,u,ul', $soloTags='br,hr,img,input,area')
51 * 446: function HTMLcleaner($content, $tags=array(),$keepAll=0,$hSC=0,$addConfig=array())
52 * 617: function get_tag_attributes($tag,$deHSC=0)
53 * 658: function split_tag_attributes($tag)
54 * 695: function bidir_htmlspecialchars($value,$dir)
55 * 715: function prefixResourcePath($main_prefix,$content,$alternatives=array())
56 * 783: function prefixRelPath($prefix,$srcVal)
57 * 801: function cleanFontTags($value,$keepFace=0,$keepSize=0,$keepColor=0)
58 * 832: function mapTags($value,$tags=array(),$ltChar='<',$ltChar2='<')
59 * 849: function unprotectTags($content,$tagList='')
60 * 882: function stripTagsExcept($value,$tagList)
61 * 905: function caseShift($str,$flag,$cacheKey='')
62 * 929: function compileTagAttribs($tagAttrib,$meta=array(), $xhtmlClean=0)
63 * 958: function get_tag_attributes_classic($tag,$deHSC=0)
64 * 971: function HTMLparserConfig($TSconfig,$keepTags=array())
65 * 1084: function XHTML_clean($content)
66 * 1106: function processTag($value,$conf,$endTag,$protected=0)
67 * 1152: function processContent($value,$dir,$conf)
68 *
69 * TOTAL FUNCTIONS: 26
70 * (This index is automatically created/updated by the extension "extdeveval")
71 *
72 */
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93 /**
94 * Functions for parsing HTML.
95 * You are encouraged to use this class in your own applications
96 *
97 * @author Kasper Skårhøj <kasper@typo3.com>
98 */
99 class t3lib_parsehtml {
100 var $caseShift_cache=array();
101
102
103 // *******************************************'
104 // COPY FROM class.tslib_content.php: / BEGIN
105 // substituteSubpart
106 // Cleaned locally 2/2003 !!!! (so different from tslib_content version)
107 // *******************************************'
108
109 /**
110 * Returns the first subpart encapsulated in the marker, $marker (possibly present in $content as a HTML comment)
111 *
112 * @param string Content with subpart wrapped in fx. "###CONTENT_PART###" inside.
113 * @param string Marker string, eg. "###CONTENT_PART###"
114 * @return string
115 */
116 function getSubpart($content, $marker) {
117 if ($marker && strstr($content,$marker)) {
118 $start = strpos($content, $marker)+strlen($marker);
119 $stop = @strpos($content, $marker, $start+1);
120 $sub = substr($content, $start, $stop-$start);
121
122 $reg=Array();
123 ereg('^[^<]*-->',$sub,$reg);
124 $start+=strlen($reg[0]);
125
126 $reg=Array();
127 ereg('<!--[^>]*$',$sub,$reg);
128 $stop-=strlen($reg[0]);
129
130 return substr($content, $start, $stop-$start);
131 }
132 }
133
134 /**
135 * Substitutes a subpart in $content with the content of $subpartContent.
136 *
137 * @param string Content with subpart wrapped in fx. "###CONTENT_PART###" inside.
138 * @param string Marker string, eg. "###CONTENT_PART###"
139 * @param array If $subpartContent happens to be an array, it's [0] and [1] elements are wrapped around the content of the subpart (fetched by getSubpart())
140 * @param boolean If $recursive is set, the function calls itself with the content set to the remaining part of the content after the second marker. This means that proceding subparts are ALSO substituted!
141 * @param boolean If set, the marker around the subpart is not removed, but kept in the output
142 * @return string Processed input content
143 */
144 function substituteSubpart($content,$marker,$subpartContent,$recursive=1,$keepMarker=0) {
145 $start = strpos($content, $marker);
146 $stop = @strpos($content, $marker, $start+1)+strlen($marker);
147 if ($start && $stop>$start) {
148 // code before
149 $before = substr($content, 0, $start);
150 $reg=Array();
151 ereg('<!--[^>]*$',$before,$reg);
152 $start-=strlen($reg[0]);
153 if ($keepMarker) {
154 $reg_k=Array();
155 if ($reg[0]) ereg('^[^>]*-->',substr($content,$start),$reg_k);
156 $before_marker = substr($content, $start, strlen($reg_k[0]?$reg_k[0]:$marker));
157 }
158 $before = substr($content, 0, $start);
159 // code after
160 $after = substr($content, $stop);
161 $reg=Array();
162 ereg('^[^<]*-->',$after,$reg);
163 $stop+=strlen($reg[0]);
164 if ($keepMarker) {
165 $reg_k=Array();
166 if ($reg[0]) ereg('<!--[^<]*$',substr($content,0,$stop),$reg_k);
167 $sLen = strlen($reg_k[0]?$reg_k[0]:$marker);
168 $after_marker = substr($content, $stop-$sLen,$sLen);
169 }
170 $after = substr($content, $stop);
171
172
173 // replace?
174 if (is_array($subpartContent)) {
175 $substContent=$subpartContent[0].$this->getSubpart($content,$marker).$subpartContent[1];
176 } else {
177 $substContent=$subpartContent;
178 }
179
180 if ($recursive && strpos($after, $marker)) {
181 return $before.($keepMarker?$before_marker:'').$substContent.($keepMarker?$after_marker:'').$this->substituteSubpart($after,$marker,$subpartContent);
182 } else {
183 return $before.($keepMarker?$before_marker:'').$substContent.($keepMarker?$after_marker:'').$after;
184 }
185 } else {
186 return $content;
187 }
188 }
189 // *******************************************'
190 // COPY FROM class.tslib_content.php: / END
191 // *******************************************'
192
193
194
195
196
197
198 /**
199 * Returns an array with the $content divided by tag-blocks specified with the list of tags, $tag
200 * Even numbers in the array are outside the blocks, Odd numbers are block-content.
201 * Use ->getAllParts() and ->removeFirstAndLastTag() to process the content if needed.
202 *
203 * @param string List of tags, comma separated.
204 * @param string HTML-content
205 * @param boolean If set, excessive end tags are ignored - you should probably set this in most cases.
206 * @return array Even numbers in the array are outside the blocks, Odd numbers are block-content.
207 * @see splitTags(), getAllParts(), removeFirstAndLastTag()
208 */
209 function splitIntoBlock($tag,$content,$eliminateExtraEndTags=0) {
210 $tags=array_unique(t3lib_div::trimExplode(',',$tag,1));
211 $regexStr = '</?('.implode('|',$tags).')(>|[[:space:]][^>]*>)';
212
213 $parts = spliti($regexStr,$content);
214
215 $newParts=array();
216 $pointer=strlen($parts[0]);
217 $buffer=$parts[0];
218 $nested=0;
219 reset($parts);
220 next($parts);
221 while(list($k,$v)=each($parts)) {
222 $isEndTag= substr($content,$pointer,2)=='</' ? 1 : 0;
223 $tagLen = strcspn(substr($content,$pointer),'>')+1;
224
225 if (!$isEndTag) { // We meet a start-tag:
226 if (!$nested) { // Ground level:
227 $newParts[]=$buffer; // previous buffer stored
228 $buffer='';
229 }
230 $nested++; // We are inside now!
231 $mbuffer=substr($content,$pointer,strlen($v)+$tagLen); // New buffer set and pointer increased
232 $pointer+=strlen($mbuffer);
233 $buffer.=$mbuffer;
234 } else { // If we meet an endtag:
235 $nested--; // decrease nested-level
236 $eliminated=0;
237 if ($eliminateExtraEndTags && $nested<0) {
238 $nested=0;
239 $eliminated=1;
240 } else {
241 $buffer.=substr($content,$pointer,$tagLen); // In any case, add the endtag to current buffer and increase pointer
242 }
243 $pointer+=$tagLen;
244 if (!$nested && !$eliminated) { // if we're back on ground level, (and not by eliminating tags...
245 $newParts[]=$buffer;
246 $buffer='';
247 }
248 $mbuffer=substr($content,$pointer,strlen($v)); // New buffer set and pointer increased
249 $pointer+=strlen($mbuffer);
250 $buffer.=$mbuffer;
251 }
252
253 }
254 $newParts[]=$buffer;
255 return $newParts;
256 }
257
258 /**
259 * Returns an array with the $content divided by tag-blocks specified with the list of tags, $tag
260 * Even numbers in the array are outside the blocks, Odd numbers are block-content.
261 * Use ->getAllParts() and ->removeFirstAndLastTag() to process the content if needed.
262 *
263 * @param string List of tags
264 * @param string HTML-content
265 * @return array Even numbers in the array are outside the blocks, Odd numbers are block-content.
266 * @see splitIntoBlock(), getAllParts(), removeFirstAndLastTag()
267 */
268 function splitTags($tag,$content) {
269 $tags=t3lib_div::trimExplode(',',$tag,1);
270 $regexStr = '<('.implode('|',$tags).')(>|[[:space:]][^>]*>)';
271 $parts = spliti($regexStr,$content);
272
273 $pointer=strlen($parts[0]);
274 $newParts=array();
275 $newParts[]=$parts[0];
276 reset($parts);
277 next($parts);
278 while(list($k,$v)=each($parts)) {
279 $tagLen = strcspn(substr($content,$pointer),'>')+1;
280
281 // Set tag:
282 $tag = substr($content,$pointer,$tagLen); // New buffer set and pointer increased
283 $newParts[]=$tag;
284 $pointer+=strlen($tag);
285
286 // Set content:
287 $newParts[]=$v;
288 $pointer+=strlen($v);
289 }
290 return $newParts;
291 }
292
293 /**
294 * Returns an array with either tag or non-tag content of the result from ->splitIntoBlock()/->splitTags()
295 *
296 * @param array Parts generated by ->splitIntoBlock() or >splitTags()
297 * @param boolean Whether to return the tag-parts (default,true) or what was outside the tags.
298 * @param boolean Whether to include the tags in the tag-parts (most useful for input made by ->splitIntoBlock())
299 * @return array Tag-parts/Non-tag-parts depending on input argument settings
300 * @see splitIntoBlock(), splitTags()
301 */
302 function getAllParts($parts,$tag_parts=1,$include_tag=1) {
303 reset($parts);
304 $newParts=array();
305 while(list($k,$v)=each($parts)) {
306 if (($k+($tag_parts?0:1))%2) {
307 if (!$include_tag) $v=$this->removeFirstAndLastTag($v);
308 $newParts[]=$v;
309 }
310 }
311 return $newParts;
312 }
313
314 /**
315 * Removes the first and last tag in the string
316 * Anything before and after the first and last tags respectively is also removed
317 *
318 * @param string
319 * @return string
320 */
321 function removeFirstAndLastTag($str) {
322 // First:
323 $endLen = strcspn($str,'>')+1;
324 $str = substr($str,$endLen);
325 // Last:
326 $str = strrev($str);
327 $endLen = strcspn($str,'<')+1;
328 $str = substr($str,$endLen);
329 // return
330 return strrev($str);
331 }
332
333 /**
334 * Returns the first tag in $str
335 * Actually everything from the begining of the $str is returned, so you better make sure the tag is the first thing...
336 *
337 * @param string
338 * @return string
339 */
340 function getFirstTag($str) {
341 // First:
342 $endLen = strcspn($str,'>')+1;
343 $str = substr($str,0,$endLen);
344 return $str;
345 }
346
347 /**
348 * Returns the NAME of the first tag in $str
349 *
350 * @param string HTML tag (The element name MUST be separated from the attributes by a space character! Just *whitespace* will not do)
351 * @return string Tag name in upper case
352 * @see getFirstTag()
353 */
354 function getFirstTagName($str) {
355 list($tag) = split('[[:space:]]',substr(strtoupper(trim($this->getFirstTag($str))),1,-1), 2);
356 return trim($tag);
357 }
358
359 /**
360 * Checks whether block/solo tags are found in the correct amounts in HTML content
361 * Block tags are tags which are required to have an equal amount of start and end tags, eg. "<table>...</table>"
362 * Solo tags are tags which are required to have ONLY start tags (possibly with an XHTML ending like ".../>")
363 * NOTICE: Correct XHTML might actually fail since "<br></br>" is allowed as well as "<br/>". However only the LATTER is accepted by this function (with "br" in the "solo-tag" list), the first example will result in a warning.
364 * NOTICE: Correct XHTML might actually fail since "<p/>" is allowed as well as "<p></p>". However only the LATTER is accepted by this function (with "p" in the "block-tag" list), the first example will result in an ERROR!
365 * NOTICE: Correct HTML version "something" allows eg. <p> and <li> to be NON-ended (implicitly ended by other tags). However this is NOT accepted by this function (with "p" and "li" in the block-tag list) and it will result in an ERROR!
366 *
367 * @param string HTML content to analyze
368 * @param string Tag names for block tags (eg. table or div or p) in lowercase, commalist (eg. "table,div,p")
369 * @param string Tag names for solo tags (eg. img, br or input) in lowercase, commalist ("img,br,input")
370 * @return array Analyse data.
371 */
372 function checkTagTypeCounts($content,$blockTags='a,b,blockquote,body,div,em,font,form,h1,h2,h3,h4,h5,h6,i,li,map,ol,option,p,pre,select,span,strong,table,td,textarea,tr,u,ul', $soloTags='br,hr,img,input,area') {
373 $content = strtolower($content);
374 $analyzedOutput=array();
375 $analyzedOutput['counts']=array(); // Counts appearances of start-tags
376 $analyzedOutput['errors']=array(); // Lists ERRORS
377 $analyzedOutput['warnings']=array(); // Lists warnings.
378 $analyzedOutput['blocks']=array(); // Lists stats for block-tags
379 $analyzedOutput['solo']=array(); // Lists stats for solo-tags
380
381 // Block tags, must have endings...
382 $blockTags = explode(',',$blockTags);
383 foreach($blockTags as $tagName) {
384 $countBegin = count(split('<'.$tagName.'[^[:alnum:]]',$content))-1;
385 $countEnd = count(split('<\/'.$tagName.'[^[:alnum:]]',$content))-1;
386 $analyzedOutput['blocks'][$tagName]=array($countBegin,$countEnd,$countBegin-$countEnd);
387 if ($countBegin) $analyzedOutput['counts'][$tagName]=$countBegin;
388 if ($countBegin-$countEnd) {
389 if ($countBegin-$countEnd > 0) {
390 $analyzedOutput['errors'][$tagName]='There were more start-tags ('.$countBegin.') than end-tags ('.$countEnd.') for the element "'.$tagName.'". There should be an equal amount!';
391 } else {
392 $analyzedOutput['warnings'][$tagName]='There were more end-tags ('.$countEnd.') than start-tags ('.$countBegin.') for the element "'.$tagName.'". There should be an equal amount! However the problem is not fatal.';
393 }
394 }
395 }
396
397 // Solo tags, must NOT have endings...
398 $soloTags = explode(',',$soloTags);
399 foreach($soloTags as $tagName) {
400 $countBegin = count(split('<'.$tagName.'[^[:alnum:]]',$content))-1;
401 $countEnd = count(split('<\/'.$tagName.'[^[:alnum:]]',$content))-1;
402 $analyzedOutput['solo'][$tagName]=array($countBegin,$countEnd);
403 if ($countBegin) $analyzedOutput['counts'][$tagName]=$countBegin;
404 if ($countEnd) {
405 $analyzedOutput['warnings'][$tagName]='There were end-tags found ('.$countEnd.') for the element "'.$tagName.'". This was not expected (although XHTML technically allows it).';
406 }
407 }
408
409 return $analyzedOutput;
410 }
411
412 /**
413 * Function that can clean up HTML content according to configuration given in the $tags array.
414 *
415 * Initializing the $tags array to allow a list of tags (in this case <B>,<I>,<U> and <A>), set it like this: $tags = array_flip(explode(',','b,a,i,u'))
416 * If the value of the $tags[$tagname] entry is an array, advanced processing of the tags is initialized. These are the options:
417 *
418 * $tags[$tagname] = Array(
419 * 'overrideAttribs' => '' If set, this string is preset as the attributes of the tag
420 * 'allowedAttribs' => '0' (zero) = no attributes allowed, '[commalist of attributes]' = only allowed attributes. If blank, all attributes are allowed.
421 * 'fixAttrib' => Array(
422 * '[attribute name]' => Array (
423 * 'default' => If no attribute exists by this name, this value is set as default value (if this value is not blank)
424 * 'always' => Boolean. If set, the attribute is always processed. Normally an attribute is processed only if it exists
425 * 'trim,intval,lower,upper' => All booleans. If any of these keys are set, the value is passed through the respective PHP-functions.
426 * 'range' => Array ('[low limit]','[high limit, optional]') Setting integer range.
427 * 'list' => Array ('[value1/default]','[value2]','[value3]') Attribute must be in this list. If not, the value is set to the first element.
428 * 'removeIfFalse' => Boolean/'blank'. If set, then the attribute is removed if it is 'false'. If this value is set to 'blank' then the value must be a blank string (that means a 'zero' value will not be removed)
429 * 'removeIfEquals' => [value] If the attribute value matches the value set here, then it is removed.
430 * 'casesensitiveComp' => 1 If set, then the removeIfEquals and list comparisons will be case sensitive. Otherwise not.
431 * )
432 * ),
433 * 'protect' => '', Boolean. If set, the tag <> is converted to &lt; and &gt;
434 * 'remap' => '', String. If set, the tagname is remapped to this tagname
435 * 'rmTagIfNoAttrib' => '', Boolean. If set, then the tag is removed if no attributes happend to be there.
436 * 'nesting' => '', Boolean/'global'. If set true, then this tag must have starting and ending tags in the correct order. Any tags not in this order will be discarded. Thus '</B><B><I></B></I></B>' will be converted to '<B><I></B></I>'. Is the value 'global' then true nesting in relation to other tags marked for 'global' nesting control is preserved. This means that if <B> and <I> are set for global nesting then this string '</B><B><I></B></I></B>' is converted to '<B></B>'
437 * )
438 *
439 * @param string $content; is the HTML-content being processed. This is also the result being returned.
440 * @param array $tags; is an array where each key is a tagname in lowercase. Only tags present as keys in this array are preserved. The value of the key can be an array with a vast number of options to configure.
441 * @param string $keepAll; boolean/'protect', if set, then all tags are kept regardless of tags present as keys in $tags-array. If 'protect' then the preserved tags have their <> converted to &lt; and &gt;
442 * @param integer $hSC; Values -1,0,1: Set to zero= disabled, set to 1 then the content BETWEEN tags is htmlspecialchar()'ed.
443 * @param array Configuration array send along as $conf to the internal functions ->processContent() and ->processTag()
444 * @return string Processed HTML content
445 */
446 function HTMLcleaner($content, $tags=array(),$keepAll=0,$hSC=0,$addConfig=array()) {
447 $newContent = array();
448 $tokArr = explode('<',$content);
449 $newContent[]=$this->processContent(current($tokArr),$hSC,$addConfig);
450 next($tokArr);
451
452 $c=1;
453 $tagRegister=array();
454 $tagStack=array();
455 while(list(,$tok)=each($tokArr)) {
456 $firstChar = substr($tok,0,1);
457 if (strcmp(trim($firstChar),'')) { // It is a tag...
458 $tagEnd = strcspn($tok,'>');
459 if (strlen($tok)!=$tagEnd) { // If there is and end-bracket...
460 $endTag = $firstChar=='/' ? 1 : 0;
461 $tagContent = substr($tok,$endTag,$tagEnd-$endTag);
462 $tagParts = split('[[:space:]]',$tagContent,2);
463 $tagName = strtolower($tagParts[0]);
464 if (isset($tags[$tagName])) {
465 if (is_array($tags[$tagName])) { // If there is processing to do for the tag:
466 // Override attributes
467 if (strcmp($tags[$tagName]['overrideAttribs'],'')) {
468 $tagParts[1]=$tags[$tagName]['overrideAttribs'];
469 }
470
471 // Allowed tags
472 if (strcmp($tags[$tagName]['allowedAttribs'],'')) {
473 if (!strcmp($tags[$tagName]['allowedAttribs'],'0')) { // No attribs allowed
474 $tagParts[1]='';
475 } elseif (trim($tagParts[1])) {
476 $tagAttrib = $this->get_tag_attributes($tagParts[1]);
477 $tagParts[1]='';
478 $newTagAttrib = array();
479 $tList = t3lib_div::trimExplode(',',strtolower($tags[$tagName]['allowedAttribs']),1);
480 while(list(,$allowTag)=each($tList)) {
481 if (isset($tagAttrib[0][$allowTag])) $newTagAttrib[$allowTag]=$tagAttrib[0][$allowTag];
482 }
483 $tagParts[1]=$this->compileTagAttribs($newTagAttrib,$tagAttrib[1]);
484 }
485 }
486
487 // Fixed attrib values
488 if (is_array($tags[$tagName]['fixAttrib'])) {
489 $tagAttrib = $this->get_tag_attributes($tagParts[1]);
490 $tagParts[1]='';
491 reset($tags[$tagName]['fixAttrib']);
492 while(list($attr,$params)=each($tags[$tagName]['fixAttrib'])) {
493 if (strcmp($params['default'],'') && !isset($tagAttrib[0][$attr])) $tagAttrib[0][$attr]=$params['default'];
494 if ($params['always'] || isset($tagAttrib[0][$attr])) {
495 if ($params['trim']) {$tagAttrib[0][$attr]=trim($tagAttrib[0][$attr]);}
496 if ($params['intval']) {$tagAttrib[0][$attr]=intval($tagAttrib[0][$attr]);}
497 if ($params['lower']) {$tagAttrib[0][$attr]=strtolower($tagAttrib[0][$attr]);}
498 if ($params['upper']) {$tagAttrib[0][$attr]=strtoupper($tagAttrib[0][$attr]);}
499 if ($params['range']) {
500 if (isset($params['range'][1])) {
501 $tagAttrib[0][$attr]=t3lib_div::intInRange($tagAttrib[0][$attr],intval($params['range'][0]),intval($params['range'][1]));
502 } else {
503 $tagAttrib[0][$attr]=t3lib_div::intInRange($tagAttrib[0][$attr],intval($params['range'][0]));
504 }
505 }
506 if (is_array($params['list'])) {
507 if (!in_array($this->caseShift($tagAttrib[0][$attr],$params['casesensitiveComp']),$this->caseShift($params['list'],$params['casesensitiveComp'],$tagName))) $tagAttrib[0][$attr]=$params['list'][0];
508 }
509 if (($params['removeIfFalse'] && $params['removeIfFalse']!='blank' && !$tagAttrib[0][$attr]) || ($params['removeIfFalse']=='blank' && !strcmp($tagAttrib[0][$attr],''))) {
510 unset($tagAttrib[0][$attr]);
511 }
512 if (strcmp($params['removeIfEquals'],'') && !strcmp($this->caseShift($tagAttrib[0][$attr],$params['casesensitiveComp']),$this->caseShift($params['removeIfEquals'],$params['casesensitiveComp']))) {
513 unset($tagAttrib[0][$attr]);
514 }
515 }
516 }
517 $tagParts[1]=$this->compileTagAttribs($tagAttrib[0],$tagAttrib[1]);
518 }
519
520 // Protecting the tag by converting < and > to &lt; and &gt; ??
521 if ($tags[$tagName]['protect']) {
522 $lt = '&lt;'; $gt = '&gt;';
523 } else {
524 $lt = '<'; $gt = '>';
525 }
526 // Remapping tag name?
527 if ($tags[$tagName]['remap']) $tagParts[0] = $tags[$tagName]['remap'];
528
529 // rmTagIfNoAttrib
530 if ($endTag || trim($tagParts[1]) || !$tags[$tagName]['rmTagIfNoAttrib']) {
531 $setTag=1;
532
533 if ($tags[$tagName]['nesting']) {
534 if (!is_array($tagRegister[$tagName])) $tagRegister[$tagName]=array();
535
536 if ($endTag) {
537 /* if ($tags[$tagName]['nesting']=='global') {
538 $lastEl = end($tagStack);
539 $correctTag = !strcmp($tagName,$lastEl);
540 } else $correctTag=1;
541 */
542 $correctTag=1;
543 if ($tags[$tagName]['nesting']=='global') {
544 $lastEl = end($tagStack);
545 if (strcmp($tagName,$lastEl)) {
546 if (in_array($tagName,$tagStack)) {
547 while(count($tagStack) && strcmp($tagName,$lastEl)) {
548 $elPos = end($tagRegister[$lastEl]);
549 unset($newContent[$elPos]);
550
551 array_pop($tagRegister[$lastEl]);
552 array_pop($tagStack);
553 $lastEl = end($tagStack);
554 }
555 } else {
556 $correctTag=0; // In this case the
557 }
558 }
559 }
560 if (!count($tagRegister[$tagName]) || !$correctTag) {
561 $setTag=0;
562 } else {
563 array_pop($tagRegister[$tagName]);
564 if ($tags[$tagName]['nesting']=='global') {array_pop($tagStack);}
565 }
566 } else {
567 array_push($tagRegister[$tagName],$c);
568 if ($tags[$tagName]['nesting']=='global') {array_push($tagStack,$tagName);}
569 }
570 }
571
572 if ($setTag) {
573 // Setting the tag
574 $newContent[$c++]=$this->processTag($lt.($endTag?'/':'').trim($tagParts[0].' '.$tagParts[1]).$gt,$addConfig,$endTag,$lt=='&lt;');
575 }
576 }
577 } else {
578 $newContent[$c++]=$this->processTag('<'.($endTag?'/':'').$tagContent.'>',$addConfig,$endTag);
579 }
580 } elseif ($keepAll) { // This is if the tag was not defined in the array for processing:
581 if (!strcmp($keepAll,'protect')) {
582 $lt = '&lt;'; $gt = '&gt;';
583 } else {
584 $lt = '<'; $gt = '>';
585 }
586 $newContent[$c++]=$this->processTag($lt.($endTag?'/':'').$tagContent.$gt,$addConfig,$endTag,$lt=='&lt;');
587 }
588 $newContent[$c++]=$this->processContent(substr($tok,$tagEnd+1),$hSC,$addConfig);
589 } else {
590 $newContent[$c++]=$this->processContent('<'.$tok,$hSC,$addConfig); // There were not end-bracket, so no tag...
591 }
592 } else {
593 $newContent[$c++]=$this->processContent('<'.$tok,$hSC,$addConfig); // It was not a tag anyways
594 }
595 }
596
597 // Unsetting tags:
598 reset($tagRegister);
599 while(list($tag,$positions)=each($tagRegister)) {
600 reset($positions);
601 while(list(,$pKey)=each($positions)) {
602 unset($newContent[$pKey]);
603 }
604 }
605
606 return implode('',$newContent);
607 }
608
609 /**
610 * Returns an array with all attributes as keys. Attributes are only lowercase a-z
611 * If a attribute is empty (shorthand), then the value for the key is empty. You can check if it existed with isset()
612 *
613 * @param string Tag: $tag is either a whole tag (eg '<TAG OPTION ATTRIB=VALUE>') or the parameterlist (ex ' OPTION ATTRIB=VALUE>')
614 * @param boolean If set, the attribute values are de-htmlspecialchar'ed. Should actually always be set!
615 * @return array array(Tag attributes,Attribute meta-data)
616 */
617 function get_tag_attributes($tag,$deHSC=0) {
618 list($components,$metaC) = $this->split_tag_attributes($tag);
619 $name = ''; // attribute name is stored here
620 $valuemode = '';
621 $attributes=array();
622 $attributesMeta=array();
623 if (is_array($components)) {
624 while (list($key,$val) = each ($components)) {
625 if ($val != '=') { // Only if $name is set (if there is an attribute, that waits for a value), that valuemode is enabled. This ensures that the attribute is assigned it's value
626 if ($valuemode) {
627 if ($name) {
628 $attributes[$name] = $deHSC?t3lib_div::htmlspecialchars_decode($val):$val;
629 $attributesMeta[$name]['dashType']=$metaC[$key];
630 $name = '';
631 }
632 } else {
633 if ($namekey = ereg_replace('[^a-zA-Z0-9_-]','',$val)) {
634 $name = strtolower($namekey);
635 $attributesMeta[$name]=array();
636 $attributesMeta[$name]['origTag']=$namekey;
637 $attributes[$name] = '';
638 }
639 }
640 $valuemode = '';
641 } else {
642 $valuemode = 'on';
643 }
644 }
645 if (is_array($attributes)) reset($attributes);
646 return array($attributes,$attributesMeta);
647 }
648 }
649
650 /**
651 * Returns an array with the 'components' from an attribute list. The result is normally analyzed by get_tag_attributes
652 * Removes tag-name if found
653 *
654 * @param string The tag or attributes
655 * @return array
656 * @access private
657 */
658 function split_tag_attributes($tag) {
659 $tag_tmp = trim(eregi_replace ('^[ ]*<[^ ]*','',$tag));
660 // Removes any > in the end of the string
661 $tag_tmp = trim(eregi_replace ('>$','',$tag_tmp));
662
663 $metaValue = array();
664 $value = array();
665 while (strcmp($tag_tmp,'')) { // Compared with empty string instead , 030102
666 $firstChar=substr($tag_tmp,0,1);
667 if (!strcmp($firstChar,'"') || !strcmp($firstChar,"'")) {
668 $reg=explode($firstChar,$tag_tmp,3);
669 $value[]=$reg[1];
670 $metaValue[]=$firstChar;
671 $tag_tmp=trim($reg[2]);
672 } elseif (!strcmp($firstChar,'=')) {
673 $value[] = '=';
674 $metaValue[]='';
675 $tag_tmp = trim(substr($tag_tmp,1)); // Removes = chars.
676 } else {
677 // There are '' around the value. We look for the next ' ' or '>'
678 $reg = split('[[:space:]=]',$tag_tmp,2);
679 $value[] = trim($reg[0]);
680 $metaValue[]='';
681 $tag_tmp = trim(substr($tag_tmp,strlen($reg[0]),1).$reg[1]);
682 }
683 }
684 if (is_array($value)) reset($value);
685 return array($value,$metaValue);
686 }
687
688 /**
689 * Converts htmlspecialchars forth ($dir=1) AND back ($dir=0)
690 *
691 * @param string Input value
692 * @param integer Direction: forth ($dir=1) AND back ($dir=0)
693 * @return string Output value
694 */
695 function bidir_htmlspecialchars($value,$dir) {
696 if ($dir>0) {
697 $value = htmlspecialchars($value);
698 } elseif ($dir<0) {
699 $value = str_replace('&gt;','>',$value);
700 $value = str_replace('&lt;','<',$value);
701 $value = str_replace('&quot;','"',$value);
702 $value = str_replace('&amp;','&',$value);
703 }
704 return $value;
705 }
706
707 /**
708 * Prefixes the relative paths of hrefs/src/action in the tags [td,table,body,img,input,form,link,script,a] in the $content with the $main_prefix or and alternative given by $alternatives
709 *
710 * @param string Prefix
711 * @param string HTML content
712 * @param array Array with alternative prefixes for certain of the tags. key=>value pairs where the keys are the tag element names in uppercase
713 * @return string Processed HTML content
714 */
715 function prefixResourcePath($main_prefix,$content,$alternatives=array()) {
716 $parts = $this->splitTags('td,table,body,img,input,form,link,script,a',$content);
717 reset($parts);
718 while(list($k,$v)=each($parts)) {
719 if ($k%2) {
720 $params = $this->get_tag_attributes($v,1);
721 $tagEnd = substr($v,-2)=='/>' ? ' />' : '>'; // Detect tag-ending so that it is re-applied correctly.
722 $firstTagName = $this->getFirstTagName($v); // The 'name' of the first tag
723 $somethingDone=0;
724 $prefix = isset($alternatives[strtoupper($firstTagName)]) ? $alternatives[strtoupper($firstTagName)] : $main_prefix;
725 switch(strtolower($firstTagName)) {
726 // background - attribute:
727 case 'td':
728 case 'body':
729 case 'table':
730 $src = $params[0]['background'];
731 if ($src) {
732 $params[0]['background'] = $this->prefixRelPath($prefix,$params[0]['background']);
733 $somethingDone=1;
734 }
735 break;
736 // src attribute
737 case 'img':
738 case 'input':
739 case 'script':
740 $src = $params[0]['src'];
741 if ($src) {
742 $params[0]['src'] = $this->prefixRelPath($prefix,$params[0]['src']);
743 $somethingDone=1;
744 }
745 break;
746 case 'link':
747 case 'a':
748 $src = $params[0]['href'];
749 if ($src) {
750 $params[0]['href'] = $this->prefixRelPath($prefix,$params[0]['href']);
751 $somethingDone=1;
752 }
753 break;
754 // action attribute
755 case 'form':
756 $src = $params[0]['action'];
757 if ($src) {
758 $params[0]['action'] = $this->prefixRelPath($prefix,$params[0]['action']);
759 $somethingDone=1;
760 }
761 break;
762 }
763 if ($somethingDone) {
764 $tagParts = split('[[:space:]]',$v,2);
765 $tagParts[1]=$this->compileTagAttribs($params[0],$params[1]);
766 $parts[$k] = '<'.trim(strtolower($firstTagName).' '.$tagParts[1]).
767 $tagEnd;
768 }
769 }
770 }
771
772 return implode('',$parts);
773 }
774
775 /**
776 * Internal sub-function for ->prefixResourcePath()
777 *
778 * @param string Prefix
779 * @param string Relative path/URL
780 * @return string Output path, prefixed if no scheme in input string
781 * @access private
782 */
783 function prefixRelPath($prefix,$srcVal) {
784 $pU = parse_url($srcVal);
785 if (!$pU['scheme']) { // If not an absolute URL.
786 $srcVal = $prefix.$srcVal;
787 }
788 return $srcVal;
789 }
790
791 /**
792 * Cleans up the input $value for fonttags.
793 * If keepFace,-Size and -Color is set then font-tags with an allowed property is kept. Else deleted.
794 *
795 * @param string HTML content with font-tags inside to clean up.
796 * @param boolean If set, keep "face" attribute
797 * @param boolean If set, keep "size" attribute
798 * @param boolean If set, keep "color" attribute
799 * @return string Processed HTML content
800 */
801 function cleanFontTags($value,$keepFace=0,$keepSize=0,$keepColor=0) {
802 $fontSplit = $this->splitIntoBlock('font',$value); // ,1 ?? - could probably be more stable if splitTags() was used since this depends on end-tags being properly set!
803 reset($fontSplit);
804 while(list($k,$v)=each($fontSplit)) {
805 if ($k%2) { // font:
806 $attribArray=$this->get_tag_attributes_classic($this->getFirstTag($v));
807 $newAttribs=array();
808 if ($keepFace && $attribArray['face']) $newAttribs[]='face="'.$attribArray['face'].'"';
809 if ($keepSize && $attribArray['size']) $newAttribs[]='size="'.$attribArray['size'].'"';
810 if ($keepColor && $attribArray['color']) $newAttribs[]='color="'.$attribArray['color'].'"';
811
812 $innerContent = $this->cleanFontTags($this->removeFirstAndLastTag($v),$keepFace,$keepSize,$keepColor);
813 if (count($newAttribs)) {
814 $fontSplit[$k]='<font '.implode(' ',$newAttribs).'>'.$innerContent.'</font>';
815 } else {
816 $fontSplit[$k]=$innerContent;
817 }
818 }
819 }
820 return implode('',$fontSplit);
821 }
822
823 /**
824 * This is used to map certain tag-names into other names.
825 *
826 * @param string HTML content
827 * @param array Array with tag key=>value pairs where key is from-tag and value is to-tag
828 * @param string Alternative less-than char to search for (search regex string)
829 * @param string Alternative less-than char to replace with (replace regex string)
830 * @return string Processed HTML content
831 */
832 function mapTags($value,$tags=array(),$ltChar='<',$ltChar2='<') {
833 reset($tags);
834 while(list($from,$to)=each($tags)) {
835 $value = eregi_replace($ltChar.$from.'>',$ltChar2.$to.'>',$value);
836 $value = eregi_replace($ltChar.$from.'[[:space:]]([^>]*)>',$ltChar2.$to.' \\1>',$value);
837 $value = eregi_replace($ltChar.'\/'.$from.'[^>]*>',$ltChar2.'/'.$to.'>',$value);
838 }
839 return $value;
840 }
841
842 /**
843 * This converts htmlspecialchar()'ed tags (from $tagList) back to real tags. Eg. '&lt;strong&gt' would be converted back to '<strong>' if found in $tagList
844 *
845 * @param string HTML content
846 * @param string Tag list, separated by comma. Lowercase!
847 * @return string Processed HTML content
848 */
849 function unprotectTags($content,$tagList='') {
850 $tagsArray = t3lib_div::trimExplode(',',$tagList,1);
851 $contentParts = explode('&lt;',$content);
852 next($contentParts); // bypass the first
853 while(list($k,$tok)=each($contentParts)) {
854 $firstChar = substr($tok,0,1);
855 if (strcmp(trim($firstChar),'')) {
856 $subparts = explode('&gt;',$tok,2);
857 $tagEnd = strlen($subparts[0]);
858 if (strlen($tok)!=$tagEnd) {
859 $endTag = $firstChar=='/' ? 1 : 0;
860 $tagContent = substr($tok,$endTag,$tagEnd-$endTag);
861 $tagParts = split('[[:space:]]',$tagContent,2);
862 $tagName = strtolower($tagParts[0]);
863 if (!strcmp($tagList,'') || in_array($tagName,$tagsArray)) {
864 $contentParts[$k] = '<'.$subparts[0].'>'.$subparts[1];
865 } else $contentParts[$k] = '&lt;'.$tok;
866 } else $contentParts[$k] = '&lt;'.$tok;
867 } else $contentParts[$k] = '&lt;'.$tok;
868 }
869
870 return implode('',$contentParts);
871 }
872
873 /**
874 * Strips tags except the tags in the list, $tagList
875 * OBSOLETE - use PHP function strip_tags()
876 *
877 * @param string Value to process
878 * @param string List of tags
879 * @return string Output value
880 * @ignore
881 */
882 function stripTagsExcept($value,$tagList) {
883 $tags=t3lib_div::trimExplode(',',$tagList,1);
884 $forthArr=array();
885 $backArr=array();
886 while(list(,$theTag)=each($tags)) {
887 $forthArr[$theTag]=md5($theTag);
888 $backArr[md5($theTag)]=$theTag;
889 }
890 $value = $this->mapTags($value,$forthArr,'<','_');
891 $value=strip_tags($value);
892 $value = $this->mapTags($value,$backArr,'_','<');
893 return $value;
894 }
895
896 /**
897 * Internal function
898 *
899 * @param string Input string
900 * @param boolean
901 * @param string
902 * @return string Output string, processed
903 * @access private
904 */
905 function caseShift($str,$flag,$cacheKey='') {
906 if (is_array($str)) {
907 if (!$cacheKey || !isset($this->caseShift_cache[$cacheKey])) {
908 reset($str);
909 while(list($k)=each($str)) {
910 $str[$k] = strtoupper($str[$k]);
911 }
912 if ($cacheKey) $this->caseShift_cache[$cacheKey]=$str;
913 } else {
914 $str = $this->caseShift_cache[$cacheKey];
915 }
916 } elseif (!$flag) $str = strtoupper($str);
917 return $str;
918 }
919
920 /**
921 * Compiling an array with tag attributes into a string
922 *
923 * @param array Tag attributes
924 * @param array Meta information about these attributes (like if they were quoted)
925 * @param boolean If set, then the attribute names will be set in lower case, value quotes in double-quotes and the value will be htmlspecialchar()'ed
926 * @return string Imploded attributes, eg: 'attribute="value" attrib2="value2"'
927 * @access private
928 */
929 function compileTagAttribs($tagAttrib,$meta=array(), $xhtmlClean=0) {
930 $accu=array();
931 reset($tagAttrib);
932 while(list($k,$v)=each($tagAttrib)) {
933 if ($xhtmlClean) {
934 $attr=strtolower($k);
935 if (strcmp($v,'') || isset($meta[$k]['dashType'])) {
936 $attr.='="'.htmlspecialchars($v).'"';
937 }
938 } else {
939 $attr=$meta[$k]['origTag']?$meta[$k]['origTag']:$k;
940 if (strcmp($v,'') || isset($meta[$k]['dashType'])) {
941 $dash=$meta[$k]['dashType']?$meta[$k]['dashType']:(t3lib_div::testInt($v)?'':'"');
942 $attr.='='.$dash.$v.$dash;
943 }
944 }
945 $accu[]=$attr;
946 }
947 return implode(' ',$accu);
948 }
949
950 /**
951 * Get tag attributes, the classic version (which had some limitations?)
952 *
953 * @param string The tag
954 * @param boolean De-htmlspecialchar flag.
955 * @return array
956 * @access private
957 */
958 function get_tag_attributes_classic($tag,$deHSC=0) {
959 $attr=$this->get_tag_attributes($tag,$deHSC);
960 return is_array($attr[0])?$attr[0]:array();
961 }
962
963 /**
964 * Converts TSconfig into an array for the HTMLcleaner function.
965 *
966 * @param array TSconfig for HTMLcleaner
967 * @param array ?
968 * @return array
969 * @access private
970 */
971 function HTMLparserConfig($TSconfig,$keepTags=array()) {
972 // Allow tags (base list, merged with incoming array)
973 $alTags = array_flip(t3lib_div::trimExplode(',',strtolower($TSconfig['allowTags']),1));
974 $keepTags = array_merge($alTags,$keepTags); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
975
976 // Set config properties.
977 if (is_array($TSconfig['tags.'])) {
978 reset($TSconfig['tags.']);
979 while(list($key,$tagC)=each($TSconfig['tags.'])) {
980 if (!is_array($tagC) && $key==strtolower($key)) {
981 if (!strcmp($tagC,'0')) unset($keepTags[$key]);
982 if (!strcmp($tagC,'1') && !isset($keepTags[$key])) $keepTags[$key]=1;
983 }
984 }
985
986 reset($TSconfig['tags.']);
987 while(list($key,$tagC)=each($TSconfig['tags.'])) {
988 if (is_array($tagC) && $key==strtolower($key)) {
989 $key=substr($key,0,-1);
990 if (!is_array($keepTags[$key])) $keepTags[$key]=array();
991 if (is_array($tagC['fixAttrib.'])) {
992 reset($tagC['fixAttrib.']);
993 while(list($atName,$atConfig)=each($tagC['fixAttrib.'])) {
994 if (is_array($atConfig)) {
995 $atName=substr($atName,0,-1);
996 if (!is_array($keepTags[$key]['fixAttrib'][$atName])) {
997 $keepTags[$key]['fixAttrib'][$atName]=array();
998 }
999 $keepTags[$key]['fixAttrib'][$atName] = array_merge($keepTags[$key]['fixAttrib'][$atName],$atConfig); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
1000 if (strcmp($keepTags[$key]['fixAttrib'][$atName]['range'],'')) $keepTags[$key]['fixAttrib'][$atName]['range'] = t3lib_div::trimExplode(',',$keepTags[$key]['fixAttrib'][$atName]['range']);
1001 if (strcmp($keepTags[$key]['fixAttrib'][$atName]['list'],'')) $keepTags[$key]['fixAttrib'][$atName]['list'] = t3lib_div::trimExplode(',',$keepTags[$key]['fixAttrib'][$atName]['list']);
1002 }
1003 }
1004 }
1005 unset($tagC['fixAttrib.']);
1006 unset($tagC['fixAttrib']);
1007 $keepTags[$key] = array_merge($keepTags[$key],$tagC); // Candidate for t3lib_div::array_merge() if integer-keys will some day make trouble...
1008 }
1009 }
1010 }
1011 // localNesting
1012 if ($TSconfig['localNesting']) {
1013 $lN = t3lib_div::trimExplode(',',strtolower($TSconfig['localNesting']),1);
1014 while(list(,$tn)=each($lN)) {
1015 if (isset($keepTags[$tn])) {
1016 $keepTags[$tn]['nesting']=1;
1017 }
1018 }
1019 }
1020 if ($TSconfig['globalNesting']) {
1021 $lN = t3lib_div::trimExplode(',',strtolower($TSconfig['globalNesting']),1);
1022 while(list(,$tn)=each($lN)) {
1023 if (isset($keepTags[$tn])) {
1024 if (!is_array($keepTags[$tn])) $keepTags[$tn]=array();
1025 $keepTags[$tn]['nesting']='global';
1026 }
1027 }
1028 }
1029 if ($TSconfig['rmTagIfNoAttrib']) {
1030 $lN = t3lib_div::trimExplode(',',strtolower($TSconfig['rmTagIfNoAttrib']),1);
1031 while(list(,$tn)=each($lN)) {
1032 if (isset($keepTags[$tn])) {
1033 if (!is_array($keepTags[$tn])) $keepTags[$tn]=array();
1034 $keepTags[$tn]['rmTagIfNoAttrib']=1;
1035 }
1036 }
1037 }
1038 if ($TSconfig['noAttrib']) {
1039 $lN = t3lib_div::trimExplode(',',strtolower($TSconfig['noAttrib']),1);
1040 while(list(,$tn)=each($lN)) {
1041 if (isset($keepTags[$tn])) {
1042 if (!is_array($keepTags[$tn])) $keepTags[$tn]=array();
1043 $keepTags[$tn]['allowedAttribs']=0;
1044 }
1045 }
1046 }
1047 if ($TSconfig['removeTags']) {
1048 $lN = t3lib_div::trimExplode(',',strtolower($TSconfig['removeTags']),1);
1049 while(list(,$tn)=each($lN)) {
1050 $keepTags[$tn]=array();
1051 $keepTags[$tn]['allowedAttribs']=0;
1052 $keepTags[$tn]['rmTagIfNoAttrib']=1;
1053 }
1054 }
1055
1056 return array($keepTags, ''.$TSconfig['keepNonMatchedTags'], intval($TSconfig['htmlSpecialChars']));
1057 }
1058
1059 /**
1060 * Tries to convert the content to be XHTML compliant and other stuff like that.
1061 * STILL EXPERIMENTAL. See comments below.
1062 *
1063 * What it does NOT do (yet) according to XHTML specs.:
1064 * - Wellformedness: Nesting is NOT checked
1065 * - name/id attribute issue is not observed at this point.
1066 * - Certain nesting of elements not allowed. Most interesting, <PRE> cannot contain img, big,small,sub,sup ...
1067 * - Wrapping scripts and style element contents in CDATA - or alternatively they should have entitites converted.
1068 * - Setting charsets may put some special requirements on both XML declaration/ meta-http-equiv. (C.9)
1069 * - UTF-8 encoding is in fact expected by XML!!
1070 * - stylesheet element and attribute names are NOT converted to lowercase
1071 * - ampersands (and entities in general I think) MUST be converted to an entity reference! (&amps;). This may mean further conversion of non-tag content before output to page. May be related to the charset issue as a whole.
1072 * - Minimized values not allowed: Must do this: selected="selected"
1073 *
1074 * What it does at this point:
1075 * - All tags (img,br,hr) is ended with "/>" - others?
1076 * - Lowercase for elements and attributes
1077 * - All attributes in quotes
1078 * - Add "alt" attribute to img-tags if it's not there already.
1079 *
1080 * @param string Content to clean up
1081 * @return string Cleaned up content returned.
1082 * @access private
1083 */
1084 function XHTML_clean($content) {
1085 $content = $this->HTMLcleaner(
1086 $content,
1087 array(), // No tags treated specially
1088 1, // Keep ALL tags.
1089 0, // All content is htmlspecialchar()'ed (or ??) - if we do, <script> content will break...
1090 array('xhtml' => 1)
1091 );
1092
1093 return $content;
1094 }
1095
1096 /**
1097 * Processing all tags themselves
1098 *
1099 * @param string TAG
1100 * @param array Configuration array passing instructions for processing. If count()==0, function will return value unprocessed. See source code for details
1101 * @param boolean Is endtag, then set this.
1102 * @param boolean If set, just return value straight away
1103 * @return strin Processed value.
1104 * @access private
1105 */
1106 function processTag($value,$conf,$endTag,$protected=0) {
1107 // Return immediately if protected or no parameters
1108 if ($protected || !count($conf)) return $value;
1109
1110 // OK then, begin processing for XHTML output:
1111 // STILL VERY EXPERIMENTAL!!
1112 if ($conf['xhtml']) {
1113 if ($endTag) { // Endtags are just set lowercase right away
1114 $value = strtolower($value);
1115 } elseif (substr($value,0,2)!='<!') { // ... and comments are ignored.
1116 $inValue = substr($value,1,(substr($value,-2)=='/>'?-2:-1)); // Finding inner value with out < >
1117 list($tagName,$tagP)=split('[[:space:]]',$inValue,2); // Separate attributes and tagname
1118 $tagName = strtolower($tagName);
1119
1120 // Process attributes
1121 $tagAttrib = $this->get_tag_attributes($tagP);
1122 if (!strcmp($tagName,'img') && !isset($tagAttrib[0]['alt'])) $tagAttrib[0]['alt']=''; // Set alt attribute for all images (not XHTML though...)
1123 $outA=array();
1124 reset($tagAttrib[0]);
1125 while(list($attrib_name,$attrib_value)=each($tagAttrib[0])) {
1126 // Set attributes: lowercase, always in quotes, with htmlspecialchars converted.
1127 $outA[]=$attrib_name.'="'.htmlspecialchars($this->bidir_htmlspecialchars($attrib_value,-1)).'"';
1128 }
1129 $newTag='<'.trim($tagName.' '.implode(' ',$outA));
1130 // All tags that are standalone (not wrapping, not having endtags) should be ended with '/>'
1131 if (t3lib_div::inList('img,br,hr',$tagName) || substr($value,-2)=='/>') {
1132 $newTag.=' />';
1133 } else {
1134 $newTag.='>';
1135 }
1136 $value = $newTag;
1137 }
1138 }
1139 return $value;
1140
1141 }
1142
1143 /**
1144 * Processing content between tags for HTML_cleaner
1145 *
1146 * @param string The value
1147 * @param integer Direction, either -1 or +1. 0 (zero) means no change to input value.
1148 * @param mixed Not used, ignore.
1149 * @return string The processed value.
1150 * @access private
1151 */
1152 function processContent($value,$dir,$conf) {
1153 if ($dir!=0) $value = $this->bidir_htmlspecialchars($value,$dir);
1154 return $value;
1155 }
1156 }
1157
1158
1159
1160 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_parsehtml.php']) {
1161 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_parsehtml.php']);
1162 }
1163 ?>