1 <?php
2 3 4 5 6 7
8
9 namespace Cross\Http;
10
11 use Cross\Exception\FrontException;
12
13 14 15 16 17
18 class Request
19 {
20 private $baseUrl;
21 private $hostInfo;
22 private $scriptUrl;
23 private static $instance;
24
25 26 27 28 29
30 public static function getInstance()
31 {
32 if (!self::$instance) {
33 self::$instance = new self();
34 }
35
36 return self::$instance;
37 }
38
39 40 41 42 43 44
45 function getScriptUrl()
46 {
47 if (!$this->scriptUrl) {
48 $this->initScriptUrl();
49 }
50
51 return $this->scriptUrl;
52 }
53
54 55 56 57 58
59 function setBaseUrl($url)
60 {
61 $this->baseUrl = $url;
62 }
63
64 65 66 67 68 69 70
71 function getBaseUrl($absolute = false)
72 {
73 if ($this->baseUrl === null) {
74 $this->baseUrl = rtrim(dirname($this->getScriptUrl()), '\\/.');
75 }
76 return $absolute ? $this->getHostInfo() . $this->baseUrl : $this->baseUrl;
77 }
78
79 80 81 82 83
84 function getIndexName()
85 {
86 return basename($this->getScriptName());
87 }
88
89 90 91 92 93
94 function getHostInfo()
95 {
96 if (!$this->hostInfo) {
97 $this->initHostInfo();
98 }
99 return $this->hostInfo;
100 }
101
102 103 104 105 106 107
108 function getCurrentUrl($absolute = true)
109 {
110 if ($absolute) {
111 return $this->getHostInfo() . $this->SERVER('REQUEST_URI');
112 }
113
114 return $this->SERVER('REQUEST_URI');
115 }
116
117 118 119 120 121
122 function getServerPort()
123 {
124 return $this->SERVER('SERVER_PORT');
125 }
126
127 128 129 130 131 132
133 function getScriptFilePath()
134 {
135 if (($scriptName = $this->SERVER('SCRIPT_FILENAME')) == null) {
136 throw new FrontException('determine the entry script URL failed!!!');
137 }
138
139 return dirname($scriptName);
140 }
141
142 143 144
145 function getUserHost()
146 {
147 return $this->SERVER('REMOTE_HOST');
148 }
149
150 151 152
153 function getRequestURI()
154 {
155 return $this->SERVER('REQUEST_URI');
156 }
157
158 159 160
161 function getRequestType()
162 {
163 return $this->SERVER('REQUEST_METHOD');
164 }
165
166 167 168
169 function getPathInfo()
170 {
171 return $this->SERVER('PATH_INFO');
172 }
173
174 175 176
177 function getQueryString()
178 {
179 return $this->SERVER('QUERY_STRING');
180 }
181
182 183 184
185 function getScriptName()
186 {
187 return $this->SERVER('SCRIPT_NAME');
188 }
189
190 191 192 193 194
195 function getUrlReferrer()
196 {
197 return $this->SERVER('HTTP_REFERER');
198 }
199
200 201 202
203 function getUserAgent()
204 {
205 return $this->SERVER('HTTP_USER_AGENT');
206 }
207
208 209 210
211 function getAcceptTypes()
212 {
213 return $this->SERVER('HTTP_ACCEPT');
214 }
215
216 217 218 219 220
221 function isPutRequest()
222 {
223 return ($this->SERVER('REQUEST_METHOD')
224 && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'PUT')) || $this->isPutViaPostRequest();
225 }
226
227 228 229 230 231
232 function isPostRequest()
233 {
234 return $this->SERVER('REQUEST_METHOD') && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'POST');
235 }
236
237 238 239 240 241
242 function isGetRequest()
243 {
244 return $this->SERVER('REQUEST_METHOD') && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'GET');
245 }
246
247 248 249 250 251
252 function isDeleteRequest()
253 {
254 return $this->SERVER('REQUEST_METHOD') && !strcasecmp($this->SERVER('REQUEST_METHOD'), 'DELETE');
255 }
256
257 258 259 260 261
262 function isAjaxRequest()
263 {
264 return 0 === strcasecmp($this->SERVER('HTTP_X_REQUESTED_WITH'), 'XMLHttpRequest');
265 }
266
267 268 269 270 271
272 function isFlashRequest()
273 {
274 return stripos($this->SERVER('HTTP_USER_AGENT'), 'Shockwave') !== false
275 || stripos($this->SERVER('HTTP_USER_AGENT'), 'Flash') !== false;
276 }
277
278 279 280 281 282 283
284 function getClientIPAddress(array $env_keys = array())
285 {
286 static $ip = null;
287 if (null === $ip) {
288 if (empty($env_keys)) {
289 $env_keys = array(
290 'HTTP_CLIENT_IP',
291 'HTTP_CF_CONNECTING_IP',
292 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
293 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED',
294 'REMOTE_ADDR'
295 );
296 }
297
298 $ip = '0.0.0.0';
299 foreach ($env_keys as $env) {
300 $env_info = $this->SERVER($env);
301 if (!empty($env_info) && 0 !== strcasecmp($env_info, 'unknown')) {
302 $ips = explode(',', $env_info);
303 foreach ($ips as $ip) {
304 $ip = trim($ip);
305 if (false !== ip2long($ip)) {
306 break 2;
307 }
308 }
309 }
310 }
311 }
312
313 return $ip;
314 }
315
316 317 318 319 320 321
322 function SERVER($name)
323 {
324 return isset($_SERVER[$name]) ? $_SERVER[$name] : '';
325 }
326
327 328 329 330 331
332 protected function isPutViaPostRequest()
333 {
334 return isset($_POST['_method']) && !strcasecmp($_POST['_method'], 'PUT');
335 }
336
337 338 339 340 341
342 private function initScriptUrl()
343 {
344 if (($scriptName = $this->SERVER('SCRIPT_FILENAME')) == null) {
345 throw new FrontException('determine the entry script URL failed!!!');
346 }
347 $scriptName = basename($scriptName);
348 if (($_scriptName = $this->SERVER('SCRIPT_NAME')) != null && basename($_scriptName) === $scriptName) {
349 $this->scriptUrl = $_scriptName;
350 } elseif (($_scriptName = $this->SERVER('PHP_SELF')) != null && basename($_scriptName) === $scriptName) {
351 $this->scriptUrl = $_scriptName;
352 } elseif (($_scriptName = $this->SERVER('ORIG_SCRIPT_NAME')) != null && basename(
353 $_scriptName
354 ) === $scriptName
355 ) {
356 $this->scriptUrl = $_scriptName;
357 } elseif (($pos = strpos($this->SERVER('PHP_SELF'), '/' . $scriptName)) !== false) {
358 $this->scriptUrl = substr($this->SERVER('SCRIPT_NAME'), 0, $pos) . '/' . $scriptName;
359 } elseif (($_documentRoot = $this->SERVER('DOCUMENT_ROOT')) != null && ($_scriptName = $this->SERVER(
360 'SCRIPT_FILENAME'
361 )) != null && strpos($_scriptName, $_documentRoot) === 0
362 ) {
363 $this->scriptUrl = str_replace('\\', '/', str_replace($_documentRoot, '', $_scriptName));
364 } else {
365 throw new FrontException('determine the entry script URL failed!!');
366 }
367 }
368
369 370 371 372 373
374 private function initHostInfo()
375 {
376 if (PHP_SAPI === 'cli') {
377 return '';
378 }
379
380 $protocol = 'http';
381 if (strcasecmp($this->SERVER('HTTPS'), 'on') === 0) {
382 $protocol = 'https';
383 }
384
385 if (($host = $this->SERVER('HTTP_HOST')) != null) {
386 $this->hostInfo = $protocol . '://' . $host;
387 } elseif (($host = $this->SERVER('SERVER_NAME')) != null) {
388 $this->hostInfo = $protocol . '://' . $host;
389 $port = $this->getServerPort();
390 if (($protocol == 'http' && $port != 80) || ($protocol == 'https' && $port != 443)) {
391 $this->hostInfo .= ':' . $port;
392 }
393 }
394
395 return '';
396 }
397 }
398
399