-
Notifications
You must be signed in to change notification settings - Fork 3
/
Uri.php
292 lines (264 loc) · 9.73 KB
/
Uri.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
<?php
/**
* @package ryanve/slash
*/
namespace slash;
class Uri extends Slash {
use traits\Mixin;
protected static $mixin = [];
/**
* Remove the scheme and :// from a URI. The first `//` and anything to
* its left is removed. Designed to be safer than using parse_url().
* Called "bar" b/c this is what you see in the address bar in web browsers
* @param string $uri
* @return string
* @example bar('foo://example.com/1') # 'example.com/1'
* @example bar('//example.com/2') # 'example.com/2'
* @example bar('www.example.com/3') # 'www.example.com/3'
*/
public static function bar($uri) {
if (!$uri || !is_string($uri)) return '';
$uri = explode('://', $uri);
isset($uri[1]) and array_shift($uri);
$uri = trim(implode('://', $uri ));
return ltrim(ltrim($uri, ':/'));
}
/**
* @param string $uri
* @return string chars after the first hash `#`
* @link http://en.wikipedia.org/wiki/Fragment_identifier
* @link http://stackoverflow.com/q/2849756/770127
* @link http://dev.airve.com/demo/speed_tests/php/fragment.php
*/
public static function fragment($uri) {
if (!isset($uri) || !is_string($uri)) return '';
$pos = strpos( $uri, '#' );
return $pos === false ? '' : substr($uri, ++$pos);
}
/**
* Get the #fragment (includes the `#`)
* @param string $uri
* @return string
*/
public static function hash($uri) {
return isset($uri) && is_string($uri) ? ($uri = strstr($uri, '#') ?: '') : '';
}
/**
* Get the "hierarchical" part of the URI. It includes everything
* after the '://' and before the ?query string or #fragment.
* @param string $uri
* @return string
*/
public static function hier($uri) {
return (string) (($uri = static::bar($uri)) ? strtok($uri, '?#') : $uri);
}
/**
* Make a URI protocol-relative a.k.a. scheme-relative.
* Or replace the scheme part of the URI to make the URI absolute with the specified scheme.
* @param string $uri
* @param string $scheme optional scheme to add (e.g. 'http')
* @return string
* @example prorel('example.com/dir/file.htm') # '//example.com/dir/file.htm'
* @example prorel('//example.com/dir/file.htm') # '//example.com/dir/file.htm'
* @example prorel('foo://example.com/dir/file.htm') # '//example.com/dir/file.htm'
* @example prorel('foo://example.com//dir//double') # '//example.com//dir//double'
*/
public static function prorel($uri, $scheme = '') {
$uri = static::bar($uri);
if ('' === $uri) return $uri;
$scheme = $scheme ? rtrim($scheme, ':/') : '';
$scheme and $scheme .= ':';
return $scheme . '//' . $uri;
}
/**
* Get the scheme (a.k.a. scheme name or protocol) part of a URI. Returns empty if the URI
* is non-absolute or invalid. Schemes can contain alpanumeric|dash|plus|period
* and must start w/ a letter. Schemes are case-insensitive and followed by `:`
* @param string $uri
* @return string
*/
public static function scheme($uri) {
# Fail fast if the uri does not look absolute. (Count < 2 means no '://')
$uri = explode('://', $uri);
if (!isset($uri[1])) return '';
# Trim the left side for safe use on form fields and use strtok to ensure we leftmost `:`
$uri = strtok(ltrim($uri[0] ), ':');
# check for invalid chars
if (!$uri || preg_match('#[^a-z0-9.+-]#', $uri = strtolower($uri))) return '';
# must start with a letter
return preg_match('#^[a-z]#', $uri) ? $uri : '';
}
/**
* Get the "authority" part of a URI. (userinfo? + hostname + port?)
* @param string $uri
* @return string
* @example authority('foo://example.com/dir/file.htm') # 'example.com'
* @example authority('foo://www.example.com/dir/file.htm') # 'www.example.com'
* @example authority('foo://user:[email protected]:800/dir/') # 'user:[email protected]:800'
*/
public static function authority($uri) {
return ($uri = static::bar($uri)) && ($uri = strtok($uri, '/?#')) && strpos($uri, '.') ? $uri : '';
}
/**
* @param string $uri
* @return string
*/
public static function userinfo($uri) {
return $uri && ($uri = static::authority($uri)) && ($i = strrpos($uri, '@')) ? substr($uri, 0, $i) : '';
}
/**
* @param string $uri
* @return string
*/
public static function user($uri) {
return (string) strtok(static::userinfo($uri), ':');
}
/**
* @param string $uri
* @return string
*/
public static function pass($uri) {
$pos = ($uri = static::userinfo($uri)) ? strpos($uri, ':') : false;
return $pos ? (string) substr($uri, ++$pos) : '';
}
/**
* Get the "hostname" part of a URI
* @param string $uri
* @return string
* @link http://en.wikipedia.org/wiki/URI_scheme#Examples
* @link http://tools.ietf.org/html/rfc3986#section-3.2.2
* @example hostname('foo://example.com/dir/file.htm') # 'example.com'
* @example hostname('foo://www.example.com/dir/file.htm') # 'www.example.com'
* @example hostname('foo://user:[email protected]:800/dir/') # 'example.com'
*/
public static function hostname($uri) {
$uri and ($uri = static::authority($uri))
and ($uri = explode('@', $uri))
and ($uri = array_pop($uri))
and ($uri = strtok($uri, ':'));
return (string) $uri;
}
/**
* Get the numeric "port" part of a URI
* @param string $uri
* @return string
* @link http://tools.ietf.org/html/rfc3986#section-3.2.3
* @example port('foo://example.com/dir/file.htm') # ''
* @example port('foo://user:[email protected]:800/dir/') # '800'
*/
public static function port($uri) {
if (!$uri) return '';
$uri = explode('@', static::authority($uri));
$uri = explode(':', array_pop($uri));
return 2 == count($uri) && ctype_digit($uri[1]) ? $uri[1] : '';
}
/**
* Get the "path" part of a URI
* @param string $uri an absolute URI or full barname
* @return string
* @link http://tools.ietf.org/html/rfc3986#section-3.3
* @example path('foo://example.com/dir/file.htm') # 'dir/file.htm'
* @example path('foo://example.com/dir/file?q=yo') # 'dir/file'
* @example path('foo://example.com/dir/file#frag') # 'dir/file'
* @example path('//user:[email protected]:800/dir/') # 'dir/'
*/
public static function path($uri) {
if (!isset($uri) || !is_string($uri)) return $uri;
# RE: http://tools.ietf.org/html/rfc3986#section-3.3
# <mailto:[email protected]> has a path of "[email protected]"
# whereas <foo://info.example.com?fred> has an empty path
$c = strpos($uri, ':');
if (false !== $c && '://' !== substr($uri, $c, 3) && (!($s = strpos($uri, '/')) || 0 < $s-$c))
elseif ($uri = static::bar($uri)) $uri = strstr($uri, '/');
return $uri and ($uri = strtok($uri, '?#')) && ($uri = str_replace('//', '/', $uri)) ? $uri : '';
}
/**
* Get the "query" part of a URI - the part after the `?` but before the #frag.
* If you need to parse the result use PHP parse_str()
* @param string $uri
* @return string
*/
public static function query($uri) {
if (!$uri) return '';
($uri = static::bar($uri))
and ($uri = strtok($uri, '#'))
and ($uri = strstr($uri, '?'))
and ($uri = substr($uri, 1 ))
and ($uri = html_entity_decode($uri));
return (string) $uri;
}
/**
* Get the part of the $uri needed for a dns-prefetch link:
* In other words, get the protocol-relative hostname. Related: prorel()
* @param string $uri
* @return string|bool
* @example prefetch('foo://example.com/dir/file.htm') # '//example.com'
* @example prefetch('foo://www.example.com/dir/file.htm') # '//www.example.com'
* @example prefetch('foo://user:[email protected]:800/dir/') # '//example.com'
*/
public static function prefetch($uri) {
return strlen($uri = static::hostname($uri)) ? '//' . $uri : $uri;
}
/**
* @param string $str
* @return string
*/
public static function novars($str) {
return isset($str) && is_string($str) ? (string) strtok($str, '?#') : '';
}
/**
* @param string $str
* @return string
*/
public static function nohash($str) {
return isset($str) && is_string($str) ? (string) strtok($str, '#') : '';
}
/**
* @return object
*/
public static function parse($uri = null) {
$o = (object) [];
if (!$uri || !is_string($uri)) return $o;
$o->uri = $uri;
$o->scheme = static::scheme($uri);
$o->bar = $bar = static::bar($uri);
$o->prorel = '//' . $bar;
if (!$bar) return $o;
# Authority / Hier / Path
$o->hier = (string) strtok($bar, '?#');
$part = explode('/', $o->hier);
$o->authority = array_shift($part);
$o->path = static::lslash(implode('/', $part));
# Authority
if (!strpos($o->authority, '.')) {
# Authority did not contain period => treat as relative path.
$o->path = static::lslash($o->authority . $o->path);
$o->authority = '';
} else {
# Separate authority.
$auth = explode('@', $o->authority);
$part = array_pop($auth); # includes host and port
$userinfo = implode('@', $auth);
# host / port:
$part = explode(':', $part);
$o->hostname = array_shift($part);
$o->prefetch = '//' . $o->hostname;
1 == count($part) && ctype_digit($part[0]) and $o->port = $part[0];
# User / Pass
if ($userinfo) {
$o->userinfo = $userinfo;
$part = explode(':', $userinfo);
$o->user = array_shift($part);
count($part) and $o->pass = implode(':', $part);
}
}
# Query
$part = explode('?', strtok($bar, '#' )); # nohash
array_shift($part); # novars
$o->query = html_entity_decode(implode('?', $part));
# Hash / Fragment
if ($hash = strstr($o->bar, '#')) $o->fragment = (string) substr($o->hash = $hash, 1);
return $o;
}
}