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

/app/vendor/nette/nette/Nette/Http/Request.php

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