|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* THttpRequest, THttpCookie, THttpCookieCollection, TUri class file |
|
5
|
|
|
* |
|
6
|
|
|
* @author Qiang Xue <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Web; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Exceptions\TInvalidDataValueException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* TUri class |
|
17
|
|
|
* |
|
18
|
|
|
* TUri represents a URI. Given a URI |
|
19
|
|
|
* http://joe:[email protected]:8080/path/to/script.php?param=value#anchor |
|
20
|
|
|
* it will be decomposed as follows, |
|
21
|
|
|
* - scheme: http |
|
22
|
|
|
* - host: example.com |
|
23
|
|
|
* - port: 8080 |
|
24
|
|
|
* - user: joe |
|
25
|
|
|
* - password: whatever |
|
26
|
|
|
* - path: /path/to/script.php |
|
27
|
|
|
* - query: param=value |
|
28
|
|
|
* - fragment: anchor |
|
29
|
|
|
* |
|
30
|
|
|
* @author Qiang Xue <[email protected]> |
|
31
|
|
|
* @since 3.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
class TUri extends \Prado\TComponent |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var array list of default ports for known schemes |
|
37
|
|
|
*/ |
|
38
|
|
|
private static $_defaultPort = [ |
|
|
|
|
|
|
39
|
|
|
'ftp' => 21, |
|
40
|
|
|
'gopher' => 70, |
|
41
|
|
|
'http' => 80, |
|
42
|
|
|
'https' => 443, |
|
43
|
|
|
'news' => 119, |
|
44
|
|
|
'nntp' => 119, |
|
45
|
|
|
'wais' => 210, |
|
46
|
|
|
'telnet' => 23, |
|
47
|
|
|
]; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var string scheme of the URI |
|
50
|
|
|
*/ |
|
51
|
|
|
private $_scheme; |
|
52
|
|
|
/** |
|
53
|
|
|
* @var string host name of the URI |
|
54
|
|
|
*/ |
|
55
|
|
|
private $_host; |
|
56
|
|
|
/** |
|
57
|
|
|
* @var int port of the URI |
|
58
|
|
|
*/ |
|
59
|
|
|
private $_port; |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string user of the URI |
|
62
|
|
|
*/ |
|
63
|
|
|
private $_user; |
|
64
|
|
|
/** |
|
65
|
|
|
* @var string password of the URI |
|
66
|
|
|
*/ |
|
67
|
|
|
private $_pass; |
|
68
|
|
|
/** |
|
69
|
|
|
* @var string path of the URI |
|
70
|
|
|
*/ |
|
71
|
|
|
private $_path; |
|
72
|
|
|
/** |
|
73
|
|
|
* @var string query string of the URI |
|
74
|
|
|
*/ |
|
75
|
|
|
private $_query; |
|
76
|
|
|
/** |
|
77
|
|
|
* @var string fragment of the URI |
|
78
|
|
|
*/ |
|
79
|
|
|
private $_fragment; |
|
80
|
|
|
/** |
|
81
|
|
|
* @var string the URI |
|
82
|
|
|
*/ |
|
83
|
|
|
private $_uri; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Constructor. |
|
87
|
|
|
* Decomposes the specified URI into parts. |
|
88
|
|
|
* @param string $uri URI to be represented |
|
89
|
|
|
* @throws TInvalidDataValueException if URI is of bad format |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct($uri) |
|
92
|
13 |
|
{ |
|
93
|
|
|
if (($ret = @parse_url($uri)) !== false) { |
|
94
|
13 |
|
// decoding??? |
|
95
|
|
|
$this->_scheme = $ret['scheme'] ?? ''; |
|
96
|
13 |
|
$this->_host = $ret['host'] ?? ''; |
|
97
|
13 |
|
$this->_port = $ret['port'] ?? ''; |
|
|
|
|
|
|
98
|
13 |
|
$this->_user = $ret['user'] ?? ''; |
|
99
|
13 |
|
$this->_pass = $ret['pass'] ?? ''; |
|
100
|
13 |
|
$this->_path = $ret['path'] ?? ''; |
|
101
|
13 |
|
$this->_query = $ret['query'] ?? ''; |
|
102
|
13 |
|
$this->_fragment = $ret['fragment'] ?? ''; |
|
103
|
13 |
|
$this->_uri = $uri; |
|
104
|
13 |
|
} else { |
|
105
|
|
|
throw new TInvalidDataValueException('uri_format_invalid', $uri); |
|
106
|
1 |
|
} |
|
107
|
|
|
parent::__construct(); |
|
108
|
13 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string URI |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function getUri() |
|
114
|
|
|
{ |
|
115
|
2 |
|
return $this->_uri; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string scheme of the URI, such as 'http', 'https', 'ftp', etc. |
|
120
|
|
|
*/ |
|
121
|
3 |
|
public function getScheme() |
|
122
|
|
|
{ |
|
123
|
3 |
|
return $this->_scheme; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string hostname of the URI |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function getHost() |
|
130
|
|
|
{ |
|
131
|
3 |
|
return $this->_host; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return int port number of the URI |
|
136
|
|
|
*/ |
|
137
|
3 |
|
public function getPort() |
|
138
|
|
|
{ |
|
139
|
3 |
|
return $this->_port; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string username of the URI |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function getUser() |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $this->_user; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return string password of the URI |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function getPassword() |
|
154
|
|
|
{ |
|
155
|
1 |
|
return $this->_pass; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @return string path of the URI |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function getPath() |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $this->_path; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string query string of the URI |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function getQuery() |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->_query; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string fragment of the URI |
|
176
|
|
|
*/ |
|
177
|
1 |
|
public function getFragment() |
|
178
|
|
|
{ |
|
179
|
1 |
|
return $this->_fragment; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|