|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Controller |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Http.php 23414 2010-11-20 10:56:11Z bittarman $ |
|
20 */ |
|
21 |
|
22 /** @see Zend_Controller_Request_Abstract */ |
|
23 require_once 'Zend/Controller/Request/Abstract.php'; |
|
24 |
|
25 /** @see Zend_Uri */ |
|
26 require_once 'Zend/Uri.php'; |
|
27 |
|
28 /** |
|
29 * Zend_Controller_Request_Http |
|
30 * |
|
31 * HTTP request object for use with Zend_Controller family. |
|
32 * |
|
33 * @uses Zend_Controller_Request_Abstract |
|
34 * @package Zend_Controller |
|
35 * @subpackage Request |
|
36 */ |
|
37 class Zend_Controller_Request_Http extends Zend_Controller_Request_Abstract |
|
38 { |
|
39 /** |
|
40 * Scheme for http |
|
41 * |
|
42 */ |
|
43 const SCHEME_HTTP = 'http'; |
|
44 |
|
45 /** |
|
46 * Scheme for https |
|
47 * |
|
48 */ |
|
49 const SCHEME_HTTPS = 'https'; |
|
50 |
|
51 /** |
|
52 * Allowed parameter sources |
|
53 * @var array |
|
54 */ |
|
55 protected $_paramSources = array('_GET', '_POST'); |
|
56 |
|
57 /** |
|
58 * REQUEST_URI |
|
59 * @var string; |
|
60 */ |
|
61 protected $_requestUri; |
|
62 |
|
63 /** |
|
64 * Base URL of request |
|
65 * @var string |
|
66 */ |
|
67 protected $_baseUrl = null; |
|
68 |
|
69 /** |
|
70 * Base path of request |
|
71 * @var string |
|
72 */ |
|
73 protected $_basePath = null; |
|
74 |
|
75 /** |
|
76 * PATH_INFO |
|
77 * @var string |
|
78 */ |
|
79 protected $_pathInfo = ''; |
|
80 |
|
81 /** |
|
82 * Instance parameters |
|
83 * @var array |
|
84 */ |
|
85 protected $_params = array(); |
|
86 |
|
87 /** |
|
88 * Raw request body |
|
89 * @var string|false |
|
90 */ |
|
91 protected $_rawBody; |
|
92 |
|
93 /** |
|
94 * Alias keys for request parameters |
|
95 * @var array |
|
96 */ |
|
97 protected $_aliases = array(); |
|
98 |
|
99 /** |
|
100 * Constructor |
|
101 * |
|
102 * If a $uri is passed, the object will attempt to populate itself using |
|
103 * that information. |
|
104 * |
|
105 * @param string|Zend_Uri $uri |
|
106 * @return void |
|
107 * @throws Zend_Controller_Request_Exception when invalid URI passed |
|
108 */ |
|
109 public function __construct($uri = null) |
|
110 { |
|
111 if (null !== $uri) { |
|
112 if (!$uri instanceof Zend_Uri) { |
|
113 $uri = Zend_Uri::factory($uri); |
|
114 } |
|
115 if ($uri->valid()) { |
|
116 $path = $uri->getPath(); |
|
117 $query = $uri->getQuery(); |
|
118 if (!empty($query)) { |
|
119 $path .= '?' . $query; |
|
120 } |
|
121 |
|
122 $this->setRequestUri($path); |
|
123 } else { |
|
124 require_once 'Zend/Controller/Request/Exception.php'; |
|
125 throw new Zend_Controller_Request_Exception('Invalid URI provided to constructor'); |
|
126 } |
|
127 } else { |
|
128 $this->setRequestUri(); |
|
129 } |
|
130 } |
|
131 |
|
132 /** |
|
133 * Access values contained in the superglobals as public members |
|
134 * Order of precedence: 1. GET, 2. POST, 3. COOKIE, 4. SERVER, 5. ENV |
|
135 * |
|
136 * @see http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx |
|
137 * @param string $key |
|
138 * @return mixed |
|
139 */ |
|
140 public function __get($key) |
|
141 { |
|
142 switch (true) { |
|
143 case isset($this->_params[$key]): |
|
144 return $this->_params[$key]; |
|
145 case isset($_GET[$key]): |
|
146 return $_GET[$key]; |
|
147 case isset($_POST[$key]): |
|
148 return $_POST[$key]; |
|
149 case isset($_COOKIE[$key]): |
|
150 return $_COOKIE[$key]; |
|
151 case ($key == 'REQUEST_URI'): |
|
152 return $this->getRequestUri(); |
|
153 case ($key == 'PATH_INFO'): |
|
154 return $this->getPathInfo(); |
|
155 case isset($_SERVER[$key]): |
|
156 return $_SERVER[$key]; |
|
157 case isset($_ENV[$key]): |
|
158 return $_ENV[$key]; |
|
159 default: |
|
160 return null; |
|
161 } |
|
162 } |
|
163 |
|
164 /** |
|
165 * Alias to __get |
|
166 * |
|
167 * @param string $key |
|
168 * @return mixed |
|
169 */ |
|
170 public function get($key) |
|
171 { |
|
172 return $this->__get($key); |
|
173 } |
|
174 |
|
175 /** |
|
176 * Set values |
|
177 * |
|
178 * In order to follow {@link __get()}, which operates on a number of |
|
179 * superglobals, setting values through overloading is not allowed and will |
|
180 * raise an exception. Use setParam() instead. |
|
181 * |
|
182 * @param string $key |
|
183 * @param mixed $value |
|
184 * @return void |
|
185 * @throws Zend_Controller_Request_Exception |
|
186 */ |
|
187 public function __set($key, $value) |
|
188 { |
|
189 require_once 'Zend/Controller/Request/Exception.php'; |
|
190 throw new Zend_Controller_Request_Exception('Setting values in superglobals not allowed; please use setParam()'); |
|
191 } |
|
192 |
|
193 /** |
|
194 * Alias to __set() |
|
195 * |
|
196 * @param string $key |
|
197 * @param mixed $value |
|
198 * @return void |
|
199 */ |
|
200 public function set($key, $value) |
|
201 { |
|
202 return $this->__set($key, $value); |
|
203 } |
|
204 |
|
205 /** |
|
206 * Check to see if a property is set |
|
207 * |
|
208 * @param string $key |
|
209 * @return boolean |
|
210 */ |
|
211 public function __isset($key) |
|
212 { |
|
213 switch (true) { |
|
214 case isset($this->_params[$key]): |
|
215 return true; |
|
216 case isset($_GET[$key]): |
|
217 return true; |
|
218 case isset($_POST[$key]): |
|
219 return true; |
|
220 case isset($_COOKIE[$key]): |
|
221 return true; |
|
222 case isset($_SERVER[$key]): |
|
223 return true; |
|
224 case isset($_ENV[$key]): |
|
225 return true; |
|
226 default: |
|
227 return false; |
|
228 } |
|
229 } |
|
230 |
|
231 /** |
|
232 * Alias to __isset() |
|
233 * |
|
234 * @param string $key |
|
235 * @return boolean |
|
236 */ |
|
237 public function has($key) |
|
238 { |
|
239 return $this->__isset($key); |
|
240 } |
|
241 |
|
242 /** |
|
243 * Set GET values |
|
244 * |
|
245 * @param string|array $spec |
|
246 * @param null|mixed $value |
|
247 * @return Zend_Controller_Request_Http |
|
248 */ |
|
249 public function setQuery($spec, $value = null) |
|
250 { |
|
251 if ((null === $value) && !is_array($spec)) { |
|
252 require_once 'Zend/Controller/Exception.php'; |
|
253 throw new Zend_Controller_Exception('Invalid value passed to setQuery(); must be either array of values or key/value pair'); |
|
254 } |
|
255 if ((null === $value) && is_array($spec)) { |
|
256 foreach ($spec as $key => $value) { |
|
257 $this->setQuery($key, $value); |
|
258 } |
|
259 return $this; |
|
260 } |
|
261 $_GET[(string) $spec] = $value; |
|
262 return $this; |
|
263 } |
|
264 |
|
265 /** |
|
266 * Retrieve a member of the $_GET superglobal |
|
267 * |
|
268 * If no $key is passed, returns the entire $_GET array. |
|
269 * |
|
270 * @todo How to retrieve from nested arrays |
|
271 * @param string $key |
|
272 * @param mixed $default Default value to use if key not found |
|
273 * @return mixed Returns null if key does not exist |
|
274 */ |
|
275 public function getQuery($key = null, $default = null) |
|
276 { |
|
277 if (null === $key) { |
|
278 return $_GET; |
|
279 } |
|
280 |
|
281 return (isset($_GET[$key])) ? $_GET[$key] : $default; |
|
282 } |
|
283 |
|
284 /** |
|
285 * Set POST values |
|
286 * |
|
287 * @param string|array $spec |
|
288 * @param null|mixed $value |
|
289 * @return Zend_Controller_Request_Http |
|
290 */ |
|
291 public function setPost($spec, $value = null) |
|
292 { |
|
293 if ((null === $value) && !is_array($spec)) { |
|
294 require_once 'Zend/Controller/Exception.php'; |
|
295 throw new Zend_Controller_Exception('Invalid value passed to setPost(); must be either array of values or key/value pair'); |
|
296 } |
|
297 if ((null === $value) && is_array($spec)) { |
|
298 foreach ($spec as $key => $value) { |
|
299 $this->setPost($key, $value); |
|
300 } |
|
301 return $this; |
|
302 } |
|
303 $_POST[(string) $spec] = $value; |
|
304 return $this; |
|
305 } |
|
306 |
|
307 /** |
|
308 * Retrieve a member of the $_POST superglobal |
|
309 * |
|
310 * If no $key is passed, returns the entire $_POST array. |
|
311 * |
|
312 * @todo How to retrieve from nested arrays |
|
313 * @param string $key |
|
314 * @param mixed $default Default value to use if key not found |
|
315 * @return mixed Returns null if key does not exist |
|
316 */ |
|
317 public function getPost($key = null, $default = null) |
|
318 { |
|
319 if (null === $key) { |
|
320 return $_POST; |
|
321 } |
|
322 |
|
323 return (isset($_POST[$key])) ? $_POST[$key] : $default; |
|
324 } |
|
325 |
|
326 /** |
|
327 * Retrieve a member of the $_COOKIE superglobal |
|
328 * |
|
329 * If no $key is passed, returns the entire $_COOKIE array. |
|
330 * |
|
331 * @todo How to retrieve from nested arrays |
|
332 * @param string $key |
|
333 * @param mixed $default Default value to use if key not found |
|
334 * @return mixed Returns null if key does not exist |
|
335 */ |
|
336 public function getCookie($key = null, $default = null) |
|
337 { |
|
338 if (null === $key) { |
|
339 return $_COOKIE; |
|
340 } |
|
341 |
|
342 return (isset($_COOKIE[$key])) ? $_COOKIE[$key] : $default; |
|
343 } |
|
344 |
|
345 /** |
|
346 * Retrieve a member of the $_SERVER superglobal |
|
347 * |
|
348 * If no $key is passed, returns the entire $_SERVER array. |
|
349 * |
|
350 * @param string $key |
|
351 * @param mixed $default Default value to use if key not found |
|
352 * @return mixed Returns null if key does not exist |
|
353 */ |
|
354 public function getServer($key = null, $default = null) |
|
355 { |
|
356 if (null === $key) { |
|
357 return $_SERVER; |
|
358 } |
|
359 |
|
360 return (isset($_SERVER[$key])) ? $_SERVER[$key] : $default; |
|
361 } |
|
362 |
|
363 /** |
|
364 * Retrieve a member of the $_ENV superglobal |
|
365 * |
|
366 * If no $key is passed, returns the entire $_ENV array. |
|
367 * |
|
368 * @param string $key |
|
369 * @param mixed $default Default value to use if key not found |
|
370 * @return mixed Returns null if key does not exist |
|
371 */ |
|
372 public function getEnv($key = null, $default = null) |
|
373 { |
|
374 if (null === $key) { |
|
375 return $_ENV; |
|
376 } |
|
377 |
|
378 return (isset($_ENV[$key])) ? $_ENV[$key] : $default; |
|
379 } |
|
380 |
|
381 /** |
|
382 * Set the REQUEST_URI on which the instance operates |
|
383 * |
|
384 * If no request URI is passed, uses the value in $_SERVER['REQUEST_URI'], |
|
385 * $_SERVER['HTTP_X_REWRITE_URL'], or $_SERVER['ORIG_PATH_INFO'] + $_SERVER['QUERY_STRING']. |
|
386 * |
|
387 * @param string $requestUri |
|
388 * @return Zend_Controller_Request_Http |
|
389 */ |
|
390 public function setRequestUri($requestUri = null) |
|
391 { |
|
392 if ($requestUri === null) { |
|
393 if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch |
|
394 $requestUri = $_SERVER['HTTP_X_REWRITE_URL']; |
|
395 } elseif ( |
|
396 // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem) |
|
397 isset($_SERVER['IIS_WasUrlRewritten']) |
|
398 && $_SERVER['IIS_WasUrlRewritten'] == '1' |
|
399 && isset($_SERVER['UNENCODED_URL']) |
|
400 && $_SERVER['UNENCODED_URL'] != '' |
|
401 ) { |
|
402 $requestUri = $_SERVER['UNENCODED_URL']; |
|
403 } elseif (isset($_SERVER['REQUEST_URI'])) { |
|
404 $requestUri = $_SERVER['REQUEST_URI']; |
|
405 // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path |
|
406 $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost(); |
|
407 if (strpos($requestUri, $schemeAndHttpHost) === 0) { |
|
408 $requestUri = substr($requestUri, strlen($schemeAndHttpHost)); |
|
409 } |
|
410 } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI |
|
411 $requestUri = $_SERVER['ORIG_PATH_INFO']; |
|
412 if (!empty($_SERVER['QUERY_STRING'])) { |
|
413 $requestUri .= '?' . $_SERVER['QUERY_STRING']; |
|
414 } |
|
415 } else { |
|
416 return $this; |
|
417 } |
|
418 } elseif (!is_string($requestUri)) { |
|
419 return $this; |
|
420 } else { |
|
421 // Set GET items, if available |
|
422 if (false !== ($pos = strpos($requestUri, '?'))) { |
|
423 // Get key => value pairs and set $_GET |
|
424 $query = substr($requestUri, $pos + 1); |
|
425 parse_str($query, $vars); |
|
426 $this->setQuery($vars); |
|
427 } |
|
428 } |
|
429 |
|
430 $this->_requestUri = $requestUri; |
|
431 return $this; |
|
432 } |
|
433 |
|
434 /** |
|
435 * Returns the REQUEST_URI taking into account |
|
436 * platform differences between Apache and IIS |
|
437 * |
|
438 * @return string |
|
439 */ |
|
440 public function getRequestUri() |
|
441 { |
|
442 if (empty($this->_requestUri)) { |
|
443 $this->setRequestUri(); |
|
444 } |
|
445 |
|
446 return $this->_requestUri; |
|
447 } |
|
448 |
|
449 /** |
|
450 * Set the base URL of the request; i.e., the segment leading to the script name |
|
451 * |
|
452 * E.g.: |
|
453 * - /admin |
|
454 * - /myapp |
|
455 * - /subdir/index.php |
|
456 * |
|
457 * Do not use the full URI when providing the base. The following are |
|
458 * examples of what not to use: |
|
459 * - http://example.com/admin (should be just /admin) |
|
460 * - http://example.com/subdir/index.php (should be just /subdir/index.php) |
|
461 * |
|
462 * If no $baseUrl is provided, attempts to determine the base URL from the |
|
463 * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and |
|
464 * ORIG_SCRIPT_NAME in its determination. |
|
465 * |
|
466 * @param mixed $baseUrl |
|
467 * @return Zend_Controller_Request_Http |
|
468 */ |
|
469 public function setBaseUrl($baseUrl = null) |
|
470 { |
|
471 if ((null !== $baseUrl) && !is_string($baseUrl)) { |
|
472 return $this; |
|
473 } |
|
474 |
|
475 if ($baseUrl === null) { |
|
476 $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : ''; |
|
477 |
|
478 if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) { |
|
479 $baseUrl = $_SERVER['SCRIPT_NAME']; |
|
480 } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) { |
|
481 $baseUrl = $_SERVER['PHP_SELF']; |
|
482 } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) { |
|
483 $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility |
|
484 } else { |
|
485 // Backtrack up the script_filename to find the portion matching |
|
486 // php_self |
|
487 $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : ''; |
|
488 $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : ''; |
|
489 $segs = explode('/', trim($file, '/')); |
|
490 $segs = array_reverse($segs); |
|
491 $index = 0; |
|
492 $last = count($segs); |
|
493 $baseUrl = ''; |
|
494 do { |
|
495 $seg = $segs[$index]; |
|
496 $baseUrl = '/' . $seg . $baseUrl; |
|
497 ++$index; |
|
498 } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos)); |
|
499 } |
|
500 |
|
501 // Does the baseUrl have anything in common with the request_uri? |
|
502 $requestUri = $this->getRequestUri(); |
|
503 |
|
504 if (0 === strpos($requestUri, $baseUrl)) { |
|
505 // full $baseUrl matches |
|
506 $this->_baseUrl = $baseUrl; |
|
507 return $this; |
|
508 } |
|
509 |
|
510 if (0 === strpos($requestUri, dirname($baseUrl))) { |
|
511 // directory portion of $baseUrl matches |
|
512 $this->_baseUrl = rtrim(dirname($baseUrl), '/'); |
|
513 return $this; |
|
514 } |
|
515 |
|
516 $truncatedRequestUri = $requestUri; |
|
517 if (($pos = strpos($requestUri, '?')) !== false) { |
|
518 $truncatedRequestUri = substr($requestUri, 0, $pos); |
|
519 } |
|
520 |
|
521 $basename = basename($baseUrl); |
|
522 if (empty($basename) || !strpos($truncatedRequestUri, $basename)) { |
|
523 // no match whatsoever; set it blank |
|
524 $this->_baseUrl = ''; |
|
525 return $this; |
|
526 } |
|
527 |
|
528 // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
529 // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
530 // from PATH_INFO or QUERY_STRING |
|
531 if ((strlen($requestUri) >= strlen($baseUrl)) |
|
532 && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0))) |
|
533 { |
|
534 $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
535 } |
|
536 } |
|
537 |
|
538 $this->_baseUrl = rtrim($baseUrl, '/'); |
|
539 return $this; |
|
540 } |
|
541 |
|
542 /** |
|
543 * Everything in REQUEST_URI before PATH_INFO |
|
544 * <form action="<?=$baseUrl?>/news/submit" method="POST"/> |
|
545 * |
|
546 * @return string |
|
547 */ |
|
548 public function getBaseUrl() |
|
549 { |
|
550 if (null === $this->_baseUrl) { |
|
551 $this->setBaseUrl(); |
|
552 } |
|
553 |
|
554 return urldecode($this->_baseUrl); |
|
555 } |
|
556 |
|
557 /** |
|
558 * Set the base path for the URL |
|
559 * |
|
560 * @param string|null $basePath |
|
561 * @return Zend_Controller_Request_Http |
|
562 */ |
|
563 public function setBasePath($basePath = null) |
|
564 { |
|
565 if ($basePath === null) { |
|
566 $filename = (isset($_SERVER['SCRIPT_FILENAME'])) |
|
567 ? basename($_SERVER['SCRIPT_FILENAME']) |
|
568 : ''; |
|
569 |
|
570 $baseUrl = $this->getBaseUrl(); |
|
571 if (empty($baseUrl)) { |
|
572 $this->_basePath = ''; |
|
573 return $this; |
|
574 } |
|
575 |
|
576 if (basename($baseUrl) === $filename) { |
|
577 $basePath = dirname($baseUrl); |
|
578 } else { |
|
579 $basePath = $baseUrl; |
|
580 } |
|
581 } |
|
582 |
|
583 if (substr(PHP_OS, 0, 3) === 'WIN') { |
|
584 $basePath = str_replace('\\', '/', $basePath); |
|
585 } |
|
586 |
|
587 $this->_basePath = rtrim($basePath, '/'); |
|
588 return $this; |
|
589 } |
|
590 |
|
591 /** |
|
592 * Everything in REQUEST_URI before PATH_INFO not including the filename |
|
593 * <img src="<?=$basePath?>/images/zend.png"/> |
|
594 * |
|
595 * @return string |
|
596 */ |
|
597 public function getBasePath() |
|
598 { |
|
599 if (null === $this->_basePath) { |
|
600 $this->setBasePath(); |
|
601 } |
|
602 |
|
603 return $this->_basePath; |
|
604 } |
|
605 |
|
606 /** |
|
607 * Set the PATH_INFO string |
|
608 * |
|
609 * @param string|null $pathInfo |
|
610 * @return Zend_Controller_Request_Http |
|
611 */ |
|
612 public function setPathInfo($pathInfo = null) |
|
613 { |
|
614 if ($pathInfo === null) { |
|
615 $baseUrl = $this->getBaseUrl(); |
|
616 |
|
617 if (null === ($requestUri = $this->getRequestUri())) { |
|
618 return $this; |
|
619 } |
|
620 |
|
621 // Remove the query string from REQUEST_URI |
|
622 if ($pos = strpos($requestUri, '?')) { |
|
623 $requestUri = substr($requestUri, 0, $pos); |
|
624 } |
|
625 |
|
626 $requestUri = urldecode($requestUri); |
|
627 |
|
628 if (null !== $baseUrl |
|
629 && ((!empty($baseUrl) && 0 === strpos($requestUri, $baseUrl)) |
|
630 || empty($baseUrl)) |
|
631 && false === ($pathInfo = substr($requestUri, strlen($baseUrl))) |
|
632 ){ |
|
633 // If substr() returns false then PATH_INFO is set to an empty string |
|
634 $pathInfo = ''; |
|
635 } elseif (null === $baseUrl |
|
636 || (!empty($baseUrl) && false === strpos($requestUri, $baseUrl)) |
|
637 ) { |
|
638 $pathInfo = $requestUri; |
|
639 } |
|
640 } |
|
641 |
|
642 $this->_pathInfo = (string) $pathInfo; |
|
643 return $this; |
|
644 } |
|
645 |
|
646 /** |
|
647 * Returns everything between the BaseUrl and QueryString. |
|
648 * This value is calculated instead of reading PATH_INFO |
|
649 * directly from $_SERVER due to cross-platform differences. |
|
650 * |
|
651 * @return string |
|
652 */ |
|
653 public function getPathInfo() |
|
654 { |
|
655 if (empty($this->_pathInfo)) { |
|
656 $this->setPathInfo(); |
|
657 } |
|
658 |
|
659 return $this->_pathInfo; |
|
660 } |
|
661 |
|
662 /** |
|
663 * Set allowed parameter sources |
|
664 * |
|
665 * Can be empty array, or contain one or more of '_GET' or '_POST'. |
|
666 * |
|
667 * @param array $paramSoures |
|
668 * @return Zend_Controller_Request_Http |
|
669 */ |
|
670 public function setParamSources(array $paramSources = array()) |
|
671 { |
|
672 $this->_paramSources = $paramSources; |
|
673 return $this; |
|
674 } |
|
675 |
|
676 /** |
|
677 * Get list of allowed parameter sources |
|
678 * |
|
679 * @return array |
|
680 */ |
|
681 public function getParamSources() |
|
682 { |
|
683 return $this->_paramSources; |
|
684 } |
|
685 |
|
686 /** |
|
687 * Set a userland parameter |
|
688 * |
|
689 * Uses $key to set a userland parameter. If $key is an alias, the actual |
|
690 * key will be retrieved and used to set the parameter. |
|
691 * |
|
692 * @param mixed $key |
|
693 * @param mixed $value |
|
694 * @return Zend_Controller_Request_Http |
|
695 */ |
|
696 public function setParam($key, $value) |
|
697 { |
|
698 $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key; |
|
699 parent::setParam($key, $value); |
|
700 return $this; |
|
701 } |
|
702 |
|
703 /** |
|
704 * Retrieve a parameter |
|
705 * |
|
706 * Retrieves a parameter from the instance. Priority is in the order of |
|
707 * userland parameters (see {@link setParam()}), $_GET, $_POST. If a |
|
708 * parameter matching the $key is not found, null is returned. |
|
709 * |
|
710 * If the $key is an alias, the actual key aliased will be used. |
|
711 * |
|
712 * @param mixed $key |
|
713 * @param mixed $default Default value to use if key not found |
|
714 * @return mixed |
|
715 */ |
|
716 public function getParam($key, $default = null) |
|
717 { |
|
718 $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key; |
|
719 |
|
720 $paramSources = $this->getParamSources(); |
|
721 if (isset($this->_params[$keyName])) { |
|
722 return $this->_params[$keyName]; |
|
723 } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) { |
|
724 return $_GET[$keyName]; |
|
725 } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) { |
|
726 return $_POST[$keyName]; |
|
727 } |
|
728 |
|
729 return $default; |
|
730 } |
|
731 |
|
732 /** |
|
733 * Retrieve an array of parameters |
|
734 * |
|
735 * Retrieves a merged array of parameters, with precedence of userland |
|
736 * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the |
|
737 * userland params will take precedence over all others). |
|
738 * |
|
739 * @return array |
|
740 */ |
|
741 public function getParams() |
|
742 { |
|
743 $return = $this->_params; |
|
744 $paramSources = $this->getParamSources(); |
|
745 if (in_array('_GET', $paramSources) |
|
746 && isset($_GET) |
|
747 && is_array($_GET) |
|
748 ) { |
|
749 $return += $_GET; |
|
750 } |
|
751 if (in_array('_POST', $paramSources) |
|
752 && isset($_POST) |
|
753 && is_array($_POST) |
|
754 ) { |
|
755 $return += $_POST; |
|
756 } |
|
757 return $return; |
|
758 } |
|
759 |
|
760 /** |
|
761 * Set parameters |
|
762 * |
|
763 * Set one or more parameters. Parameters are set as userland parameters, |
|
764 * using the keys specified in the array. |
|
765 * |
|
766 * @param array $params |
|
767 * @return Zend_Controller_Request_Http |
|
768 */ |
|
769 public function setParams(array $params) |
|
770 { |
|
771 foreach ($params as $key => $value) { |
|
772 $this->setParam($key, $value); |
|
773 } |
|
774 return $this; |
|
775 } |
|
776 |
|
777 /** |
|
778 * Set a key alias |
|
779 * |
|
780 * Set an alias used for key lookups. $name specifies the alias, $target |
|
781 * specifies the actual key to use. |
|
782 * |
|
783 * @param string $name |
|
784 * @param string $target |
|
785 * @return Zend_Controller_Request_Http |
|
786 */ |
|
787 public function setAlias($name, $target) |
|
788 { |
|
789 $this->_aliases[$name] = $target; |
|
790 return $this; |
|
791 } |
|
792 |
|
793 /** |
|
794 * Retrieve an alias |
|
795 * |
|
796 * Retrieve the actual key represented by the alias $name. |
|
797 * |
|
798 * @param string $name |
|
799 * @return string|null Returns null when no alias exists |
|
800 */ |
|
801 public function getAlias($name) |
|
802 { |
|
803 if (isset($this->_aliases[$name])) { |
|
804 return $this->_aliases[$name]; |
|
805 } |
|
806 |
|
807 return null; |
|
808 } |
|
809 |
|
810 /** |
|
811 * Retrieve the list of all aliases |
|
812 * |
|
813 * @return array |
|
814 */ |
|
815 public function getAliases() |
|
816 { |
|
817 return $this->_aliases; |
|
818 } |
|
819 |
|
820 /** |
|
821 * Return the method by which the request was made |
|
822 * |
|
823 * @return string |
|
824 */ |
|
825 public function getMethod() |
|
826 { |
|
827 return $this->getServer('REQUEST_METHOD'); |
|
828 } |
|
829 |
|
830 /** |
|
831 * Was the request made by POST? |
|
832 * |
|
833 * @return boolean |
|
834 */ |
|
835 public function isPost() |
|
836 { |
|
837 if ('POST' == $this->getMethod()) { |
|
838 return true; |
|
839 } |
|
840 |
|
841 return false; |
|
842 } |
|
843 |
|
844 /** |
|
845 * Was the request made by GET? |
|
846 * |
|
847 * @return boolean |
|
848 */ |
|
849 public function isGet() |
|
850 { |
|
851 if ('GET' == $this->getMethod()) { |
|
852 return true; |
|
853 } |
|
854 |
|
855 return false; |
|
856 } |
|
857 |
|
858 /** |
|
859 * Was the request made by PUT? |
|
860 * |
|
861 * @return boolean |
|
862 */ |
|
863 public function isPut() |
|
864 { |
|
865 if ('PUT' == $this->getMethod()) { |
|
866 return true; |
|
867 } |
|
868 |
|
869 return false; |
|
870 } |
|
871 |
|
872 /** |
|
873 * Was the request made by DELETE? |
|
874 * |
|
875 * @return boolean |
|
876 */ |
|
877 public function isDelete() |
|
878 { |
|
879 if ('DELETE' == $this->getMethod()) { |
|
880 return true; |
|
881 } |
|
882 |
|
883 return false; |
|
884 } |
|
885 |
|
886 /** |
|
887 * Was the request made by HEAD? |
|
888 * |
|
889 * @return boolean |
|
890 */ |
|
891 public function isHead() |
|
892 { |
|
893 if ('HEAD' == $this->getMethod()) { |
|
894 return true; |
|
895 } |
|
896 |
|
897 return false; |
|
898 } |
|
899 |
|
900 /** |
|
901 * Was the request made by OPTIONS? |
|
902 * |
|
903 * @return boolean |
|
904 */ |
|
905 public function isOptions() |
|
906 { |
|
907 if ('OPTIONS' == $this->getMethod()) { |
|
908 return true; |
|
909 } |
|
910 |
|
911 return false; |
|
912 } |
|
913 |
|
914 /** |
|
915 * Is the request a Javascript XMLHttpRequest? |
|
916 * |
|
917 * Should work with Prototype/Script.aculo.us, possibly others. |
|
918 * |
|
919 * @return boolean |
|
920 */ |
|
921 public function isXmlHttpRequest() |
|
922 { |
|
923 return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest'); |
|
924 } |
|
925 |
|
926 /** |
|
927 * Is this a Flash request? |
|
928 * |
|
929 * @return boolean |
|
930 */ |
|
931 public function isFlashRequest() |
|
932 { |
|
933 $header = strtolower($this->getHeader('USER_AGENT')); |
|
934 return (strstr($header, ' flash')) ? true : false; |
|
935 } |
|
936 |
|
937 /** |
|
938 * Is https secure request |
|
939 * |
|
940 * @return boolean |
|
941 */ |
|
942 public function isSecure() |
|
943 { |
|
944 return ($this->getScheme() === self::SCHEME_HTTPS); |
|
945 } |
|
946 |
|
947 /** |
|
948 * Return the raw body of the request, if present |
|
949 * |
|
950 * @return string|false Raw body, or false if not present |
|
951 */ |
|
952 public function getRawBody() |
|
953 { |
|
954 if (null === $this->_rawBody) { |
|
955 $body = file_get_contents('php://input'); |
|
956 |
|
957 if (strlen(trim($body)) > 0) { |
|
958 $this->_rawBody = $body; |
|
959 } else { |
|
960 $this->_rawBody = false; |
|
961 } |
|
962 } |
|
963 return $this->_rawBody; |
|
964 } |
|
965 |
|
966 /** |
|
967 * Return the value of the given HTTP header. Pass the header name as the |
|
968 * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the |
|
969 * Accept header, 'Accept-Encoding' to get the Accept-Encoding header. |
|
970 * |
|
971 * @param string $header HTTP header name |
|
972 * @return string|false HTTP header value, or false if not found |
|
973 * @throws Zend_Controller_Request_Exception |
|
974 */ |
|
975 public function getHeader($header) |
|
976 { |
|
977 if (empty($header)) { |
|
978 require_once 'Zend/Controller/Request/Exception.php'; |
|
979 throw new Zend_Controller_Request_Exception('An HTTP header name is required'); |
|
980 } |
|
981 |
|
982 // Try to get it from the $_SERVER array first |
|
983 $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); |
|
984 if (isset($_SERVER[$temp])) { |
|
985 return $_SERVER[$temp]; |
|
986 } |
|
987 |
|
988 // This seems to be the only way to get the Authorization header on |
|
989 // Apache |
|
990 if (function_exists('apache_request_headers')) { |
|
991 $headers = apache_request_headers(); |
|
992 if (isset($headers[$header])) { |
|
993 return $headers[$header]; |
|
994 } |
|
995 $header = strtolower($header); |
|
996 foreach ($headers as $key => $value) { |
|
997 if (strtolower($key) == $header) { |
|
998 return $value; |
|
999 } |
|
1000 } |
|
1001 } |
|
1002 |
|
1003 return false; |
|
1004 } |
|
1005 |
|
1006 /** |
|
1007 * Get the request URI scheme |
|
1008 * |
|
1009 * @return string |
|
1010 */ |
|
1011 public function getScheme() |
|
1012 { |
|
1013 return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP; |
|
1014 } |
|
1015 |
|
1016 /** |
|
1017 * Get the HTTP host. |
|
1018 * |
|
1019 * "Host" ":" host [ ":" port ] ; Section 3.2.2 |
|
1020 * Note the HTTP Host header is not the same as the URI host. |
|
1021 * It includes the port while the URI host doesn't. |
|
1022 * |
|
1023 * @return string |
|
1024 */ |
|
1025 public function getHttpHost() |
|
1026 { |
|
1027 $host = $this->getServer('HTTP_HOST'); |
|
1028 if (!empty($host)) { |
|
1029 return $host; |
|
1030 } |
|
1031 |
|
1032 $scheme = $this->getScheme(); |
|
1033 $name = $this->getServer('SERVER_NAME'); |
|
1034 $port = $this->getServer('SERVER_PORT'); |
|
1035 |
|
1036 if(null === $name) { |
|
1037 return ''; |
|
1038 } |
|
1039 elseif (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) { |
|
1040 return $name; |
|
1041 } else { |
|
1042 return $name . ':' . $port; |
|
1043 } |
|
1044 } |
|
1045 |
|
1046 /** |
|
1047 * Get the client's IP addres |
|
1048 * |
|
1049 * @param boolean $checkProxy |
|
1050 * @return string |
|
1051 */ |
|
1052 public function getClientIp($checkProxy = true) |
|
1053 { |
|
1054 if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) { |
|
1055 $ip = $this->getServer('HTTP_CLIENT_IP'); |
|
1056 } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) { |
|
1057 $ip = $this->getServer('HTTP_X_FORWARDED_FOR'); |
|
1058 } else { |
|
1059 $ip = $this->getServer('REMOTE_ADDR'); |
|
1060 } |
|
1061 |
|
1062 return $ip; |
|
1063 } |
|
1064 } |