-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphpuri.php
124 lines (113 loc) · 3.22 KB
/
phpuri.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
<?php
/**
* A php library for converting relative urls to absolute.
* Website: https://github.com/monkeysuffrage/phpuri
*
* <pre>
* echo phpUri::parse('https://www.google.com/')->join('foo');
* //==> https://www.google.com/foo
* </pre>
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @author P Guardiario <[email protected]>
* @version 1.0
*/
/**
* phpUri
*/
class phpUri{
/**
* http(s)://
* @var string
*/
public $scheme;
/**
* www.example.com
* @var string
*/
public $authority;
/**
* /search
* @var string
*/
public $path;
/**
* ?q=foo
* @var string
*/
public $query;
/**
* #bar
* @var string
*/
public $fragment;
private function __construct($string){
preg_match_all('/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/', $string ,$m);
$this->scheme = $m[2][0];
$this->authority = $m[4][0];
$this->path = $m[5][0];
$this->query = $m[7][0];
$this->fragment = $m[9][0];
}
private function to_str(){
$ret = "";
if(!empty($this->scheme)) $ret .= "$this->scheme:";
if(!empty($this->authority)) $ret .= "//$this->authority";
$ret .= $this->normalize_path($this->path);
if(!empty($this->query)) $ret .= "?$this->query";
if(!empty($this->fragment)) $ret .= "#$this->fragment";
return $ret;
}
private function normalize_path($path){
if(empty($path)) return '';
$normalized_path = $path;
$normalized_path = preg_replace('`//+`', '/' , $normalized_path, -1, $c0);
$normalized_path = preg_replace('`^/\\.\\.?/`', '/' , $normalized_path, -1, $c1);
$normalized_path = preg_replace('`/\\.(/|$)`', '/' , $normalized_path, -1, $c2);
$normalized_path = preg_replace('`/[^/]*?/\\.\\.(/|$)`', '/' , $normalized_path, -1, $c3);
$num_matches = $c0 + $c1 + $c2 + $c3;
return ($num_matches > 0) ? $this->normalize_path($normalized_path) : $normalized_path;
}
/**
* Parse an url string
* @param string $url the url to parse
* @return phpUri
*/
public static function parse($url){
$uri = new phpUri($url);
return $uri;
}
/**
* Join with a relative url
* @param string $relative the relative url to join
* @return string
*/
public function join($relative){
$uri = new phpUri($relative);
switch(true){
case !empty($uri->scheme): break;
case !empty($uri->authority): break;
case empty($uri->path):
$uri->path = $this->path;
if(empty($uri->query)) $uri->query = $this->query;
case strpos($uri->path, '/') === 0: break;
default:
$base_path = $this->path;
if(strpos($base_path, '/') === false){
$base_path = '';
} else {
$base_path = preg_replace ('/\/[^\/]+$/' ,'/' , $base_path);
}
if(empty($base_path) && empty($this->authority)) $base_path = '/';
$uri->path = $base_path . $uri->path;
}
if(empty($uri->scheme)){
$uri->scheme = $this->scheme;
if(empty($uri->authority)) $uri->authority = $this->authority;
}
return $uri->to_str();
}
}
?>