2
0

Http.php 27 KB

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