]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/kses.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / kses.php
1 <?php
2 /**
3  * HTML/XHTML filter that only allows some elements and attributes
4  *
5  * Added wp_ prefix to avoid conflicts with existing kses users
6  *
7  * @version 0.2.2
8  * @copyright (C) 2002, 2003, 2005
9  * @author Ulf Harnhammar <metaur@users.sourceforge.net>
10  *
11  * @package External
12  * @subpackage KSES
13  *
14  * @internal
15  * *** CONTACT INFORMATION ***
16  * E-mail:      metaur at users dot sourceforge dot net
17  * Web page:    http://sourceforge.net/projects/kses
18  * Paper mail:  Ulf Harnhammar
19  *              Ymergatan 17 C
20  *              753 25  Uppsala
21  *              SWEDEN
22  *
23  * [kses strips evil scripts!]
24  */
25
26 /**
27  * You can override this in your my-hacks.php file
28  * You can also override this in a plugin file. The
29  * my-hacks.php is deprecated in its usage.
30  *
31  * @since 1.2.0
32  */
33 if (!defined('CUSTOM_TAGS'))
34         define('CUSTOM_TAGS', false);
35
36 if (!CUSTOM_TAGS) {
37         /**
38          * Kses global for default allowable HTML tags
39          *
40          * Can be override by using CUSTOM_TAGS constant
41          * @global array $allowedposttags
42          * @since 2.0.0
43          */
44         $allowedposttags = array(
45                 'address' => array(),
46                 'a' => array(
47                         'class' => array (),
48                         'href' => array (),
49                         'id' => array (),
50                         'title' => array (),
51                         'rel' => array (),
52                         'rev' => array (),
53                         'name' => array (),
54                         'target' => array()),
55                 'abbr' => array(
56                         'class' => array (),
57                         'title' => array ()),
58                 'acronym' => array(
59                         'title' => array ()),
60                 'b' => array(),
61                 'big' => array(),
62                 'blockquote' => array(
63                         'id' => array (),
64                         'cite' => array (),
65                         'class' => array(),
66                         'lang' => array(),
67                         'xml:lang' => array()),
68                 'br' => array (
69                         'class' => array ()),
70                 'button' => array(
71                         'disabled' => array (),
72                         'name' => array (),
73                         'type' => array (),
74                         'value' => array ()),
75                 'caption' => array(
76                         'align' => array (),
77                         'class' => array ()),
78                 'cite' => array (
79                         'class' => array(),
80                         'dir' => array(),
81                         'lang' => array(),
82                         'title' => array ()),
83                 'code' => array (
84                         'style' => array()),
85                 'col' => array(
86                         'align' => array (),
87                         'char' => array (),
88                         'charoff' => array (),
89                         'span' => array (),
90                         'dir' => array(),
91                         'style' => array (),
92                         'valign' => array (),
93                         'width' => array ()),
94                 'del' => array(
95                         'datetime' => array ()),
96                 'dd' => array(),
97                 'div' => array(
98                         'align' => array (),
99                         'class' => array (),
100                         'dir' => array (),
101                         'lang' => array(),
102                         'style' => array (),
103                         'xml:lang' => array()),
104                 'dl' => array(),
105                 'dt' => array(),
106                 'em' => array(),
107                 'fieldset' => array(),
108                 'font' => array(
109                         'color' => array (),
110                         'face' => array (),
111                         'size' => array ()),
112                 'form' => array(
113                         'action' => array (),
114                         'accept' => array (),
115                         'accept-charset' => array (),
116                         'enctype' => array (),
117                         'method' => array (),
118                         'name' => array (),
119                         'target' => array ()),
120                 'h1' => array(
121                         'align' => array (),
122                         'class' => array ()),
123                 'h2' => array(
124                         'align' => array (),
125                         'class' => array ()),
126                 'h3' => array(
127                         'align' => array (),
128                         'class' => array ()),
129                 'h4' => array(
130                         'align' => array (),
131                         'class' => array ()),
132                 'h5' => array(
133                         'align' => array (),
134                         'class' => array ()),
135                 'h6' => array(
136                         'align' => array (),
137                         'class' => array ()),
138                 'hr' => array(
139                         'align' => array (),
140                         'class' => array (),
141                         'noshade' => array (),
142                         'size' => array (),
143                         'width' => array ()),
144                 'i' => array(),
145                 'img' => array(
146                         'alt' => array (),
147                         'align' => array (),
148                         'border' => array (),
149                         'class' => array (),
150                         'height' => array (),
151                         'hspace' => array (),
152                         'longdesc' => array (),
153                         'vspace' => array (),
154                         'src' => array (),
155                         'style' => array (),
156                         'width' => array ()),
157                 'ins' => array(
158                         'datetime' => array (),
159                         'cite' => array ()),
160                 'kbd' => array(),
161                 'label' => array(
162                         'for' => array ()),
163                 'legend' => array(
164                         'align' => array ()),
165                 'li' => array (
166                         'align' => array (),
167                         'class' => array ()),
168                 'p' => array(
169                         'class' => array (),
170                         'align' => array (),
171                         'dir' => array(),
172                         'lang' => array(),
173                         'style' => array (),
174                         'xml:lang' => array()),
175                 'pre' => array(
176                         'style' => array(),
177                         'width' => array ()),
178                 'q' => array(
179                         'cite' => array ()),
180                 's' => array(),
181                 'span' => array (
182                         'class' => array (),
183                         'dir' => array (),
184                         'align' => array (),
185                         'style' => array (),
186                         'title' => array ()),
187                 'strike' => array(),
188                 'strong' => array(),
189                 'sub' => array(),
190                 'sup' => array(),
191                 'table' => array(
192                         'align' => array (),
193                         'bgcolor' => array (),
194                         'border' => array (),
195                         'cellpadding' => array (),
196                         'cellspacing' => array (),
197                         'class' => array (),
198                         'dir' => array(),
199                         'id' => array(),
200                         'rules' => array (),
201                         'style' => array (),
202                         'summary' => array (),
203                         'width' => array ()),
204                 'tbody' => array(
205                         'align' => array (),
206                         'char' => array (),
207                         'charoff' => array (),
208                         'valign' => array ()),
209                 'td' => array(
210                         'abbr' => array (),
211                         'align' => array (),
212                         'axis' => array (),
213                         'bgcolor' => array (),
214                         'char' => array (),
215                         'charoff' => array (),
216                         'class' => array (),
217                         'colspan' => array (),
218                         'dir' => array(),
219                         'headers' => array (),
220                         'height' => array (),
221                         'nowrap' => array (),
222                         'rowspan' => array (),
223                         'scope' => array (),
224                         'style' => array (),
225                         'valign' => array (),
226                         'width' => array ()),
227                 'textarea' => array(
228                         'cols' => array (),
229                         'rows' => array (),
230                         'disabled' => array (),
231                         'name' => array (),
232                         'readonly' => array ()),
233                 'tfoot' => array(
234                         'align' => array (),
235                         'char' => array (),
236                         'class' => array (),
237                         'charoff' => array (),
238                         'valign' => array ()),
239                 'th' => array(
240                         'abbr' => array (),
241                         'align' => array (),
242                         'axis' => array (),
243                         'bgcolor' => array (),
244                         'char' => array (),
245                         'charoff' => array (),
246                         'class' => array (),
247                         'colspan' => array (),
248                         'headers' => array (),
249                         'height' => array (),
250                         'nowrap' => array (),
251                         'rowspan' => array (),
252                         'scope' => array (),
253                         'valign' => array (),
254                         'width' => array ()),
255                 'thead' => array(
256                         'align' => array (),
257                         'char' => array (),
258                         'charoff' => array (),
259                         'class' => array (),
260                         'valign' => array ()),
261                 'title' => array(),
262                 'tr' => array(
263                         'align' => array (),
264                         'bgcolor' => array (),
265                         'char' => array (),
266                         'charoff' => array (),
267                         'class' => array (),
268                         'style' => array (),
269                         'valign' => array ()),
270                 'tt' => array(),
271                 'u' => array(),
272                 'ul' => array (
273                         'class' => array (),
274                         'style' => array (), 
275                         'type' => array ()),
276                 'ol' => array (
277                         'class' => array (),
278                         'start' => array (),
279                         'style' => array (), 
280                         'type' => array ()),
281                 'var' => array ());
282         /**
283          * Kses allowed HTML elements
284          *
285          * @global array $allowedtags
286          * @since 1.0.0
287          */
288         $allowedtags = array(
289                 'a' => array(
290                         'href' => array (),
291                         'title' => array ()),
292                 'abbr' => array(
293                         'title' => array ()),
294                 'acronym' => array(
295                         'title' => array ()),
296                 'b' => array(),
297                 'blockquote' => array(
298                         'cite' => array ()),
299                 //      'br' => array(),
300                 'cite' => array (),
301                 'code' => array(),
302                 'del' => array(
303                         'datetime' => array ()),
304                 //      'dd' => array(),
305                 //      'dl' => array(),
306                 //      'dt' => array(),
307                 'em' => array (), 'i' => array (),
308                 //      'ins' => array('datetime' => array(), 'cite' => array()),
309                 //      'li' => array(),
310                 //      'ol' => array(),
311                 //      'p' => array(),
312                 'q' => array(
313                         'cite' => array ()),
314                 'strike' => array(),
315                 'strong' => array(),
316                 //      'sub' => array(),
317                 //      'sup' => array(),
318                 //      'u' => array(),
319                 //      'ul' => array(),
320         );
321 }
322
323 /**
324  * wp_kses() - Filters content and keeps only allowable HTML elements.
325  *
326  * This function makes sure that only the allowed HTML element names,
327  * attribute names and attribute values plus only sane HTML entities
328  * will occur in $string. You have to remove any slashes from PHP's
329  * magic quotes before you call this function.
330  *
331  * The default allowed protocols are 'http', 'https', 'ftp', 'mailto',
332  * 'news', 'irc', 'gopher', 'nntp', 'feed', and finally 'telnet. This
333  * covers all common link protocols, except for 'javascript' which
334  * should not be allowed for untrusted users.
335  *
336  * @since 1.0.0
337  *
338  * @param string $string Content to filter through kses
339  * @param array $allowed_html List of allowed HTML elements
340  * @param array $allowed_protocols Optional. Allowed protocol in links.
341  * @return string Filtered content with only allowed HTML elements
342  */
343 function wp_kses($string, $allowed_html, $allowed_protocols = array ('http', 'https', 'ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet')) {
344         $string = wp_kses_no_null($string);
345         $string = wp_kses_js_entities($string);
346         $string = wp_kses_normalize_entities($string);
347         $allowed_html_fixed = wp_kses_array_lc($allowed_html);
348         $string = wp_kses_hook($string, $allowed_html_fixed, $allowed_protocols); // WP changed the order of these funcs and added args to wp_kses_hook
349         return wp_kses_split($string, $allowed_html_fixed, $allowed_protocols);
350 }
351
352 /**
353  * wp_kses_hook() - You add any kses hooks here.
354  *
355  * There is currently only one kses WordPress hook and it is
356  * called here. All parameters are passed to the hooks and
357  * expected to recieve a string.
358  *
359  * @since 1.0.0
360  *
361  * @param string $string Content to filter through kses
362  * @param array $allowed_html List of allowed HTML elements
363  * @param array $allowed_protocols Allowed protocol in links
364  * @return string Filtered content through 'pre_kses' hook
365  */
366 function wp_kses_hook($string, $allowed_html, $allowed_protocols) {
367         $string = apply_filters('pre_kses', $string, $allowed_html, $allowed_protocols);
368         return $string;
369 }
370
371 /**
372  * wp_kses_version() - This function returns kses' version number.
373  *
374  * @since 1.0.0
375  *
376  * @return string Version Number
377  */
378 function wp_kses_version() {
379         return '0.2.2';
380 }
381
382 /**
383  * wp_kses_split() - Searches for HTML tags, no matter how malformed
384  *
385  * It also matches stray ">" characters.
386  *
387  * @since 1.0.0
388  *
389  * @param string $string Content to filter
390  * @param array $allowed_html Allowed HTML elements
391  * @param array $allowed_protocols Allowed protocols to keep
392  * @return string Content with fixed HTML tags
393  */
394 function wp_kses_split($string, $allowed_html, $allowed_protocols) {
395         return preg_replace('%((<!--.*?(-->|$))|(<[^>]*(>|$)|>))%e',
396         "wp_kses_split2('\\1', \$allowed_html, ".'$allowed_protocols)', $string);
397 }
398
399 /**
400  * wp_kses_split2() - Callback for wp_kses_split for fixing malformed HTML tags
401  *
402  * This function does a lot of work. It rejects some very malformed things
403  * like <:::>. It returns an empty string, if the element isn't allowed (look
404  * ma, no strip_tags()!). Otherwise it splits the tag into an element and an
405  * attribute list.
406  *
407  * After the tag is split into an element and an attribute list, it is run
408  * through another filter which will remove illegal attributes and once
409  * that is completed, will be returned.
410  *
411  * @since 1.0.0
412  * @uses wp_kses_attr()
413  *
414  * @param string $string Content to filter
415  * @param array $allowed_html Allowed HTML elements
416  * @param array $allowed_protocols Allowed protocols to keep
417  * @return string Fixed HTML element
418  */
419 function wp_kses_split2($string, $allowed_html, $allowed_protocols) {
420         $string = wp_kses_stripslashes($string);
421
422         if (substr($string, 0, 1) != '<')
423                 return '&gt;';
424         # It matched a ">" character
425
426         if (preg_match('%^<!--(.*?)(-->)?$%', $string, $matches)) {
427                 $string = str_replace(array('<!--', '-->'), '', $matches[1]);
428                 while ( $string != $newstring = wp_kses($string, $allowed_html, $allowed_protocols) )
429                         $string = $newstring;
430                 if ( $string == '' )
431                         return '';
432                 return "<!--{$string}-->";
433         }
434         # Allow HTML comments
435
436         if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches))
437                 return '';
438         # It's seriously malformed
439
440         $slash = trim($matches[1]);
441         $elem = $matches[2];
442         $attrlist = $matches[3];
443
444         if (!@isset($allowed_html[strtolower($elem)]))
445                 return '';
446         # They are using a not allowed HTML element
447
448         if ($slash != '')
449                 return "<$slash$elem>";
450         # No attributes are allowed for closing elements
451
452         return wp_kses_attr("$slash$elem", $attrlist, $allowed_html, $allowed_protocols);
453 }
454
455 /**
456  * wp_kses_attr() - Removes all attributes, if none are allowed for this element
457  *
458  * If some are allowed it calls wp_kses_hair() to split them further, and then
459  * it builds up new HTML code from the data that kses_hair() returns. It also
460  * removes "<" and ">" characters, if there are any left. One more thing it
461  * does is to check if the tag has a closing XHTML slash, and if it does, it
462  * puts one in the returned code as well.
463  *
464  * @since 1.0.0
465  *
466  * @param string $element HTML element/tag
467  * @param string $attr HTML attributes from HTML element to closing HTML element tag
468  * @param array $allowed_html Allowed HTML elements
469  * @param array $allowed_protocols Allowed protocols to keep
470  * @return string Sanitized HTML element
471  */
472 function wp_kses_attr($element, $attr, $allowed_html, $allowed_protocols) {
473         # Is there a closing XHTML slash at the end of the attributes?
474
475         $xhtml_slash = '';
476         if (preg_match('%\s/\s*$%', $attr))
477                 $xhtml_slash = ' /';
478
479         # Are any attributes allowed at all for this element?
480
481         if (@ count($allowed_html[strtolower($element)]) == 0)
482                 return "<$element$xhtml_slash>";
483
484         # Split it
485
486         $attrarr = wp_kses_hair($attr, $allowed_protocols);
487
488         # Go through $attrarr, and save the allowed attributes for this element
489         # in $attr2
490
491         $attr2 = '';
492
493         foreach ($attrarr as $arreach) {
494                 if (!@ isset ($allowed_html[strtolower($element)][strtolower($arreach['name'])]))
495                         continue; # the attribute is not allowed
496
497                 $current = $allowed_html[strtolower($element)][strtolower($arreach['name'])];
498                 if ($current == '')
499                         continue; # the attribute is not allowed
500
501                 if (!is_array($current))
502                         $attr2 .= ' '.$arreach['whole'];
503                 # there are no checks
504
505                 else {
506                         # there are some checks
507                         $ok = true;
508                         foreach ($current as $currkey => $currval)
509                                 if (!wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval)) {
510                                         $ok = false;
511                                         break;
512                                 }
513
514                         if ($ok)
515                                 $attr2 .= ' '.$arreach['whole']; # it passed them
516                 } # if !is_array($current)
517         } # foreach
518
519         # Remove any "<" or ">" characters
520
521         $attr2 = preg_replace('/[<>]/', '', $attr2);
522
523         return "<$element$attr2$xhtml_slash>";
524 }
525
526 /**
527  * wp_kses_hair() - Builds an attribute list from string containing attributes.
528  *
529  * This function does a lot of work. It parses an attribute list into an array
530  * with attribute data, and tries to do the right thing even if it gets weird
531  * input. It will add quotes around attribute values that don't have any quotes
532  * or apostrophes around them, to make it easier to produce HTML code that will
533  * conform to W3C's HTML specification. It will also remove bad URL protocols
534  * from attribute values.
535  *
536  * @since 1.0.0
537  *
538  * @param string $attr Attribute list from HTML element to closing HTML element tag
539  * @param array $allowed_protocols Allowed protocols to keep
540  * @return array List of attributes after parsing
541  */
542 function wp_kses_hair($attr, $allowed_protocols) {
543         $attrarr = array ();
544         $mode = 0;
545         $attrname = '';
546
547         # Loop through the whole attribute list
548
549         while (strlen($attr) != 0) {
550                 $working = 0; # Was the last operation successful?
551
552                 switch ($mode) {
553                         case 0 : # attribute name, href for instance
554
555                                 if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) {
556                                         $attrname = $match[1];
557                                         $working = $mode = 1;
558                                         $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr);
559                                 }
560
561                                 break;
562
563                         case 1 : # equals sign or valueless ("selected")
564
565                                 if (preg_match('/^\s*=\s*/', $attr)) # equals sign
566                                         {
567                                         $working = 1;
568                                         $mode = 2;
569                                         $attr = preg_replace('/^\s*=\s*/', '', $attr);
570                                         break;
571                                 }
572
573                                 if (preg_match('/^\s+/', $attr)) # valueless
574                                         {
575                                         $working = 1;
576                                         $mode = 0;
577                                         $attrarr[] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
578                                         $attr = preg_replace('/^\s+/', '', $attr);
579                                 }
580
581                                 break;
582
583                         case 2 : # attribute value, a URL after href= for instance
584
585                                 if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match))
586                                         # "value"
587                                         {
588                                         $thisval = wp_kses_bad_protocol($match[1], $allowed_protocols);
589
590                                         $attrarr[] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
591                                         $working = 1;
592                                         $mode = 0;
593                                         $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
594                                         break;
595                                 }
596
597                                 if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match))
598                                         # 'value'
599                                         {
600                                         $thisval = wp_kses_bad_protocol($match[1], $allowed_protocols);
601
602                                         $attrarr[] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname='$thisval'", 'vless' => 'n');
603                                         $working = 1;
604                                         $mode = 0;
605                                         $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
606                                         break;
607                                 }
608
609                                 if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match))
610                                         # value
611                                         {
612                                         $thisval = wp_kses_bad_protocol($match[1], $allowed_protocols);
613
614                                         $attrarr[] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
615                                         # We add quotes to conform to W3C's HTML spec.
616                                         $working = 1;
617                                         $mode = 0;
618                                         $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
619                                 }
620
621                                 break;
622                 } # switch
623
624                 if ($working == 0) # not well formed, remove and try again
625                 {
626                         $attr = wp_kses_html_error($attr);
627                         $mode = 0;
628                 }
629         } # while
630
631         if ($mode == 1)
632                 # special case, for when the attribute list ends with a valueless
633                 # attribute like "selected"
634                 $attrarr[] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
635
636         return $attrarr;
637 }
638
639 /**
640  * wp_kses_check_attr_val() - Performs different checks for attribute values.
641  *
642  * The currently implemented checks are "maxlen", "minlen", "maxval", "minval"
643  * and "valueless" with even more checks to come soon.
644  *
645  * @since 1.0.0
646  *
647  * @param string $value Attribute value
648  * @param string $vless Whether the value is valueless or not. Use 'y' or 'n'
649  * @param string $checkname What $checkvalue is checking for.
650  * @param mixed $checkvalue What constraint the value should pass
651  * @return bool Whether check passes (true) or not (false)
652  */
653 function wp_kses_check_attr_val($value, $vless, $checkname, $checkvalue) {
654         $ok = true;
655
656         switch (strtolower($checkname)) {
657                 case 'maxlen' :
658                         # The maxlen check makes sure that the attribute value has a length not
659                         # greater than the given value. This can be used to avoid Buffer Overflows
660                         # in WWW clients and various Internet servers.
661
662                         if (strlen($value) > $checkvalue)
663                                 $ok = false;
664                         break;
665
666                 case 'minlen' :
667                         # The minlen check makes sure that the attribute value has a length not
668                         # smaller than the given value.
669
670                         if (strlen($value) < $checkvalue)
671                                 $ok = false;
672                         break;
673
674                 case 'maxval' :
675                         # The maxval check does two things: it checks that the attribute value is
676                         # an integer from 0 and up, without an excessive amount of zeroes or
677                         # whitespace (to avoid Buffer Overflows). It also checks that the attribute
678                         # value is not greater than the given value.
679                         # This check can be used to avoid Denial of Service attacks.
680
681                         if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
682                                 $ok = false;
683                         if ($value > $checkvalue)
684                                 $ok = false;
685                         break;
686
687                 case 'minval' :
688                         # The minval check checks that the attribute value is a positive integer,
689                         # and that it is not smaller than the given value.
690
691                         if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
692                                 $ok = false;
693                         if ($value < $checkvalue)
694                                 $ok = false;
695                         break;
696
697                 case 'valueless' :
698                         # The valueless check checks if the attribute has a value
699                         # (like <a href="blah">) or not (<option selected>). If the given value
700                         # is a "y" or a "Y", the attribute must not have a value.
701                         # If the given value is an "n" or an "N", the attribute must have one.
702
703                         if (strtolower($checkvalue) != $vless)
704                                 $ok = false;
705                         break;
706         } # switch
707
708         return $ok;
709 }
710
711 /**
712  * wp_kses_bad_protocol() - Sanitize string from bad protocols
713  *
714  * This function removes all non-allowed protocols from the beginning
715  * of $string. It ignores whitespace and the case of the letters, and
716  * it does understand HTML entities. It does its work in a while loop,
717  * so it won't be fooled by a string like "javascript:javascript:alert(57)".
718  *
719  * @since 1.0.0
720  *
721  * @param string $string Content to filter bad protocols from
722  * @param array $allowed_protocols Allowed protocols to keep
723  * @return string Filtered content
724  */
725 function wp_kses_bad_protocol($string, $allowed_protocols) {
726         $string = wp_kses_no_null($string);
727         $string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature"
728         $string2 = $string.'a';
729
730         while ($string != $string2) {
731                 $string2 = $string;
732                 $string = wp_kses_bad_protocol_once($string, $allowed_protocols);
733         } # while
734
735         return $string;
736 }
737
738 /**
739  * wp_kses_no_null() - Removes any NULL characters in $string.
740  *
741  * @since 1.0.0
742  *
743  * @param string $string
744  * @return string
745  */
746 function wp_kses_no_null($string) {
747         $string = preg_replace('/\0+/', '', $string);
748         $string = preg_replace('/(\\\\0)+/', '', $string);
749
750         return $string;
751 }
752
753 /**
754  * wp_kses_stripslashes() - Strips slashes from in front of quotes
755  *
756  * This function changes the character sequence  \"  to just  "
757  * It leaves all other slashes alone. It's really weird, but the
758  * quoting from preg_replace(//e) seems to require this.
759  *
760  * @since 1.0.0
761  *
762  * @param string $string String to strip slashes
763  * @return string Fixed strings with quoted slashes
764  */
765 function wp_kses_stripslashes($string) {
766         return preg_replace('%\\\\"%', '"', $string);
767 }
768
769 /**
770  * wp_kses_array_lc() - Goes through an array and changes the keys to all lower case.
771  *
772  * @since 1.0.0
773  *
774  * @param array $inarray Unfiltered array
775  * @return array Fixed array with all lowercase keys
776  */
777 function wp_kses_array_lc($inarray) {
778         $outarray = array ();
779
780         foreach ($inarray as $inkey => $inval) {
781                 $outkey = strtolower($inkey);
782                 $outarray[$outkey] = array ();
783
784                 foreach ($inval as $inkey2 => $inval2) {
785                         $outkey2 = strtolower($inkey2);
786                         $outarray[$outkey][$outkey2] = $inval2;
787                 } # foreach $inval
788         } # foreach $inarray
789
790         return $outarray;
791 }
792
793 /**
794  * wp_kses_js_entities() - Removes the HTML JavaScript entities found in early versions of Netscape 4.
795  *
796  * @since 1.0.0
797  *
798  * @param string $string
799  * @return string
800  */
801 function wp_kses_js_entities($string) {
802         return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
803 }
804
805 /**
806  * wp_kses_html_error() - Handles parsing errors in wp_kses_hair()
807  *
808  * The general plan is to remove everything to and including some
809  * whitespace, but it deals with quotes and apostrophes as well.
810  *
811  * @since 1.0.0
812  *
813  * @param string $string
814  * @return string
815  */
816 function wp_kses_html_error($string) {
817         return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string);
818 }
819
820 /**
821  * wp_kses_bad_protocol_once() - Sanitizes content from bad protocols and other characters
822  *
823  * This function searches for URL protocols at the beginning of $string,
824  * while handling whitespace and HTML entities.
825  *
826  * @since 1.0.0
827  *
828  * @param string $string Content to check for bad protocols
829  * @param string $allowed_protocols Allowed protocols
830  * @return string Sanitized content
831  */
832 function wp_kses_bad_protocol_once($string, $allowed_protocols) {
833         global $_kses_allowed_protocols;
834         $_kses_allowed_protocols = $allowed_protocols;
835
836         $string2 = preg_split('/:|&#58;|&#x3a;/i', $string, 2);
837         if ( isset($string2[1]) && !preg_match('%/\?%', $string2[0]) )
838                 $string = wp_kses_bad_protocol_once2($string2[0], $allowed_protocols) . trim($string2[1]);
839         else
840                 $string = preg_replace_callback('/^((&[^;]*;|[\sA-Za-z0-9])*)'.'(:|&#58;|&#[Xx]3[Aa];)\s*/', create_function('$matches', 'global $_kses_allowed_protocols; return wp_kses_bad_protocol_once2($matches[1], $_kses_allowed_protocols);'), $string);
841
842         return $string;
843 }
844
845 /**
846  * wp_kses_bad_protocol_once2() - Callback for wp_kses_bad_protocol_once() regular expression.
847  *
848  * This function processes URL protocols, checks to see if they're in the
849  * white-list or not, and returns different data depending on the answer.
850  *
851  * @since 1.0.0
852  *
853  * @param string $string Content to check for bad protocols
854  * @param array $allowed_protocols Allowed protocols
855  * @return string Sanitized content
856  */
857 function wp_kses_bad_protocol_once2($string, $allowed_protocols) {
858         $string2 = wp_kses_decode_entities($string);
859         $string2 = preg_replace('/\s/', '', $string2);
860         $string2 = wp_kses_no_null($string2);
861         $string2 = preg_replace('/\xad+/', '', $string2);
862         # deals with Opera "feature"
863         $string2 = strtolower($string2);
864
865         $allowed = false;
866         foreach ($allowed_protocols as $one_protocol)
867                 if (strtolower($one_protocol) == $string2) {
868                         $allowed = true;
869                         break;
870                 }
871
872         if ($allowed)
873                 return "$string2:";
874         else
875                 return '';
876 }
877
878 /**
879  * wp_kses_normalize_entities() - Converts and fixes HTML entities
880  *
881  * This function normalizes HTML entities. It will convert "AT&T" to the
882  * correct "AT&amp;T", "&#00058;" to "&#58;", "&#XYZZY;" to "&amp;#XYZZY;"
883  * and so on.
884  *
885  * @since 1.0.0
886  *
887  * @param string $string Content to normalize entities
888  * @return string Content with normalized entities
889  */
890 function wp_kses_normalize_entities($string) {
891         # Disarm all entities by converting & to &amp;
892
893         $string = str_replace('&', '&amp;', $string);
894
895         # Change back the allowed entities in our entity whitelist
896
897         $string = preg_replace('/&amp;([A-Za-z][A-Za-z0-9]{0,19});/', '&\\1;', $string);
898         $string = preg_replace_callback('/&amp;#0*([0-9]{1,5});/', create_function('$matches', 'return wp_kses_normalize_entities2($matches[1]);'), $string);
899         $string = preg_replace('/&amp;#([Xx])0*(([0-9A-Fa-f]{2}){1,2});/', '&#\\1\\2;', $string);
900
901         return $string;
902 }
903
904 /**
905  * wp_kses_normalize_entities2() - Callback for wp_kses_normalize_entities() regular expression
906  *
907  * This function helps wp_kses_normalize_entities() to only accept 16 bit
908  * values and nothing more for &#number; entities.
909  *
910  * @since 1.0.0
911  *
912  * @param int $i Number encoded entity
913  * @return string Correctly encoded entity
914  */
915 function wp_kses_normalize_entities2($i) {
916         return (($i > 65535) ? "&amp;#$i;" : "&#$i;");
917 }
918
919 /**
920  * wp_kses_decode_entities() - Convert all entities to their character counterparts.
921  *
922  * This function decodes numeric HTML entities (&#65; and &#x41;). It
923  * doesn't do anything with other entities like &auml;, but we don't need
924  * them in the URL protocol whitelisting system anyway.
925  *
926  * @since 1.0.0
927  *
928  * @param string $string Content to change entities
929  * @return string Content after decoded entities
930  */
931 function wp_kses_decode_entities($string) {
932         $string = preg_replace('/&#([0-9]+);/e', 'chr("\\1")', $string);
933         $string = preg_replace('/&#[Xx]([0-9A-Fa-f]+);/e', 'chr(hexdec("\\1"))', $string);
934
935         return $string;
936 }
937
938 /**
939  * wp_filter_kses() - Sanitize content with allowed HTML Kses rules
940  *
941  * @since 1.0.0
942  * @uses $allowedtags
943  *
944  * @param string $data Content to filter
945  * @return string Filtered content
946  */
947 function wp_filter_kses($data) {
948         global $allowedtags;
949         return addslashes( wp_kses(stripslashes( $data ), $allowedtags) );
950 }
951
952 /**
953  * wp_filter_post_kses() - Sanitize content for allowed HTML tags for post content
954  *
955  * Post content refers to the page contents of the 'post' type and not
956  * $_POST data from forms.
957  *
958  * @since 2.0.0
959  * @uses $allowedposttags
960  *
961  * @param string $data Post content to filter
962  * @return string Filtered post content with allowed HTML tags and attributes intact.
963  */
964 function wp_filter_post_kses($data) {
965         global $allowedposttags;
966         return addslashes ( wp_kses(stripslashes( $data ), $allowedposttags) );
967 }
968
969 /**
970  * wp_filter_nohtml_kses() - Strips all of the HTML in the content
971  *
972  * @since 2.1.0
973  *
974  * @param string $data Content to strip all HTML from
975  * @return string Filtered content without any HTML
976  */
977 function wp_filter_nohtml_kses($data) {
978         return addslashes ( wp_kses(stripslashes( $data ), array()) );
979 }
980
981 /**
982  * kses_init_filters() - Adds all Kses input form content filters
983  *
984  * All hooks have default priority. The wp_filter_kses() fucntion
985  * is added to the 'pre_comment_content' and 'title_save_pre'
986  * hooks. The wp_filter_post_kses() function is added to the
987  * 'content_save_pre', 'excerpt_save_pre', and 'content_filtered_save_pre'
988  * hooks.
989  *
990  * @since 2.0.0
991  * @uses add_filter() See description for what functions are added to what hooks.
992  */
993 function kses_init_filters() {
994         // Normal filtering.
995         add_filter('pre_comment_content', 'wp_filter_kses');
996         add_filter('title_save_pre', 'wp_filter_kses');
997
998         // Post filtering
999         add_filter('content_save_pre', 'wp_filter_post_kses');
1000         add_filter('excerpt_save_pre', 'wp_filter_post_kses');
1001         add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1002 }
1003
1004 /**
1005  * kses_remove_filters() - Removes all Kses input form content filters
1006  *
1007  * A quick procedural method to removing all of the filters
1008  * that kses uses for content in WordPress Loop.
1009  *
1010  * Does not remove the kses_init() function from 'init' hook
1011  * (priority is default). Also does not remove kses_init()
1012  * function from 'set_current_user' hook (priority is also
1013  * default).
1014  *
1015  * @since 2.0.6
1016  */
1017 function kses_remove_filters() {
1018         // Normal filtering.
1019         remove_filter('pre_comment_content', 'wp_filter_kses');
1020         remove_filter('title_save_pre', 'wp_filter_kses');
1021
1022         // Post filtering
1023         remove_filter('content_save_pre', 'wp_filter_post_kses');
1024         remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
1025         remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1026 }
1027
1028 /**
1029  * kses_init() - Sets up most of the Kses filters for input form content
1030  *
1031  * If you remove the kses_init() function from 'init' hook and
1032  * 'set_current_user' (priority is default), then none of the
1033  * Kses filter hooks will be added.
1034  *
1035  * First removes all of the Kses filters in case the current user
1036  * does not need to have Kses filter the content. If the user does
1037  * not have unfiltered html capability, then Kses filters are added.
1038  *
1039  * @uses kses_remove_filters() Removes the Kses filters
1040  * @uses kses_init_filters() Adds the Kses filters back if the user
1041  *              does not have unfiltered HTML capability.
1042  * @since 2.0.0
1043  */
1044 function kses_init() {
1045         kses_remove_filters();
1046
1047         if (current_user_can('unfiltered_html') == false)
1048                 kses_init_filters();
1049 }
1050
1051 add_action('init', 'kses_init');
1052 add_action('set_current_user', 'kses_init');
1053 ?>