📄 php5.class.kses.php
字号:
<?php
/*
* ==========================================================================================
*
* This program is free software and open source software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit
* http://www.gnu.org/licenses/gpl.html
*
* ==========================================================================================
*/
/**
* Class file for PHP5 OOP version of kses
*
* This is an updated version of kses to work with PHP5 that works under E_STRICT.
*
* This version is a bit of a rewrite to match my own coding style and use some of the
* capabilities allowed in PHP5. Since this was a significant rewrite, but it still
* maintains backward compatibility syntax-wise, the version number is now 1.0.0. Any
* minor changes that do not break compatibility will be indicated in the second or third
* digits. Anything that breaks compatibility will change the major version number.
*
* PHP5 specific changes:
* + Private methods are now in place
* + __construct() is now used rather then the standard class name 'kses()'
* + Kses will not load in any version less that PHP5
* Other modifications:
* + PHPdoc style documentation has been added to the class. See http://www.phpdoc.org/ for more info.
* + Method names have been changed to reflect status as verbs
* + One line methods have been folded into the code
* + Some methods are now deprecated due to nomenclature style change. See method documentation for specifics.
* + Kses5 now works in E_STRICT
* + Version number is 1.0.0 to reflect serious code changes
* + Addition of methods AddProtocols(), filterKsestextHook(), RemoveProtocol(), RemoveProtocols() and SetProtocols()
* + Deprecated _hook(), Protocols()
*
* @package kses
* @subpackage kses5
*/
if(substr(phpversion(), 0, 1) < 5)
{
die("Class kses requires PHP 5 or higher.");
}
/**
* Only install KSES5 once
*/
if(!defined('KSES_CLASS_PHP5'))
{
define('KSES_CLASS_PHP5', true);
/**
* Kses strips evil scripts!
*
* This class provides the capability for removing unwanted HTML/XHTML, attributes from
* tags, and protocols contained in links. The net result is a much more powerful tool
* than the PHP internal strip_tags()
*
* This is a fork of a slick piece of procedural code called 'kses' written by Ulf Harnhammar.
*
* The original class for PHP4 was basically a wrapper around all of the functions in
* the procedural code written by Ulf, and was released 7/25/2003.
*
* This version is a bit of a rewrite to match my own coding style and use some of the
* capabilities allowed in PHP5. Since this was a significant rewrite, but it still
* maintains backward compatibility syntax-wise, the version number is now 1.0.0. Any
* minor changes that do not break compatibility will be indicated in the second or third
* digits. Anything that breaks compatibility will change the major version number.
*
* PHP5 specific changes:
* + Private methods are now in place
* + __construct() is now used rather then the standard class name 'kses()'
* + Kses5 will not load in any version less that PHP5
* Other modifications:
* + PHPdoc style documentation has been added to the class. See http://www.phpdoc.org/ for more info.
* + Method names have been changed to reflect status as verbs
* + One line methods have been folded into the code
* + Some methods are now deprecated due to nomenclature style change. See method documentation for specifics.
* + Kses now works in E_STRICT
* + Initial Version number set to 1.0.0 to reflect serious code changes
* + Addition of methods AddProtocols(), filterKsestextHook(), RemoveProtocol(), RemoveProtocols() and SetProtocols()
* + Deprecated _hook(), Protocols()
* + Integrated code from kses 0.2.2 into class.
* + Added methods DumpProtocols(), DumpMethods()
*
* @author Richard R. V锟絪quez, Jr. (Original procedural code by Ulf H锟絩nhammar)
* @link http://sourceforge.net/projects/kses/ Home Page for Kses
* @link http://chaos.org/contact/ Contact page with current email address for Richard Vasquez
* @copyright Richard R. V锟絪quez, Jr. 2005
* @version PHP5 OOP 1.0.2
* @license http://www.gnu.org/licenses/gpl.html GNU Public License
* @package kses
*/
class kses5
{
/**#@+
* @access private
* @var array
*/
private $allowed_protocols;
private $allowed_html;
/**#@-*/
/**
* Constructor for kses.
*
* This sets a default collection of protocols allowed in links, and creates an
* empty set of allowed HTML tags.
* @since PHP5 OOP 1.0.0
*/
public function __construct()
{
/**
* You could add protocols such as ftp, new, gopher, mailto, irc, etc.
*
* The base values the original kses provided were:
* 'http', 'https', 'ftp', 'news', 'nntp', 'telnet', 'gopher', 'mailto'
*/
$this->allowed_protocols = array('http', 'ftp', 'mailto');
$this->allowed_html = array();
}
/**
* Basic task of kses - parses $string and strips it as required.
*
* This method strips all the disallowed (X)HTML tags, attributes
* and protocols from the input $string.
*
* @access public
* @param string $string String to be stripped of 'evil scripts'
* @return string The stripped string
* @since PHP4 OOP 0.0.1
*/
public function Parse($string = "")
{
if (get_magic_quotes_gpc())
{
$string = stripslashes($string);
}
$string = $this->removeNulls($string);
// Remove JavaScript entities from early Netscape 4 versions
$string = preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
$string = $this->normalizeEntities($string);
$string = $this->filterKsesTextHook($string);
$string = preg_replace('%(<' . '[^>]*' . '(>|$)' . '|>)%e', "\$this->stripTags('\\1')", $string);
return $string;
}
/**
* Allows for single/batch addition of protocols
*
* This method accepts one argument that can be either a string
* or an array of strings. Invalid data will be ignored.
*
* The argument will be processed, and each string will be added
* via AddProtocol().
*
* @access public
* @param mixed , A string or array of protocols that will be added to the internal list of allowed protocols.
* @return bool Status of adding valid protocols.
* @see AddProtocol()
* @since PHP5 OOP 1.0.0
*/
public function AddProtocols()
{
$c_args = func_num_args();
if($c_args != 1)
{
trigger_error("kses5::AddProtocols() did not receive an argument.", E_USER_WARNING);
return false;
}
$protocol_data = func_get_arg(0);
if(is_array($protocol_data) && count($protocol_data) > 0)
{
foreach($protocol_data as $protocol)
{
$this->AddProtocol($protocol);
}
return true;
}
elseif(is_string($protocol_data))
{
$this->AddProtocol($protocol_data);
return true;
}
else
{
trigger_error("kses5::AddProtocols() did not receive a string or an array.", E_USER_WARNING);
return false;
}
}
/**
* Allows for single/batch addition of protocols
*
* @deprecated Use AddProtocols()
* @see AddProtocols()
* @return bool
* @since PHP4 OOP 0.0.1
*/
public function Protocols()
{
$c_args = func_num_args();
if($c_args != 1)
{
trigger_error("kses5::Protocols() did not receive an argument.", E_USER_WARNING);
return false;
}
return $this->AddProtocols(func_get_arg(0));
}
/**
* Adds a single protocol to $this->allowed_protocols.
*
* This method accepts a string argument and adds it to
* the list of allowed protocols to keep when performing
* Parse().
*
* @access public
* @param string $protocol The name of the protocol to be added.
* @return bool Status of adding valid protocol.
* @since PHP4 OOP 0.0.1
*/
public function AddProtocol($protocol = "")
{
if(!is_string($protocol))
{
trigger_error("kses5::AddProtocol() requires a string.", E_USER_WARNING);
return false;
}
// Remove any inadvertent ':' at the end of the protocol.
if(substr($protocol, strlen($protocol) - 1, 1) == ":")
{
$protocol = substr($protocol, 0, strlen($protocol) - 1);
}
$protocol = strtolower(trim($protocol));
if($protocol == "")
{
trigger_error("kses5::AddProtocol() tried to add an empty/NULL protocol.", E_USER_WARNING);
return false;
}
// prevent duplicate protocols from being added.
if(!in_array($protocol, $this->allowed_protocols))
{
array_push($this->allowed_protocols, $protocol);
sort($this->allowed_protocols);
}
return true;
}
/**
* Removes a single protocol from $this->allowed_protocols.
*
* This method accepts a string argument and removes it from
* the list of allowed protocols to keep when performing
* Parse().
*
* @access public
* @param string $protocol The name of the protocol to be removed.
* @return bool Status of removing valid protocol.
* @since PHP5 OOP 1.0.0
*/
public function RemoveProtocol($protocol = "")
{
if(!is_string($protocol))
{
trigger_error("kses5::RemoveProtocol() requires a string.", E_USER_WARNING);
return false;
}
// Remove any inadvertent ':' at the end of the protocol.
if(substr($protocol, strlen($protocol) - 1, 1) == ":")
{
$protocol = substr($protocol, 0, strlen($protocol) - 1);
}
$protocol = strtolower(trim($protocol));
if($protocol == "")
{
trigger_error("kses5::RemoveProtocol() tried to remove an empty/NULL protocol.", E_USER_WARNING);
return false;
}
// Ensures that the protocol exists before removing it.
if(in_array($protocol, $this->allowed_protocols))
{
$this->allowed_protocols = array_diff($this->allowed_protocols, array($protocol));
sort($this->allowed_protocols);
}
return true;
}
/**
* Allows for single/batch removal of protocols
*
* This method accepts one argument that can be either a string
* or an array of strings. Invalid data will be ignored.
*
* The argument will be processed, and each string will be removed
* via RemoveProtocol().
*
* @access public
* @param mixed , A string or array of protocols that will be removed from the internal list of allowed protocols.
* @return bool Status of removing valid protocols.
* @see RemoveProtocol()
* @since PHP5 OOP 1.0.0
*/
public function RemoveProtocols()
{
$c_args = func_num_args();
if($c_args != 1)
{
return false;
}
$protocol_data = func_get_arg(0);
if(is_array($protocol_data) && count($protocol_data) > 0)
{
foreach($protocol_data as $protocol)
{
$this->RemoveProtocol($protocol);
}
}
elseif(is_string($protocol_data))
{
$this->RemoveProtocol($protocol_data);
return true;
}
else
{
trigger_error("kses5::RemoveProtocols() did not receive a string or an array.", E_USER_WARNING);
return false;
}
}
/**
* Allows for single/batch replacement of protocols
*
* This method accepts one argument that can be either a string
* or an array of strings. Invalid data will be ignored.
*
* Existing protocols will be removed, then the argument will be
* processed, and each string will be added via AddProtocol().
*
* @access public
* @param mixed , A string or array of protocols that will be the new internal list of allowed protocols.
* @return bool Status of replacing valid protocols.
* @since PHP5 OOP 1.0.1
* @see AddProtocol()
*/
public function SetProtocols()
{
$c_args = func_num_args();
if($c_args != 1)
{
trigger_error("kses5::SetProtocols() did not receive an argument.", E_USER_WARNING);
return false;
}
$protocol_data = func_get_arg(0);
if(is_array($protocol_data) && count($protocol_data) > 0)
{
$this->allowed_protocols = array();
foreach($protocol_data as $protocol)
{
$this->AddProtocol($protocol);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -