kses.php

来自「php 开发的内容管理系统」· PHP 代码 · 共 545 行 · 第 1/2 页

PHP
545
字号
					$mode = 0;					$attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);				}				break;		} # switch		if ($working == 0) # not well formed, remove and try again			{			$attr = wp_kses_html_error($attr);			$mode = 0;		}	} # while	if ($mode == 1)		# special case, for when the attribute list ends with a valueless		# attribute like "selected"		$attrarr[] = array ('name' => $attrname, 'value' => '', 'whole' => $attrname, 'vless' => 'y');	return $attrarr;} # function wp_kses_hairfunction wp_kses_check_attr_val($value, $vless, $checkname, $checkvalue)################################################################################ This function performs different checks for attribute values. The currently# implemented checks are "maxlen", "minlen", "maxval", "minval" and "valueless"# with even more checks to come soon.###############################################################################{	$ok = true;	switch (strtolower($checkname)) {		case 'maxlen' :			# The maxlen check makes sure that the attribute value has a length not			# greater than the given value. This can be used to avoid Buffer Overflows			# in WWW clients and various Internet servers.			if (strlen($value) > $checkvalue)				$ok = false;			break;		case 'minlen' :			# The minlen check makes sure that the attribute value has a length not			# smaller than the given value.			if (strlen($value) < $checkvalue)				$ok = false;			break;		case 'maxval' :			# The maxval check does two things: it checks that the attribute value is			# an integer from 0 and up, without an excessive amount of zeroes or			# whitespace (to avoid Buffer Overflows). It also checks that the attribute			# value is not greater than the given value.			# This check can be used to avoid Denial of Service attacks.			if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))				$ok = false;			if ($value > $checkvalue)				$ok = false;			break;		case 'minval' :			# The minval check checks that the attribute value is a positive integer,			# and that it is not smaller than the given value.			if (!preg_match('/^\s{0,6}[0-9]{1,6}\s{0,6}$/', $value))				$ok = false;			if ($value < $checkvalue)				$ok = false;			break;		case 'valueless' :			# The valueless check checks if the attribute has a value			# (like <a href="blah">) or not (<option selected>). If the given value			# is a "y" or a "Y", the attribute must not have a value.			# If the given value is an "n" or an "N", the attribute must have one.			if (strtolower($checkvalue) != $vless)				$ok = false;			break;	} # switch	return $ok;} # function wp_kses_check_attr_valfunction wp_kses_bad_protocol($string, $allowed_protocols)################################################################################ This function removes all non-allowed protocols from the beginning of# $string. It ignores whitespace and the case of the letters, and it does# understand HTML entities. It does its work in a while loop, so it won't be# fooled by a string like "javascript:javascript:alert(57)".###############################################################################{	$string = wp_kses_no_null($string);	$string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature"	$string2 = $string.'a';	while ($string != $string2) {		$string2 = $string;		$string = wp_kses_bad_protocol_once($string, $allowed_protocols);	} # while	return $string;} # function wp_kses_bad_protocolfunction wp_kses_no_null($string)################################################################################ This function removes any NULL characters in $string.###############################################################################{	$string = preg_replace('/\0+/', '', $string);	$string = preg_replace('/(\\\\0)+/', '', $string);	return $string;} # function wp_kses_no_nullfunction wp_kses_stripslashes($string)################################################################################ This function changes the character sequence  \"  to just  "# It leaves all other slashes alone. It's really weird, but the quoting from# preg_replace(//e) seems to require this.###############################################################################{	return preg_replace('%\\\\"%', '"', $string);} # function wp_kses_stripslashesfunction wp_kses_array_lc($inarray)################################################################################ This function goes through an array, and changes the keys to all lower case.###############################################################################{	$outarray = array ();	foreach ($inarray as $inkey => $inval) {		$outkey = strtolower($inkey);		$outarray[$outkey] = array ();		foreach ($inval as $inkey2 => $inval2) {			$outkey2 = strtolower($inkey2);			$outarray[$outkey][$outkey2] = $inval2;		} # foreach $inval	} # foreach $inarray	return $outarray;} # function wp_kses_array_lcfunction wp_kses_js_entities($string)################################################################################ This function removes the HTML JavaScript entities found in early versions of# Netscape 4.###############################################################################{	return preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);} # function wp_kses_js_entitiesfunction wp_kses_html_error($string)################################################################################ This function deals with parsing errors in wp_kses_hair(). The general plan is# to remove everything to and including some whitespace, but it deals with# quotes and apostrophes as well.###############################################################################{	return preg_replace('/^("[^"]*("|$)|\'[^\']*(\'|$)|\S)*\s*/', '', $string);} # function wp_kses_html_errorfunction wp_kses_bad_protocol_once($string, $allowed_protocols)################################################################################ This function searches for URL protocols at the beginning of $string, while# handling whitespace and HTML entities.###############################################################################{	return preg_replace('/^((&[^;]*;|[\sA-Za-z0-9])*)'.'(:|&#58;|&#[Xx]3[Aa];)\s*/e', 'wp_kses_bad_protocol_once2("\\1", $allowed_protocols)', $string);} # function wp_kses_bad_protocol_oncefunction wp_kses_bad_protocol_once2($string, $allowed_protocols)################################################################################ This function processes URL protocols, checks to see if they're in the white-# list or not, and returns different data depending on the answer.###############################################################################{	$string2 = wp_kses_decode_entities($string);	$string2 = preg_replace('/\s/', '', $string2);	$string2 = wp_kses_no_null($string2);	$string2 = preg_replace('/\xad+/', '', $string2);	# deals with Opera "feature"	$string2 = strtolower($string2);	$allowed = false;	foreach ($allowed_protocols as $one_protocol)		if (strtolower($one_protocol) == $string2) {			$allowed = true;			break;		}	if ($allowed)		return "$string2:";	else		return '';} # function wp_kses_bad_protocol_once2function wp_kses_normalize_entities($string)################################################################################ This function normalizes HTML entities. It will convert "AT&T" to the correct# "AT&amp;T", "&#00058;" to "&#58;", "&#XYZZY;" to "&amp;#XYZZY;" and so on.###############################################################################{	# Disarm all entities by converting & to &amp;	$string = str_replace('&', '&amp;', $string);	# Change back the allowed entities in our entity whitelist	$string = preg_replace('/&amp;([A-Za-z][A-Za-z0-9]{0,19});/', '&\\1;', $string);	$string = preg_replace('/&amp;#0*([0-9]{1,5});/e', 'wp_kses_normalize_entities2("\\1")', $string);	$string = preg_replace('/&amp;#([Xx])0*(([0-9A-Fa-f]{2}){1,2});/', '&#\\1\\2;', $string);	return $string;} # function wp_kses_normalize_entitiesfunction wp_kses_normalize_entities2($i)################################################################################ This function helps wp_kses_normalize_entities() to only accept 16 bit values# and nothing more for &#number; entities.###############################################################################{	return (($i > 65535) ? "&amp;#$i;" : "&#$i;");} # function wp_kses_normalize_entities2function wp_kses_decode_entities($string)################################################################################ This function decodes numeric HTML entities (&#65; and &#x41;). It doesn't# do anything with other entities like &auml;, but we don't need them in the# URL protocol whitelisting system anyway.###############################################################################{	$string = preg_replace('/&#([0-9]+);/e', 'chr("\\1")', $string);	$string = preg_replace('/&#[Xx]([0-9A-Fa-f]+);/e', 'chr(hexdec("\\1"))', $string);	return $string;} # function wp_kses_decode_entitiesfunction wp_filter_kses($data) {	global $allowedtags;	return wp_kses($data, $allowedtags);}function wp_filter_post_kses($data) {	global $allowedposttags;	return addslashes ( wp_kses(stripslashes( $data ), $allowedposttags) );}function kses_init_filters() {		add_filter('pre_comment_author', 'wp_filter_kses');		add_filter('pre_comment_content', 'wp_filter_kses');		add_filter('content_save_pre', 'wp_filter_post_kses');		add_filter('title_save_pre', 'wp_filter_kses');}function kses_init() {	remove_filter('pre_comment_author', 'wp_filter_kses');	remove_filter('pre_comment_content', 'wp_filter_kses');	remove_filter('content_save_pre', 'wp_filter_post_kses');	remove_filter('title_save_pre', 'wp_filter_kses');	if (current_user_can('unfiltered_html') == false)		kses_init_filters();}add_action('init', 'kses_init');add_action('set_current_user', 'kses_init');?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?