📄 kses.php
字号:
'value' => $thisval,
'whole' => "$attrname=\"$thisval\"",
'vless' => 'n');
# We add quotes to conform to W3C's HTML spec.
$working = 1; $mode = 0;
$attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
}
break;
} # switch
if ($working == 0) # not well formed, remove and try again
{
$attr = 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 kses_hair
function 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 kses_check_attr_val
function 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 = kses_no_null($string);
$string = preg_replace('/\xad+/', '', $string); # deals with Opera "feature"
$string2 = $string.'a';
while ($string != $string2)
{
$string2 = $string;
$string = kses_bad_protocol_once($string, $allowed_protocols);
} # while
return $string;
} # function kses_bad_protocol
function 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 kses_no_null
function 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 kses_stripslashes
function 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 kses_array_lc
function 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 kses_js_entities
function kses_html_error($string)
###############################################################################
# This function deals with parsing errors in 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 kses_html_error
function kses_bad_protocol_once($string, $allowed_protocols)
###############################################################################
# This function searches for URL protocols at the beginning of $string, while
# handling whitespace and HTML entities.
###############################################################################
{
$string2 = preg_split('/:|:|:/i', $string, 2);
if(isset($string2[1]) && !preg_match('%/\?%',$string2[0]))
{
$string = kses_bad_protocol_once2($string2[0],$allowed_protocols).trim($string2[1]);
}
return $string;
} # function kses_bad_protocol_once
function 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 = kses_decode_entities($string);
$string2 = preg_replace('/\s/', '', $string2);
$string2 = 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 kses_bad_protocol_once2
function kses_normalize_entities($string)
###############################################################################
# This function normalizes HTML entities. It will convert "AT&T" to the correct
# "AT&T", ":" to ":", "&#XYZZY;" to "&#XYZZY;" and so on.
###############################################################################
{
# Disarm all entities by converting & to &
$string = str_replace('&', '&', $string);
# Change back the allowed entities in our entity whitelist
$string = preg_replace('/&([A-Za-z][A-Za-z0-9]{0,19});/',
'&\\1;', $string);
$string = preg_replace('/&#0*([0-9]{1,5});/e',
'kses_normalize_entities2("\\1")', $string);
$string = preg_replace('/&#([Xx])0*(([0-9A-Fa-f]{2}){1,2});/',
'&#\\1\\2;', $string);
return $string;
} # function kses_normalize_entities
function kses_normalize_entities2($i)
###############################################################################
# This function helps kses_normalize_entities() to only accept 16 bit values
# and nothing more for &#number; entities.
###############################################################################
{
return (($i > 65535) ? "&#$i;" : "&#$i;");
} # function kses_normalize_entities2
function kses_decode_entities($string)
###############################################################################
# This function decodes numeric HTML entities (A and A). It doesn't
# do anything with other entities like ä, 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 kses_decode_entities
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -