1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Phalcon\Html; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Escaper |
16
|
|
|
* |
17
|
|
|
* @property bool $doubleEncode |
18
|
|
|
* @property string $encoding |
19
|
|
|
* @property int $flags |
20
|
|
|
*/ |
21
|
|
|
class Escaper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $doubleEncode = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $encoding = "utf-8"; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ENT_QUOTES | ENT_SUBSTITUTE |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $flags = 11; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Escape HTML attributes |
42
|
|
|
* |
43
|
|
|
* @param string $input |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
20 |
|
public function attributes(string $input): string |
48
|
|
|
{ |
49
|
20 |
|
return $input; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Escape CSS |
54
|
|
|
* |
55
|
|
|
* @param string $input |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function css(string $input): string |
60
|
|
|
{ |
61
|
|
|
return $input; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns whether we will use double encoding or not |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function getDoubleEncode(): bool |
70
|
|
|
{ |
71
|
|
|
return $this->doubleEncode; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the encoding used |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getEncoding(): string |
80
|
|
|
{ |
81
|
|
|
return $this->encoding; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the htmlspecialchars flags |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
|
|
public function getFlags(): int |
90
|
|
|
{ |
91
|
|
|
return $this->flags; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Escapes HTML |
96
|
|
|
* |
97
|
|
|
* @param mixed $input |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
11 |
|
public function html($input): string |
102
|
|
|
{ |
103
|
11 |
|
return htmlspecialchars( |
104
|
11 |
|
$input, |
105
|
11 |
|
$this->flags, |
106
|
11 |
|
$this->encoding, |
107
|
11 |
|
$this->doubleEncode |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Escape JS |
113
|
|
|
* |
114
|
|
|
* @param string $input |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function js(string $input): string |
119
|
|
|
{ |
120
|
|
|
return $input; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sets whether we will used double encoding or not |
125
|
|
|
* |
126
|
|
|
* @param bool $flag |
127
|
|
|
* |
128
|
|
|
* @return Escaper |
129
|
|
|
*/ |
130
|
|
|
public function setDoubleEncode(bool $flag): Escaper |
131
|
|
|
{ |
132
|
|
|
$this->doubleEncode = $flag; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Sets the encoding to be used |
139
|
|
|
* |
140
|
|
|
* @param string $encoding |
141
|
|
|
* |
142
|
|
|
* @return Escaper |
143
|
|
|
*/ |
144
|
|
|
public function setEncoding(string $encoding): Escaper |
145
|
|
|
{ |
146
|
|
|
$this->encoding = $encoding; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Escapes a URL |
153
|
|
|
* |
154
|
|
|
* @param string $url |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function url(string $url): string |
159
|
|
|
{ |
160
|
|
|
return rawurldecode($url); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|