|
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
|
|
|
private static function getHostFromServer(array &$aURL, $sKey) |
|
26
|
|
|
{ |
|
27
|
|
|
if(empty($aURL['host']) && !empty($_SERVER[$sKey])) |
|
28
|
|
|
{ |
|
29
|
|
|
if(strpos($_SERVER[$sKey], ':') > 0) |
|
30
|
|
|
{ |
|
31
|
|
|
list($aURL['host'], $aURL['port']) = explode(':', $_SERVER[$sKey]); |
|
32
|
|
|
} |
|
33
|
|
|
else |
|
34
|
|
|
{ |
|
35
|
|
|
$aURL['host'] = $_SERVER[$sKey]; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Detect the URI of the current request |
|
42
|
|
|
* |
|
43
|
|
|
* @return string The URI |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function detect() |
|
46
|
|
|
{ |
|
47
|
|
|
$aURL = []; |
|
48
|
|
|
// Try to get the request URL |
|
49
|
|
|
if(!empty($_SERVER['REQUEST_URI'])) |
|
50
|
|
|
{ |
|
51
|
|
|
$_SERVER['REQUEST_URI'] = str_replace(['"',"'",'<','>'], ['%22','%27','%3C','%3E'], $_SERVER['REQUEST_URI']); |
|
52
|
|
|
$aURL = parse_url($_SERVER['REQUEST_URI']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
// Fill in the empty values |
|
56
|
|
|
if(empty($aURL['scheme'])) |
|
57
|
|
|
{ |
|
58
|
|
|
if(!empty($_SERVER['HTTP_SCHEME'])) |
|
59
|
|
|
{ |
|
60
|
|
|
$aURL['scheme'] = $_SERVER['HTTP_SCHEME']; |
|
61
|
|
|
} |
|
62
|
|
|
else |
|
63
|
|
|
{ |
|
64
|
|
|
$aURL['scheme'] = ((!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http'); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
self::getHostFromServer($aURL, 'HTTP_X_FORWARDED_HOST'); |
|
|
|
|
|
|
69
|
|
|
self::getHostFromServer($aURL, 'HTTP_HOST'); |
|
70
|
|
|
self::getHostFromServer($aURL, 'SERVER_NAME'); |
|
71
|
|
|
if(empty($aURL['host'])) |
|
72
|
|
|
{ |
|
73
|
|
|
throw new \Jaxon\Exception\URI(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if(empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) |
|
77
|
|
|
{ |
|
78
|
|
|
$aURL['port'] = $_SERVER['SERVER_PORT']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if(!empty($aURL['path']) && strlen(basename($aURL['path'])) == 0) |
|
82
|
|
|
{ |
|
83
|
|
|
unset($aURL['path']); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if(empty($aURL['path'])) |
|
87
|
|
|
{ |
|
88
|
|
|
if(!empty($_SERVER['PATH_INFO'])) |
|
89
|
|
|
{ |
|
90
|
|
|
$sPath = parse_url($_SERVER['PATH_INFO']); |
|
91
|
|
|
} |
|
92
|
|
|
else |
|
93
|
|
|
{ |
|
94
|
|
|
$sPath = parse_url($_SERVER['PHP_SELF']); |
|
95
|
|
|
} |
|
96
|
|
|
if(isset($sPath['path'])) |
|
97
|
|
|
{ |
|
98
|
|
|
$aURL['path'] = str_replace(['"',"'",'<','>'], ['%22','%27','%3C','%3E'], $sPath['path']); |
|
99
|
|
|
} |
|
100
|
|
|
unset($sPath); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if(empty($aURL['query']) && !empty($_SERVER['QUERY_STRING'])) |
|
104
|
|
|
{ |
|
105
|
|
|
$aURL['query'] = $_SERVER['QUERY_STRING']; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if(!empty($aURL['query'])) |
|
109
|
|
|
{ |
|
110
|
|
|
$aURL['query'] = '?'.$aURL['query']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Build the URL: Start with scheme, user and pass |
|
114
|
|
|
$sURL = $aURL['scheme'].'://'; |
|
115
|
|
|
if(!empty($aURL['user'])) |
|
116
|
|
|
{ |
|
117
|
|
|
$sURL.= $aURL['user']; |
|
118
|
|
|
if(!empty($aURL['pass'])) |
|
119
|
|
|
{ |
|
120
|
|
|
$sURL.= ':'.$aURL['pass']; |
|
121
|
|
|
} |
|
122
|
|
|
$sURL.= '@'; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
// Add the host |
|
126
|
|
|
$sURL.= $aURL['host']; |
|
127
|
|
|
|
|
128
|
|
|
// Add the port if needed |
|
129
|
|
|
if(!empty($aURL['port']) |
|
130
|
|
|
&& (($aURL['scheme'] == 'http' && $aURL['port'] != 80) |
|
131
|
|
|
|| ($aURL['scheme'] == 'https' && $aURL['port'] != 443))) |
|
132
|
|
|
{ |
|
133
|
|
|
$sURL.= ':'.$aURL['port']; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
// Add the path and the query string |
|
137
|
|
|
$sURL.= $aURL['path'].@$aURL['query']; |
|
138
|
|
|
|
|
139
|
|
|
// Clean up |
|
140
|
|
|
unset($aURL); |
|
141
|
|
|
|
|
142
|
|
|
$aURL = explode("?", $sURL); |
|
143
|
|
|
|
|
144
|
|
|
if(1 < count($aURL)) |
|
145
|
|
|
{ |
|
146
|
|
|
$aQueries = explode("&", $aURL[1]); |
|
147
|
|
|
|
|
148
|
|
|
foreach($aQueries as $sKey => $sQuery) |
|
149
|
|
|
{ |
|
150
|
|
|
if("jxnGenerate" == substr($sQuery, 0, 11)) |
|
151
|
|
|
unset($aQueries[$sKey]); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$sQueries = implode("&", $aQueries); |
|
155
|
|
|
|
|
156
|
|
|
$aURL[1] = $sQueries; |
|
157
|
|
|
|
|
158
|
|
|
$sURL = implode("?", $aURL); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $sURL; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check looks for type mismatches where the missing type is
false. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTimeobject or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalsebefore passing on the value to another function or method that may not be able to handle afalse.