-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathNormalizer.php
executable file
·388 lines (322 loc) · 11.4 KB
/
Normalizer.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
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
<?php
namespace URL;
/**
* Syntax based normalization of URI's
*
* This normalises URI's based on the specification RFC 3986
* https://tools.ietf.org/html/rfc3986
*
* Example usage:
* <code>
* require_once 'vendor/autoload.php';
*
* $url = 'eXAMPLE://a/./b/../b/%63/%7bfoo%7d';
* $un = new URL\Normalizer( $url );
* echo $un->normalize();
*
* // result: "example://a/b/c/%7Bfoo%7D"
* </code>
*
* @author Glen Scott <glen@glenscott.co.uk>
*/
class Normalizer
{
private $url;
private $scheme;
private $host;
private $port;
private $user;
private $pass;
private $path;
private $query;
private $fragment;
private $default_scheme_ports = array( 'http:' => 80, 'https:' => 443, );
private $components = array( 'scheme', 'host', 'port', 'user', 'pass', 'path', 'query', 'fragment', );
private $remove_empty_delimiters;
private $sort_query_params;
/**
* Does the original URL have a ? query delimiter
*/
private $query_delimiter;
public function __construct($url = null, $remove_empty_delimiters = false, $sort_query_params = false)
{
if ($url) {
$this->setUrl($url);
}
$this->remove_empty_delimiters = $remove_empty_delimiters;
$this->sort_query_params = $sort_query_params;
}
private function getQuery($query)
{
$qs = array();
foreach ($query as $qk => $qv) {
if (is_array($qv)) {
$qs[rawurldecode($qk)] = $this->getQuery($qv);
} else {
$qs[rawurldecode($qk)] = rawurldecode($qv);
}
}
return $qs;
}
public function getUrl()
{
return $this->url;
}
public function setUrl($url)
{
$this->url = $url;
if (strpos($this->url, '?') !== false) {
$this->query_delimiter = true;
} else {
$this->query_delimiter = false;
}
// parse URL into respective parts
$url_components = $this->mbParseUrl($this->url);
if (! $url_components) {
// Reset URL
$this->url = '';
// Flush properties
foreach ($this->components as $key) {
if (property_exists($this, $key)) {
$this->$key = '';
}
}
return false;
} else {
// Update properties
foreach ($url_components as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
}
}
// Flush missing components
$missing_components = array_diff(
array_values($this->components),
array_keys($url_components)
);
foreach ($missing_components as $key) {
if (property_exists($this, $key)) {
$this->$key = '';
}
}
return true;
}
}
public function normalize()
{
// URI Syntax Components
// scheme authority path query fragment
// @link https://tools.ietf.org/html/rfc3986#section-3
// Scheme
// @link https://tools.ietf.org/html/rfc3986#section-3.1
if ($this->scheme) {
// Converting the scheme to lower case
$this->scheme = strtolower($this->scheme) . ':';
}
// Authority
// @link https://tools.ietf.org/html/rfc3986#section-3.2
$authority = '';
if ($this->host) {
$authority .= '//';
// User Information
// @link https://tools.ietf.org/html/rfc3986#section-3.2.1
if ($this->user) {
if ($this->pass) {
$authority .= $this->user . ':' . $this->pass . '@';
} else {
$authority .= $this->user . '@';
}
}
// Host
// @link https://tools.ietf.org/html/rfc3986#section-3.2.2
// Converting the host to lower case
if (mb_detect_encoding($this->host) == 'UTF-8') {
$authority .= mb_strtolower($this->host, 'UTF-8');
} else {
$authority .= strtolower($this->host);
}
// Port
// @link https://tools.ietf.org/html/rfc3986#section-3.2.3
// Removing the default port
if (isset($this->default_scheme_ports[$this->scheme] )
&& $this->port == $this->default_scheme_ports[$this->scheme]) {
$this->port = '';
}
if ($this->port) {
$authority .= ':' . $this->port;
}
}
// Path
// @link https://tools.ietf.org/html/rfc3986#section-3.3
if ($this->path) {
$this->path = $this->removeAdditionalPathPrefixSlashes($this->path);
$this->path = $this->removeDotSegments($this->path);
$this->path = $this->urlDecodeUnreservedChars($this->path);
$this->path = $this->urlDecodeReservedSubDelimChars($this->path);
} elseif ($this->url) {
// Add default path only when valid URL is present
// Adding trailing /
$this->path = '/';
}
// Query
// @link https://tools.ietf.org/html/rfc3986#section-3.4
if ($this->query) {
$query = $this->parseStr($this->query);
//encodes every parameter correctly
$qs = $this->getQuery($query);
$this->query = '?';
if ($this->sort_query_params) {
ksort($qs);
}
foreach ($qs as $key => $val) {
if (strlen($this->query) > 1) {
$this->query .= '&';
}
if (is_array($val)) {
for ($i = 0; $i < count($val); $i++) {
if ($i > 0) {
$this->query .= '&';
}
$this->query .= rawurlencode($key) . '=' . rawurlencode($val[$i]);
}
} else {
$this->query .= rawurlencode($key) . '=' . rawurlencode($val);
}
}
// Fix http_build_query adding equals sign to empty keys
$this->query = str_replace('=&', '&', rtrim($this->query, '='));
} else {
if ($this->query_delimiter && ! $this->remove_empty_delimiters) {
$this->query = '?';
}
}
// Fragment
// @link https://tools.ietf.org/html/rfc3986#section-3.5
if ($this->fragment) {
$this->fragment = rawurldecode($this->fragment);
$this->fragment = rawurlencode($this->fragment);
$this->fragment = '#' . $this->fragment;
}
$this->setUrl($this->scheme . $authority . $this->path . $this->query . $this->fragment);
return $this->getUrl();
}
/**
* Path segment normalization
* https://tools.ietf.org/html/rfc3986#section-5.2.4
*/
public function removeDotSegments($path)
{
$new_path = '';
while (! empty($path)) {
// A
$pattern_a = '!^(\.\./|\./)!x';
$pattern_b_1 = '!^(/\./)!x';
$pattern_b_2 = '!^(/\.)$!x';
$pattern_c = '!^(/\.\./|/\.\.)!x';
$pattern_d = '!^(\.|\.\.)$!x';
$pattern_e = '!(/*[^/]*)!x';
if (preg_match($pattern_a, $path)) {
// remove prefix from $path
$path = preg_replace($pattern_a, '', $path);
} elseif (preg_match($pattern_b_1, $path, $matches) || preg_match($pattern_b_2, $path, $matches)) {
$path = preg_replace("!^" . $matches[1] . "!", '/', $path);
} elseif (preg_match($pattern_c, $path, $matches)) {
$path = preg_replace('!^' . preg_quote($matches[1], '!') . '!x', '/', $path);
// remove the last segment and its preceding "/" (if any) from output buffer
$new_path = preg_replace('!/([^/]+)$!x', '', $new_path);
} elseif (preg_match($pattern_d, $path)) {
$path = preg_replace($pattern_d, '', $path);
} else {
if (preg_match($pattern_e, $path, $matches)) {
$first_path_segment = $matches[1];
$path = preg_replace('/^' . preg_quote($first_path_segment, '/') . '/', '', $path, 1);
$new_path .= $first_path_segment;
}
}
}
return $new_path;
}
public function getScheme()
{
return $this->scheme;
}
/**
* Decode unreserved characters
*
* @link https://tools.ietf.org/html/rfc3986#section-2.3
*/
public function urlDecodeUnreservedChars($string)
{
$string = rawurldecode($string);
$string = rawurlencode($string);
$string = str_replace(array( '%2F', '%3A', '%40' ), array( '/', ':', '@' ), $string);
return $string;
}
/**
* Decode reserved sub-delims
*
* @link https://tools.ietf.org/html/rfc3986#section-2.2
*/
public function urlDecodeReservedSubDelimChars($string)
{
return str_replace(
array( '%21', '%24', '%26', '%27', '%28', '%29', '%2A', '%2B', '%2C', '%3B', '%3D' ),
array( '!', '$', '&', "'", '(', ')', '*', '+', ',', ';', '=' ),
$string
);
}
/**
* Replacement for PHP's parse_string which does not deal with spaces or dots in key names
*
* @param string $string URL query string
* @return array key value pairs
*/
private function parseStr($string)
{
$params = array();
$pairs = explode('&', $string);
foreach ($pairs as $pair) {
if (! $pair) {
continue;
}
$var = explode('=', $pair, 2);
$val = ( isset( $var[1] ) ? $var[1] : '' );
if (isset($params[$var[0]])) {
if (is_array($params[$var[0]])) {
$params[$var[0]][] = $val;
} else {
$params[$var[0]] = array($params[$var[0]], $val);
}
} else {
$params[$var[0]] = $val;
}
}
return $params;
}
private function mbParseUrl($url)
{
$result = false;
// Build arrays of values we need to decode before parsing
$entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%24', '%2C', '%2F', '%3F', '%23', '%5B', '%5D');
$replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "$", ",", "/", "?", "#", "[", "]");
// Create encoded URL with special URL characters decoded so it can be parsed
// All other characters will be encoded
$encodedURL = str_replace($entities, $replacements, urlencode($url));
// Parse the encoded URL
$encodedParts = parse_url($encodedURL);
// Now, decode each value of the resulting array
if ($encodedParts) {
foreach ($encodedParts as $key => $value) {
$result[$key] = urldecode(str_replace($replacements, $entities, $value));
}
}
return $result;
}
/*
* Converts ////foo to /foo within each path segment
*/
private function removeAdditionalPathPrefixSlashes($path)
{
return preg_replace('/(\/)+/', '/', $path);
}
}