forked from devknob/phpuri
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpuri.php
More file actions
208 lines (180 loc) · 4.36 KB
/
phpuri.php
File metadata and controls
208 lines (180 loc) · 4.36 KB
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
<?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 <pguardiario@gmail.com>
* @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 ];
/**
* CHANGE:
* @author Dominik Habichtsberg <Dominik.Habichtsberg@Hbg-IT.de>
* @since 24 Mai 2015 10:02 Uhr
*
* Former code: $this->path = ( empty( $m[ 5 ][ 0 ] ) ) ? '/' : $m[ 5 ][ 0 ];
* No tests failed, when the path is empty.
* With the former code, the relative urls //g and #s failed
*/
$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 );
/**
* CHANGE:
* @author Dominik Habichtsberg <Dominik.Habichtsberg@Hbg-IT.de>
* @since 24 Mai 2015 10:05 Uhr
* changed limit form -1 to 1, because climbing up the directory-tree failed
*/
$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 );
/**
* CHANGE:
* @author Dominik Habichtsberg <Dominik.Habichtsberg@Hbg-IT.de>
* @since 24 Mai 2015 10:25 Uhr
* The base-url should always have a path
*/
if ( empty( $uri->path ) )
{
$uri->path = '/';
}
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;
}
break;
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();
}
}