-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
UriPartsFilter.php
141 lines (125 loc) · 4.08 KB
/
UriPartsFilter.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
<?php
/**
* @package Grav\Framework\Uri
*
* @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Uri;
/**
* Class Uri
* @package Grav\Framework\Uri
*/
class UriPartsFilter
{
const HOSTNAME_REGEX = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/u';
/**
* @param string $scheme
* @return string
* @throws \InvalidArgumentException If the scheme is invalid.
*/
public static function filterScheme($scheme)
{
if (!\is_string($scheme)) {
throw new \InvalidArgumentException('Uri scheme must be a string');
}
return strtolower($scheme);
}
/**
* Filters the user info string.
*
* @param string $info The raw user or password.
* @return string The percent-encoded user or password string.
* @throws \InvalidArgumentException
*/
public static function filterUserInfo($info)
{
if (!\is_string($info)) {
throw new \InvalidArgumentException('Uri user info must be a string');
}
return preg_replace_callback(
'/(?:[^a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=]+|%(?![A-Fa-f0-9]{2}))/u',
function ($match) {
return rawurlencode($match[0]);
},
$info
) ?? '';
}
/**
* @param string $host
* @return string
* @throws \InvalidArgumentException If the host is invalid.
*/
public static function filterHost($host)
{
if (!\is_string($host)) {
throw new \InvalidArgumentException('Uri host must be a string');
}
if (filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
$host = '[' . $host . ']';
} elseif ($host && preg_match(static::HOSTNAME_REGEX, $host) !== 1) {
throw new \InvalidArgumentException('Uri host name validation failed');
}
return strtolower($host);
}
/**
* Filter Uri port.
*
* This method
*
* @param int|null $port
* @return int|null
* @throws \InvalidArgumentException If the port is invalid.
*/
public static function filterPort($port = null)
{
if (null === $port || (\is_int($port) && ($port >= 0 && $port <= 65535))) {
return $port;
}
throw new \InvalidArgumentException('Uri port must be null or an integer between 0 and 65535');
}
/**
* Filter Uri path.
*
* This method percent-encodes all reserved characters in the provided path string. This method
* will NOT double-encode characters that are already percent-encoded.
*
* @param string $path The raw uri path.
* @return string The RFC 3986 percent-encoded uri path.
* @throws \InvalidArgumentException If the path is invalid.
* @link http://www.faqs.org/rfcs/rfc3986.html
*/
public static function filterPath($path)
{
if (!\is_string($path)) {
throw new \InvalidArgumentException('Uri path must be a string');
}
return preg_replace_callback(
'/(?:[^a-zA-Z0-9_\-\.~:@&=\+\$,\/;%]+|%(?![A-Fa-f0-9]{2}))/u',
function ($match) {
return rawurlencode($match[0]);
},
$path
) ?? '';
}
/**
* Filters the query string or fragment of a URI.
*
* @param string $query The raw uri query string.
* @return string The percent-encoded query string.
* @throws \InvalidArgumentException If the query is invalid.
*/
public static function filterQueryOrFragment($query)
{
if (!\is_string($query)) {
throw new \InvalidArgumentException('Uri query string and fragment must be a string');
}
return preg_replace_callback(
'/(?:[^a-zA-Z0-9_\-\.~!\$&\'\(\)\*\+,;=%:@\/\?]+|%(?![A-Fa-f0-9]{2}))/u',
function ($match) {
return rawurlencode($match[0]);
},
$query
) ?? '';
}
}