2
0

Http.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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$
  20. */
  21. /** @see Zend_Controller_Request_Abstract */
  22. require_once 'Zend/Controller/Request/Abstract.php';
  23. /** @see 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 (
  363. // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
  364. isset($_SERVER['IIS_WasUrlRewritten'])
  365. && $_SERVER['IIS_WasUrlRewritten'] == '1'
  366. && isset($_SERVER['UNENCODED_URL'])
  367. && $_SERVER['UNENCODED_URL'] != ''
  368. ) {
  369. $requestUri = $_SERVER['UNENCODED_URL'];
  370. } elseif (isset($_SERVER['REQUEST_URI'])) {
  371. $requestUri = $_SERVER['REQUEST_URI'];
  372. // Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
  373. $schemeAndHttpHost = $this->getScheme() . '://' . $this->getHttpHost();
  374. if (strpos($requestUri, $schemeAndHttpHost) === 0) {
  375. $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
  376. }
  377. } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
  378. $requestUri = $_SERVER['ORIG_PATH_INFO'];
  379. if (!empty($_SERVER['QUERY_STRING'])) {
  380. $requestUri .= '?' . $_SERVER['QUERY_STRING'];
  381. }
  382. } else {
  383. return $this;
  384. }
  385. } elseif (!is_string($requestUri)) {
  386. return $this;
  387. } else {
  388. // Set GET items, if available
  389. if (false !== ($pos = strpos($requestUri, '?'))) {
  390. // Get key => value pairs and set $_GET
  391. $query = substr($requestUri, $pos + 1);
  392. parse_str($query, $vars);
  393. $this->setQuery($vars);
  394. }
  395. }
  396. $this->_requestUri = $requestUri;
  397. return $this;
  398. }
  399. /**
  400. * Returns the REQUEST_URI taking into account
  401. * platform differences between Apache and IIS
  402. *
  403. * @return string
  404. */
  405. public function getRequestUri()
  406. {
  407. if (empty($this->_requestUri)) {
  408. $this->setRequestUri();
  409. }
  410. return $this->_requestUri;
  411. }
  412. /**
  413. * Set the base URL of the request; i.e., the segment leading to the script name
  414. *
  415. * E.g.:
  416. * - /admin
  417. * - /myapp
  418. * - /subdir/index.php
  419. *
  420. * Do not use the full URI when providing the base. The following are
  421. * examples of what not to use:
  422. * - http://example.com/admin (should be just /admin)
  423. * - http://example.com/subdir/index.php (should be just /subdir/index.php)
  424. *
  425. * If no $baseUrl is provided, attempts to determine the base URL from the
  426. * environment, using SCRIPT_FILENAME, SCRIPT_NAME, PHP_SELF, and
  427. * ORIG_SCRIPT_NAME in its determination.
  428. *
  429. * @param mixed $baseUrl
  430. * @return Zend_Controller_Request_Http
  431. */
  432. public function setBaseUrl($baseUrl = null)
  433. {
  434. if ((null !== $baseUrl) && !is_string($baseUrl)) {
  435. return $this;
  436. }
  437. if ($baseUrl === null) {
  438. $filename = (isset($_SERVER['SCRIPT_FILENAME'])) ? basename($_SERVER['SCRIPT_FILENAME']) : '';
  439. if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
  440. $baseUrl = $_SERVER['SCRIPT_NAME'];
  441. } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
  442. $baseUrl = $_SERVER['PHP_SELF'];
  443. } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
  444. $baseUrl = $_SERVER['ORIG_SCRIPT_NAME']; // 1and1 shared hosting compatibility
  445. } else {
  446. // Backtrack up the script_filename to find the portion matching
  447. // php_self
  448. $path = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
  449. $file = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
  450. $segs = explode('/', trim($file, '/'));
  451. $segs = array_reverse($segs);
  452. $index = 0;
  453. $last = count($segs);
  454. $baseUrl = '';
  455. do {
  456. $seg = $segs[$index];
  457. $baseUrl = '/' . $seg . $baseUrl;
  458. ++$index;
  459. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  460. }
  461. // Does the baseUrl have anything in common with the request_uri?
  462. $requestUri = $this->getRequestUri();
  463. if (0 === strpos($requestUri, $baseUrl)) {
  464. // full $baseUrl matches
  465. $this->_baseUrl = $baseUrl;
  466. return $this;
  467. }
  468. if (0 === strpos($requestUri, dirname($baseUrl))) {
  469. // directory portion of $baseUrl matches
  470. $this->_baseUrl = rtrim(dirname($baseUrl), '/');
  471. return $this;
  472. }
  473. $truncatedRequestUri = $requestUri;
  474. if (($pos = strpos($requestUri, '?')) !== false) {
  475. $truncatedRequestUri = substr($requestUri, 0, $pos);
  476. }
  477. $basename = basename($baseUrl);
  478. if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
  479. // no match whatsoever; set it blank
  480. $this->_baseUrl = '';
  481. return $this;
  482. }
  483. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  484. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  485. // from PATH_INFO or QUERY_STRING
  486. if ((strlen($requestUri) >= strlen($baseUrl))
  487. && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0)))
  488. {
  489. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  490. }
  491. }
  492. $this->_baseUrl = rtrim($baseUrl, '/');
  493. return $this;
  494. }
  495. /**
  496. * Everything in REQUEST_URI before PATH_INFO
  497. * <form action="<?=$baseUrl?>/news/submit" method="POST"/>
  498. *
  499. * @return string
  500. */
  501. public function getBaseUrl()
  502. {
  503. if (null === $this->_baseUrl) {
  504. $this->setBaseUrl();
  505. }
  506. return $this->_baseUrl;
  507. }
  508. /**
  509. * Set the base path for the URL
  510. *
  511. * @param string|null $basePath
  512. * @return Zend_Controller_Request_Http
  513. */
  514. public function setBasePath($basePath = null)
  515. {
  516. if ($basePath === null) {
  517. $filename = (isset($_SERVER['SCRIPT_FILENAME']))
  518. ? basename($_SERVER['SCRIPT_FILENAME'])
  519. : '';
  520. $baseUrl = $this->getBaseUrl();
  521. if (empty($baseUrl)) {
  522. $this->_basePath = '';
  523. return $this;
  524. }
  525. if (basename($baseUrl) === $filename) {
  526. $basePath = dirname($baseUrl);
  527. } else {
  528. $basePath = $baseUrl;
  529. }
  530. }
  531. if (substr(PHP_OS, 0, 3) === 'WIN') {
  532. $basePath = str_replace('\\', '/', $basePath);
  533. }
  534. $this->_basePath = rtrim($basePath, '/');
  535. return $this;
  536. }
  537. /**
  538. * Everything in REQUEST_URI before PATH_INFO not including the filename
  539. * <img src="<?=$basePath?>/images/zend.png"/>
  540. *
  541. * @return string
  542. */
  543. public function getBasePath()
  544. {
  545. if (null === $this->_basePath) {
  546. $this->setBasePath();
  547. }
  548. return $this->_basePath;
  549. }
  550. /**
  551. * Set the PATH_INFO string
  552. *
  553. * @param string|null $pathInfo
  554. * @return Zend_Controller_Request_Http
  555. */
  556. public function setPathInfo($pathInfo = null)
  557. {
  558. if ($pathInfo === null) {
  559. $baseUrl = $this->getBaseUrl();
  560. if (null === ($requestUri = $this->getRequestUri())) {
  561. return $this;
  562. }
  563. // Remove the query string from REQUEST_URI
  564. if ($pos = strpos($requestUri, '?')) {
  565. $requestUri = substr($requestUri, 0, $pos);
  566. }
  567. if (null !== $baseUrl
  568. && ((!empty($baseUrl) && 0 === strpos($requestUri, $baseUrl))
  569. || empty($baseUrl))
  570. && false === ($pathInfo = substr($requestUri, strlen($baseUrl)))
  571. ){
  572. // If substr() returns false then PATH_INFO is set to an empty string
  573. $pathInfo = '';
  574. } elseif (null === $baseUrl
  575. || (!empty($baseUrl) && false === strpos($requestUri, $baseUrl))
  576. ) {
  577. $pathInfo = $requestUri;
  578. }
  579. }
  580. $this->_pathInfo = (string) $pathInfo;
  581. return $this;
  582. }
  583. /**
  584. * Returns everything between the BaseUrl and QueryString.
  585. * This value is calculated instead of reading PATH_INFO
  586. * directly from $_SERVER due to cross-platform differences.
  587. *
  588. * @return string
  589. */
  590. public function getPathInfo()
  591. {
  592. if (empty($this->_pathInfo)) {
  593. $this->setPathInfo();
  594. }
  595. return $this->_pathInfo;
  596. }
  597. /**
  598. * Set allowed parameter sources
  599. *
  600. * Can be empty array, or contain one or more of '_GET' or '_POST'.
  601. *
  602. * @param array $paramSoures
  603. * @return Zend_Controller_Request_Http
  604. */
  605. public function setParamSources(array $paramSources = array())
  606. {
  607. $this->_paramSources = $paramSources;
  608. return $this;
  609. }
  610. /**
  611. * Get list of allowed parameter sources
  612. *
  613. * @return array
  614. */
  615. public function getParamSources()
  616. {
  617. return $this->_paramSources;
  618. }
  619. /**
  620. * Set a userland parameter
  621. *
  622. * Uses $key to set a userland parameter. If $key is an alias, the actual
  623. * key will be retrieved and used to set the parameter.
  624. *
  625. * @param mixed $key
  626. * @param mixed $value
  627. * @return Zend_Controller_Request_Http
  628. */
  629. public function setParam($key, $value)
  630. {
  631. $key = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  632. parent::setParam($key, $value);
  633. return $this;
  634. }
  635. /**
  636. * Retrieve a parameter
  637. *
  638. * Retrieves a parameter from the instance. Priority is in the order of
  639. * userland parameters (see {@link setParam()}), $_GET, $_POST. If a
  640. * parameter matching the $key is not found, null is returned.
  641. *
  642. * If the $key is an alias, the actual key aliased will be used.
  643. *
  644. * @param mixed $key
  645. * @param mixed $default Default value to use if key not found
  646. * @return mixed
  647. */
  648. public function getParam($key, $default = null)
  649. {
  650. $keyName = (null !== ($alias = $this->getAlias($key))) ? $alias : $key;
  651. $paramSources = $this->getParamSources();
  652. if (isset($this->_params[$keyName])) {
  653. return $this->_params[$keyName];
  654. } elseif (in_array('_GET', $paramSources) && (isset($_GET[$keyName]))) {
  655. return $_GET[$keyName];
  656. } elseif (in_array('_POST', $paramSources) && (isset($_POST[$keyName]))) {
  657. return $_POST[$keyName];
  658. }
  659. return $default;
  660. }
  661. /**
  662. * Retrieve an array of parameters
  663. *
  664. * Retrieves a merged array of parameters, with precedence of userland
  665. * params (see {@link setParam()}), $_GET, $_POST (i.e., values in the
  666. * userland params will take precedence over all others).
  667. *
  668. * @return array
  669. */
  670. public function getParams()
  671. {
  672. $return = $this->_params;
  673. $paramSources = $this->getParamSources();
  674. if (in_array('_GET', $paramSources)
  675. && isset($_GET)
  676. && is_array($_GET)
  677. ) {
  678. $return += $_GET;
  679. }
  680. if (in_array('_POST', $paramSources)
  681. && isset($_POST)
  682. && is_array($_POST)
  683. ) {
  684. $return += $_POST;
  685. }
  686. return $return;
  687. }
  688. /**
  689. * Set parameters
  690. *
  691. * Set one or more parameters. Parameters are set as userland parameters,
  692. * using the keys specified in the array.
  693. *
  694. * @param array $params
  695. * @return Zend_Controller_Request_Http
  696. */
  697. public function setParams(array $params)
  698. {
  699. foreach ($params as $key => $value) {
  700. $this->setParam($key, $value);
  701. }
  702. return $this;
  703. }
  704. /**
  705. * Set a key alias
  706. *
  707. * Set an alias used for key lookups. $name specifies the alias, $target
  708. * specifies the actual key to use.
  709. *
  710. * @param string $name
  711. * @param string $target
  712. * @return Zend_Controller_Request_Http
  713. */
  714. public function setAlias($name, $target)
  715. {
  716. $this->_aliases[$name] = $target;
  717. return $this;
  718. }
  719. /**
  720. * Retrieve an alias
  721. *
  722. * Retrieve the actual key represented by the alias $name.
  723. *
  724. * @param string $name
  725. * @return string|null Returns null when no alias exists
  726. */
  727. public function getAlias($name)
  728. {
  729. if (isset($this->_aliases[$name])) {
  730. return $this->_aliases[$name];
  731. }
  732. return null;
  733. }
  734. /**
  735. * Retrieve the list of all aliases
  736. *
  737. * @return array
  738. */
  739. public function getAliases()
  740. {
  741. return $this->_aliases;
  742. }
  743. /**
  744. * Return the method by which the request was made
  745. *
  746. * @return string
  747. */
  748. public function getMethod()
  749. {
  750. return $this->getServer('REQUEST_METHOD');
  751. }
  752. /**
  753. * Was the request made by POST?
  754. *
  755. * @return boolean
  756. */
  757. public function isPost()
  758. {
  759. if ('POST' == $this->getMethod()) {
  760. return true;
  761. }
  762. return false;
  763. }
  764. /**
  765. * Was the request made by GET?
  766. *
  767. * @return boolean
  768. */
  769. public function isGet()
  770. {
  771. if ('GET' == $this->getMethod()) {
  772. return true;
  773. }
  774. return false;
  775. }
  776. /**
  777. * Was the request made by PUT?
  778. *
  779. * @return boolean
  780. */
  781. public function isPut()
  782. {
  783. if ('PUT' == $this->getMethod()) {
  784. return true;
  785. }
  786. return false;
  787. }
  788. /**
  789. * Was the request made by DELETE?
  790. *
  791. * @return boolean
  792. */
  793. public function isDelete()
  794. {
  795. if ('DELETE' == $this->getMethod()) {
  796. return true;
  797. }
  798. return false;
  799. }
  800. /**
  801. * Was the request made by HEAD?
  802. *
  803. * @return boolean
  804. */
  805. public function isHead()
  806. {
  807. if ('HEAD' == $this->getMethod()) {
  808. return true;
  809. }
  810. return false;
  811. }
  812. /**
  813. * Was the request made by OPTIONS?
  814. *
  815. * @return boolean
  816. */
  817. public function isOptions()
  818. {
  819. if ('OPTIONS' == $this->getMethod()) {
  820. return true;
  821. }
  822. return false;
  823. }
  824. /**
  825. * Is the request a Javascript XMLHttpRequest?
  826. *
  827. * Should work with Prototype/Script.aculo.us, possibly others.
  828. *
  829. * @return boolean
  830. */
  831. public function isXmlHttpRequest()
  832. {
  833. return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  834. }
  835. /**
  836. * Is this a Flash request?
  837. *
  838. * @return boolean
  839. */
  840. public function isFlashRequest()
  841. {
  842. $header = strtolower($this->getHeader('USER_AGENT'));
  843. return (strstr($header, ' flash')) ? true : false;
  844. }
  845. /**
  846. * Is https secure request
  847. *
  848. * @return boolean
  849. */
  850. public function isSecure()
  851. {
  852. return ($this->getScheme() === self::SCHEME_HTTPS);
  853. }
  854. /**
  855. * Return the raw body of the request, if present
  856. *
  857. * @return string|false Raw body, or false if not present
  858. */
  859. public function getRawBody()
  860. {
  861. if (null === $this->_rawBody) {
  862. $body = file_get_contents('php://input');
  863. if (strlen(trim($body)) > 0) {
  864. $this->_rawBody = $body;
  865. } else {
  866. $this->_rawBody = false;
  867. }
  868. }
  869. return $this->_rawBody;
  870. }
  871. /**
  872. * Return the value of the given HTTP header. Pass the header name as the
  873. * plain, HTTP-specified header name. Ex.: Ask for 'Accept' to get the
  874. * Accept header, 'Accept-Encoding' to get the Accept-Encoding header.
  875. *
  876. * @param string $header HTTP header name
  877. * @return string|false HTTP header value, or false if not found
  878. * @throws Zend_Controller_Request_Exception
  879. */
  880. public function getHeader($header)
  881. {
  882. if (empty($header)) {
  883. require_once 'Zend/Controller/Request/Exception.php';
  884. throw new Zend_Controller_Request_Exception('An HTTP header name is required');
  885. }
  886. // Try to get it from the $_SERVER array first
  887. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  888. if (!empty($_SERVER[$temp])) {
  889. return $_SERVER[$temp];
  890. }
  891. // This seems to be the only way to get the Authorization header on
  892. // Apache
  893. if (function_exists('apache_request_headers')) {
  894. $headers = apache_request_headers();
  895. if (!empty($headers[$header])) {
  896. return $headers[$header];
  897. }
  898. }
  899. return false;
  900. }
  901. /**
  902. * Get the request URI scheme
  903. *
  904. * @return string
  905. */
  906. public function getScheme()
  907. {
  908. return ($this->getServer('HTTPS') == 'on') ? self::SCHEME_HTTPS : self::SCHEME_HTTP;
  909. }
  910. /**
  911. * Get the HTTP host.
  912. *
  913. * "Host" ":" host [ ":" port ] ; Section 3.2.2
  914. * Note the HTTP Host header is not the same as the URI host.
  915. * It includes the port while the URI host doesn't.
  916. *
  917. * @return string
  918. */
  919. public function getHttpHost()
  920. {
  921. $host = $this->getServer('HTTP_HOST');
  922. if (!empty($host)) {
  923. return $host;
  924. }
  925. $scheme = $this->getScheme();
  926. $name = $this->getServer('SERVER_NAME');
  927. $port = $this->getServer('SERVER_PORT');
  928. if (($scheme == self::SCHEME_HTTP && $port == 80) || ($scheme == self::SCHEME_HTTPS && $port == 443)) {
  929. return $name;
  930. } else {
  931. return $name . ':' . $port;
  932. }
  933. }
  934. /**
  935. * Get the client's IP addres
  936. *
  937. * @param boolean $checkProxy
  938. * @return string
  939. */
  940. public function getClientIp($checkProxy = true)
  941. {
  942. if ($checkProxy && $this->getServer('HTTP_CLIENT_IP') != null) {
  943. $ip = $this->getServer('HTTP_CLIENT_IP');
  944. } else if ($checkProxy && $this->getServer('HTTP_X_FORWARDED_FOR') != null) {
  945. $ip = $this->getServer('HTTP_X_FORWARDED_FOR');
  946. } else {
  947. $ip = $this->getServer('REMOTE_ADDR');
  948. }
  949. return $ip;
  950. }
  951. }