Http.php 28 KB

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