]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/3rdparty/htmlpurifier/HTMLPurifier/PercentEncoder.php
remove autoload section in composer.json
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / PercentEncoder.php
CommitLineData
d4949327
NL
1<?php\r
2\r
3/**\r
4 * Class that handles operations involving percent-encoding in URIs.\r
5 *\r
6 * @warning\r
7 * Be careful when reusing instances of PercentEncoder. The object\r
8 * you use for normalize() SHOULD NOT be used for encode(), or\r
9 * vice-versa.\r
10 */\r
11class HTMLPurifier_PercentEncoder\r
12{\r
13\r
14 /**\r
15 * Reserved characters to preserve when using encode().\r
16 * @type array\r
17 */\r
18 protected $preserve = array();\r
19\r
20 /**\r
21 * String of characters that should be preserved while using encode().\r
22 * @param bool $preserve\r
23 */\r
24 public function __construct($preserve = false)\r
25 {\r
26 // unreserved letters, ought to const-ify\r
27 for ($i = 48; $i <= 57; $i++) { // digits\r
28 $this->preserve[$i] = true;\r
29 }\r
30 for ($i = 65; $i <= 90; $i++) { // upper-case\r
31 $this->preserve[$i] = true;\r
32 }\r
33 for ($i = 97; $i <= 122; $i++) { // lower-case\r
34 $this->preserve[$i] = true;\r
35 }\r
36 $this->preserve[45] = true; // Dash -\r
37 $this->preserve[46] = true; // Period .\r
38 $this->preserve[95] = true; // Underscore _\r
39 $this->preserve[126]= true; // Tilde ~\r
40\r
41 // extra letters not to escape\r
42 if ($preserve !== false) {\r
43 for ($i = 0, $c = strlen($preserve); $i < $c; $i++) {\r
44 $this->preserve[ord($preserve[$i])] = true;\r
45 }\r
46 }\r
47 }\r
48\r
49 /**\r
50 * Our replacement for urlencode, it encodes all non-reserved characters,\r
51 * as well as any extra characters that were instructed to be preserved.\r
52 * @note\r
53 * Assumes that the string has already been normalized, making any\r
54 * and all percent escape sequences valid. Percents will not be\r
55 * re-escaped, regardless of their status in $preserve\r
56 * @param string $string String to be encoded\r
57 * @return string Encoded string.\r
58 */\r
59 public function encode($string)\r
60 {\r
61 $ret = '';\r
62 for ($i = 0, $c = strlen($string); $i < $c; $i++) {\r
63 if ($string[$i] !== '%' && !isset($this->preserve[$int = ord($string[$i])])) {\r
64 $ret .= '%' . sprintf('%02X', $int);\r
65 } else {\r
66 $ret .= $string[$i];\r
67 }\r
68 }\r
69 return $ret;\r
70 }\r
71\r
72 /**\r
73 * Fix up percent-encoding by decoding unreserved characters and normalizing.\r
74 * @warning This function is affected by $preserve, even though the\r
75 * usual desired behavior is for this not to preserve those\r
76 * characters. Be careful when reusing instances of PercentEncoder!\r
77 * @param string $string String to normalize\r
78 * @return string\r
79 */\r
80 public function normalize($string)\r
81 {\r
82 if ($string == '') {\r
83 return '';\r
84 }\r
85 $parts = explode('%', $string);\r
86 $ret = array_shift($parts);\r
87 foreach ($parts as $part) {\r
88 $length = strlen($part);\r
89 if ($length < 2) {\r
90 $ret .= '%25' . $part;\r
91 continue;\r
92 }\r
93 $encoding = substr($part, 0, 2);\r
94 $text = substr($part, 2);\r
95 if (!ctype_xdigit($encoding)) {\r
96 $ret .= '%25' . $part;\r
97 continue;\r
98 }\r
99 $int = hexdec($encoding);\r
100 if (isset($this->preserve[$int])) {\r
101 $ret .= chr($int) . $text;\r
102 continue;\r
103 }\r
104 $encoding = strtoupper($encoding);\r
105 $ret .= '%' . $encoding . $text;\r
106 }\r
107 return $ret;\r
108 }\r
109}\r
110\r
111// vim: et sw=4 sts=4\r