|
1
|
|
|
<?php |
|
2
|
|
|
namespace vsc\infrastructure\urls; |
|
3
|
|
|
|
|
4
|
|
|
use vsc\infrastructure\BaseObject; |
|
5
|
|
|
use vsc\presentation\requests\HttpRequestA; |
|
6
|
|
|
|
|
7
|
|
|
class UrlParserA extends BaseObject implements UrlParserInterface { |
|
8
|
|
|
static protected $queryEncodingType = PHP_QUERY_RFC1738; |
|
9
|
|
|
|
|
10
|
|
|
private $sUrl; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Url |
|
13
|
|
|
*/ |
|
14
|
|
|
private $oUrl; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $sUrl |
|
|
|
|
|
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct($sUrl = null) { |
|
20
|
|
|
$this->setUrl($sUrl); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __toString() { |
|
27
|
|
|
return $this->getCompleteUri(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* This is somewhat ugly. |
|
32
|
|
|
* Returns an instance of the class on which the method is called |
|
33
|
|
|
* @return Url |
|
34
|
|
|
*/ |
|
35
|
|
|
public static function getCurrentUrl() { |
|
36
|
|
|
return static::parse_url(static::getRequestUri()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public static function getRequestUri() { |
|
43
|
4 |
|
if (is_array($_SERVER) && array_key_exists('REQUEST_URI', $_SERVER)) { |
|
44
|
3 |
|
if (array_key_exists('HTTP_HOST', $_SERVER)) { |
|
45
|
2 |
|
$uri = 'http' . (HttpRequestA::isSecure() ? 's' : '') . '://' . $_SERVER['HTTP_HOST']; |
|
46
|
|
|
} else { |
|
47
|
1 |
|
$uri = ''; |
|
48
|
|
|
} |
|
49
|
3 |
|
$uri .= $_SERVER['REQUEST_URI']; |
|
50
|
|
|
} else { |
|
51
|
1 |
|
$uri = ''; |
|
52
|
|
|
} |
|
53
|
4 |
|
return $uri; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $sUrl |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public static function urlHasScheme($sUrl) { |
|
61
|
1 |
|
$firstPos = min(strpos($sUrl, ':'), strpos($sUrl, '/')); |
|
62
|
1 |
|
$sScheme = substr($sUrl, 0, $firstPos); |
|
63
|
1 |
|
return Url::isValidScheme($sScheme); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Url $oUrl |
|
68
|
|
|
* @param array $aParsed |
|
69
|
|
|
* @return Url |
|
70
|
|
|
*/ |
|
71
|
1 |
|
private static function loadParsedUrl($oUrl, $aParsed) { |
|
72
|
1 |
|
if (isset($aParsed['scheme'])) { |
|
73
|
1 |
|
$oUrl->setScheme($aParsed['scheme']); |
|
74
|
|
|
} |
|
75
|
1 |
|
if (isset($aParsed['host'])) { |
|
76
|
1 |
|
$oUrl->setHost($aParsed['host']); |
|
77
|
|
|
} |
|
78
|
1 |
|
if (isset($aParsed['port'])) { |
|
79
|
1 |
|
$oUrl->setPort($aParsed['port']); |
|
80
|
|
|
} |
|
81
|
1 |
|
if (isset($aParsed['path'])) { |
|
82
|
|
|
$oUrl->setPath($aParsed['path']); |
|
83
|
|
|
} |
|
84
|
1 |
|
if (isset($aParsed['query'])) { |
|
85
|
|
|
$oUrl->setRawQuery($aParsed['query']); |
|
86
|
|
|
} |
|
87
|
1 |
|
if (isset($aParsed['fragment'])) { |
|
88
|
|
|
$oUrl->setFragment($aParsed['fragment']); |
|
89
|
|
|
} |
|
90
|
1 |
|
return $oUrl; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* This exists as the php::parse_url function sometimes breaks inexplicably |
|
95
|
|
|
* @param string $sUrl |
|
|
|
|
|
|
96
|
|
|
* @return Url |
|
97
|
|
|
*/ |
|
98
|
25 |
|
protected static function parse_url($sUrl = null) { |
|
99
|
25 |
|
if (is_null($sUrl)) { |
|
100
|
1 |
|
return new Url(); |
|
101
|
|
|
} |
|
102
|
24 |
|
if (!stristr($sUrl, '://') && (substr($sUrl, 0, 2) != '//')) { |
|
103
|
19 |
|
$oUrl = new Url(); |
|
104
|
19 |
|
$oUrl->setScheme('file'); |
|
105
|
19 |
|
$oUrl->setPath($sUrl); |
|
106
|
19 |
|
return $oUrl; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
try { |
|
110
|
5 |
|
if (!self::urlHasScheme($sUrl)) { |
|
111
|
2 |
|
$sUrl = (HttpRequestA::isSecure() ? 'https:' : 'http:') . $sUrl; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
5 |
|
if (mb_detect_encoding($sUrl) !== 'ASCII') { |
|
115
|
|
|
$sUrl = rawurlencode($sUrl); |
|
116
|
|
|
} |
|
117
|
5 |
|
$aParsed = parse_url($sUrl); |
|
118
|
5 |
|
return self::loadParsedUrl(new Url(), $aParsed); |
|
|
|
|
|
|
119
|
|
|
} catch (\Exception $e) { |
|
120
|
|
|
// failed php::parse_url |
|
121
|
|
|
return new Url(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $sUrl |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setUrl($sUrl) { |
|
129
|
|
|
$this->sUrl = $sUrl; |
|
130
|
|
|
$this->oUrl = static::parse_url($sUrl); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function getSubdomainOf($sRootDomain) { |
|
|
|
|
|
|
134
|
|
|
$sHost = strtolower($this->oUrl->getHost()); |
|
135
|
|
|
$sSubDomains = stristr($sHost, '.' . $sRootDomain, true); |
|
136
|
|
|
|
|
137
|
|
|
return self::getTldOf($sSubDomains); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
public static function getTldOf($sHost) { |
|
|
|
|
|
|
141
|
1 |
|
if (ip2long($sHost) > 0 || empty($sHost)) { return false; } |
|
142
|
|
|
|
|
143
|
1 |
|
if (!strrpos($sHost, '.')) { |
|
144
|
1 |
|
return $sHost; |
|
145
|
|
|
} else { |
|
146
|
1 |
|
return substr($sHost, strrpos($sHost, '.') + 1); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public static function getCurrentHostName() { |
|
|
|
|
|
|
151
|
|
|
return $_SERVER['HTTP_HOST']; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
3 |
|
public function getParentPath($iSteps = 0) { |
|
155
|
3 |
|
$sPath = self::normalizePath($this->oUrl->getPath()); |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
3 |
|
$len = strlen($sPath); |
|
159
|
3 |
|
$bHasPrefixSlash = (($len > 0) && ($sPath[0] == '/')); |
|
160
|
3 |
|
$bHasSuffixSlash = (($len > 1) && ($sPath[$len - 1] == '/')); |
|
161
|
|
|
|
|
162
|
|
|
// removing the folders from the path if there are parent references (../) |
|
163
|
3 |
|
$sPath = trim($sPath, '/'); |
|
164
|
3 |
|
$aPath = explode('/', $sPath); |
|
165
|
|
|
|
|
166
|
3 |
|
if ($iSteps > 0) { |
|
167
|
|
|
// removing last $iSteps components of the path |
|
168
|
3 |
|
$aPath = array_slice($aPath, 0, -$iSteps); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// in case of actually getting the parent, we need to append the ending / |
|
172
|
|
|
// as we don't have a file as the last element in the path - same case for paths without a good termination |
|
173
|
3 |
|
$sPath = ($bHasPrefixSlash ? '/' : '') . implode('/', $aPath) . ($bHasSuffixSlash ? '/' : ''); |
|
174
|
3 |
|
return $sPath; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
4 |
|
public function setPath($sPath) { |
|
178
|
4 |
|
$this->oUrl->setPath($sPath); |
|
179
|
4 |
|
} |
|
180
|
|
|
|
|
181
|
1 |
|
public function getPath() { |
|
182
|
1 |
|
return $this->getParentPath(0); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $sPath |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public static function isAbsolutePath($sPath) { |
|
190
|
1 |
|
return substr($sPath, 0, 1) == '/'; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return Url |
|
|
|
|
|
|
195
|
|
|
*/ |
|
196
|
1 |
|
static public function getSiteUri() { |
|
197
|
1 |
|
return static::url(static::getRequestUri())->getHost(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param int $iSteps |
|
202
|
|
|
* @return string |
|
203
|
|
|
* @internal param bool $bFull |
|
204
|
|
|
*/ |
|
205
|
3 |
|
public function getCompleteParentUri($iSteps = 1) { |
|
206
|
3 |
|
$sUrl = $this->oUrl->getScheme() . |
|
207
|
3 |
|
$this->oUrl->getHost() . |
|
208
|
3 |
|
$this->getParentPath($iSteps) . |
|
209
|
3 |
|
$this->oUrl->getRawQueryString() . |
|
210
|
3 |
|
$this->oUrl->getFragment(); |
|
211
|
|
|
|
|
212
|
3 |
|
return $sUrl; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return string |
|
217
|
|
|
* @internal param bool $bFull |
|
218
|
|
|
*/ |
|
219
|
3 |
|
public function getCompleteUri() { |
|
220
|
3 |
|
return $this->getCompleteParentUri(0); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $sUri |
|
225
|
|
|
* @param string $sTermination |
|
226
|
|
|
* @return bool |
|
227
|
|
|
*/ |
|
228
|
26 |
|
public static function hasGoodTermination($sUri, $sTermination = '/') { |
|
229
|
|
|
// last element should be an / or in the last part after / there should be a . |
|
230
|
26 |
|
return (substr($sUri, -1) == $sTermination || stristr(substr($sUri, strrpos($sUri, $sTermination)), '.')); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param string $sUrl |
|
235
|
|
|
* @return Url |
|
236
|
|
|
*/ |
|
237
|
18 |
|
static public function url($sUrl) { |
|
238
|
18 |
|
return static::parse_url($sUrl); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param string $sPath |
|
243
|
|
|
* @return string |
|
244
|
|
|
*/ |
|
245
|
42 |
|
static public function normalizePath($sPath) { |
|
246
|
42 |
|
if (empty ($sPath)) { |
|
247
|
1 |
|
return ''; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
41 |
|
$len = strlen($sPath); |
|
251
|
41 |
|
$bHasPrefixSlash = ($sPath[0] == '/'); |
|
252
|
41 |
|
$bHasSuffixSlash = (($len > 1) && ($sPath[$len - 1] == '/')); |
|
253
|
|
|
// removing the folders from the path if there are parent references (../) |
|
254
|
41 |
|
$sPath = trim($sPath, '/'); |
|
255
|
41 |
|
$aPath = explode('/', $sPath); |
|
256
|
|
|
|
|
257
|
41 |
|
$iCnt = 0; |
|
258
|
41 |
|
foreach ($aPath as $iKey => $sFolder) { |
|
259
|
41 |
|
switch ($sFolder) { |
|
260
|
41 |
|
case '..': |
|
261
|
12 |
|
$iCnt++; |
|
262
|
|
|
|
|
263
|
12 |
|
unset ($aPath[$iKey]); |
|
264
|
12 |
|
if (array_key_exists($iKey - 1, $aPath)) { |
|
265
|
9 |
|
$iPrevKey = $iKey - 1; |
|
266
|
|
|
} else { |
|
267
|
3 |
|
$sPrev = prev($aPath); |
|
268
|
3 |
|
$iPrevKey = array_search($sPrev, $aPath); |
|
269
|
|
|
} |
|
270
|
12 |
|
unset ($aPath[$iPrevKey]); |
|
271
|
12 |
|
break; |
|
272
|
38 |
|
case '.': |
|
273
|
37 |
|
case '': |
|
274
|
16 |
|
unset ($aPath[$iKey]); |
|
275
|
41 |
|
break; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
41 |
|
$sPath = ($bHasPrefixSlash ? '/' : ''); |
|
280
|
41 |
|
if (count($aPath) > 0) { |
|
281
|
30 |
|
$sPath .= implode('/', $aPath) . ($bHasSuffixSlash ? '/' : ''); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
41 |
|
return $sPath; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.