kses
[ class tree: kses ] [ index: kses ] [ all elements ]

Class: kses5

Source Location: /main/inc/lib/kses-0.2.2/oop/php5.class.kses.php

Class Overview


Kses strips evil scripts!


Author(s):

  • Richard R. V�squez, Jr. (Original procedural code by Ulf H�rnhammar)

Version:

  • PHP5 OOP 1.0.2

Copyright:

  • Richard R. V�squez, Jr. 2005

Variables

Methods



Class Details

[line 107]
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()




Tags:

author:  Richard R. V�squez, Jr. (Original procedural code by Ulf H�rnhammar)
version:  PHP5 OOP 1.0.2
copyright:  Richard R. V�squez, Jr. 2005
link:  http://chaos.org/contact/ Contact page with current email address for Richard Vasquez
link:  http://sourceforge.net/projects/kses/ Home Page for Kses
license:  GNU Public License


[ Top ]


Class Variables

$allowed_html =

[line 114]



Tags:

access:  private

Type:   array


[ Top ]

$allowed_protocols =

[line 113]



Tags:

access:  private

Type:   array


[ Top ]



Class Methods


constructor __construct [line 124]

kses5 __construct( )

Constructor for kses.

This sets a default collection of protocols allowed in links, and creates an empty set of allowed HTML tags.




Tags:

since:  PHP5 OOP 1.0.0
access:  public


[ Top ]

method AddHTML [line 448]

bool AddHTML( [string $tag = ""], [array $attribs = array()])

Adds valid (X)HTML with corresponding attributes that will be kept when stripping 'evil scripts'.

This method accepts one argument that can be either a string or an array of strings. Invalid data will be ignored.




Tags:

return:  Status of Adding (X)HTML and attributes.
since:  PHP4 OOP 0.0.1
access:  public


Parameters:

string   $tag   (X)HTML tag that will be allowed after stripping text.
array   $attribs   Associative array of allowed attributes - key => attribute name - value => attribute parameter

[ Top ]

method AddProtocol [line 240]

bool AddProtocol( [string $protocol = ""])

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().




Tags:

return:  Status of adding valid protocol.
since:  PHP4 OOP 0.0.1
access:  public


Parameters:

string   $protocol   The name of the protocol to be added.

[ Top ]

method AddProtocols [line 177]

bool AddProtocols( mixed 0)

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().




Tags:

return:  Status of adding valid protocols.
see:  kses5::AddProtocol()
since:  PHP5 OOP 1.0.0
access:  public


Parameters:

mixed   0   , A string or array of protocols that will be added to the internal list of allowed protocols.

[ Top ]

method checkAttributeValue [line 989]

bool checkAttributeValue( string $value, string $vless, string $checkname, string $checkvalue)

Controller method for performing checks on attribute values.

This method calls the appropriate method as specified by $checkname with the parameters $value, $vless, and $checkvalue, and returns the result of the call.

This method's functionality can be expanded by creating new methods that would match checkAttributeValue[$checkname].

Current checks implemented are: "maxlen", "minlen", "maxval", "minval" and "valueless"




Tags:

return:  Indicates whether the check passed or not
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

string   $value   The value of the attribute to be checked.
string   $vless   Indicates whether the the value is supposed to be valueless
string   $checkname   The check to be performed
string   $checkvalue   The value that is to be checked against

[ Top ]

method checkAttributeValueMaxlen [line 1015]

bool checkAttributeValueMaxlen( string $value, int $checkvalue)

Helper method invoked by checkAttributeValue().

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.




Tags:

return:  Indicates whether the check passed or not
see:  kses5::checkAttributeValue()
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

string   $value   The value of the attribute to be checked.
int   $checkvalue   The maximum value allowed

[ Top ]

method checkAttributeValueMaxval [line 1063]

bool checkAttributeValueMaxval( int $value, int $checkvalue)

Helper method invoked by checkAttributeValue().

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.




Tags:

return:  Indicates whether the check passed or not
see:  kses5::checkAttributeValue()
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

int   $value   The value of the attribute to be checked.
int   $checkvalue   The maximum numeric value allowed

[ Top ]

method checkAttributeValueMinlen [line 1037]

bool checkAttributeValueMinlen( string $value, int $checkvalue)

Helper method invoked by checkAttributeValue().

The minlen check makes sure that the attribute value has a length not smaller than the given value.




Tags:

return:  Indicates whether the check passed or not
see:  kses5::checkAttributeValue()
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

string   $value   The value of the attribute to be checked.
int   $checkvalue   The minimum value allowed

[ Top ]

method checkAttributeValueMinval [line 1089]

bool checkAttributeValueMinval( int $value, int $checkvalue)

Helper method invoked by checkAttributeValue().

The minval check checks that the attribute value is a positive integer, and that it is not smaller than the given value.




Tags:

return:  Indicates whether the check passed or not
see:  kses5::checkAttributeValue()
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

int   $value   The value of the attribute to be checked.
int   $checkvalue   The minimum numeric value allowed

[ Top ]

method checkAttributeValueValueless [line 1119]

bool checkAttributeValueValueless( int $value, mixed $checkvalue, string $vless)

Helper method invoked by checkAttributeValue().

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.




Tags:

return:  Indicates whether the check passed or not
see:  kses5::checkAttributeValue()
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

int   $value   The value of the attribute to be checked.
mixed   $checkvalue   This variable is ignored for this test
string   $vless   Flag indicating if this attribute is not supposed to have an attribute

[ Top ]

method combAttributes [line 796]

array combAttributes( string $attr)

This method combs through an attribute list string and returns an associative array of attributes and values.

This method does a lot of work. It parses an attribute list into an array with attribute data, and tries to do the right thing even if it gets weird input. It will add quotes around attribute values that don't have any quotes or apostrophes around them, to make it easier to produce HTML code that will conform to W3C's HTML specification. It will also remove bad URL protocols from attribute values.




Tags:

return:  Associative array containing data on attribute and value
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $attr   Text containing tag attributes for parsing

[ Top ]

method decodeEntities [line 1140]

string decodeEntities( $string, string $value)

Decodes numeric HTML entities

This method 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 white listing system anyway.




Tags:

return:  Decoded entity
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $value   The entitiy to be decoded.
   $string  

[ Top ]

method DumpElements [line 430]

array DumpElements( )

Raw dump of allowed (X)HTML elements

This returns an indexed array of allowed (X)HTML elements and attributes for a particular KSES instantiation.




Tags:

return:  The list of allowed elements.
since:  PHP5 OOP 1.0.2
access:  public


[ Top ]

method DumpProtocols [line 415]

array DumpProtocols( )

Raw dump of allowed protocols

This returns an indexed array of allowed protocols for a particular KSES instantiation.




Tags:

return:  The list of allowed protocols.
since:  PHP5 OOP 1.0.2
access:  public


[ Top ]

method filterKsesTextHook [line 592]

string filterKsesTextHook( string $string)

Allows for additional user defined modifications to text.

This method allows for additional modifications to be performed on a string that's being run through Parse(). Currently, it returns the input string 'as is'.

This method is provided for users to extend the kses class for their own requirements.




Tags:

return:  User modified string.
see:  kses5::Parse()
since:  PHP5 OOP 1.0.0
access:  private


Parameters:

string   $string   String to perfrom additional modifications on.

[ Top ]

method filterProtocols [line 947]

string filterProtocols( string $string)

Helper method used by removeBadProtocols()

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.




Tags:

return:  String with removed protocols
see:  kses5::removeBadProtocols()
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $string   String to check for protocols

[ Top ]

method makeArrayKeysLowerCase [line 619]

array makeArrayKeysLowerCase( array $in_array)

This method goes through an array, and changes the keys to all lower case.



Tags:

return:  Modified array
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

array   $in_array   Associative array

[ Top ]

method normalizeEntities [line 522]

string normalizeEntities( string $string)

Normalizes HTML entities

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.




Tags:

return:  String with normalized entities
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $string  

[ Top ]

method normalizeEntities16bit [line 571]

string normalizeEntities16bit( string $i)

Helper method used by normalizeEntites()

This method helps normalizeEntities() to only accept 16 bit values and nothing more for &#number; entities.

This method helps normalize_entities() during a preg_replace() where a &#(0)*XXXXX; occurs. The '(0)*XXXXXX' value is converted to a number and the result is returned as a numeric entity if the number is less than 65536. Otherwise, the value is returned 'as is'.




Tags:

return:  Normalized numeric entity
see:  kses5::normalizeEntities()
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $i  

[ Top ]

method Parse [line 147]

string Parse( [string $string = ""])

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.




Tags:

return:  The stripped string
since:  PHP4 OOP 0.0.1
access:  public


Parameters:

string   $string   String to be stripped of 'evil scripts'

[ Top ]

method Protocols [line 216]

bool Protocols( )

Allows for single/batch addition of protocols



Tags:

see:  kses5::AddProtocols()
deprecated:  Use AddProtocols()
since:  PHP4 OOP 0.0.1
access:  public


[ Top ]

method removeBadProtocols [line 921]

string removeBadProtocols( string $string)

This method removes disallowed protocols.

This method 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)".




Tags:

return:  String with removed protocols
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $string   String to check for protocols

[ Top ]

method removeNulls [line 504]

string removeNulls( string $string)

This method removes any NULL characters in $string.



Tags:

return:  String without any NULL/chr(173)
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $string  

[ Top ]

method RemoveProtocol [line 282]

bool RemoveProtocol( [string $protocol = ""])

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().




Tags:

return:  Status of removing valid protocol.
since:  PHP5 OOP 1.0.0
access:  public


Parameters:

string   $protocol   The name of the protocol to be removed.

[ Top ]

method RemoveProtocols [line 328]

bool RemoveProtocols( mixed 0)

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().




Tags:

return:  Status of removing valid protocols.
see:  kses5::RemoveProtocol()
since:  PHP5 OOP 1.0.0
access:  public


Parameters:

mixed   0   , A string or array of protocols that will be removed from the internal list of allowed protocols.

[ Top ]

method SetProtocols [line 372]

bool SetProtocols( mixed 0)

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().




Tags:

return:  Status of replacing valid protocols.
see:  kses5::AddProtocol()
since:  PHP5 OOP 1.0.1
access:  public


Parameters:

mixed   0   , A string or array of protocols that will be the new internal list of allowed protocols.

[ Top ]

method stripAttributes [line 711]

string stripAttributes( string $element, string $attr)

This method strips out disallowed attributes for (X)HTML tags.

This method removes all attributes if none are allowed for this element. If some are allowed it calls combAttributes() to split them further, and then it builds up new HTML code from the data that combAttributes() returns. It also removes "<" and ">" characters, if there are any left. One more thing it does is to check if the tag has a closing XHTML slash, and if it does, it puts one in the returned code as well.




Tags:

return:  Resulting valid (X)HTML or ''
see:  kses5::combAttributes()
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $element   (X)HTML tag to check
string   $attr   Text containing attributes to check for validity.

[ Top ]

method stripTags [line 657]

string stripTags( string $string)

This method strips out disallowed and/or mangled (X)HTML tags along with assigned attributes.

This method does a lot of work. It rejects some very malformed things like <:::>. It returns an empty string if the element isn't allowed (look ma, no strip_tags()!). Otherwise it splits the tag into an element and an allowed attribute list.




Tags:

return:  Modified string minus disallowed/mangled (X)HTML and attributes
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $string  

[ Top ]

method Version [line 1160]

string Version( )

Returns PHP5 OOP version # of kses.

Since this class has been refactored and documented and proven to work, I'm fixing the version number at 1.0.0.

This version is syntax compatible with the PHP4 OOP version 0.0.2. Future versions may not be syntax compatible.




Tags:

return:  Version number
since:  PHP4 OOP 0.0.1
access:  public


[ Top ]

method _hook [line 606]

string _hook( string $string)

Allows for additional user defined modifications to text.



Tags:

see:  kses5::filterKsesTextHook()
deprecated:  use filterKsesTextHook()
since:  PHP4 OOP 0.0.1
access:  private


Parameters:

string   $string  

[ Top ]


Documentation generated on Thu, 12 Jun 2008 14:12:57 -0500 by phpDocumentor 1.4.1