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