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