PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Nette/Http/Request.php

http://github.com/nette/nette
PHP | 371 lines | 156 code | 91 blank | 124 comment | 14 complexity | 56ff681e0af05cd71112f37f13386284 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework (http://nette.org)
  4. *
  5. * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6. *
  7. * For the full copyright and license information, please view
  8. * the file license.txt that was distributed with this source code.
  9. */
  10. namespace Nette\Http;
  11. use Nette;
  12. /**
  13. * HttpRequest provides access scheme for request sent via HTTP.
  14. *
  15. * @author David Grudl
  16. *
  17. * @property-read UrlScript $url
  18. * @property-read mixed $query
  19. * @property-read bool $post
  20. * @property-read array $files
  21. * @property-read array $cookies
  22. * @property-read string $method
  23. * @property-read array $headers
  24. * @property-read Url|NULL $referer
  25. * @property-read bool $secured
  26. * @property-read bool $ajax
  27. * @property-read string $remoteAddress
  28. * @property-read string $remoteHost
  29. */
  30. class Request extends Nette\Object implements IRequest
  31. {
  32. /** @var string */
  33. private $method;
  34. /** @var UrlScript */
  35. private $url;
  36. /** @var array */
  37. private $query;
  38. /** @var array */
  39. private $post;
  40. /** @var array */
  41. private $files;
  42. /** @var array */
  43. private $cookies;
  44. /** @var array */
  45. private $headers;
  46. /** @var string */
  47. private $remoteAddress;
  48. /** @var string */
  49. private $remoteHost;
  50. public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
  51. $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
  52. {
  53. $this->url = $url;
  54. $this->url->freeze();
  55. if ($query === NULL) {
  56. parse_str($url->query, $this->query);
  57. } else {
  58. $this->query = (array) $query;
  59. }
  60. $this->post = (array) $post;
  61. $this->files = (array) $files;
  62. $this->cookies = (array) $cookies;
  63. $this->headers = (array) $headers;
  64. $this->method = $method;
  65. $this->remoteAddress = $remoteAddress;
  66. $this->remoteHost = $remoteHost;
  67. }
  68. /**
  69. * Returns URL object.
  70. * @return UrlScript
  71. */
  72. final public function getUrl()
  73. {
  74. return $this->url;
  75. }
  76. /** @deprecated */
  77. function getUri()
  78. {
  79. trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::getUrl() instead.', E_USER_WARNING);
  80. return $this->getUrl();
  81. }
  82. /********************* query, post, files & cookies ****************d*g**/
  83. /**
  84. * Returns variable provided to the script via URL query ($_GET).
  85. * If no key is passed, returns the entire array.
  86. * @param string key
  87. * @param mixed default value
  88. * @return mixed
  89. */
  90. final public function getQuery($key = NULL, $default = NULL)
  91. {
  92. if (func_num_args() === 0) {
  93. return $this->query;
  94. } elseif (isset($this->query[$key])) {
  95. return $this->query[$key];
  96. } else {
  97. return $default;
  98. }
  99. }
  100. /**
  101. * Returns variable provided to the script via POST method ($_POST).
  102. * If no key is passed, returns the entire array.
  103. * @param string key
  104. * @param mixed default value
  105. * @return mixed
  106. */
  107. final public function getPost($key = NULL, $default = NULL)
  108. {
  109. if (func_num_args() === 0) {
  110. return $this->post;
  111. } elseif (isset($this->post[$key])) {
  112. return $this->post[$key];
  113. } else {
  114. return $default;
  115. }
  116. }
  117. /**
  118. * Returns uploaded file.
  119. * @param string key (or more keys)
  120. * @return FileUpload
  121. */
  122. final public function getFile($key)
  123. {
  124. $args = func_get_args();
  125. return Nette\Utils\Arrays::get($this->files, $args, NULL);
  126. }
  127. /**
  128. * Returns uploaded files.
  129. * @return array
  130. */
  131. final public function getFiles()
  132. {
  133. return $this->files;
  134. }
  135. /**
  136. * Returns variable provided to the script via HTTP cookies.
  137. * @param string key
  138. * @param mixed default value
  139. * @return mixed
  140. */
  141. final public function getCookie($key, $default = NULL)
  142. {
  143. if (func_num_args() === 0) {
  144. return $this->cookies;
  145. } elseif (isset($this->cookies[$key])) {
  146. return $this->cookies[$key];
  147. } else {
  148. return $default;
  149. }
  150. }
  151. /**
  152. * Returns variables provided to the script via HTTP cookies.
  153. * @return array
  154. */
  155. final public function getCookies()
  156. {
  157. return $this->cookies;
  158. }
  159. /********************* method & headers ****************d*g**/
  160. /**
  161. * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
  162. * @return string
  163. */
  164. public function getMethod()
  165. {
  166. return $this->method;
  167. }
  168. /**
  169. * Checks if the request method is the given one.
  170. * @param string
  171. * @return bool
  172. */
  173. public function isMethod($method)
  174. {
  175. return strcasecmp($this->method, $method) === 0;
  176. }
  177. /**
  178. * Checks if the request method is POST.
  179. * @return bool
  180. */
  181. public function isPost()
  182. {
  183. return $this->isMethod('POST');
  184. }
  185. /**
  186. * Return the value of the HTTP header. Pass the header name as the
  187. * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
  188. * @param string
  189. * @param mixed
  190. * @return mixed
  191. */
  192. final public function getHeader($header, $default = NULL)
  193. {
  194. $header = strtolower($header);
  195. if (isset($this->headers[$header])) {
  196. return $this->headers[$header];
  197. } else {
  198. return $default;
  199. }
  200. }
  201. /**
  202. * Returns all HTTP headers.
  203. * @return array
  204. */
  205. public function getHeaders()
  206. {
  207. return $this->headers;
  208. }
  209. /**
  210. * Returns referrer.
  211. * @return Url|NULL
  212. */
  213. final public function getReferer()
  214. {
  215. return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
  216. }
  217. /**
  218. * Is the request is sent via secure channel (https).
  219. * @return bool
  220. */
  221. public function isSecured()
  222. {
  223. return $this->url->scheme === 'https';
  224. }
  225. /**
  226. * Is AJAX request?
  227. * @return bool
  228. */
  229. public function isAjax()
  230. {
  231. return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
  232. }
  233. /**
  234. * Returns the IP address of the remote client.
  235. * @return string
  236. */
  237. public function getRemoteAddress()
  238. {
  239. return $this->remoteAddress;
  240. }
  241. /**
  242. * Returns the host of the remote client.
  243. * @return string
  244. */
  245. public function getRemoteHost()
  246. {
  247. if (!$this->remoteHost) {
  248. $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
  249. }
  250. return $this->remoteHost;
  251. }
  252. /**
  253. * Parse Accept-Language header and returns prefered language.
  254. * @param array Supported languages
  255. * @return string
  256. */
  257. public function detectLanguage(array $langs)
  258. {
  259. $header = $this->getHeader('Accept-Language');
  260. if (!$header) {
  261. return NULL;
  262. }
  263. $s = strtolower($header); // case insensitive
  264. $s = strtr($s, '_', '-'); // cs_CZ means cs-CZ
  265. rsort($langs); // first more specific
  266. preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
  267. if (!$matches[0]) {
  268. return NULL;
  269. }
  270. $max = 0;
  271. $lang = NULL;
  272. foreach ($matches[1] as $key => $value) {
  273. $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
  274. if ($q > $max) {
  275. $max = $q; $lang = $value;
  276. }
  277. }
  278. return $lang;
  279. }
  280. }