|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* URI.php - Jaxon request URI detector |
|
5
|
|
|
* |
|
6
|
|
|
* Detect and parse the URI of the Jaxon request being processed. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\Utils\Http; |
|
16
|
|
|
|
|
17
|
|
|
class URI |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
|
|
|
|
|
20
|
|
|
* Get the URL from the $_SERVER var |
|
21
|
|
|
* |
|
22
|
|
|
* @var array &$aURL The URL data |
|
23
|
|
|
* @var string $sKey The key in the $_SERVER array |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
private function getHostFromServer(array &$aURL, $sKey) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
if(empty($aURL['host']) && !empty($_SERVER[$sKey])) |
|
30
|
|
|
{ |
|
31
|
|
|
if(strpos($_SERVER[$sKey], ':') > 0) |
|
32
|
|
|
{ |
|
33
|
|
|
list($aURL['host'], $aURL['port']) = explode(':', $_SERVER[$sKey]); |
|
34
|
|
|
} |
|
35
|
|
|
else |
|
36
|
|
|
{ |
|
37
|
|
|
$aURL['host'] = $_SERVER[$sKey]; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Detect the URI of the current request |
|
44
|
|
|
* |
|
45
|
|
|
* @return string The URI |
|
46
|
|
|
*/ |
|
47
|
|
|
public function detect() |
|
48
|
|
|
{ |
|
49
|
|
|
$aURL = []; |
|
50
|
|
|
// Try to get the request URL |
|
51
|
|
|
if(!empty($_SERVER['REQUEST_URI'])) |
|
52
|
|
|
{ |
|
53
|
|
|
$_SERVER['REQUEST_URI'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $_SERVER['REQUEST_URI']); |
|
54
|
|
|
$aURL = parse_url($_SERVER['REQUEST_URI']); |
|
|
|
|
|
|
55
|
|
|
if(!is_array($aURL)) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$aURL = []; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// Fill in the empty values |
|
62
|
|
|
if(empty($aURL['scheme'])) |
|
63
|
|
|
{ |
|
64
|
|
|
if(!empty($_SERVER['HTTP_SCHEME'])) |
|
65
|
|
|
{ |
|
66
|
|
|
$aURL['scheme'] = $_SERVER['HTTP_SCHEME']; |
|
67
|
|
|
} |
|
68
|
|
|
else |
|
69
|
|
|
{ |
|
70
|
|
|
$aURL['scheme'] = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->getHostFromServer($aURL, 'HTTP_X_FORWARDED_HOST'); |
|
75
|
|
|
$this->getHostFromServer($aURL, 'HTTP_HOST'); |
|
76
|
|
|
$this->getHostFromServer($aURL, 'SERVER_NAME'); |
|
77
|
|
|
if(empty($aURL['host'])) |
|
78
|
|
|
{ |
|
79
|
|
|
throw new \Jaxon\Exception\URI(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if(empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) |
|
83
|
|
|
{ |
|
84
|
|
|
$aURL['port'] = $_SERVER['SERVER_PORT']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if(!empty($aURL['path']) && strlen(basename($aURL['path'])) == 0) |
|
88
|
|
|
{ |
|
89
|
|
|
unset($aURL['path']); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if(empty($aURL['path'])) |
|
93
|
|
|
{ |
|
94
|
|
|
if(!empty($_SERVER['PATH_INFO'])) |
|
95
|
|
|
{ |
|
96
|
|
|
$sPath = parse_url($_SERVER['PATH_INFO']); |
|
97
|
|
|
} |
|
98
|
|
|
else |
|
99
|
|
|
{ |
|
100
|
|
|
$sPath = parse_url($_SERVER['PHP_SELF']); |
|
101
|
|
|
} |
|
102
|
|
|
if(isset($sPath['path'])) |
|
103
|
|
|
{ |
|
104
|
|
|
$aURL['path'] = str_replace(['"', "'", '<', '>'], ['%22', '%27', '%3C', '%3E'], $sPath['path']); |
|
105
|
|
|
} |
|
106
|
|
|
unset($sPath); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if(empty($aURL['query'])) |
|
110
|
|
|
{ |
|
111
|
|
|
$aURL['query'] = empty($_SERVER['QUERY_STRING']) ? '' : $_SERVER['QUERY_STRING']; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if(!empty($aURL['query'])) |
|
115
|
|
|
{ |
|
116
|
|
|
$aURL['query'] = '?' . $aURL['query']; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// Build the URL: Start with scheme, user and pass |
|
120
|
|
|
$sURL = $aURL['scheme'] . '://'; |
|
121
|
|
|
if(!empty($aURL['user'])) |
|
122
|
|
|
{ |
|
123
|
|
|
$sURL .= $aURL['user']; |
|
124
|
|
|
if(!empty($aURL['pass'])) |
|
125
|
|
|
{ |
|
126
|
|
|
$sURL .= ':' . $aURL['pass']; |
|
127
|
|
|
} |
|
128
|
|
|
$sURL .= '@'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Add the host |
|
132
|
|
|
$sURL .= $aURL['host']; |
|
133
|
|
|
|
|
134
|
|
|
// Add the port if needed |
|
135
|
|
|
if(!empty($aURL['port']) && |
|
136
|
|
|
(($aURL['scheme'] == 'http' && $aURL['port'] != 80) || |
|
137
|
|
|
($aURL['scheme'] == 'https' && $aURL['port'] != 443))) |
|
138
|
|
|
{ |
|
139
|
|
|
$sURL .= ':' . $aURL['port']; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Add the path and the query string |
|
143
|
|
|
$sURL .= $aURL['path'] . $aURL['query']; |
|
144
|
|
|
|
|
145
|
|
|
// Clean up |
|
146
|
|
|
unset($aURL); |
|
147
|
|
|
|
|
148
|
|
|
$aURL = explode("?", $sURL); |
|
149
|
|
|
|
|
150
|
|
|
if(1 < count($aURL)) |
|
151
|
|
|
{ |
|
152
|
|
|
$aQueries = explode("&", $aURL[1]); |
|
153
|
|
|
|
|
154
|
|
|
foreach($aQueries as $sKey => $sQuery) |
|
155
|
|
|
{ |
|
156
|
|
|
if("jxnGenerate" == substr($sQuery, 0, 11)) |
|
157
|
|
|
{ |
|
158
|
|
|
unset($aQueries[$sKey]); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
$sQueries = implode("&", $aQueries); |
|
163
|
|
|
|
|
164
|
|
|
$aURL[1] = $sQueries; |
|
165
|
|
|
|
|
166
|
|
|
$sURL = implode("?", $aURL); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $sURL; |
|
170
|
|
|
} |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|