/
Escaper.zep
416 lines (372 loc) · 9.02 KB
/
Escaper.zep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Html;
use Phalcon\Html\Escaper\EscaperInterface;
/**
* Phalcon\Html\Escaper
*
* Escapes different kinds of text securing them. By using this component you
* may prevent XSS attacks.
*
* This component only works with UTF-8. The PREG extension needs to be compiled
* with UTF-8 support.
*
*```php
* $escaper = new \Phalcon\Html\Escaper();
*
* $escaped = $escaper->escapeCss("font-family: <Verdana>");
*
* echo $escaped; // font\2D family\3A \20 \3C Verdana\3E
*```
*/
class Escaper implements EscaperInterface
{
/**
* @var bool
*/
protected doubleEncode = true;
/**
* @var string
*/
protected encoding = "utf-8";
/**
* ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401
*
* @var int
*/
protected flags = 11;
/**
* Escapes a HTML attribute string or array
*
* If the input is an array, the keys are the attribute names and the
* values are attribute values. If a value is boolean (true/false) then
* the attribute will have no value:
* `['disabled' => true]` -> `'disabled``
*
* The resulting string will have attribute pairs separated by a space.
*
* @param array|string $input
*
* @return string
*/
public function attributes(var input) -> string
{
var key, result, value;
if (typeof input !== "string" && typeof input !== "array") {
throw new Exception("Input must be an array or a string");
}
if (typeof input === "string") {
return this->phpHtmlSpecialChars(input);
}
let result = "";
for key, value in input {
if (null === value || false === value) {
continue;
}
let key = trim(key);
if (typeof value === "array") {
let value = implode(" ", value);
}
let result .= this->phpHtmlSpecialChars(key);
if (true !== value) {
let result .= "=\""
. this->phpHtmlSpecialChars(value)
. "\"";
}
let result .= " ";
}
return rtrim(result);
}
/**
* Escape CSS strings by replacing non-alphanumeric chars by their
* hexadecimal escaped representation
*
* @param string $input
*
* @return string
*/
public function css(string input) -> string
{
/**
* Normalize encoding to UTF-32
* Escape the string
*/
return this->doEscapeCss(this->normalizeEncoding(input));
}
/**
* Detect the character encoding of a string to be handled by an encoder.
* Special-handling for chr(172) and chr(128) to chr(159) which fail to be
* detected by mb_detect_encoding()
*
* @param string $input
*
* @return string|null
*/
final public function detectEncoding(string input) -> string | null
{
var charset;
/**
* Check if charset is ASCII or ISO-8859-1
*/
let charset = phalcon_is_basic_charset(input);
if typeof charset == "string" {
return charset;
}
/**
* We require mbstring extension here
*/
if !function_exists("mb_detect_encoding") {
return null;
}
/**
* Strict encoding detection with fallback to non-strict detection.
* Check encoding
*/
for charset in ["UTF-32", "UTF-8", "ISO-8859-1", "ASCII"] {
if (false !== mb_detect_encoding(input, charset, true)) {
return charset;
}
}
/**
* Fallback to global detection
*/
return mb_detect_encoding(input);
}
/**
* Escape CSS strings by replacing non-alphanumeric chars by their
* hexadecimal escaped representation
*
* @param string $input
*
* @return string
* @deprecated
*/
public function escapeCss(string input) -> string
{
return this->css(input);
}
/**
* Escape JavaScript strings by replacing non-alphanumeric chars by their
* hexadecimal escaped representation
*
* @param string $input
*
* @return string
* @deprecated
*/
public function escapeJs(string input) -> string
{
return this->js(input);
}
/**
* Escapes a HTML string. Internally uses htmlspecialchars
*
* @param string|null $input
*
* @return string
* @deprecated
*/
public function escapeHtml(string input = null) -> string
{
return this->html((string) input);
}
/**
* Escapes a HTML attribute string
*
* @param string|null $input
*
* @return string
* @deprecated
*/
public function escapeHtmlAttr(string input = null) -> string
{
return this->attributes((string) input);
}
/**
* Escapes a URL. Internally uses rawurlencode
*
* @param string $input
*
* @return string
* @deprecated
*/
public function escapeUrl(string input) -> string
{
return this->url(input);
}
/**
* @return string
*/
public function getEncoding() -> string
{
return this->encoding;
}
/**
* @return int
*/
public function getFlags() -> int
{
return this->flags;
}
/**
* Escapes a HTML string. Internally uses htmlspecialchars
*
* @param string|null $input
*
* @return string
*/
public function html(string input = null) -> string
{
if !input {
return "";
}
return htmlspecialchars(
input,
this->flags,
this->encoding,
this->doubleEncode
);
}
/**
* Escape javascript strings by replacing non-alphanumeric chars by their
* hexadecimal escaped representation
*
* @param string $input
*
* @return string
*/
public function js(string input) -> string
{
/**
* Normalize encoding to UTF-32
* Escape the string
*/
return this->doEscapeJs(this->normalizeEncoding(input));
}
/**
* Utility to normalize a string's encoding to UTF-32.
*
* @param string $input
*
* @return string
*/
final public function normalizeEncoding(string input) -> string
{
/**
* Convert to UTF-32 (4 byte characters, regardless of actual number of
* bytes in the character).
*/
return mb_convert_encoding(
input,
"UTF-32",
this->detectEncoding(input)
);
}
/**
* Sets the double_encode to be used by the escaper
*
*```php
* $escaper->setDoubleEncode(false);
*```
*
* @param bool $doubleEncode
*/
public function setDoubleEncode(bool doubleEncode) -> <Escaper>
{
let this->doubleEncode = doubleEncode;
return this;
}
/**
* Sets the encoding to be used by the escaper
*
*```php
* $escaper->setEncoding("utf-8");
*```
*
* @param string $encoding
*/
public function setEncoding(string encoding) -> <EscaperInterface>
{
let this->encoding = encoding;
return this;
}
/**
* Sets the HTML quoting type for htmlspecialchars
*
*```php
* $escaper->setFlags(ENT_XHTML);
*```
*
* @param int $flags
*/
public function setFlags(int flags) -> <EscaperInterface>
{
let this->flags = flags;
return this;
}
/**
* Sets the HTML quoting type for htmlspecialchars
*
*```php
* $escaper->setHtmlQuoteType(ENT_XHTML);
*```
*
* @param int $flags
* @deprecated
*/
public function setHtmlQuoteType(int flags) -> <EscaperInterface>
{
return this->setFlags(flags);
}
/**
* Escapes a URL. Internally uses rawurlencode
*
* @param string $input
*
* @return string
*/
public function url(string input) -> string
{
return rawurlencode(input);
}
/**
* Proxy method for testing
*
* @param string $input
*
* @return string
*/
protected function phpHtmlSpecialChars(string input) -> string
{
return htmlspecialchars(
input,
ENT_QUOTES,
this->encoding,
this->doubleEncode
);
}
/**
* @param string $input
*
* @return string
*/
private function doEscapeCss(string input) -> string
{
return phalcon_escape_css(input);
}
/**
* @param string $input
*
* @return string
*/
private function doEscapeJs(string input) -> string
{
return phalcon_escape_js(input);
}
}