This repository has been archived by the owner on Nov 23, 2017. It is now read-only.
/
AbstractUrl.php
430 lines (385 loc) · 11 KB
/
AbstractUrl.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
/**
* This file is part of the League.url library
*
* @license http://opensource.org/licenses/MIT
* @link https://github.com/thephpleague/url/
* @version 3.2.0
* @package League.url
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Url;
use League\Url\Components\Fragment;
use League\Url\Components\Host;
use League\Url\Components\Pass;
use League\Url\Components\Path;
use League\Url\Components\Port;
use League\Url\Components\Query;
use League\Url\Components\Scheme;
use League\Url\Components\User;
use RuntimeException;
/**
* A Factory to ease League\Url\Url Object instantiation
*
* @package League.url
* @since 3.0.0
*/
abstract class AbstractUrl implements UrlInterface
{
/**
* Scheme
*
* @var \League\Url\Components\Scheme
*/
protected $scheme;
/**
* User
*
* @var \League\Url\Components\User
*/
protected $user;
/**
* Pass
*
* @var \League\Url\Components\Pass
*/
protected $pass;
/**
* Host
*
* @var \League\Url\Components\Host
*/
protected $host;
/**
* Port
*
*@var \League\Url\Components\Port
*/
protected $port;
/**
* Path
*
* @var \League\Url\Components\Path
*/
protected $path;
/**
* Query
*
* @var \League\Url\Components\Query
*/
protected $query;
/**
* Fragment
*
* @var \League\Url\Components\Fragment
*/
protected $fragment;
/**
* {@inheritdoc}
*/
public function __toString()
{
$url = $this->getBaseUrl().$this->getRelativeUrl();
if ('/' == $url) {
return '';
}
return $url;
}
/**
* {@inheritdoc}
*/
public function getRelativeUrl(UrlInterface $ref_url = null)
{
if (is_null($ref_url)) {
return $this->path->getUriComponent()
.$this->query->getUriComponent()
.$this->fragment->getUriComponent();
} elseif ($this->getBaseUrl() != $ref_url->getBaseUrl()) {
return $this->__toString();
}
return $this->path->getRelativePath($ref_url->getPath())
.$this->query->getUriComponent()
.$this->fragment->getUriComponent();
}
/**
* {@inheritdoc}
*/
public function getUserInfo()
{
$user = $this->user->getUriComponent().$this->pass->getUriComponent();
if ('' != $user) {
$user .= '@';
}
return $user;
}
/**
* {@inheritdoc}
*/
public function getAuthority()
{
$user = $this->getUserInfo();
$host = $this->host->getUriComponent();
$port = $this->port->getUriComponent();
return $user.$host.$port;
}
/**
* {@inheritdoc}
*/
public function getBaseUrl()
{
$scheme = $this->scheme->getUriComponent();
$auth = $this->getAuthority();
if ('' != $auth && '' == $scheme) {
$scheme = '//';
}
return $scheme.$auth;
}
/**
* {@inheritdoc}
*/
public function sameValueAs(UrlInterface $url)
{
return $this->__toString() == $url->__toString();
}
/**
* Retuns a array representation like parse_url
* But includes all components
*
* @return array
*/
public function toArray()
{
return array(
'scheme' => $this->scheme->get(),
'user' => $this->user->get(),
'pass' => $this->pass->get(),
'host' => $this->host->get(),
'port' => $this->port->get(),
'path' => $this->path->get(),
'query' => $this->query->get(),
'fragment' => $this->fragment->get(),
);
}
/**
* Return a instance of Url from a string
*
* @param string $url a string or an object that implement the __toString method
*
* @return static
*
* @throws RuntimeException If the URL can not be parse
*/
public static function createFromUrl($url)
{
$url = (string) $url;
$url = trim($url);
$original_url = $url;
$url = self::sanitizeUrl($url);
//if no valid scheme is found we add one
if (is_null($url)) {
throw new RuntimeException(sprintf('The given URL: `%s` could not be parse', $original_url));
}
$components = @parse_url($url);
if (false === $components) {
throw new RuntimeException(sprintf('The given URL: `%s` could not be parse', $original_url));
}
$components = array_merge(array(
'scheme' => null,
'user' => null,
'pass' => null,
'host' => null,
'port' => null,
'path' => null,
'query' => null,
'fragment' => null,
), $components);
$components = self::formatAuthComponent($components);
$components = self::formatPathComponent($components, $original_url);
return new static(
new Scheme($components['scheme']),
new User($components['user']),
new Pass($components['pass']),
new Host($components['host']),
new Port($components['port']),
new Path($components['path']),
new Query($components['query']),
new Fragment($components['fragment'])
);
}
protected static function sanitizeUrl($url)
{
if ('' == $url || strpos($url, '//') === 0) {
return $url;
} elseif (! preg_match(',^((http|ftp|ws)s?:),i', $url, $matches)) {
return '//'.$url;
}
$scheme_length = strlen($matches[0]);
if (strpos(substr($url, $scheme_length), '//') === 0) {
return $url;
}
return null;
}
/**
* Return a instance of Url from a server array
*
* @param array $server the server array
*
* @return static
*
* @throws RuntimeException If the URL can not be parse
*/
public static function createFromServer(array $server)
{
$scheme = self::fetchServerScheme($server);
$host = self::fetchServerHost($server);
$port = self::fetchServerPort($server);
$request = self::fetchServerRequestUri($server);
return self::createFromUrl($scheme.$host.$port.$request);
}
/**
* Return the Server URL scheme component
*
* @param array $server the server array
*
* @return string
*/
protected static function fetchServerScheme(array $server)
{
$scheme = '';
if (isset($server['SERVER_PROTOCOL'])) {
$scheme = explode('/', $server['SERVER_PROTOCOL']);
$scheme = strtolower($scheme[0]);
if (isset($server['HTTPS']) && 'off' != $server['HTTPS']) {
$scheme .= 's';
}
$scheme .= ':';
}
return $scheme.'//';
}
/**
* Return the Server URL host component
*
* @param array $server the server array
*
* @return string
*
* @throws \RuntimeException If no host is detected
*/
protected static function fetchServerHost(array $server)
{
if (isset($server['HTTP_HOST'])) {
$header = $server['HTTP_HOST'];
if (! preg_match('/(:\d+)$/', $header, $matches)) {
return $header;
}
return substr($header, 0, -strlen($matches[1]));
} elseif (isset($server['SERVER_ADDR'])) {
return $server['SERVER_ADDR'];
}
throw new RuntimeException('Host could not be detected');
}
/**
* Return the Server URL port component
*
* @param array $server the server array
*
* @return string
*/
protected static function fetchServerPort(array $server)
{
$port = '';
if (isset($server['SERVER_PORT']) && '80' != $server['SERVER_PORT']) {
$port = ':'. (int) $server['SERVER_PORT'];
}
return $port;
}
/**
* Return the Server URL Request Uri component
*
* @param array $server the server array
*
* @return string
*/
protected static function fetchServerRequestUri(array $server)
{
if (isset($server['REQUEST_URI'])) {
return $server['REQUEST_URI'];
} elseif (isset($server['PHP_SELF'])) {
return $server['PHP_SELF'];
}
return '/';
}
/**
* Reformat the component according to the auth content
*
* @param array $components the result from parse_url
*
* @return array
*/
protected static function formatAuthComponent(array $components)
{
if (!is_null($components['scheme'])
&& is_null($components['host'])
&& !empty($components['path'])
&& strpos($components['path'], '@') !== false
) {
$tmp = explode('@', $components['path'], 2);
$components['user'] = $components['scheme'];
$components['pass'] = $tmp[0];
$components['path'] = $tmp[1];
$components['scheme'] = null;
}
return $components;
}
/**
* Reformat the component according to the host content
*
* @param array $components the result from parse_url
*
* @return array
*/
protected static function formatHostComponent(array $components)
{
if (strpos($components['host'], '@')) {
list($auth, $components['host']) = explode('@', $components['host']);
$components['user'] = $auth;
$components['pass'] = null;
if (false !== strpos($auth, ':')) {
list($components['user'], $components['pass']) = explode(':', $auth);
}
}
return $components;
}
/**
* Reformat the component according to the path content
*
* @param array $components the result from parse_url
* @param string $url the original URL to be parse
*
* @return array
*/
protected static function formatPathComponent(array $components, $url)
{
if (is_null($components['scheme'])
&& is_null($components['host'])
&& !empty($components['path'])
) {
if (0 === strpos($components['path'], '///')) {
//even with the added scheme the URL is still broken
throw new RuntimeException(sprintf('The given URL: `%s` could not be parse', $url));
} elseif (0 === strpos($components['path'], '//')) {
$tmp = substr($components['path'], 2);
$components['path'] = null;
$res = explode('/', $tmp, 2);
$components['host'] = $res[0];
if (isset($res[1])) {
$components['path'] = $res[1];
}
$components = self::formatHostComponent($components);
}
}
return $components;
}
}