-
Notifications
You must be signed in to change notification settings - Fork 0
/
Escaper.php
203 lines (173 loc) · 5.43 KB
/
Escaper.php
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
<?php
/**
* Qubus\Security
*
* @link https://github.com/QubusPHP/security
* @copyright 2020
* @author Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
declare(strict_types=1);
namespace Qubus\Security;
use Qubus\EventDispatcher\ActionFilter\Observer;
use function urlencode;
use function urldecode;
use function strlen;
use function strip_tags;
use function parse_url;
use function mb_convert_encoding;
use function is_array;
use function in_array;
use function htmlspecialchars;
use function filter_var;
use const FILTER_VALIDATE_URL;
use const FILTER_SANITIZE_SPECIAL_CHARS;
use const ENT_QUOTES;
use const ENT_HTML5;
class Escaper implements CleanHtmlEntities
{
/**
* Convert special characters to HTML entities
*
* @param string $string The string being converted.
* @param int $flags A bitmask of one or more flags.
* @param string $encoding An optional argument defining the encoding used when converting characters.
* @param bool $doubleEncoding When double_encode is turned off PHP will not encode existing html entities,
* the default is to convert everything.
*/
private function htmlSpecialChars(
string $string,
int $flags = ENT_QUOTES | ENT_HTML5,
string $encoding = 'UTF-8',
bool $doubleEncoding = true
): string {
if (0 === strlen($string)) {
return '';
}
if (in_array($encoding, ['utf8', 'utf-8', 'UTF8', 'UTF-8'])) {
$encoding = 'UTF-8';
}
/**
* Filter the character encoding.
*
* @param string $encoding Default: UTF-8.
*/
$encoding = (new Observer())->filter->applyFilter('escaper_character_encoding', $encoding);
/**
* Filter double encoding.
*
* @param bool $doubleEncoding Default: true.
*/
$doubleEncoding = (new Observer())->filter->applyFilter('escaper_double_encoding', (bool) $doubleEncoding);
return htmlspecialchars($string, $flags, $encoding, $doubleEncoding);
}
/**
* Escaping for HTML blocks.
*
* @return string Escaped HTML block.
*/
public function html(string $string): string
{
$utf8String = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
return $this->htmlSpecialChars($utf8String, ENT_QUOTES);
}
/**
* Escaping for textarea.
*
* @return string Escaped string.
*/
public function textarea(string $string): string
{
$utf8String = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
return $this->htmlSpecialChars($utf8String, ENT_QUOTES);
}
/**
* Escaping for url.
*
* @param string $url The url to be escaped.
* @param array $scheme The url scheme.
* @param bool $encode Whether url params should be encoded.
* @return string The escaped $url after the `escUrl` filter is applied.
*/
public function url(string $url, array $scheme = [], bool $encode = false): string
{
$rawUrl = $url;
if ('' === $url) {
return $url;
}
/**
* First step of defense is to strip all tags.
*/
$escUrl = strip_tags($url);
/**
* Run url through a filter, and then validate it.
*/
$newUrl = filter_var(urldecode($escUrl), FILTER_SANITIZE_SPECIAL_CHARS);
if (! filter_var($newUrl, FILTER_VALIDATE_URL)) {
return '';
}
/**
* Merge default schemes with provided scheme(s).
*/
$scheme = array_merge($scheme, ['http', 'https']);
/**
* Break down the url into it's parts and then rebuild it.
*/
$uri = parse_url($newUrl);
if (! is_array($uri)) {
return '#';
}
if (! in_array($uri['scheme'], $scheme, true)) {
return '#';
}
$query = $uri['query'] ?? '';
$result = '';
if (isset($uri['scheme'])) {
$result .= $uri['scheme'] . ':';
}
if (isset($uri['host'])) {
$result .= '//' . $uri['host'];
}
if (isset($uri['port'])) {
$result .= ':' . $uri['port'];
}
if (isset($uri['path'])) {
$result .= $uri['path'];
}
$fragment = $uri['fragment'] ?? '';
$newQuery = $query . $fragment;
if ($query) {
$newQuery = '?' . $query . $fragment;
}
$cleanUrl = $result . $newQuery;
if ($encode) {
$cleanUrl = $result . $newQuery . urlencode($fragment);
}
return $cleanUrl;
}
/**
* Escaping for HTML attributes.
*
* @return string Escaped HTML attribute.
*/
public function attr(string $string): string
{
$utf8String = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
return $this->htmlSpecialChars($utf8String, ENT_QUOTES);
}
/**
* Escaping for inline javascript.
*
* Example usage:
*
* $esc_js = json_encode("Joshua's \"code\"");
* $attribute = $this->js("alert($esc_js);");
* echo '<input type="button" value="push" onclick="'.$attribute.'" />';
*
* @return string Escaped inline javascript.
*/
public function js(string $string): string
{
return $this->attr($string);
}
}