Remove ?>'s from files. They're pointless, and just asking for people to mess with...
[lhc/web/wiklou.git] / includes / Sanitizer.php
1 <?php
2 /**
3 * XHTML sanitizer for MediaWiki
4 *
5 * Copyright (C) 2002-2005 Brion Vibber <brion@pobox.com> et al
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @addtogroup Parser
24 */
25
26 /**
27 * Regular expression to match various types of character references in
28 * Sanitizer::normalizeCharReferences and Sanitizer::decodeCharReferences
29 */
30 define( 'MW_CHAR_REFS_REGEX',
31 '/&([A-Za-z0-9\x80-\xff]+);
32 |&\#([0-9]+);
33 |&\#x([0-9A-Za-z]+);
34 |&\#X([0-9A-Za-z]+);
35 |(&)/x' );
36
37 /**
38 * Regular expression to match HTML/XML attribute pairs within a tag.
39 * Allows some... latitude.
40 * Used in Sanitizer::fixTagAttributes and Sanitizer::decodeTagAttributes
41 */
42 $attrib = '[A-Za-z0-9]';
43 $space = '[\x09\x0a\x0d\x20]';
44 define( 'MW_ATTRIBS_REGEX',
45 "/(?:^|$space)($attrib+)
46 ($space*=$space*
47 (?:
48 # The attribute value: quoted or alone
49 \"([^<\"]*)\"
50 | '([^<']*)'
51 | ([a-zA-Z0-9!#$%&()*,\\-.\\/:;<>?@[\\]^_`{|}~]+)
52 | (\#[0-9a-fA-F]+) # Technically wrong, but lots of
53 # colors are specified like this.
54 # We'll be normalizing it.
55 )
56 )?(?=$space|\$)/sx" );
57
58 /**
59 * List of all named character entities defined in HTML 4.01
60 * http://www.w3.org/TR/html4/sgml/entities.html
61 * @private
62 */
63 global $wgHtmlEntities;
64 $wgHtmlEntities = array(
65 'Aacute' => 193,
66 'aacute' => 225,
67 'Acirc' => 194,
68 'acirc' => 226,
69 'acute' => 180,
70 'AElig' => 198,
71 'aelig' => 230,
72 'Agrave' => 192,
73 'agrave' => 224,
74 'alefsym' => 8501,
75 'Alpha' => 913,
76 'alpha' => 945,
77 'amp' => 38,
78 'and' => 8743,
79 'ang' => 8736,
80 'Aring' => 197,
81 'aring' => 229,
82 'asymp' => 8776,
83 'Atilde' => 195,
84 'atilde' => 227,
85 'Auml' => 196,
86 'auml' => 228,
87 'bdquo' => 8222,
88 'Beta' => 914,
89 'beta' => 946,
90 'brvbar' => 166,
91 'bull' => 8226,
92 'cap' => 8745,
93 'Ccedil' => 199,
94 'ccedil' => 231,
95 'cedil' => 184,
96 'cent' => 162,
97 'Chi' => 935,
98 'chi' => 967,
99 'circ' => 710,
100 'clubs' => 9827,
101 'cong' => 8773,
102 'copy' => 169,
103 'crarr' => 8629,
104 'cup' => 8746,
105 'curren' => 164,
106 'dagger' => 8224,
107 'Dagger' => 8225,
108 'darr' => 8595,
109 'dArr' => 8659,
110 'deg' => 176,
111 'Delta' => 916,
112 'delta' => 948,
113 'diams' => 9830,
114 'divide' => 247,
115 'Eacute' => 201,
116 'eacute' => 233,
117 'Ecirc' => 202,
118 'ecirc' => 234,
119 'Egrave' => 200,
120 'egrave' => 232,
121 'empty' => 8709,
122 'emsp' => 8195,
123 'ensp' => 8194,
124 'Epsilon' => 917,
125 'epsilon' => 949,
126 'equiv' => 8801,
127 'Eta' => 919,
128 'eta' => 951,
129 'ETH' => 208,
130 'eth' => 240,
131 'Euml' => 203,
132 'euml' => 235,
133 'euro' => 8364,
134 'exist' => 8707,
135 'fnof' => 402,
136 'forall' => 8704,
137 'frac12' => 189,
138 'frac14' => 188,
139 'frac34' => 190,
140 'frasl' => 8260,
141 'Gamma' => 915,
142 'gamma' => 947,
143 'ge' => 8805,
144 'gt' => 62,
145 'harr' => 8596,
146 'hArr' => 8660,
147 'hearts' => 9829,
148 'hellip' => 8230,
149 'Iacute' => 205,
150 'iacute' => 237,
151 'Icirc' => 206,
152 'icirc' => 238,
153 'iexcl' => 161,
154 'Igrave' => 204,
155 'igrave' => 236,
156 'image' => 8465,
157 'infin' => 8734,
158 'int' => 8747,
159 'Iota' => 921,
160 'iota' => 953,
161 'iquest' => 191,
162 'isin' => 8712,
163 'Iuml' => 207,
164 'iuml' => 239,
165 'Kappa' => 922,
166 'kappa' => 954,
167 'Lambda' => 923,
168 'lambda' => 955,
169 'lang' => 9001,
170 'laquo' => 171,
171 'larr' => 8592,
172 'lArr' => 8656,
173 'lceil' => 8968,
174 'ldquo' => 8220,
175 'le' => 8804,
176 'lfloor' => 8970,
177 'lowast' => 8727,
178 'loz' => 9674,
179 'lrm' => 8206,
180 'lsaquo' => 8249,
181 'lsquo' => 8216,
182 'lt' => 60,
183 'macr' => 175,
184 'mdash' => 8212,
185 'micro' => 181,
186 'middot' => 183,
187 'minus' => 8722,
188 'Mu' => 924,
189 'mu' => 956,
190 'nabla' => 8711,
191 'nbsp' => 160,
192 'ndash' => 8211,
193 'ne' => 8800,
194 'ni' => 8715,
195 'not' => 172,
196 'notin' => 8713,
197 'nsub' => 8836,
198 'Ntilde' => 209,
199 'ntilde' => 241,
200 'Nu' => 925,
201 'nu' => 957,
202 'Oacute' => 211,
203 'oacute' => 243,
204 'Ocirc' => 212,
205 'ocirc' => 244,
206 'OElig' => 338,
207 'oelig' => 339,
208 'Ograve' => 210,
209 'ograve' => 242,
210 'oline' => 8254,
211 'Omega' => 937,
212 'omega' => 969,
213 'Omicron' => 927,
214 'omicron' => 959,
215 'oplus' => 8853,
216 'or' => 8744,
217 'ordf' => 170,
218 'ordm' => 186,
219 'Oslash' => 216,
220 'oslash' => 248,
221 'Otilde' => 213,
222 'otilde' => 245,
223 'otimes' => 8855,
224 'Ouml' => 214,
225 'ouml' => 246,
226 'para' => 182,
227 'part' => 8706,
228 'permil' => 8240,
229 'perp' => 8869,
230 'Phi' => 934,
231 'phi' => 966,
232 'Pi' => 928,
233 'pi' => 960,
234 'piv' => 982,
235 'plusmn' => 177,
236 'pound' => 163,
237 'prime' => 8242,
238 'Prime' => 8243,
239 'prod' => 8719,
240 'prop' => 8733,
241 'Psi' => 936,
242 'psi' => 968,
243 'quot' => 34,
244 'radic' => 8730,
245 'rang' => 9002,
246 'raquo' => 187,
247 'rarr' => 8594,
248 'rArr' => 8658,
249 'rceil' => 8969,
250 'rdquo' => 8221,
251 'real' => 8476,
252 'reg' => 174,
253 'rfloor' => 8971,
254 'Rho' => 929,
255 'rho' => 961,
256 'rlm' => 8207,
257 'rsaquo' => 8250,
258 'rsquo' => 8217,
259 'sbquo' => 8218,
260 'Scaron' => 352,
261 'scaron' => 353,
262 'sdot' => 8901,
263 'sect' => 167,
264 'shy' => 173,
265 'Sigma' => 931,
266 'sigma' => 963,
267 'sigmaf' => 962,
268 'sim' => 8764,
269 'spades' => 9824,
270 'sub' => 8834,
271 'sube' => 8838,
272 'sum' => 8721,
273 'sup' => 8835,
274 'sup1' => 185,
275 'sup2' => 178,
276 'sup3' => 179,
277 'supe' => 8839,
278 'szlig' => 223,
279 'Tau' => 932,
280 'tau' => 964,
281 'there4' => 8756,
282 'Theta' => 920,
283 'theta' => 952,
284 'thetasym' => 977,
285 'thinsp' => 8201,
286 'THORN' => 222,
287 'thorn' => 254,
288 'tilde' => 732,
289 'times' => 215,
290 'trade' => 8482,
291 'Uacute' => 218,
292 'uacute' => 250,
293 'uarr' => 8593,
294 'uArr' => 8657,
295 'Ucirc' => 219,
296 'ucirc' => 251,
297 'Ugrave' => 217,
298 'ugrave' => 249,
299 'uml' => 168,
300 'upsih' => 978,
301 'Upsilon' => 933,
302 'upsilon' => 965,
303 'Uuml' => 220,
304 'uuml' => 252,
305 'weierp' => 8472,
306 'Xi' => 926,
307 'xi' => 958,
308 'Yacute' => 221,
309 'yacute' => 253,
310 'yen' => 165,
311 'Yuml' => 376,
312 'yuml' => 255,
313 'Zeta' => 918,
314 'zeta' => 950,
315 'zwj' => 8205,
316 'zwnj' => 8204 );
317
318 /**
319 * Character entity aliases accepted by MediaWiki
320 */
321 global $wgHtmlEntityAliases;
322 $wgHtmlEntityAliases = array(
323 'רלמ' => 'rlm',
324 'رلم' => 'rlm',
325 );
326
327
328 /**
329 * XHTML sanitizer for MediaWiki
330 * @addtogroup Parser
331 */
332 class Sanitizer {
333 /**
334 * Cleans up HTML, removes dangerous tags and attributes, and
335 * removes HTML comments
336 * @private
337 * @param string $text
338 * @param callback $processCallback to do any variable or parameter replacements in HTML attribute values
339 * @param array $args for the processing callback
340 * @return string
341 */
342 static function removeHTMLtags( $text, $processCallback = null, $args = array() ) {
343 global $wgUseTidy;
344
345 static $htmlpairs, $htmlsingle, $htmlsingleonly, $htmlnest, $tabletags,
346 $htmllist, $listtags, $htmlsingleallowed, $htmlelements, $staticInitialised;
347
348 wfProfileIn( __METHOD__ );
349
350 if ( !$staticInitialised ) {
351
352 $htmlpairs = array( # Tags that must be closed
353 'b', 'del', 'i', 'ins', 'u', 'font', 'big', 'small', 'sub', 'sup', 'h1',
354 'h2', 'h3', 'h4', 'h5', 'h6', 'cite', 'code', 'em', 's',
355 'strike', 'strong', 'tt', 'var', 'div', 'center',
356 'blockquote', 'ol', 'ul', 'dl', 'table', 'caption', 'pre',
357 'ruby', 'rt' , 'rb' , 'rp', 'p', 'span', 'u'
358 );
359 $htmlsingle = array(
360 'br', 'hr', 'li', 'dt', 'dd'
361 );
362 $htmlsingleonly = array( # Elements that cannot have close tags
363 'br', 'hr'
364 );
365 $htmlnest = array( # Tags that can be nested--??
366 'table', 'tr', 'td', 'th', 'div', 'blockquote', 'ol', 'ul',
367 'dl', 'font', 'big', 'small', 'sub', 'sup', 'span'
368 );
369 $tabletags = array( # Can only appear inside table, we will close them
370 'td', 'th', 'tr',
371 );
372 $htmllist = array( # Tags used by list
373 'ul','ol',
374 );
375 $listtags = array( # Tags that can appear in a list
376 'li',
377 );
378
379 $htmlsingleallowed = array_merge( $htmlsingle, $tabletags );
380 $htmlelements = array_merge( $htmlsingle, $htmlpairs, $htmlnest );
381
382 # Convert them all to hashtables for faster lookup
383 $vars = array( 'htmlpairs', 'htmlsingle', 'htmlsingleonly', 'htmlnest', 'tabletags',
384 'htmllist', 'listtags', 'htmlsingleallowed', 'htmlelements' );
385 foreach ( $vars as $var ) {
386 $$var = array_flip( $$var );
387 }
388 $staticInitialised = true;
389 }
390
391 # Remove HTML comments
392 $text = Sanitizer::removeHTMLcomments( $text );
393 $bits = explode( '<', $text );
394 $text = str_replace( '>', '&gt;', array_shift( $bits ) );
395 if(!$wgUseTidy) {
396 $tagstack = $tablestack = array();
397 foreach ( $bits as $x ) {
398 $regs = array();
399 if( preg_match( '!^(/?)(\\w+)([^>]*?)(/{0,1}>)([^<]*)$!', $x, $regs ) ) {
400 list( /* $qbar */, $slash, $t, $params, $brace, $rest ) = $regs;
401 } else {
402 $slash = $t = $params = $brace = $rest = null;
403 }
404
405 $badtag = 0 ;
406 if ( isset( $htmlelements[$t = strtolower( $t )] ) ) {
407 # Check our stack
408 if ( $slash ) {
409 # Closing a tag...
410 if( isset( $htmlsingleonly[$t] ) ) {
411 $badtag = 1;
412 } elseif ( ( $ot = @array_pop( $tagstack ) ) != $t ) {
413 if ( isset( $htmlsingleallowed[$ot] ) ) {
414 # Pop all elements with an optional close tag
415 # and see if we find a match below them
416 $optstack = array();
417 array_push ($optstack, $ot);
418 while ( ( ( $ot = @array_pop( $tagstack ) ) != $t ) &&
419 isset( $htmlsingleallowed[$ot] ) )
420 {
421 array_push ($optstack, $ot);
422 }
423 if ( $t != $ot ) {
424 # No match. Push the optinal elements back again
425 $badtag = 1;
426 while ( $ot = @array_pop( $optstack ) ) {
427 array_push( $tagstack, $ot );
428 }
429 }
430 } else {
431 @array_push( $tagstack, $ot );
432 # <li> can be nested in <ul> or <ol>, skip those cases:
433 if(!(isset( $htmllist[$ot] ) && isset( $listtags[$t] ) )) {
434 $badtag = 1;
435 }
436 }
437 } else {
438 if ( $t == 'table' ) {
439 $tagstack = array_pop( $tablestack );
440 }
441 }
442 $newparams = '';
443 } else {
444 # Keep track for later
445 if ( isset( $tabletags[$t] ) &&
446 ! in_array( 'table', $tagstack ) ) {
447 $badtag = 1;
448 } else if ( in_array( $t, $tagstack ) &&
449 ! isset( $htmlnest [$t ] ) ) {
450 $badtag = 1 ;
451 # Is it a self closed htmlpair ? (bug 5487)
452 } else if( $brace == '/>' &&
453 isset( $htmlpairs[$t] ) ) {
454 $badtag = 1;
455 } elseif( isset( $htmlsingleonly[$t] ) ) {
456 # Hack to force empty tag for uncloseable elements
457 $brace = '/>';
458 } else if( isset( $htmlsingle[$t] ) ) {
459 # Hack to not close $htmlsingle tags
460 $brace = NULL;
461 } else if( isset( $tabletags[$t] )
462 && in_array($t ,$tagstack) ) {
463 // New table tag but forgot to close the previous one
464 $text .= "</$t>";
465 } else {
466 if ( $t == 'table' ) {
467 array_push( $tablestack, $tagstack );
468 $tagstack = array();
469 }
470 array_push( $tagstack, $t );
471 }
472
473 # Replace any variables or template parameters with
474 # plaintext results.
475 if( is_callable( $processCallback ) ) {
476 call_user_func_array( $processCallback, array( &$params, $args ) );
477 }
478
479 # Strip non-approved attributes from the tag
480 $newparams = Sanitizer::fixTagAttributes( $params, $t );
481 }
482 if ( ! $badtag ) {
483 $rest = str_replace( '>', '&gt;', $rest );
484 $close = ( $brace == '/>' && !$slash ) ? ' /' : '';
485 $text .= "<$slash$t$newparams$close>$rest";
486 continue;
487 }
488 }
489 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
490 }
491 # Close off any remaining tags
492 while ( is_array( $tagstack ) && ($t = array_pop( $tagstack )) ) {
493 $text .= "</$t>\n";
494 if ( $t == 'table' ) { $tagstack = array_pop( $tablestack ); }
495 }
496 } else {
497 # this might be possible using tidy itself
498 foreach ( $bits as $x ) {
499 preg_match( '/^(\\/?)(\\w+)([^>]*?)(\\/{0,1}>)([^<]*)$/',
500 $x, $regs );
501 @list( /* $qbar */, $slash, $t, $params, $brace, $rest ) = $regs;
502 if ( isset( $htmlelements[$t = strtolower( $t )] ) ) {
503 if( is_callable( $processCallback ) ) {
504 call_user_func_array( $processCallback, array( &$params, $args ) );
505 }
506 $newparams = Sanitizer::fixTagAttributes( $params, $t );
507 $rest = str_replace( '>', '&gt;', $rest );
508 $text .= "<$slash$t$newparams$brace$rest";
509 } else {
510 $text .= '&lt;' . str_replace( '>', '&gt;', $x);
511 }
512 }
513 }
514 wfProfileOut( __METHOD__ );
515 return $text;
516 }
517
518 /**
519 * Remove '<!--', '-->', and everything between.
520 * To avoid leaving blank lines, when a comment is both preceded
521 * and followed by a newline (ignoring spaces), trim leading and
522 * trailing spaces and one of the newlines.
523 *
524 * @private
525 * @param string $text
526 * @return string
527 */
528 static function removeHTMLcomments( $text ) {
529 wfProfileIn( __METHOD__ );
530 while (($start = strpos($text, '<!--')) !== false) {
531 $end = strpos($text, '-->', $start + 4);
532 if ($end === false) {
533 # Unterminated comment; bail out
534 break;
535 }
536
537 $end += 3;
538
539 # Trim space and newline if the comment is both
540 # preceded and followed by a newline
541 $spaceStart = max($start - 1, 0);
542 $spaceLen = $end - $spaceStart;
543 while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > 0) {
544 $spaceStart--;
545 $spaceLen++;
546 }
547 while (substr($text, $spaceStart + $spaceLen, 1) === ' ')
548 $spaceLen++;
549 if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceStart + $spaceLen, 1) === "\n") {
550 # Remove the comment, leading and trailing
551 # spaces, and leave only one newline.
552 $text = substr_replace($text, "\n", $spaceStart, $spaceLen + 1);
553 }
554 else {
555 # Remove just the comment.
556 $text = substr_replace($text, '', $start, $end - $start);
557 }
558 }
559 wfProfileOut( __METHOD__ );
560 return $text;
561 }
562
563 /**
564 * Take an array of attribute names and values and normalize or discard
565 * illegal values for the given element type.
566 *
567 * - Discards attributes not on a whitelist for the given element
568 * - Unsafe style attributes are discarded
569 *
570 * @param array $attribs
571 * @param string $element
572 * @return array
573 *
574 * @todo Check for legal values where the DTD limits things.
575 * @todo Check for unique id attribute :P
576 */
577 static function validateTagAttributes( $attribs, $element ) {
578 $whitelist = array_flip( Sanitizer::attributeWhitelist( $element ) );
579 $out = array();
580 foreach( $attribs as $attribute => $value ) {
581 if( !isset( $whitelist[$attribute] ) ) {
582 continue;
583 }
584 # Strip javascript "expression" from stylesheets.
585 # http://msdn.microsoft.com/workshop/author/dhtml/overview/recalc.asp
586 if( $attribute == 'style' ) {
587 $value = Sanitizer::checkCss( $value );
588 if( $value === false ) {
589 # haxx0r
590 continue;
591 }
592 }
593
594 if ( $attribute === 'id' )
595 $value = Sanitizer::escapeId( $value );
596
597 // If this attribute was previously set, override it.
598 // Output should only have one attribute of each name.
599 $out[$attribute] = $value;
600 }
601 return $out;
602 }
603
604 /**
605 * Pick apart some CSS and check it for forbidden or unsafe structures.
606 * Returns a sanitized string, or false if it was just too evil.
607 *
608 * Currently URL references, 'expression', 'tps' are forbidden.
609 *
610 * @param string $value
611 * @return mixed
612 */
613 static function checkCss( $value ) {
614 $stripped = Sanitizer::decodeCharReferences( $value );
615
616 // Remove any comments; IE gets token splitting wrong
617 $stripped = StringUtils::delimiterReplace( '/*', '*/', ' ', $stripped );
618
619 $value = $stripped;
620
621 // ... and continue checks
622 $stripped = preg_replace( '!\\\\([0-9A-Fa-f]{1,6})[ \\n\\r\\t\\f]?!e',
623 'codepointToUtf8(hexdec("$1"))', $stripped );
624 $stripped = str_replace( '\\', '', $stripped );
625 if( preg_match( '/(?:expression|tps*:\/\/|url\\s*\().*/is',
626 $stripped ) ) {
627 # haxx0r
628 return false;
629 }
630
631 return $value;
632 }
633
634 /**
635 * Take a tag soup fragment listing an HTML element's attributes
636 * and normalize it to well-formed XML, discarding unwanted attributes.
637 * Output is safe for further wikitext processing, with escaping of
638 * values that could trigger problems.
639 *
640 * - Normalizes attribute names to lowercase
641 * - Discards attributes not on a whitelist for the given element
642 * - Turns broken or invalid entities into plaintext
643 * - Double-quotes all attribute values
644 * - Attributes without values are given the name as attribute
645 * - Double attributes are discarded
646 * - Unsafe style attributes are discarded
647 * - Prepends space if there are attributes.
648 *
649 * @param string $text
650 * @param string $element
651 * @return string
652 */
653 static function fixTagAttributes( $text, $element ) {
654 if( trim( $text ) == '' ) {
655 return '';
656 }
657
658 $stripped = Sanitizer::validateTagAttributes(
659 Sanitizer::decodeTagAttributes( $text ), $element );
660
661 $attribs = array();
662 foreach( $stripped as $attribute => $value ) {
663 $encAttribute = htmlspecialchars( $attribute );
664 $encValue = Sanitizer::safeEncodeAttribute( $value );
665
666 $attribs[] = "$encAttribute=\"$encValue\"";
667 }
668 return count( $attribs ) ? ' ' . implode( ' ', $attribs ) : '';
669 }
670
671 /**
672 * Encode an attribute value for HTML output.
673 * @param $text
674 * @return HTML-encoded text fragment
675 */
676 static function encodeAttribute( $text ) {
677 $encValue = htmlspecialchars( $text );
678
679 // Whitespace is normalized during attribute decoding,
680 // so if we've been passed non-spaces we must encode them
681 // ahead of time or they won't be preserved.
682 $encValue = strtr( $encValue, array(
683 "\n" => '&#10;',
684 "\r" => '&#13;',
685 "\t" => '&#9;',
686 ) );
687
688 return $encValue;
689 }
690
691 /**
692 * Encode an attribute value for HTML tags, with extra armoring
693 * against further wiki processing.
694 * @param $text
695 * @return HTML-encoded text fragment
696 */
697 static function safeEncodeAttribute( $text ) {
698 $encValue = Sanitizer::encodeAttribute( $text );
699
700 # Templates and links may be expanded in later parsing,
701 # creating invalid or dangerous output. Suppress this.
702 $encValue = strtr( $encValue, array(
703 '<' => '&lt;', // This should never happen,
704 '>' => '&gt;', // we've received invalid input
705 '"' => '&quot;', // which should have been escaped.
706 '{' => '&#123;',
707 '[' => '&#91;',
708 "''" => '&#39;&#39;',
709 'ISBN' => '&#73;SBN',
710 'RFC' => '&#82;FC',
711 'PMID' => '&#80;MID',
712 '|' => '&#124;',
713 '__' => '&#95;_',
714 ) );
715
716 # Stupid hack
717 $encValue = preg_replace_callback(
718 '/(' . wfUrlProtocols() . ')/',
719 array( 'Sanitizer', 'armorLinksCallback' ),
720 $encValue );
721 return $encValue;
722 }
723
724 /**
725 * Given a value escape it so that it can be used in an id attribute and
726 * return it, this does not validate the value however (see first link)
727 *
728 * @see http://www.w3.org/TR/html401/types.html#type-name Valid characters
729 * in the id and
730 * name attributes
731 * @see http://www.w3.org/TR/html401/struct/links.html#h-12.2.3 Anchors with the id attribute
732 *
733 * @static
734 *
735 * @param string $id
736 * @return string
737 */
738 static function escapeId( $id ) {
739 static $replace = array(
740 '%3A' => ':',
741 '%' => '.'
742 );
743
744 $id = urlencode( Sanitizer::decodeCharReferences( strtr( $id, ' ', '_' ) ) );
745
746 return str_replace( array_keys( $replace ), array_values( $replace ), $id );
747 }
748
749 /**
750 * Given a value, escape it so that it can be used as a CSS class and
751 * return it.
752 *
753 * @todo For extra validity, input should be validated UTF-8.
754 *
755 * @see http://www.w3.org/TR/CSS21/syndata.html Valid characters/format
756 *
757 * @param string $class
758 * @return string
759 */
760 static function escapeClass( $class ) {
761 // Convert ugly stuff to underscores and kill underscores in ugly places
762 return rtrim(preg_replace(
763 array('/(^[0-9\\-])|[\\x00-\\x20!"#$%&\'()*+,.\\/:;<=>?@[\\]^`{|}~]|\\xC2\\xA0/','/_+/'),
764 '_',
765 $class ), '_');
766 }
767
768 /**
769 * Regex replace callback for armoring links against further processing.
770 * @param array $matches
771 * @return string
772 * @private
773 */
774 private static function armorLinksCallback( $matches ) {
775 return str_replace( ':', '&#58;', $matches[1] );
776 }
777
778 /**
779 * Return an associative array of attribute names and values from
780 * a partial tag string. Attribute names are forces to lowercase,
781 * character references are decoded to UTF-8 text.
782 *
783 * @param string
784 * @return array
785 */
786 static function decodeTagAttributes( $text ) {
787 $attribs = array();
788
789 if( trim( $text ) == '' ) {
790 return $attribs;
791 }
792
793 $pairs = array();
794 if( !preg_match_all(
795 MW_ATTRIBS_REGEX,
796 $text,
797 $pairs,
798 PREG_SET_ORDER ) ) {
799 return $attribs;
800 }
801
802 foreach( $pairs as $set ) {
803 $attribute = strtolower( $set[1] );
804 $value = Sanitizer::getTagAttributeCallback( $set );
805
806 // Normalize whitespace
807 $value = preg_replace( '/[\t\r\n ]+/', ' ', $value );
808 $value = trim( $value );
809
810 // Decode character references
811 $attribs[$attribute] = Sanitizer::decodeCharReferences( $value );
812 }
813 return $attribs;
814 }
815
816 /**
817 * Pick the appropriate attribute value from a match set from the
818 * MW_ATTRIBS_REGEX matches.
819 *
820 * @param array $set
821 * @return string
822 * @private
823 */
824 private static function getTagAttributeCallback( $set ) {
825 if( isset( $set[6] ) ) {
826 # Illegal #XXXXXX color with no quotes.
827 return $set[6];
828 } elseif( isset( $set[5] ) ) {
829 # No quotes.
830 return $set[5];
831 } elseif( isset( $set[4] ) ) {
832 # Single-quoted
833 return $set[4];
834 } elseif( isset( $set[3] ) ) {
835 # Double-quoted
836 return $set[3];
837 } elseif( !isset( $set[2] ) ) {
838 # In XHTML, attributes must have a value.
839 # For 'reduced' form, return explicitly the attribute name here.
840 return $set[1];
841 } else {
842 throw new MWException( "Tag conditions not met. This should never happen and is a bug." );
843 }
844 }
845
846 /**
847 * Normalize whitespace and character references in an XML source-
848 * encoded text for an attribute value.
849 *
850 * See http://www.w3.org/TR/REC-xml/#AVNormalize for background,
851 * but note that we're not returning the value, but are returning
852 * XML source fragments that will be slapped into output.
853 *
854 * @param string $text
855 * @return string
856 * @private
857 */
858 private static function normalizeAttributeValue( $text ) {
859 return str_replace( '"', '&quot;',
860 self::normalizeWhitespace(
861 Sanitizer::normalizeCharReferences( $text ) ) );
862 }
863
864 private static function normalizeWhitespace( $text ) {
865 return preg_replace(
866 '/\r\n|[\x20\x0d\x0a\x09]/',
867 ' ',
868 $text );
869 }
870
871 /**
872 * Ensure that any entities and character references are legal
873 * for XML and XHTML specifically. Any stray bits will be
874 * &amp;-escaped to result in a valid text fragment.
875 *
876 * a. any named char refs must be known in XHTML
877 * b. any numeric char refs must be legal chars, not invalid or forbidden
878 * c. use &#x, not &#X
879 * d. fix or reject non-valid attributes
880 *
881 * @param string $text
882 * @return string
883 * @private
884 */
885 static function normalizeCharReferences( $text ) {
886 return preg_replace_callback(
887 MW_CHAR_REFS_REGEX,
888 array( 'Sanitizer', 'normalizeCharReferencesCallback' ),
889 $text );
890 }
891 /**
892 * @param string $matches
893 * @return string
894 */
895 static function normalizeCharReferencesCallback( $matches ) {
896 $ret = null;
897 if( $matches[1] != '' ) {
898 $ret = Sanitizer::normalizeEntity( $matches[1] );
899 } elseif( $matches[2] != '' ) {
900 $ret = Sanitizer::decCharReference( $matches[2] );
901 } elseif( $matches[3] != '' ) {
902 $ret = Sanitizer::hexCharReference( $matches[3] );
903 } elseif( $matches[4] != '' ) {
904 $ret = Sanitizer::hexCharReference( $matches[4] );
905 }
906 if( is_null( $ret ) ) {
907 return htmlspecialchars( $matches[0] );
908 } else {
909 return $ret;
910 }
911 }
912
913 /**
914 * If the named entity is defined in the HTML 4.0/XHTML 1.0 DTD,
915 * return the named entity reference as is. If the entity is a
916 * MediaWiki-specific alias, returns the HTML equivalent. Otherwise,
917 * returns HTML-escaped text of pseudo-entity source (eg &amp;foo;)
918 *
919 * @param string $name
920 * @return string
921 * @static
922 */
923 static function normalizeEntity( $name ) {
924 global $wgHtmlEntities, $wgHtmlEntityAliases;
925 if ( isset( $wgHtmlEntityAliases[$name] ) ) {
926 return "&{$wgHtmlEntityAliases[$name]};";
927 } elseif( isset( $wgHtmlEntities[$name] ) ) {
928 return "&$name;";
929 } else {
930 return "&amp;$name;";
931 }
932 }
933
934 static function decCharReference( $codepoint ) {
935 $point = intval( $codepoint );
936 if( Sanitizer::validateCodepoint( $point ) ) {
937 return sprintf( '&#%d;', $point );
938 } else {
939 return null;
940 }
941 }
942
943 static function hexCharReference( $codepoint ) {
944 $point = hexdec( $codepoint );
945 if( Sanitizer::validateCodepoint( $point ) ) {
946 return sprintf( '&#x%x;', $point );
947 } else {
948 return null;
949 }
950 }
951
952 /**
953 * Returns true if a given Unicode codepoint is a valid character in XML.
954 * @param int $codepoint
955 * @return bool
956 */
957 private static function validateCodepoint( $codepoint ) {
958 return ($codepoint == 0x09)
959 || ($codepoint == 0x0a)
960 || ($codepoint == 0x0d)
961 || ($codepoint >= 0x20 && $codepoint <= 0xd7ff)
962 || ($codepoint >= 0xe000 && $codepoint <= 0xfffd)
963 || ($codepoint >= 0x10000 && $codepoint <= 0x10ffff);
964 }
965
966 /**
967 * Decode any character references, numeric or named entities,
968 * in the text and return a UTF-8 string.
969 *
970 * @param string $text
971 * @return string
972 * @public
973 * @static
974 */
975 public static function decodeCharReferences( $text ) {
976 return preg_replace_callback(
977 MW_CHAR_REFS_REGEX,
978 array( 'Sanitizer', 'decodeCharReferencesCallback' ),
979 $text );
980 }
981
982 /**
983 * @param string $matches
984 * @return string
985 */
986 static function decodeCharReferencesCallback( $matches ) {
987 if( $matches[1] != '' ) {
988 return Sanitizer::decodeEntity( $matches[1] );
989 } elseif( $matches[2] != '' ) {
990 return Sanitizer::decodeChar( intval( $matches[2] ) );
991 } elseif( $matches[3] != '' ) {
992 return Sanitizer::decodeChar( hexdec( $matches[3] ) );
993 } elseif( $matches[4] != '' ) {
994 return Sanitizer::decodeChar( hexdec( $matches[4] ) );
995 }
996 # Last case should be an ampersand by itself
997 return $matches[0];
998 }
999
1000 /**
1001 * Return UTF-8 string for a codepoint if that is a valid
1002 * character reference, otherwise U+FFFD REPLACEMENT CHARACTER.
1003 * @param int $codepoint
1004 * @return string
1005 * @private
1006 */
1007 static function decodeChar( $codepoint ) {
1008 if( Sanitizer::validateCodepoint( $codepoint ) ) {
1009 return codepointToUtf8( $codepoint );
1010 } else {
1011 return UTF8_REPLACEMENT;
1012 }
1013 }
1014
1015 /**
1016 * If the named entity is defined in the HTML 4.0/XHTML 1.0 DTD,
1017 * return the UTF-8 encoding of that character. Otherwise, returns
1018 * pseudo-entity source (eg &foo;)
1019 *
1020 * @param string $name
1021 * @return string
1022 */
1023 static function decodeEntity( $name ) {
1024 global $wgHtmlEntities, $wgHtmlEntityAliases;
1025 if ( isset( $wgHtmlEntityAliases[$name] ) ) {
1026 $name = $wgHtmlEntityAliases[$name];
1027 }
1028 if( isset( $wgHtmlEntities[$name] ) ) {
1029 return codepointToUtf8( $wgHtmlEntities[$name] );
1030 } else {
1031 return "&$name;";
1032 }
1033 }
1034
1035 /**
1036 * Fetch the whitelist of acceptable attributes for a given
1037 * element name.
1038 *
1039 * @param string $element
1040 * @return array
1041 */
1042 static function attributeWhitelist( $element ) {
1043 static $list;
1044 if( !isset( $list ) ) {
1045 $list = Sanitizer::setupAttributeWhitelist();
1046 }
1047 return isset( $list[$element] )
1048 ? $list[$element]
1049 : array();
1050 }
1051
1052 /**
1053 * @todo Document it a bit
1054 * @return array
1055 */
1056 static function setupAttributeWhitelist() {
1057 $common = array( 'id', 'class', 'lang', 'dir', 'title', 'style' );
1058 $block = array_merge( $common, array( 'align' ) );
1059 $tablealign = array( 'align', 'char', 'charoff', 'valign' );
1060 $tablecell = array( 'abbr',
1061 'axis',
1062 'headers',
1063 'scope',
1064 'rowspan',
1065 'colspan',
1066 'nowrap', # deprecated
1067 'width', # deprecated
1068 'height', # deprecated
1069 'bgcolor' # deprecated
1070 );
1071
1072 # Numbers refer to sections in HTML 4.01 standard describing the element.
1073 # See: http://www.w3.org/TR/html4/
1074 $whitelist = array (
1075 # 7.5.4
1076 'div' => $block,
1077 'center' => $common, # deprecated
1078 'span' => $block, # ??
1079
1080 # 7.5.5
1081 'h1' => $block,
1082 'h2' => $block,
1083 'h3' => $block,
1084 'h4' => $block,
1085 'h5' => $block,
1086 'h6' => $block,
1087
1088 # 7.5.6
1089 # address
1090
1091 # 8.2.4
1092 # bdo
1093
1094 # 9.2.1
1095 'em' => $common,
1096 'strong' => $common,
1097 'cite' => $common,
1098 # dfn
1099 'code' => $common,
1100 # samp
1101 # kbd
1102 'var' => $common,
1103 # abbr
1104 # acronym
1105
1106 # 9.2.2
1107 'blockquote' => array_merge( $common, array( 'cite' ) ),
1108 # q
1109
1110 # 9.2.3
1111 'sub' => $common,
1112 'sup' => $common,
1113
1114 # 9.3.1
1115 'p' => $block,
1116
1117 # 9.3.2
1118 'br' => array( 'id', 'class', 'title', 'style', 'clear' ),
1119
1120 # 9.3.4
1121 'pre' => array_merge( $common, array( 'width' ) ),
1122
1123 # 9.4
1124 'ins' => array_merge( $common, array( 'cite', 'datetime' ) ),
1125 'del' => array_merge( $common, array( 'cite', 'datetime' ) ),
1126
1127 # 10.2
1128 'ul' => array_merge( $common, array( 'type' ) ),
1129 'ol' => array_merge( $common, array( 'type', 'start' ) ),
1130 'li' => array_merge( $common, array( 'type', 'value' ) ),
1131
1132 # 10.3
1133 'dl' => $common,
1134 'dd' => $common,
1135 'dt' => $common,
1136
1137 # 11.2.1
1138 'table' => array_merge( $common,
1139 array( 'summary', 'width', 'border', 'frame',
1140 'rules', 'cellspacing', 'cellpadding',
1141 'align', 'bgcolor',
1142 ) ),
1143
1144 # 11.2.2
1145 'caption' => array_merge( $common, array( 'align' ) ),
1146
1147 # 11.2.3
1148 'thead' => array_merge( $common, $tablealign ),
1149 'tfoot' => array_merge( $common, $tablealign ),
1150 'tbody' => array_merge( $common, $tablealign ),
1151
1152 # 11.2.4
1153 'colgroup' => array_merge( $common, array( 'span', 'width' ), $tablealign ),
1154 'col' => array_merge( $common, array( 'span', 'width' ), $tablealign ),
1155
1156 # 11.2.5
1157 'tr' => array_merge( $common, array( 'bgcolor' ), $tablealign ),
1158
1159 # 11.2.6
1160 'td' => array_merge( $common, $tablecell, $tablealign ),
1161 'th' => array_merge( $common, $tablecell, $tablealign ),
1162
1163 # 15.2.1
1164 'tt' => $common,
1165 'b' => $common,
1166 'i' => $common,
1167 'big' => $common,
1168 'small' => $common,
1169 'strike' => $common,
1170 's' => $common,
1171 'u' => $common,
1172
1173 # 15.2.2
1174 'font' => array_merge( $common, array( 'size', 'color', 'face' ) ),
1175 # basefont
1176
1177 # 15.3
1178 'hr' => array_merge( $common, array( 'noshade', 'size', 'width' ) ),
1179
1180 # XHTML Ruby annotation text module, simple ruby only.
1181 # http://www.w3c.org/TR/ruby/
1182 'ruby' => $common,
1183 # rbc
1184 # rtc
1185 'rb' => $common,
1186 'rt' => $common, #array_merge( $common, array( 'rbspan' ) ),
1187 'rp' => $common,
1188 );
1189 return $whitelist;
1190 }
1191
1192 /**
1193 * Take a fragment of (potentially invalid) HTML and return
1194 * a version with any tags removed, encoded as plain text.
1195 *
1196 * Warning: this return value must be further escaped for literal
1197 * inclusion in HTML output as of 1.10!
1198 *
1199 * @param string $text HTML fragment
1200 * @return string
1201 */
1202 static function stripAllTags( $text ) {
1203 # Actual <tags>
1204 $text = StringUtils::delimiterReplace( '<', '>', '', $text );
1205
1206 # Normalize &entities and whitespace
1207 $text = self::decodeCharReferences( $text );
1208 $text = self::normalizeWhitespace( $text );
1209
1210 return $text;
1211 }
1212
1213 /**
1214 * Hack up a private DOCTYPE with HTML's standard entity declarations.
1215 * PHP 4 seemed to know these if you gave it an HTML doctype, but
1216 * PHP 5.1 doesn't.
1217 *
1218 * Use for passing XHTML fragments to PHP's XML parsing functions
1219 *
1220 * @return string
1221 * @static
1222 */
1223 static function hackDocType() {
1224 global $wgHtmlEntities;
1225 $out = "<!DOCTYPE html [\n";
1226 foreach( $wgHtmlEntities as $entity => $codepoint ) {
1227 $out .= "<!ENTITY $entity \"&#$codepoint;\">";
1228 }
1229 $out .= "]>\n";
1230 return $out;
1231 }
1232
1233 static function cleanUrl( $url, $hostname=true ) {
1234 # Normalize any HTML entities in input. They will be
1235 # re-escaped by makeExternalLink().
1236 $url = Sanitizer::decodeCharReferences( $url );
1237
1238 # Escape any control characters introduced by the above step
1239 $url = preg_replace( '/[\][<>"\\x00-\\x20\\x7F]/e', "urlencode('\\0')", $url );
1240
1241 # Validate hostname portion
1242 $matches = array();
1243 if( preg_match( '!^([^:]+:)(//[^/]+)?(.*)$!iD', $url, $matches ) ) {
1244 list( /* $whole */, $protocol, $host, $rest ) = $matches;
1245
1246 // Characters that will be ignored in IDNs.
1247 // http://tools.ietf.org/html/3454#section-3.1
1248 // Strip them before further processing so blacklists and such work.
1249 $strip = "/
1250 \\s| # general whitespace
1251 \xc2\xad| # 00ad SOFT HYPHEN
1252 \xe1\xa0\x86| # 1806 MONGOLIAN TODO SOFT HYPHEN
1253 \xe2\x80\x8b| # 200b ZERO WIDTH SPACE
1254 \xe2\x81\xa0| # 2060 WORD JOINER
1255 \xef\xbb\xbf| # feff ZERO WIDTH NO-BREAK SPACE
1256 \xcd\x8f| # 034f COMBINING GRAPHEME JOINER
1257 \xe1\xa0\x8b| # 180b MONGOLIAN FREE VARIATION SELECTOR ONE
1258 \xe1\xa0\x8c| # 180c MONGOLIAN FREE VARIATION SELECTOR TWO
1259 \xe1\xa0\x8d| # 180d MONGOLIAN FREE VARIATION SELECTOR THREE
1260 \xe2\x80\x8c| # 200c ZERO WIDTH NON-JOINER
1261 \xe2\x80\x8d| # 200d ZERO WIDTH JOINER
1262 [\xef\xb8\x80-\xef\xb8\x8f] # fe00-fe00f VARIATION SELECTOR-1-16
1263 /xuD";
1264
1265 $host = preg_replace( $strip, '', $host );
1266
1267 // @fixme: validate hostnames here
1268
1269 return $protocol . $host . $rest;
1270 } else {
1271 return $url;
1272 }
1273 }
1274
1275 }
1276
1277