3 # kses 0.2.2 - HTML/XHTML filter that only allows some elements and attributes
4 # Copyright (C) 2002, 2003, 2005 Ulf Harnhammar
6 # This program is free software and open source software; you can redistribute
7 # it and/or modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the License,
9 # or (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit
19 # http://www.gnu.org/licenses/gpl.html
21 # *** CONTACT INFORMATION ***
23 # E-mail: metaur at users dot sourceforge dot net
24 # Web page: http://sourceforge.net/projects/kses
25 # Paper mail: Ulf Harnhammar
30 # [kses strips evil scripts!]
33 function kses($string, $allowed_html, $allowed_protocols =
34 array('http', 'https', 'ftp', 'news', 'nntp', 'telnet',
36 ###############################################################################
37 # This function makes sure that only the allowed HTML element names, attribute
38 # names and attribute values plus only sane HTML entities will occur in
39 # $string. You have to remove any slashes from PHP's magic quotes before you
41 ###############################################################################
43 $string = kses_no_null($string);
44 $string = kses_js_entities($string);
45 $string = kses_normalize_entities($string);
46 $string = kses_hook($string);
47 $allowed_html_fixed = kses_array_lc($allowed_html);
48 return kses_split($string, $allowed_html_fixed, $allowed_protocols);
52 function kses_hook($string)
53 ###############################################################################
54 # You add any kses hooks here.
55 ###############################################################################
58 } # function kses_hook
61 function kses_version()
62 ###############################################################################
63 # This function returns kses' version number.
64 ###############################################################################
67 } # function kses_version
70 function kses_split($string, $allowed_html, $allowed_protocols)
71 ###############################################################################
72 # This function searches for HTML tags, no matter how malformed. It also
73 # matches stray ">" characters.
74 ###############################################################################
76 return preg_replace('%(<'. # EITHER: <
77 '[^>]*'. # things that aren't >
78 '(>|$)'. # > or end of string
79 '|>)%e', # OR: just a >
80 "kses_split2('\\1', \$allowed_html, ".
81 '$allowed_protocols)',
83 } # function kses_split
86 function kses_split2($string, $allowed_html, $allowed_protocols)
87 ###############################################################################
88 # This function does a lot of work. It rejects some very malformed things
89 # like <:::>. It returns an empty string, if the element isn't allowed (look
90 # ma, no strip_tags()!). Otherwise it splits the tag into an element and an
92 ###############################################################################
94 $string = kses_stripslashes($string);
96 if (substr($string, 0, 1) != '<')
98 # It matched a ">" character
100 if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches))
102 # It's seriously malformed
104 $slash = trim($matches[1]);
106 $attrlist = $matches[3];
108 if (!@isset
($allowed_html[strtolower($elem)]))
110 # They are using a not allowed HTML element
113 return "<$slash$elem>";
114 # No attributes are allowed for closing elements
116 return kses_attr("$slash$elem", $attrlist, $allowed_html,
118 } # function kses_split2
121 function kses_attr($element, $attr, $allowed_html, $allowed_protocols)
122 ###############################################################################
123 # This function removes all attributes, if none are allowed for this element.
124 # If some are allowed it calls kses_hair() to split them further, and then it
125 # builds up new HTML code from the data that kses_hair() returns. It also
126 # removes "<" and ">" characters, if there are any left. One more thing it
127 # does is to check if the tag has a closing XHTML slash, and if it does,
128 # it puts one in the returned code as well.
129 ###############################################################################
131 # Is there a closing XHTML slash at the end of the attributes?
134 if (preg_match('%\s/\s*$%', $attr))
137 # Are any attributes allowed at all for this element?
139 if (@count
($allowed_html[strtolower($element)]) == 0)
140 return "<$element$xhtml_slash>";
144 $attrarr = kses_hair($attr, $allowed_protocols);
146 # Go through $attrarr, and save the allowed attributes for this element
151 foreach ($attrarr as $arreach)
153 if (!@isset
($allowed_html[strtolower($element)]
154 [strtolower($arreach['name'])]))
155 continue; # the attribute is not allowed
157 $current = $allowed_html[strtolower($element)]
158 [strtolower($arreach['name'])];
160 if (!is_array($current))
161 $attr2 .= ' '.$arreach['whole'];
162 # there are no checks
166 # there are some checks
168 foreach ($current as $currkey => $currval)
169 if (!kses_check_attr_val($arreach['value'], $arreach['vless'],
171 { $ok = false; break; }
174 $attr2 .= ' '.$arreach['whole']; # it passed them
175 } # if !is_array($current)
178 # Remove any "<" or ">" characters
180 $attr2 = preg_replace('/[<>]/', '', $attr2);
182 return "<$element$attr2$xhtml_slash>";
183 } # function kses_attr
186 function kses_hair($attr, $allowed_protocols)
187 ###############################################################################
188 # This function does a lot of work. It parses an attribute list into an array
189 # with attribute data, and tries to do the right thing even if it gets weird
190 # input. It will add quotes around attribute values that don't have any quotes
191 # or apostrophes around them, to make it easier to produce HTML code that will
192 # conform to W3C's HTML specification. It will also remove bad URL protocols
193 # from attribute values.
194 ###############################################################################
200 # Loop through the whole attribute list
202 while (strlen($attr) != 0)
204 $working = 0; # Was the last operation successful?
208 case 0: # attribute name, href for instance
210 if (preg_match('/^([-a-zA-Z]+)/', $attr, $match))
212 $attrname = $match[1];
213 $working = $mode = 1;
214 $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr);
219 case 1: # equals sign or valueless ("selected")
221 if (preg_match('/^\s*=\s*/', $attr)) # equals sign
223 $working = 1; $mode = 2;
224 $attr = preg_replace('/^\s*=\s*/', '', $attr);
228 if (preg_match('/^\s+/', $attr)) # valueless
230 $working = 1; $mode = 0;
232 ('name' => $attrname,
234 'whole' => $attrname,
236 $attr = preg_replace('/^\s+/', '', $attr);
241 case 2: # attribute value, a URL after href= for instance
243 if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match))
246 $thisval = kses_bad_protocol($match[1], $allowed_protocols);
249 ('name' => $attrname,
251 'whole' => "$attrname=\"$thisval\"",
253 $working = 1; $mode = 0;
254 $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
258 if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match))
261 $thisval = kses_bad_protocol($match[1], $allowed_protocols);
264 ('name' => $attrname,
266 'whole' => "$attrname='$thisval'",
268 $working = 1; $mode = 0;
269 $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
273 if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match))
276 $thisval = kses_bad_protocol($match[1], $allowed_protocols);
279 ('name' => $attrname,
281 'whole' => "$attrname=\"$thisval\"",
283 # We add quotes to conform to W3C's HTML spec.
284 $working = 1; $mode = 0;
285 $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
291 if ($working == 0) # not well formed, remove and try again
293 $attr = kses_html_error($attr);
299 # special case, for when the attribute list ends with a valueless
300 # attribute like "selected"
302 ('name' => $attrname,
304 'whole' => $attrname,
308 } # function kses_hair
311 function kses_check_attr_val($value, $vless, $checkname, $checkvalue)
312 ###############################################################################
313 # This function performs different checks for attribute values. The currently
314 # implemented checks are "maxlen", "minlen", "maxval", "minval" and "valueless"
315 # with even more checks to come soon.
316 ###############################################################################
320 switch (strtolower($checkname))
323 # The maxlen check makes sure that the attribute value has a length not
324 # greater than the given value. This can be used to avoid Buffer Overflows
325 # in WWW clients and various Internet servers.
327 if (strlen($value) > $checkvalue)
332 # The minlen check makes sure that the attribute value has a length not
333 # smaller than the given value.
335 if (strlen($value) < $checkvalue)
340 # The maxval check does two things: it checks that the attribute value is
341 # an integer from 0 and up, without an excessive amount of zeroes or
342 # whitespace (to avoid Buffer Overflows). It also checks that the attribute
343 # value is not greater than the given value.
344 # This check can be used to avoid Denial of Service attacks.
346 if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
348 if ($value > $checkvalue)
353 # The minval check checks that the attribute value is a positive integer,
354 # and that it is not smaller than the given value.
356 if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
358 if ($value < $checkvalue)
363 # The valueless check checks if the attribute has a value
364 # (like <a href="blah">) or not (<option selected>). If the given value
365 # is a "y" or a "Y", the attribute must not have a value.
366 # If the given value is an "n" or an "N", the attribute must have one.
368 if (strtolower($checkvalue) != $vless)
374 } # function kses_check_attr_val
377 function kses_bad_protocol($string, $allowed_protocols)
378 ###############################################################################
379 # This function removes all non-allowed protocols from the beginning of
380 # $string. It ignores whitespace and the case of the letters, and it does
381 # understand HTML entities. It does its work in a while loop, so it won't be
382 # fooled by a string like "javascript:javascript:alert(57)".
383 ###############################################################################
385 $string = kses_no_null($string);
386 $string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature"
387 $string2 = $string.'a';
389 while ($string != $string2)
392 $string = kses_bad_protocol_once($string, $allowed_protocols);
396 } # function kses_bad_protocol
399 function kses_no_null($string)
400 ###############################################################################
401 # This function removes any NULL characters in $string.
402 ###############################################################################
404 $string = preg_replace('/\0+/', '', $string);
405 $string = preg_replace('/(\\\\0)+/', '', $string);
408 } # function kses_no_null
411 function kses_stripslashes($string)
412 ###############################################################################
413 # This function changes the character sequence \" to just "
414 # It leaves all other slashes alone. It's really weird, but the quoting from
415 # preg_replace(//e) seems to require this.
416 ###############################################################################
418 return preg_replace('%\\\\"%', '"', $string);
419 } # function kses_stripslashes
422 function kses_array_lc($inarray)
423 ###############################################################################
424 # This function goes through an array, and changes the keys to all lower case.
425 ###############################################################################
429 foreach ($inarray as $inkey => $inval)
431 $outkey = strtolower($inkey);
432 $outarray[$outkey] = array();
434 foreach ($inval as $inkey2 => $inval2)
436 $outkey2 = strtolower($inkey2);
437 $outarray[$outkey][$outkey2] = $inval2;
442 } # function kses_array_lc
445 function kses_js_entities($string)
446 ###############################################################################
447 # This function removes the HTML JavaScript entities found in early versions of
449 ###############################################################################
451 return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
452 } # function kses_js_entities
455 function kses_html_error($string)
456 ###############################################################################
457 # This function deals with parsing errors in kses_hair(). The general plan is
458 # to remove everything to and including some whitespace, but it deals with
459 # quotes and apostrophes as well.
460 ###############################################################################
462 return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string);
463 } # function kses_html_error
466 function kses_bad_protocol_once($string, $allowed_protocols)
467 ###############################################################################
468 # This function searches for URL protocols at the beginning of $string, while
469 # handling whitespace and HTML entities.
470 ###############################################################################
472 return preg_replace('/^((&[^;]*;|[\sA-Za-z0-9])*)'.
473 '(:|�*58;|&#[Xx]0*3[Aa];)\s*/e',
474 'kses_bad_protocol_once2("\\1", $allowed_protocols)',
476 } # function kses_bad_protocol_once
479 function kses_bad_protocol_once2($string, $allowed_protocols)
480 ###############################################################################
481 # This function processes URL protocols, checks to see if they're in the white-
482 # list or not, and returns different data depending on the answer.
483 ###############################################################################
485 $string2 = kses_decode_entities($string);
486 $string2 = preg_replace('/\s/', '', $string2);
487 $string2 = kses_no_null($string2);
488 $string2 = preg_replace('/\xad+/', '', $string2);
489 # deals with Opera "feature"
490 $string2 = strtolower($string2);
493 foreach ($allowed_protocols as $one_protocol)
494 if (strtolower($one_protocol) == $string2)
504 } # function kses_bad_protocol_once2
507 function kses_normalize_entities($string)
508 ###############################################################################
509 # This function normalizes HTML entities. It will convert "AT&T" to the correct
510 # "AT&T", ":" to ":", "&#XYZZY;" to "&#XYZZY;" and so on.
511 ###############################################################################
513 # Disarm all entities by converting & to &
515 $string = str_replace('&', '&', $string);
517 # Change back the allowed entities in our entity whitelist
519 $string = preg_replace('/&([A-Za-z][A-Za-z0-9]{0,19});/',
521 $string = preg_replace('/&#0*([0-9]{1,5});/e',
522 'kses_normalize_entities2("\\1")', $string);
523 $string = preg_replace('/&#([Xx])0*(([0-9A-Fa-f]{2}){1,2});/',
524 '&#\\1\\2;', $string);
527 } # function kses_normalize_entities
530 function kses_normalize_entities2($i)
531 ###############################################################################
532 # This function helps kses_normalize_entities() to only accept 16 bit values
533 # and nothing more for &#number; entities.
534 ###############################################################################
536 return (($i > 65535) ?
"&#$i;" : "&#$i;");
537 } # function kses_normalize_entities2
540 function kses_decode_entities($string)
541 ###############################################################################
542 # This function decodes numeric HTML entities (A and A). It doesn't
543 # do anything with other entities like ä, but we don't need them in the
544 # URL protocol whitelisting system anyway.
545 ###############################################################################
547 $string = preg_replace('/&#([0-9]+);/e', 'chr("\\1")', $string);
548 $string = preg_replace('/&#[Xx]([0-9A-Fa-f]+);/e', 'chr(hexdec("\\1"))',
552 } # function kses_decode_entities