]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/kses.php
WordPress 3.9.2-scripts
[autoinstalls/wordpress.git] / wp-includes / kses.php
1 <?php
2 /**
3  * kses 0.2.2 - HTML/XHTML filter that only allows some elements and attributes
4  * Copyright (C) 2002, 2003, 2005  Ulf Harnhammar
5  *
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.
10  *
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
14  * more details.
15  *
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  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  * http://www.gnu.org/licenses/gpl.html
20  *
21  * [kses strips evil scripts!]
22  *
23  * Added wp_ prefix to avoid conflicts with existing kses users
24  *
25  * @version 0.2.2
26  * @copyright (C) 2002, 2003, 2005
27  * @author Ulf Harnhammar <http://advogato.org/person/metaur/>
28  *
29  * @package External
30  * @subpackage KSES
31  *
32  */
33
34 /**
35  * You can override this in a plugin.
36  *
37  * The wp_kses_allowed_html filter is more powerful and supplies context.
38  * CUSTOM_TAGS is not recommended and should be considered deprecated.
39  *
40  * @see wp_kses_allowed_html()
41  *
42  * @since 1.2.0
43  */
44 if ( ! defined( 'CUSTOM_TAGS' ) )
45         define( 'CUSTOM_TAGS', false );
46
47 if ( ! CUSTOM_TAGS ) {
48         /**
49          * Kses global for default allowable HTML tags.
50          *
51          * Can be override by using CUSTOM_TAGS constant.
52          *
53          * @global array $allowedposttags
54          * @since 2.0.0
55          */
56         $allowedposttags = array(
57                 'address' => array(),
58                 'a' => array(
59                         'href' => true,
60                         'rel' => true,
61                         'rev' => true,
62                         'name' => true,
63                         'target' => true,
64                 ),
65                 'abbr' => array(),
66                 'acronym' => array(),
67                 'area' => array(
68                         'alt' => true,
69                         'coords' => true,
70                         'href' => true,
71                         'nohref' => true,
72                         'shape' => true,
73                         'target' => true,
74                 ),
75                 'article' => array(
76                         'align' => true,
77                         'dir' => true,
78                         'lang' => true,
79                         'xml:lang' => true,
80                 ),
81                 'aside' => array(
82                         'align' => true,
83                         'dir' => true,
84                         'lang' => true,
85                         'xml:lang' => true,
86                 ),
87                 'b' => array(),
88                 'big' => array(),
89                 'blockquote' => array(
90                         'cite' => true,
91                         'lang' => true,
92                         'xml:lang' => true,
93                 ),
94                 'br' => array(),
95                 'button' => array(
96                         'disabled' => true,
97                         'name' => true,
98                         'type' => true,
99                         'value' => true,
100                 ),
101                 'caption' => array(
102                         'align' => true,
103                 ),
104                 'cite' => array(
105                         'dir' => true,
106                         'lang' => true,
107                 ),
108                 'code' => array(),
109                 'col' => array(
110                         'align' => true,
111                         'char' => true,
112                         'charoff' => true,
113                         'span' => true,
114                         'dir' => true,
115                         'valign' => true,
116                         'width' => true,
117                 ),
118                 'del' => array(
119                         'datetime' => true,
120                 ),
121                 'dd' => array(),
122                 'dfn' => array(),
123                 'details' => array(
124                         'align' => true,
125                         'dir' => true,
126                         'lang' => true,
127                         'open' => true,
128                         'xml:lang' => true,
129                 ),
130                 'div' => array(
131                         'align' => true,
132                         'dir' => true,
133                         'lang' => true,
134                         'xml:lang' => true,
135                 ),
136                 'dl' => array(),
137                 'dt' => array(),
138                 'em' => array(),
139                 'fieldset' => array(),
140                 'figure' => array(
141                         'align' => true,
142                         'dir' => true,
143                         'lang' => true,
144                         'xml:lang' => true,
145                 ),
146                 'figcaption' => array(
147                         'align' => true,
148                         'dir' => true,
149                         'lang' => true,
150                         'xml:lang' => true,
151                 ),
152                 'font' => array(
153                         'color' => true,
154                         'face' => true,
155                         'size' => true,
156                 ),
157                 'footer' => array(
158                         'align' => true,
159                         'dir' => true,
160                         'lang' => true,
161                         'xml:lang' => true,
162                 ),
163                 'form' => array(
164                         'action' => true,
165                         'accept' => true,
166                         'accept-charset' => true,
167                         'enctype' => true,
168                         'method' => true,
169                         'name' => true,
170                         'target' => true,
171                 ),
172                 'h1' => array(
173                         'align' => true,
174                 ),
175                 'h2' => array(
176                         'align' => true,
177                 ),
178                 'h3' => array(
179                         'align' => true,
180                 ),
181                 'h4' => array(
182                         'align' => true,
183                 ),
184                 'h5' => array(
185                         'align' => true,
186                 ),
187                 'h6' => array(
188                         'align' => true,
189                 ),
190                 'header' => array(
191                         'align' => true,
192                         'dir' => true,
193                         'lang' => true,
194                         'xml:lang' => true,
195                 ),
196                 'hgroup' => array(
197                         'align' => true,
198                         'dir' => true,
199                         'lang' => true,
200                         'xml:lang' => true,
201                 ),
202                 'hr' => array(
203                         'align' => true,
204                         'noshade' => true,
205                         'size' => true,
206                         'width' => true,
207                 ),
208                 'i' => array(),
209                 'img' => array(
210                         'alt' => true,
211                         'align' => true,
212                         'border' => true,
213                         'height' => true,
214                         'hspace' => true,
215                         'longdesc' => true,
216                         'vspace' => true,
217                         'src' => true,
218                         'usemap' => true,
219                         'width' => true,
220                 ),
221                 'ins' => array(
222                         'datetime' => true,
223                         'cite' => true,
224                 ),
225                 'kbd' => array(),
226                 'label' => array(
227                         'for' => true,
228                 ),
229                 'legend' => array(
230                         'align' => true,
231                 ),
232                 'li' => array(
233                         'align' => true,
234                         'value' => true,
235                 ),
236                 'map' => array(
237                         'name' => true,
238                 ),
239                 'mark' => array(),
240                 'menu' => array(
241                         'type' => true,
242                 ),
243                 'nav' => array(
244                         'align' => true,
245                         'dir' => true,
246                         'lang' => true,
247                         'xml:lang' => true,
248                 ),
249                 'p' => array(
250                         'align' => true,
251                         'dir' => true,
252                         'lang' => true,
253                         'xml:lang' => true,
254                 ),
255                 'pre' => array(
256                         'width' => true,
257                 ),
258                 'q' => array(
259                         'cite' => true,
260                 ),
261                 's' => array(),
262                 'samp' => array(),
263                 'span' => array(
264                         'dir' => true,
265                         'align' => true,
266                         'lang' => true,
267                         'xml:lang' => true,
268                 ),
269                 'section' => array(
270                         'align' => true,
271                         'dir' => true,
272                         'lang' => true,
273                         'xml:lang' => true,
274                 ),
275                 'small' => array(),
276                 'strike' => array(),
277                 'strong' => array(),
278                 'sub' => array(),
279                 'summary' => array(
280                         'align' => true,
281                         'dir' => true,
282                         'lang' => true,
283                         'xml:lang' => true,
284                 ),
285                 'sup' => array(),
286                 'table' => array(
287                         'align' => true,
288                         'bgcolor' => true,
289                         'border' => true,
290                         'cellpadding' => true,
291                         'cellspacing' => true,
292                         'dir' => true,
293                         'rules' => true,
294                         'summary' => true,
295                         'width' => true,
296                 ),
297                 'tbody' => array(
298                         'align' => true,
299                         'char' => true,
300                         'charoff' => true,
301                         'valign' => true,
302                 ),
303                 'td' => array(
304                         'abbr' => true,
305                         'align' => true,
306                         'axis' => true,
307                         'bgcolor' => true,
308                         'char' => true,
309                         'charoff' => true,
310                         'colspan' => true,
311                         'dir' => true,
312                         'headers' => true,
313                         'height' => true,
314                         'nowrap' => true,
315                         'rowspan' => true,
316                         'scope' => true,
317                         'valign' => true,
318                         'width' => true,
319                 ),
320                 'textarea' => array(
321                         'cols' => true,
322                         'rows' => true,
323                         'disabled' => true,
324                         'name' => true,
325                         'readonly' => true,
326                 ),
327                 'tfoot' => array(
328                         'align' => true,
329                         'char' => true,
330                         'charoff' => true,
331                         'valign' => true,
332                 ),
333                 'th' => array(
334                         'abbr' => true,
335                         'align' => true,
336                         'axis' => true,
337                         'bgcolor' => true,
338                         'char' => true,
339                         'charoff' => true,
340                         'colspan' => true,
341                         'headers' => true,
342                         'height' => true,
343                         'nowrap' => true,
344                         'rowspan' => true,
345                         'scope' => true,
346                         'valign' => true,
347                         'width' => true,
348                 ),
349                 'thead' => array(
350                         'align' => true,
351                         'char' => true,
352                         'charoff' => true,
353                         'valign' => true,
354                 ),
355                 'title' => array(),
356                 'tr' => array(
357                         'align' => true,
358                         'bgcolor' => true,
359                         'char' => true,
360                         'charoff' => true,
361                         'valign' => true,
362                 ),
363                 'tt' => array(),
364                 'u' => array(),
365                 'ul' => array(
366                         'type' => true,
367                 ),
368                 'ol' => array(
369                         'start' => true,
370                         'type' => true,
371                 ),
372                 'var' => array(),
373         );
374
375         /**
376          * Kses allowed HTML elements.
377          *
378          * @global array $allowedtags
379          * @since 1.0.0
380          */
381         $allowedtags = array(
382                 'a' => array(
383                         'href' => true,
384                         'title' => true,
385                 ),
386                 'abbr' => array(
387                         'title' => true,
388                 ),
389                 'acronym' => array(
390                         'title' => true,
391                 ),
392                 'b' => array(),
393                 'blockquote' => array(
394                         'cite' => true,
395                 ),
396                 'cite' => array(),
397                 'code' => array(),
398                 'del' => array(
399                         'datetime' => true,
400                 ),
401                 'em' => array(),
402                 'i' => array(),
403                 'q' => array(
404                         'cite' => true,
405                 ),
406                 'strike' => array(),
407                 'strong' => array(),
408         );
409
410         $allowedentitynames = array(
411                 'nbsp',    'iexcl',  'cent',    'pound',  'curren', 'yen',
412                 'brvbar',  'sect',   'uml',     'copy',   'ordf',   'laquo',
413                 'not',     'shy',    'reg',     'macr',   'deg',    'plusmn',
414                 'acute',   'micro',  'para',    'middot', 'cedil',  'ordm',
415                 'raquo',   'iquest', 'Agrave',  'Aacute', 'Acirc',  'Atilde',
416                 'Auml',    'Aring',  'AElig',   'Ccedil', 'Egrave', 'Eacute',
417                 'Ecirc',   'Euml',   'Igrave',  'Iacute', 'Icirc',  'Iuml',
418                 'ETH',     'Ntilde', 'Ograve',  'Oacute', 'Ocirc',  'Otilde',
419                 'Ouml',    'times',  'Oslash',  'Ugrave', 'Uacute', 'Ucirc',
420                 'Uuml',    'Yacute', 'THORN',   'szlig',  'agrave', 'aacute',
421                 'acirc',   'atilde', 'auml',    'aring',  'aelig',  'ccedil',
422                 'egrave',  'eacute', 'ecirc',   'euml',   'igrave', 'iacute',
423                 'icirc',   'iuml',   'eth',     'ntilde', 'ograve', 'oacute',
424                 'ocirc',   'otilde', 'ouml',    'divide', 'oslash', 'ugrave',
425                 'uacute',  'ucirc',  'uuml',    'yacute', 'thorn',  'yuml',
426                 'quot',    'amp',    'lt',      'gt',     'apos',   'OElig',
427                 'oelig',   'Scaron', 'scaron',  'Yuml',   'circ',   'tilde',
428                 'ensp',    'emsp',   'thinsp',  'zwnj',   'zwj',    'lrm',
429                 'rlm',     'ndash',  'mdash',   'lsquo',  'rsquo',  'sbquo',
430                 'ldquo',   'rdquo',  'bdquo',   'dagger', 'Dagger', 'permil',
431                 'lsaquo',  'rsaquo', 'euro',    'fnof',   'Alpha',  'Beta',
432                 'Gamma',   'Delta',  'Epsilon', 'Zeta',   'Eta',    'Theta',
433                 'Iota',    'Kappa',  'Lambda',  'Mu',     'Nu',     'Xi',
434                 'Omicron', 'Pi',     'Rho',     'Sigma',  'Tau',    'Upsilon',
435                 'Phi',     'Chi',    'Psi',     'Omega',  'alpha',  'beta',
436                 'gamma',   'delta',  'epsilon', 'zeta',   'eta',    'theta',
437                 'iota',    'kappa',  'lambda',  'mu',     'nu',     'xi',
438                 'omicron', 'pi',     'rho',     'sigmaf', 'sigma',  'tau',
439                 'upsilon', 'phi',    'chi',     'psi',    'omega',  'thetasym',
440                 'upsih',   'piv',    'bull',    'hellip', 'prime',  'Prime',
441                 'oline',   'frasl',  'weierp',  'image',  'real',   'trade',
442                 'alefsym', 'larr',   'uarr',    'rarr',   'darr',   'harr',
443                 'crarr',   'lArr',   'uArr',    'rArr',   'dArr',   'hArr',
444                 'forall',  'part',   'exist',   'empty',  'nabla',  'isin',
445                 'notin',   'ni',     'prod',    'sum',    'minus',  'lowast',
446                 'radic',   'prop',   'infin',   'ang',    'and',    'or',
447                 'cap',     'cup',    'int',     'sim',    'cong',   'asymp',
448                 'ne',      'equiv',  'le',      'ge',     'sub',    'sup',
449                 'nsub',    'sube',   'supe',    'oplus',  'otimes', 'perp',
450                 'sdot',    'lceil',  'rceil',   'lfloor', 'rfloor', 'lang',
451                 'rang',    'loz',    'spades',  'clubs',  'hearts', 'diams',
452                 'sup1',    'sup2',   'sup3',    'frac14', 'frac12', 'frac34',
453                 'there4',
454         );
455
456         $allowedposttags = array_map( '_wp_add_global_attributes', $allowedposttags );
457 } else {
458         $allowedtags = wp_kses_array_lc( $allowedtags );
459         $allowedposttags = wp_kses_array_lc( $allowedposttags );
460 }
461
462 /**
463  * Filters content and keeps only allowable HTML elements.
464  *
465  * This function makes sure that only the allowed HTML element names, attribute
466  * names and attribute values plus only sane HTML entities will occur in
467  * $string. You have to remove any slashes from PHP's magic quotes before you
468  * call this function.
469  *
470  * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news',
471  * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This
472  * covers all common link protocols, except for 'javascript' which should not
473  * be allowed for untrusted users.
474  *
475  * @since 1.0.0
476  *
477  * @param string $string Content to filter through kses
478  * @param array $allowed_html List of allowed HTML elements
479  * @param array $allowed_protocols Optional. Allowed protocol in links.
480  * @return string Filtered content with only allowed HTML elements
481  */
482 function wp_kses( $string, $allowed_html, $allowed_protocols = array() ) {
483         if ( empty( $allowed_protocols ) )
484                 $allowed_protocols = wp_allowed_protocols();
485         $string = wp_kses_no_null($string);
486         $string = wp_kses_js_entities($string);
487         $string = wp_kses_normalize_entities($string);
488         $string = wp_kses_hook($string, $allowed_html, $allowed_protocols); // WP changed the order of these funcs and added args to wp_kses_hook
489         return wp_kses_split($string, $allowed_html, $allowed_protocols);
490 }
491
492 /**
493  * Return a list of allowed tags and attributes for a given context.
494  *
495  * @since 3.5.0
496  *
497  * @param string $context The context for which to retrieve tags. Allowed values are
498  *  post | strip | data | entities or the name of a field filter such as pre_user_description.
499  * @return array List of allowed tags and their allowed attributes.
500  */
501 function wp_kses_allowed_html( $context = '' ) {
502         global $allowedposttags, $allowedtags, $allowedentitynames;
503
504         if ( is_array( $context ) ) {
505                 /**
506                  * Filter HTML elements allowed for a given context.
507                  *
508                  * @since 3.5.0
509                  *
510                  * @param string $tags    Allowed tags, attributes, and/or entities.
511                  * @param string $context Context to judge allowed tags by. Allowed values are 'post',
512                  *                        'data', 'strip', 'entities', 'explicit', or the name of a filter.
513                  */
514                 return apply_filters( 'wp_kses_allowed_html', $context, 'explicit' );
515         }
516
517         switch ( $context ) {
518                 case 'post':
519                         /** This filter is documented in wp-includes/kses.php */
520                         return apply_filters( 'wp_kses_allowed_html', $allowedposttags, $context );
521                         break;
522                 case 'user_description':
523                 case 'pre_user_description':
524                         $tags = $allowedtags;
525                         $tags['a']['rel'] = true;
526                         /** This filter is documented in wp-includes/kses.php */
527                         return apply_filters( 'wp_kses_allowed_html', $tags, $context );
528                         break;
529                 case 'strip':
530                         /** This filter is documented in wp-includes/kses.php */
531                         return apply_filters( 'wp_kses_allowed_html', array(), $context );
532                         break;
533                 case 'entities':
534                         /** This filter is documented in wp-includes/kses.php */
535                         return apply_filters( 'wp_kses_allowed_html', $allowedentitynames, $context);
536                         break;
537                 case 'data':
538                 default:
539                         /** This filter is documented in wp-includes/kses.php */
540                         return apply_filters( 'wp_kses_allowed_html', $allowedtags, $context );
541         }
542 }
543
544 /**
545  * You add any kses hooks here.
546  *
547  * There is currently only one kses WordPress hook and it is called here. All
548  * parameters are passed to the hooks and expected to receive a string.
549  *
550  * @since 1.0.0
551  *
552  * @param string $string Content to filter through kses
553  * @param array $allowed_html List of allowed HTML elements
554  * @param array $allowed_protocols Allowed protocol in links
555  * @return string Filtered content through 'pre_kses' hook
556  */
557 function wp_kses_hook( $string, $allowed_html, $allowed_protocols ) {
558         /**
559          * Filter content to be run through kses.
560          *
561          * @since 2.3.0
562          *
563          * @param string $string            Content to run through kses.
564          * @param array  $allowed_html      Allowed HTML elements.
565          * @param array  $allowed_protocols Allowed protocol in links.
566          */
567         $string = apply_filters( 'pre_kses', $string, $allowed_html, $allowed_protocols );
568         return $string;
569 }
570
571 /**
572  * This function returns kses' version number.
573  *
574  * @since 1.0.0
575  *
576  * @return string KSES Version Number
577  */
578 function wp_kses_version() {
579         return '0.2.2';
580 }
581
582 /**
583  * Searches for HTML tags, no matter how malformed.
584  *
585  * It also matches stray ">" characters.
586  *
587  * @since 1.0.0
588  *
589  * @param string $string Content to filter
590  * @param array $allowed_html Allowed HTML elements
591  * @param array $allowed_protocols Allowed protocols to keep
592  * @return string Content with fixed HTML tags
593  */
594 function wp_kses_split( $string, $allowed_html, $allowed_protocols ) {
595         global $pass_allowed_html, $pass_allowed_protocols;
596         $pass_allowed_html = $allowed_html;
597         $pass_allowed_protocols = $allowed_protocols;
598         return preg_replace_callback( '%(<!--.*?(-->|$))|(<[^>]*(>|$)|>)%', '_wp_kses_split_callback', $string );
599 }
600
601 /**
602  * Callback for wp_kses_split.
603  *
604  * @since 3.1.0
605  * @access private
606  */
607 function _wp_kses_split_callback( $match ) {
608         global $pass_allowed_html, $pass_allowed_protocols;
609         return wp_kses_split2( $match[0], $pass_allowed_html, $pass_allowed_protocols );
610 }
611
612 /**
613  * Callback for wp_kses_split for fixing malformed HTML tags.
614  *
615  * This function does a lot of work. It rejects some very malformed things like
616  * <:::>. It returns an empty string, if the element isn't allowed (look ma, no
617  * strip_tags()!). Otherwise it splits the tag into an element and an attribute
618  * list.
619  *
620  * After the tag is split into an element and an attribute list, it is run
621  * through another filter which will remove illegal attributes and once that is
622  * completed, will be returned.
623  *
624  * @access private
625  * @since 1.0.0
626  * @uses wp_kses_attr()
627  *
628  * @param string $string Content to filter
629  * @param array $allowed_html Allowed HTML elements
630  * @param array $allowed_protocols Allowed protocols to keep
631  * @return string Fixed HTML element
632  */
633 function wp_kses_split2($string, $allowed_html, $allowed_protocols) {
634         $string = wp_kses_stripslashes($string);
635
636         if (substr($string, 0, 1) != '<')
637                 return '&gt;';
638         # It matched a ">" character
639
640         if ( '<!--' == substr( $string, 0, 4 ) ) {
641                 $string = str_replace( array('<!--', '-->'), '', $string );
642                 while ( $string != ($newstring = wp_kses($string, $allowed_html, $allowed_protocols)) )
643                         $string = $newstring;
644                 if ( $string == '' )
645                         return '';
646                 // prevent multiple dashes in comments
647                 $string = preg_replace('/--+/', '-', $string);
648                 // prevent three dashes closing a comment
649                 $string = preg_replace('/-$/', '', $string);
650                 return "<!--{$string}-->";
651         }
652         # Allow HTML comments
653
654         if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?$%', $string, $matches))
655                 return '';
656         # It's seriously malformed
657
658         $slash = trim($matches[1]);
659         $elem = $matches[2];
660         $attrlist = $matches[3];
661
662         if ( ! is_array( $allowed_html ) )
663                 $allowed_html = wp_kses_allowed_html( $allowed_html );
664
665         if ( ! isset($allowed_html[strtolower($elem)]) )
666                 return '';
667         # They are using a not allowed HTML element
668
669         if ($slash != '')
670                 return "</$elem>";
671         # No attributes are allowed for closing elements
672
673         return wp_kses_attr( $elem, $attrlist, $allowed_html, $allowed_protocols );
674 }
675
676 /**
677  * Removes all attributes, if none are allowed for this element.
678  *
679  * If some are allowed it calls wp_kses_hair() to split them further, and then
680  * it builds up new HTML code from the data that kses_hair() returns. It also
681  * removes "<" and ">" characters, if there are any left. One more thing it does
682  * is to check if the tag has a closing XHTML slash, and if it does, it puts one
683  * in the returned code as well.
684  *
685  * @since 1.0.0
686  *
687  * @param string $element HTML element/tag
688  * @param string $attr HTML attributes from HTML element to closing HTML element tag
689  * @param array $allowed_html Allowed HTML elements
690  * @param array $allowed_protocols Allowed protocols to keep
691  * @return string Sanitized HTML element
692  */
693 function wp_kses_attr($element, $attr, $allowed_html, $allowed_protocols) {
694         # Is there a closing XHTML slash at the end of the attributes?
695
696         if ( ! is_array( $allowed_html ) )
697                 $allowed_html = wp_kses_allowed_html( $allowed_html );
698
699         $xhtml_slash = '';
700         if (preg_match('%\s*/\s*$%', $attr))
701                 $xhtml_slash = ' /';
702
703         # Are any attributes allowed at all for this element?
704         if ( ! isset($allowed_html[strtolower($element)]) || count($allowed_html[strtolower($element)]) == 0 )
705                 return "<$element$xhtml_slash>";
706
707         # Split it
708         $attrarr = wp_kses_hair($attr, $allowed_protocols);
709
710         # Go through $attrarr, and save the allowed attributes for this element
711         # in $attr2
712         $attr2 = '';
713
714         $allowed_attr = $allowed_html[strtolower($element)];
715         foreach ($attrarr as $arreach) {
716                 if ( ! isset( $allowed_attr[strtolower($arreach['name'])] ) )
717                         continue; # the attribute is not allowed
718
719                 $current = $allowed_attr[strtolower($arreach['name'])];
720                 if ( $current == '' )
721                         continue; # the attribute is not allowed
722
723                 if ( strtolower( $arreach['name'] ) == 'style' ) {
724                         $orig_value = $arreach['value'];
725                         $value = safecss_filter_attr( $orig_value );
726
727                         if ( empty( $value ) )
728                                 continue;
729
730                         $arreach['value'] = $value;
731                         $arreach['whole'] = str_replace( $orig_value, $value, $arreach['whole'] );
732                 }
733
734                 if ( ! is_array($current) ) {
735                         $attr2 .= ' '.$arreach['whole'];
736                 # there are no checks
737
738                 } else {
739                         # there are some checks
740                         $ok = true;
741                         foreach ($current as $currkey => $currval) {
742                                 if ( ! wp_kses_check_attr_val($arreach['value'], $arreach['vless'], $currkey, $currval) ) {
743                                         $ok = false;
744                                         break;
745                                 }
746                         }
747
748                         if ( $ok )
749                                 $attr2 .= ' '.$arreach['whole']; # it passed them
750                 } # if !is_array($current)
751         } # foreach
752
753         # Remove any "<" or ">" characters
754         $attr2 = preg_replace('/[<>]/', '', $attr2);
755
756         return "<$element$attr2$xhtml_slash>";
757 }
758
759 /**
760  * Builds an attribute list from string containing attributes.
761  *
762  * This function does a lot of work. It parses an attribute list into an array
763  * with attribute data, and tries to do the right thing even if it gets weird
764  * input. It will add quotes around attribute values that don't have any quotes
765  * or apostrophes around them, to make it easier to produce HTML code that will
766  * conform to W3C's HTML specification. It will also remove bad URL protocols
767  * from attribute values. It also reduces duplicate attributes by using the
768  * attribute defined first (foo='bar' foo='baz' will result in foo='bar').
769  *
770  * @since 1.0.0
771  *
772  * @param string $attr Attribute list from HTML element to closing HTML element tag
773  * @param array $allowed_protocols Allowed protocols to keep
774  * @return array List of attributes after parsing
775  */
776 function wp_kses_hair($attr, $allowed_protocols) {
777         $attrarr = array();
778         $mode = 0;
779         $attrname = '';
780         $uris = array('xmlns', 'profile', 'href', 'src', 'cite', 'classid', 'codebase', 'data', 'usemap', 'longdesc', 'action');
781
782         # Loop through the whole attribute list
783
784         while (strlen($attr) != 0) {
785                 $working = 0; # Was the last operation successful?
786
787                 switch ($mode) {
788                         case 0 : # attribute name, href for instance
789
790                                 if ( preg_match('/^([-a-zA-Z:]+)/', $attr, $match ) ) {
791                                         $attrname = $match[1];
792                                         $working = $mode = 1;
793                                         $attr = preg_replace( '/^[-a-zA-Z:]+/', '', $attr );
794                                 }
795
796                                 break;
797
798                         case 1 : # equals sign or valueless ("selected")
799
800                                 if (preg_match('/^\s*=\s*/', $attr)) # equals sign
801                                         {
802                                         $working = 1;
803                                         $mode = 2;
804                                         $attr = preg_replace('/^\s*=\s*/', '', $attr);
805                                         break;
806                                 }
807
808                                 if (preg_match('/^\s+/', $attr)) # valueless
809                                         {
810                                         $working = 1;
811                                         $mode = 0;
812                                         if(false === array_key_exists($attrname, $attrarr)) {
813                                                 $attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
814                                         }
815                                         $attr = preg_replace('/^\s+/', '', $attr);
816                                 }
817
818                                 break;
819
820                         case 2 : # attribute value, a URL after href= for instance
821
822                                 if (preg_match('%^"([^"]*)"(\s+|/?$)%', $attr, $match))
823                                         # "value"
824                                         {
825                                         $thisval = $match[1];
826                                         if ( in_array(strtolower($attrname), $uris) )
827                                                 $thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
828
829                                         if(false === array_key_exists($attrname, $attrarr)) {
830                                                 $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
831                                         }
832                                         $working = 1;
833                                         $mode = 0;
834                                         $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
835                                         break;
836                                 }
837
838                                 if (preg_match("%^'([^']*)'(\s+|/?$)%", $attr, $match))
839                                         # 'value'
840                                         {
841                                         $thisval = $match[1];
842                                         if ( in_array(strtolower($attrname), $uris) )
843                                                 $thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
844
845                                         if(false === array_key_exists($attrname, $attrarr)) {
846                                                 $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname='$thisval'", 'vless' => 'n');
847                                         }
848                                         $working = 1;
849                                         $mode = 0;
850                                         $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
851                                         break;
852                                 }
853
854                                 if (preg_match("%^([^\s\"']+)(\s+|/?$)%", $attr, $match))
855                                         # value
856                                         {
857                                         $thisval = $match[1];
858                                         if ( in_array(strtolower($attrname), $uris) )
859                                                 $thisval = wp_kses_bad_protocol($thisval, $allowed_protocols);
860
861                                         if(false === array_key_exists($attrname, $attrarr)) {
862                                                 $attrarr[$attrname] = array ('name' => $attrname, 'value' => $thisval, 'whole' => "$attrname=\"$thisval\"", 'vless' => 'n');
863                                         }
864                                         # We add quotes to conform to W3C's HTML spec.
865                                         $working = 1;
866                                         $mode = 0;
867                                         $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
868                                 }
869
870                                 break;
871                 } # switch
872
873                 if ($working == 0) # not well formed, remove and try again
874                 {
875                         $attr = wp_kses_html_error($attr);
876                         $mode = 0;
877                 }
878         } # while
879
880         if ($mode == 1 && false === array_key_exists($attrname, $attrarr))
881                 # special case, for when the attribute list ends with a valueless
882                 # attribute like "selected"
883                 $attrarr[$attrname] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');
884
885         return $attrarr;
886 }
887
888 /**
889  * Performs different checks for attribute values.
890  *
891  * The currently implemented checks are "maxlen", "minlen", "maxval", "minval"
892  * and "valueless".
893  *
894  * @since 1.0.0
895  *
896  * @param string $value Attribute value
897  * @param string $vless Whether the value is valueless. Use 'y' or 'n'
898  * @param string $checkname What $checkvalue is checking for.
899  * @param mixed $checkvalue What constraint the value should pass
900  * @return bool Whether check passes
901  */
902 function wp_kses_check_attr_val($value, $vless, $checkname, $checkvalue) {
903         $ok = true;
904
905         switch (strtolower($checkname)) {
906                 case 'maxlen' :
907                         # The maxlen check makes sure that the attribute value has a length not
908                         # greater than the given value. This can be used to avoid Buffer Overflows
909                         # in WWW clients and various Internet servers.
910
911                         if (strlen($value) > $checkvalue)
912                                 $ok = false;
913                         break;
914
915                 case 'minlen' :
916                         # The minlen check makes sure that the attribute value has a length not
917                         # smaller than the given value.
918
919                         if (strlen($value) < $checkvalue)
920                                 $ok = false;
921                         break;
922
923                 case 'maxval' :
924                         # The maxval check does two things: it checks that the attribute value is
925                         # an integer from 0 and up, without an excessive amount of zeroes or
926                         # whitespace (to avoid Buffer Overflows). It also checks that the attribute
927                         # value is not greater than the given value.
928                         # This check can be used to avoid Denial of Service attacks.
929
930                         if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
931                                 $ok = false;
932                         if ($value > $checkvalue)
933                                 $ok = false;
934                         break;
935
936                 case 'minval' :
937                         # The minval check makes sure that the attribute value is a positive integer,
938                         # and that it is not smaller than the given value.
939
940                         if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))
941                                 $ok = false;
942                         if ($value < $checkvalue)
943                                 $ok = false;
944                         break;
945
946                 case 'valueless' :
947                         # The valueless check makes sure if the attribute has a value
948                         # (like <a href="blah">) or not (<option selected>). If the given value
949                         # is a "y" or a "Y", the attribute must not have a value.
950                         # If the given value is an "n" or an "N", the attribute must have one.
951
952                         if (strtolower($checkvalue) != $vless)
953                                 $ok = false;
954                         break;
955         } # switch
956
957         return $ok;
958 }
959
960 /**
961  * Sanitize string from bad protocols.
962  *
963  * This function removes all non-allowed protocols from the beginning of
964  * $string. It ignores whitespace and the case of the letters, and it does
965  * understand HTML entities. It does its work in a while loop, so it won't be
966  * fooled by a string like "javascript:javascript:alert(57)".
967  *
968  * @since 1.0.0
969  *
970  * @param string $string Content to filter bad protocols from
971  * @param array $allowed_protocols Allowed protocols to keep
972  * @return string Filtered content
973  */
974 function wp_kses_bad_protocol($string, $allowed_protocols) {
975         $string = wp_kses_no_null($string);
976         $iterations = 0;
977
978         do {
979                 $original_string = $string;
980                 $string = wp_kses_bad_protocol_once($string, $allowed_protocols);
981         } while ( $original_string != $string && ++$iterations < 6 );
982
983         if ( $original_string != $string )
984                 return '';
985
986         return $string;
987 }
988
989 /**
990  * Removes any null characters in $string.
991  *
992  * @since 1.0.0
993  *
994  * @param string $string
995  * @return string
996  */
997 function wp_kses_no_null($string) {
998         $string = preg_replace('/\0+/', '', $string);
999         $string = preg_replace('/(\\\\0)+/', '', $string);
1000
1001         return $string;
1002 }
1003
1004 /**
1005  * Strips slashes from in front of quotes.
1006  *
1007  * This function changes the character sequence \" to just ". It leaves all
1008  * other slashes alone. It's really weird, but the quoting from
1009  * preg_replace(//e) seems to require this.
1010  *
1011  * @since 1.0.0
1012  *
1013  * @param string $string String to strip slashes
1014  * @return string Fixed string with quoted slashes
1015  */
1016 function wp_kses_stripslashes($string) {
1017         return preg_replace('%\\\\"%', '"', $string);
1018 }
1019
1020 /**
1021  * Goes through an array and changes the keys to all lower case.
1022  *
1023  * @since 1.0.0
1024  *
1025  * @param array $inarray Unfiltered array
1026  * @return array Fixed array with all lowercase keys
1027  */
1028 function wp_kses_array_lc($inarray) {
1029         $outarray = array ();
1030
1031         foreach ( (array) $inarray as $inkey => $inval) {
1032                 $outkey = strtolower($inkey);
1033                 $outarray[$outkey] = array ();
1034
1035                 foreach ( (array) $inval as $inkey2 => $inval2) {
1036                         $outkey2 = strtolower($inkey2);
1037                         $outarray[$outkey][$outkey2] = $inval2;
1038                 } # foreach $inval
1039         } # foreach $inarray
1040
1041         return $outarray;
1042 }
1043
1044 /**
1045  * Removes the HTML JavaScript entities found in early versions of Netscape 4.
1046  *
1047  * @since 1.0.0
1048  *
1049  * @param string $string
1050  * @return string
1051  */
1052 function wp_kses_js_entities($string) {
1053         return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
1054 }
1055
1056 /**
1057  * Handles parsing errors in wp_kses_hair().
1058  *
1059  * The general plan is to remove everything to and including some whitespace,
1060  * but it deals with quotes and apostrophes as well.
1061  *
1062  * @since 1.0.0
1063  *
1064  * @param string $string
1065  * @return string
1066  */
1067 function wp_kses_html_error($string) {
1068         return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string);
1069 }
1070
1071 /**
1072  * Sanitizes content from bad protocols and other characters.
1073  *
1074  * This function searches for URL protocols at the beginning of $string, while
1075  * handling whitespace and HTML entities.
1076  *
1077  * @since 1.0.0
1078  *
1079  * @param string $string Content to check for bad protocols
1080  * @param string $allowed_protocols Allowed protocols
1081  * @return string Sanitized content
1082  */
1083 function wp_kses_bad_protocol_once($string, $allowed_protocols, $count = 1 ) {
1084         $string2 = preg_split( '/:|&#0*58;|&#x0*3a;/i', $string, 2 );
1085         if ( isset($string2[1]) && ! preg_match('%/\?%', $string2[0]) ) {
1086                 $string = trim( $string2[1] );
1087                 $protocol = wp_kses_bad_protocol_once2( $string2[0], $allowed_protocols );
1088                 if ( 'feed:' == $protocol ) {
1089                         if ( $count > 2 )
1090                                 return '';
1091                         $string = wp_kses_bad_protocol_once( $string, $allowed_protocols, ++$count );
1092                         if ( empty( $string ) )
1093                                 return $string;
1094                 }
1095                 $string = $protocol . $string;
1096         }
1097
1098         return $string;
1099 }
1100
1101 /**
1102  * Callback for wp_kses_bad_protocol_once() regular expression.
1103  *
1104  * This function processes URL protocols, checks to see if they're in the
1105  * whitelist or not, and returns different data depending on the answer.
1106  *
1107  * @access private
1108  * @since 1.0.0
1109  *
1110  * @param string $string URI scheme to check against the whitelist
1111  * @param string $allowed_protocols Allowed protocols
1112  * @return string Sanitized content
1113  */
1114 function wp_kses_bad_protocol_once2( $string, $allowed_protocols ) {
1115         $string2 = wp_kses_decode_entities($string);
1116         $string2 = preg_replace('/\s/', '', $string2);
1117         $string2 = wp_kses_no_null($string2);
1118         $string2 = strtolower($string2);
1119
1120         $allowed = false;
1121         foreach ( (array) $allowed_protocols as $one_protocol )
1122                 if ( strtolower($one_protocol) == $string2 ) {
1123                         $allowed = true;
1124                         break;
1125                 }
1126
1127         if ($allowed)
1128                 return "$string2:";
1129         else
1130                 return '';
1131 }
1132
1133 /**
1134  * Converts and fixes HTML entities.
1135  *
1136  * This function normalizes HTML entities. It will convert "AT&T" to the correct
1137  * "AT&amp;T", "&#00058;" to "&#58;", "&#XYZZY;" to "&amp;#XYZZY;" and so on.
1138  *
1139  * @since 1.0.0
1140  *
1141  * @param string $string Content to normalize entities
1142  * @return string Content with normalized entities
1143  */
1144 function wp_kses_normalize_entities($string) {
1145         # Disarm all entities by converting & to &amp;
1146
1147         $string = str_replace('&', '&amp;', $string);
1148
1149         # Change back the allowed entities in our entity whitelist
1150
1151         $string = preg_replace_callback('/&amp;([A-Za-z]{2,8}[0-9]{0,2});/', 'wp_kses_named_entities', $string);
1152         $string = preg_replace_callback('/&amp;#(0*[0-9]{1,7});/', 'wp_kses_normalize_entities2', $string);
1153         $string = preg_replace_callback('/&amp;#[Xx](0*[0-9A-Fa-f]{1,6});/', 'wp_kses_normalize_entities3', $string);
1154
1155         return $string;
1156 }
1157
1158 /**
1159  * Callback for wp_kses_normalize_entities() regular expression.
1160  *
1161  * This function only accepts valid named entity references, which are finite,
1162  * case-sensitive, and highly scrutinized by HTML and XML validators.
1163  *
1164  * @since 3.0.0
1165  *
1166  * @param array $matches preg_replace_callback() matches array
1167  * @return string Correctly encoded entity
1168  */
1169 function wp_kses_named_entities($matches) {
1170         global $allowedentitynames;
1171
1172         if ( empty($matches[1]) )
1173                 return '';
1174
1175         $i = $matches[1];
1176         return ( ( ! in_array($i, $allowedentitynames) ) ? "&amp;$i;" : "&$i;" );
1177 }
1178
1179 /**
1180  * Callback for wp_kses_normalize_entities() regular expression.
1181  *
1182  * This function helps wp_kses_normalize_entities() to only accept 16-bit values
1183  * and nothing more for &#number; entities.
1184  *
1185  * @access private
1186  * @since 1.0.0
1187  *
1188  * @param array $matches preg_replace_callback() matches array
1189  * @return string Correctly encoded entity
1190  */
1191 function wp_kses_normalize_entities2($matches) {
1192         if ( empty($matches[1]) )
1193                 return '';
1194
1195         $i = $matches[1];
1196         if (valid_unicode($i)) {
1197                 $i = str_pad(ltrim($i,'0'), 3, '0', STR_PAD_LEFT);
1198                 $i = "&#$i;";
1199         } else {
1200                 $i = "&amp;#$i;";
1201         }
1202
1203         return $i;
1204 }
1205
1206 /**
1207  * Callback for wp_kses_normalize_entities() for regular expression.
1208  *
1209  * This function helps wp_kses_normalize_entities() to only accept valid Unicode
1210  * numeric entities in hex form.
1211  *
1212  * @access private
1213  *
1214  * @param array $matches preg_replace_callback() matches array
1215  * @return string Correctly encoded entity
1216  */
1217 function wp_kses_normalize_entities3($matches) {
1218         if ( empty($matches[1]) )
1219                 return '';
1220
1221         $hexchars = $matches[1];
1222         return ( ( ! valid_unicode(hexdec($hexchars)) ) ? "&amp;#x$hexchars;" : '&#x'.ltrim($hexchars,'0').';' );
1223 }
1224
1225 /**
1226  * Helper function to determine if a Unicode value is valid.
1227  *
1228  * @param int $i Unicode value
1229  * @return bool True if the value was a valid Unicode number
1230  */
1231 function valid_unicode($i) {
1232         return ( $i == 0x9 || $i == 0xa || $i == 0xd ||
1233                         ($i >= 0x20 && $i <= 0xd7ff) ||
1234                         ($i >= 0xe000 && $i <= 0xfffd) ||
1235                         ($i >= 0x10000 && $i <= 0x10ffff) );
1236 }
1237
1238 /**
1239  * Convert all entities to their character counterparts.
1240  *
1241  * This function decodes numeric HTML entities (&#65; and &#x41;). It doesn't do
1242  * anything with other entities like &auml;, but we don't need them in the URL
1243  * protocol whitelisting system anyway.
1244  *
1245  * @since 1.0.0
1246  *
1247  * @param string $string Content to change entities
1248  * @return string Content after decoded entities
1249  */
1250 function wp_kses_decode_entities($string) {
1251         $string = preg_replace_callback('/&#([0-9]+);/', '_wp_kses_decode_entities_chr', $string);
1252         $string = preg_replace_callback('/&#[Xx]([0-9A-Fa-f]+);/', '_wp_kses_decode_entities_chr_hexdec', $string);
1253
1254         return $string;
1255 }
1256
1257 /**
1258  * Regex callback for wp_kses_decode_entities()
1259  *
1260  * @param array $match preg match
1261  * @return string
1262  */
1263 function _wp_kses_decode_entities_chr( $match ) {
1264         return chr( $match[1] );
1265 }
1266
1267 /**
1268  * Regex callback for wp_kses_decode_entities()
1269  *
1270  * @param array $match preg match
1271  * @return string
1272  */
1273 function _wp_kses_decode_entities_chr_hexdec( $match ) {
1274         return chr( hexdec( $match[1] ) );
1275 }
1276
1277 /**
1278  * Sanitize content with allowed HTML Kses rules.
1279  *
1280  * @since 1.0.0
1281  * @uses $allowedtags
1282  *
1283  * @param string $data Content to filter, expected to be escaped with slashes
1284  * @return string Filtered content
1285  */
1286 function wp_filter_kses( $data ) {
1287         return addslashes( wp_kses( stripslashes( $data ), current_filter() ) );
1288 }
1289
1290 /**
1291  * Sanitize content with allowed HTML Kses rules.
1292  *
1293  * @since 2.9.0
1294  * @uses $allowedtags
1295  *
1296  * @param string $data Content to filter, expected to not be escaped
1297  * @return string Filtered content
1298  */
1299 function wp_kses_data( $data ) {
1300         return wp_kses( $data , current_filter() );
1301 }
1302
1303 /**
1304  * Sanitize content for allowed HTML tags for post content.
1305  *
1306  * Post content refers to the page contents of the 'post' type and not $_POST
1307  * data from forms.
1308  *
1309  * @since 2.0.0
1310  *
1311  * @param string $data Post content to filter, expected to be escaped with slashes
1312  * @return string Filtered post content with allowed HTML tags and attributes intact.
1313  */
1314 function wp_filter_post_kses($data) {
1315         return addslashes ( wp_kses( stripslashes( $data ), 'post' ) );
1316 }
1317
1318 /**
1319  * Sanitize content for allowed HTML tags for post content.
1320  *
1321  * Post content refers to the page contents of the 'post' type and not $_POST
1322  * data from forms.
1323  *
1324  * @since 2.9.0
1325  *
1326  * @param string $data Post content to filter
1327  * @return string Filtered post content with allowed HTML tags and attributes intact.
1328  */
1329 function wp_kses_post($data) {
1330         return wp_kses( $data , 'post' );
1331 }
1332
1333 /**
1334  * Strips all of the HTML in the content.
1335  *
1336  * @since 2.1.0
1337  *
1338  * @param string $data Content to strip all HTML from
1339  * @return string Filtered content without any HTML
1340  */
1341 function wp_filter_nohtml_kses( $data ) {
1342         return addslashes ( wp_kses( stripslashes( $data ), 'strip' ) );
1343 }
1344
1345 /**
1346  * Adds all Kses input form content filters.
1347  *
1348  * All hooks have default priority. The wp_filter_kses() function is added to
1349  * the 'pre_comment_content' and 'title_save_pre' hooks.
1350  *
1351  * The wp_filter_post_kses() function is added to the 'content_save_pre',
1352  * 'excerpt_save_pre', and 'content_filtered_save_pre' hooks.
1353  *
1354  * @since 2.0.0
1355  * @uses add_filter() See description for what functions are added to what hooks.
1356  */
1357 function kses_init_filters() {
1358         // Normal filtering
1359         add_filter('title_save_pre', 'wp_filter_kses');
1360
1361         // Comment filtering
1362         if ( current_user_can( 'unfiltered_html' ) )
1363                 add_filter( 'pre_comment_content', 'wp_filter_post_kses' );
1364         else
1365                 add_filter( 'pre_comment_content', 'wp_filter_kses' );
1366
1367         // Post filtering
1368         add_filter('content_save_pre', 'wp_filter_post_kses');
1369         add_filter('excerpt_save_pre', 'wp_filter_post_kses');
1370         add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1371 }
1372
1373 /**
1374  * Removes all Kses input form content filters.
1375  *
1376  * A quick procedural method to removing all of the filters that kses uses for
1377  * content in WordPress Loop.
1378  *
1379  * Does not remove the kses_init() function from 'init' hook (priority is
1380  * default). Also does not remove kses_init() function from 'set_current_user'
1381  * hook (priority is also default).
1382  *
1383  * @since 2.0.6
1384  */
1385 function kses_remove_filters() {
1386         // Normal filtering
1387         remove_filter('title_save_pre', 'wp_filter_kses');
1388
1389         // Comment filtering
1390         remove_filter( 'pre_comment_content', 'wp_filter_post_kses' );
1391         remove_filter( 'pre_comment_content', 'wp_filter_kses' );
1392
1393         // Post filtering
1394         remove_filter('content_save_pre', 'wp_filter_post_kses');
1395         remove_filter('excerpt_save_pre', 'wp_filter_post_kses');
1396         remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
1397 }
1398
1399 /**
1400  * Sets up most of the Kses filters for input form content.
1401  *
1402  * If you remove the kses_init() function from 'init' hook and
1403  * 'set_current_user' (priority is default), then none of the Kses filter hooks
1404  * will be added.
1405  *
1406  * First removes all of the Kses filters in case the current user does not need
1407  * to have Kses filter the content. If the user does not have unfiltered_html
1408  * capability, then Kses filters are added.
1409  *
1410  * @uses kses_remove_filters() Removes the Kses filters
1411  * @uses kses_init_filters() Adds the Kses filters back if the user
1412  *              does not have unfiltered HTML capability.
1413  * @since 2.0.0
1414  */
1415 function kses_init() {
1416         kses_remove_filters();
1417
1418         if (current_user_can('unfiltered_html') == false)
1419                 kses_init_filters();
1420 }
1421
1422 add_action('init', 'kses_init');
1423 add_action('set_current_user', 'kses_init');
1424
1425 /**
1426  * Inline CSS filter
1427  *
1428  * @since 2.8.1
1429  */
1430 function safecss_filter_attr( $css, $deprecated = '' ) {
1431         if ( !empty( $deprecated ) )
1432                 _deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented
1433
1434         $css = wp_kses_no_null($css);
1435         $css = str_replace(array("\n","\r","\t"), '', $css);
1436
1437         if ( preg_match( '%[\\(&=}]|/\*%', $css ) ) // remove any inline css containing \ ( & } = or comments
1438                 return '';
1439
1440         $css_array = explode( ';', trim( $css ) );
1441
1442         /**
1443          * Filter list of allowed CSS attributes.
1444          *
1445          * @since 2.8.1
1446          *
1447          * @param array $attr List of allowed CSS attributes.
1448          */
1449         $allowed_attr = apply_filters( 'safe_style_css', array( 'text-align', 'margin', 'color', 'float',
1450         'border', 'background', 'background-color', 'border-bottom', 'border-bottom-color',
1451         'border-bottom-style', 'border-bottom-width', 'border-collapse', 'border-color', 'border-left',
1452         'border-left-color', 'border-left-style', 'border-left-width', 'border-right', 'border-right-color',
1453         'border-right-style', 'border-right-width', 'border-spacing', 'border-style', 'border-top',
1454         'border-top-color', 'border-top-style', 'border-top-width', 'border-width', 'caption-side',
1455         'clear', 'cursor', 'direction', 'font', 'font-family', 'font-size', 'font-style',
1456         'font-variant', 'font-weight', 'height', 'letter-spacing', 'line-height', 'margin-bottom',
1457         'margin-left', 'margin-right', 'margin-top', 'overflow', 'padding', 'padding-bottom',
1458         'padding-left', 'padding-right', 'padding-top', 'text-decoration', 'text-indent', 'vertical-align',
1459         'width' ) );
1460
1461         if ( empty($allowed_attr) )
1462                 return $css;
1463
1464         $css = '';
1465         foreach ( $css_array as $css_item ) {
1466                 if ( $css_item == '' )
1467                         continue;
1468                 $css_item = trim( $css_item );
1469                 $found = false;
1470                 if ( strpos( $css_item, ':' ) === false ) {
1471                         $found = true;
1472                 } else {
1473                         $parts = explode( ':', $css_item );
1474                         if ( in_array( trim( $parts[0] ), $allowed_attr ) )
1475                                 $found = true;
1476                 }
1477                 if ( $found ) {
1478                         if( $css != '' )
1479                                 $css .= ';';
1480                         $css .= $css_item;
1481                 }
1482         }
1483
1484         return $css;
1485 }
1486
1487 /**
1488  * Helper function to add global attributes to a tag in the allowed html list.
1489  *
1490  * @since 3.5.0
1491  * @access private
1492  *
1493  * @param array $value An array of attributes.
1494  * @return array The array of attributes with global attributes added.
1495  */
1496 function _wp_add_global_attributes( $value ) {
1497         $global_attributes = array(
1498                 'class' => true,
1499                 'id' => true,
1500                 'style' => true,
1501                 'title' => true,
1502                 'role' => true,
1503         );
1504
1505         if ( true === $value )
1506                 $value = array();
1507
1508         if ( is_array( $value ) )
1509                 return array_merge( $value, $global_attributes );
1510
1511         return $value;
1512 }