ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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().

Parameters
bool$preserve

Definition at line 24 of file PercentEncoder.php.

References $c, $i, and $preserve.

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

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
string$stringString to be encoded
Returns
string Encoded string.

Definition at line 59 of file PercentEncoder.php.

References $c, $i, $int, and $ret.

60  {
61  $ret = '';
62  for ($i = 0, $c = strlen($string); $i < $c; $i++) {
63  if ($string[$i] !== '%' && !isset($this->preserve[$int = ord($string[$i])])) {
64  $ret .= '%' . sprintf('%02X', $int);
65  } else {
66  $ret .= $string[$i];
67  }
68  }
69  return $ret;
70  }
$ret
Definition: parser.php:6
$i
Definition: disco.tpl.php:19
$int

◆ 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
string$stringString to normalize
Returns
string

Definition at line 80 of file PercentEncoder.php.

References $int, $ret, and $text.

81  {
82  if ($string == '') {
83  return '';
84  }
85  $parts = explode('%', $string);
86  $ret = array_shift($parts);
87  foreach ($parts as $part) {
88  $length = strlen($part);
89  if ($length < 2) {
90  $ret .= '%25' . $part;
91  continue;
92  }
93  $encoding = substr($part, 0, 2);
94  $text = substr($part, 2);
95  if (!ctype_xdigit($encoding)) {
96  $ret .= '%25' . $part;
97  continue;
98  }
99  $int = hexdec($encoding);
100  if (isset($this->preserve[$int])) {
101  $ret .= chr($int) . $text;
102  continue;
103  }
104  $encoding = strtoupper($encoding);
105  $ret .= '%' . $encoding . $text;
106  }
107  return $ret;
108  }
$text
Definition: errorreport.php:18
$ret
Definition: parser.php:6
$int

Field Documentation

◆ $preserve

HTMLPurifier_PercentEncoder::$preserve = array()
protected

Reserved characters to preserve when using encode().

array

Definition at line 18 of file PercentEncoder.php.

Referenced by __construct().


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