Zend Framework  3.0
Public Member Functions | Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
Escaper Class Reference

Context specific methods for use in secure output escaping. More...

Public Member Functions

 __construct ($encoding=null)
 Constructor: Single parameter allows setting of global encoding for use by the current object.
 
 getEncoding ()
 Return the encoding that all output/input is expected to be encoded in.
 
 escapeHtml ($string)
 Escape a string for the HTML Body context where there are very few characters of special meaning.
 
 escapeHtmlAttr ($string)
 Escape a string for the HTML Attribute context.
 
 escapeJs ($string)
 Escape a string for the Javascript context.
 
 escapeUrl ($string)
 Escape a string for the URI or Parameter contexts.
 
 escapeCss ($string)
 Escape a string for the CSS context.
 

Protected Member Functions

 htmlAttrMatcher ($matches)
 Callback function for preg_replace_callback that applies HTML Attribute escaping to all matches.
 
 jsMatcher ($matches)
 Callback function for preg_replace_callback that applies Javascript escaping to all matches.
 
 cssMatcher ($matches)
 Callback function for preg_replace_callback that applies CSS escaping to all matches.
 
 toUtf8 ($string)
 Converts a string to UTF-8 from the base encoding.
 
 fromUtf8 ($string)
 Converts a string from UTF-8 to the base encoding.
 
 isUtf8 ($string)
 Checks if a given string appears to be valid UTF-8 or not.
 
 convertEncoding ($string, $to, $from)
 Encoding conversion helper which wraps iconv and mbstring where they exist or throws and exception where neither is available.
 

Protected Attributes

 $encoding = 'utf-8'
 
 $htmlSpecialCharsFlags
 
 $htmlAttrMatcher
 
 $jsMatcher
 
 $cssMatcher
 
 $supportedEncodings
 

Static Protected Attributes

static $htmlNamedEntityMap
 

Detailed Description

Context specific methods for use in secure output escaping.

Constructor & Destructor Documentation

__construct (   $encoding = null)

Constructor: Single parameter allows setting of global encoding for use by the current object.

Parameters
string$encoding
Exceptions
Exception\InvalidArgumentException

Member Function Documentation

convertEncoding (   $string,
  $to,
  $from 
)
protected

Encoding conversion helper which wraps iconv and mbstring where they exist or throws and exception where neither is available.

Parameters
string$string
string$to
array | string$from
Exceptions
Exception\RuntimeException
Returns
string
cssMatcher (   $matches)
protected

Callback function for preg_replace_callback that applies CSS escaping to all matches.

Parameters
array$matches
Returns
string
escapeCss (   $string)

Escape a string for the CSS context.

CSS escaping can be applied to any string being inserted into CSS and escapes everything except alphanumerics.

Parameters
string$string
Returns
string
escapeHtml (   $string)

Escape a string for the HTML Body context where there are very few characters of special meaning.

Internally this will use htmlspecialchars().

Parameters
string$string
Returns
string
escapeHtmlAttr (   $string)

Escape a string for the HTML Attribute context.

We use an extended set of characters to escape that are not covered by htmlspecialchars() to cover cases where an attribute might be unquoted or quoted illegally (e.g. backticks are valid quotes for IE).

Parameters
string$string
Returns
string
escapeJs (   $string)

Escape a string for the Javascript context.

This does not use json_encode(). An extended set of characters are escaped beyond ECMAScript's rules for Javascript literal string escaping in order to prevent misinterpretation of Javascript as HTML leading to the injection of special characters and entities. The escaping used should be tolerant of cases where HTML escaping was not applied on top of Javascript escaping correctly. Backslash escaping is not used as it still leaves the escaped character as-is and so is not useful in a HTML context.

Parameters
string$string
Returns
string
escapeUrl (   $string)

Escape a string for the URI or Parameter contexts.

This should not be used to escape an entire URI - only a subcomponent being inserted. The function is a simple proxy to rawurlencode() which now implements RFC 3986 since PHP 5.3 completely.

Parameters
string$string
Returns
string
fromUtf8 (   $string)
protected

Converts a string from UTF-8 to the base encoding.

The base encoding is set via this class' constructor.

Parameters
string$string
Returns
string
getEncoding ( )

Return the encoding that all output/input is expected to be encoded in.

Returns
string
htmlAttrMatcher (   $matches)
protected

Callback function for preg_replace_callback that applies HTML Attribute escaping to all matches.

Parameters
array$matches
Returns
string

The following replaces characters undefined in HTML with the hex entity for the Unicode replacement character.

Check if the current character to escape has a name entity we should replace it with while grabbing the integer value of the character.

Per OWASP recommendations, we'll use upper hex entities for any other characters where a named entity does not exist.

isUtf8 (   $string)
protected

Checks if a given string appears to be valid UTF-8 or not.

Parameters
string$string
Returns
bool
jsMatcher (   $matches)
protected

Callback function for preg_replace_callback that applies Javascript escaping to all matches.

Parameters
array$matches
Returns
string
toUtf8 (   $string)
protected

Converts a string to UTF-8 from the base encoding.

The base encoding is set via this class' constructor.

Parameters
string$string
Exceptions
Exception\RuntimeException
Returns
string

Member Data Documentation

$cssMatcher
protected
$encoding = 'utf-8'
protected
$htmlAttrMatcher
protected
$htmlNamedEntityMap
staticprotected
Initial value:
= [
34 => 'quot'
$htmlSpecialCharsFlags
protected
$jsMatcher
protected
$supportedEncodings
protected
Initial value:
= [
'iso-8859-1', 'iso8859-1', 'iso-8859-5', 'iso8859-5',
'iso-8859-15', 'iso8859-15', 'utf-8', 'cp866',
'ibm866', '866', 'cp1251', 'windows-1251',
'win-1251', '1251', 'cp1252', 'windows-1252',
'1252', 'koi8-r', 'koi8-ru', 'koi8r',
'big5', '950', 'gb2312', '936',
'big5-hkscs', 'shift_jis', 'sjis', 'sjis-win',
'cp932', '932', 'euc-jp', 'eucjp',
'eucjp-win', 'macroman'
]