PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Nette/Http/Request.php

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