ILIAS  release_4-4 Revision
HTMLPurifier_PercentEncoder Class Reference

Class that handles operations involving percent-encoding in URIs. More...

+ Collaboration diagram for HTMLPurifier_PercentEncoder:

Public Member Functions

 __construct ($preserve=false)
 String of characters that should be preserved while using encode(). More...
 
 encode ($string)
 Our replacement for urlencode, it encodes all non-reserved characters, as well as any extra characters that were instructed to be preserved. More...
 
 normalize ($string)
 Fix up percent-encoding by decoding unreserved characters and normalizing. More...
 

Protected Attributes

 $preserve = array()
 Reserved characters to preserve when using encode(). More...
 

Detailed Description

Class that handles operations involving percent-encoding in URIs.

Warning
Be careful when reusing instances of PercentEncoder. The object you use for normalize() SHOULD NOT be used for encode(), or vice-versa.

Definition at line 11 of file PercentEncoder.php.

Constructor & Destructor Documentation

◆ __construct()

HTMLPurifier_PercentEncoder::__construct (   $preserve = false)

String of characters that should be preserved while using encode().

Definition at line 22 of file PercentEncoder.php.

References $preserve.

22  {
23  // unreserved letters, ought to const-ify
24  for ($i = 48; $i <= 57; $i++) $this->preserve[$i] = true; // digits
25  for ($i = 65; $i <= 90; $i++) $this->preserve[$i] = true; // upper-case
26  for ($i = 97; $i <= 122; $i++) $this->preserve[$i] = true; // lower-case
27  $this->preserve[45] = true; // Dash -
28  $this->preserve[46] = true; // Period .
29  $this->preserve[95] = true; // Underscore _
30  $this->preserve[126]= true; // Tilde ~
31 
32  // extra letters not to escape
33  if ($preserve !== false) {
34  for ($i = 0, $c = strlen($preserve); $i < $c; $i++) {
35  $this->preserve[ord($preserve[$i])] = true;
36  }
37  }
38  }
$preserve
Reserved characters to preserve when using encode().

Member Function Documentation

◆ encode()

HTMLPurifier_PercentEncoder::encode (   $string)

Our replacement for urlencode, it encodes all non-reserved characters, as well as any extra characters that were instructed to be preserved.

Note
Assumes that the string has already been normalized, making any and all percent escape sequences valid. Percents will not be re-escaped, regardless of their status in $preserve
Parameters
$stringString to be encoded
Returns
Encoded string.

Definition at line 50 of file PercentEncoder.php.

References $ret.

50  {
51  $ret = '';
52  for ($i = 0, $c = strlen($string); $i < $c; $i++) {
53  if ($string[$i] !== '%' && !isset($this->preserve[$int = ord($string[$i])]) ) {
54  $ret .= '%' . sprintf('%02X', $int);
55  } else {
56  $ret .= $string[$i];
57  }
58  }
59  return $ret;
60  }

◆ normalize()

HTMLPurifier_PercentEncoder::normalize (   $string)

Fix up percent-encoding by decoding unreserved characters and normalizing.

Warning
This function is affected by $preserve, even though the usual desired behavior is for this not to preserve those characters. Be careful when reusing instances of PercentEncoder!
Parameters
$stringString to normalize

Definition at line 69 of file PercentEncoder.php.

References $ret.

69  {
70  if ($string == '') return '';
71  $parts = explode('%', $string);
72  $ret = array_shift($parts);
73  foreach ($parts as $part) {
74  $length = strlen($part);
75  if ($length < 2) {
76  $ret .= '%25' . $part;
77  continue;
78  }
79  $encoding = substr($part, 0, 2);
80  $text = substr($part, 2);
81  if (!ctype_xdigit($encoding)) {
82  $ret .= '%25' . $part;
83  continue;
84  }
85  $int = hexdec($encoding);
86  if (isset($this->preserve[$int])) {
87  $ret .= chr($int) . $text;
88  continue;
89  }
90  $encoding = strtoupper($encoding);
91  $ret .= '%' . $encoding . $text;
92  }
93  return $ret;
94  }

Field Documentation

◆ $preserve

HTMLPurifier_PercentEncoder::$preserve = array()
protected

Reserved characters to preserve when using encode().

Definition at line 17 of file PercentEncoder.php.

Referenced by __construct().


The documentation for this class was generated from the following file: