Http.php 30 KB

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