Class that handles operations involving percent-encoding in URIs.
More...
|
| __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...
|
|
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.
◆ __construct()
HTMLPurifier_PercentEncoder::__construct |
( |
|
$preserve = false | ) |
|
String of characters that should be preserved while using encode().
- Parameters
-
Definition at line 24 of file PercentEncoder.php.
25 {
26
27 for ($i = 48; $i <= 57; $i++) {
28 $this->preserve[$i] = true;
29 }
30 for ($i = 65; $i <= 90; $i++) {
31 $this->preserve[$i] = true;
32 }
33 for ($i = 97; $i <= 122; $i++) {
34 $this->preserve[$i] = true;
35 }
36 $this->preserve[45] = true;
37 $this->preserve[46] = true;
38 $this->preserve[95] = true;
39 $this->preserve[126]= true;
40
41
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().
References $preserve.
◆ 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 | $string | String to be encoded |
- Returns
- string Encoded string.
Definition at line 59 of file PercentEncoder.php.
60 {
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 {
67 }
68 }
70 }
References $ret.
◆ 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 | $string | String to normalize |
- Returns
- string
Definition at line 80 of file PercentEncoder.php.
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 }
108 }
References $ret.
◆ $preserve
HTMLPurifier_PercentEncoder::$preserve = array() |
|
protected |
The documentation for this class was generated from the following file: