1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Http;
13:
14: use Nette;
15:
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: class Request extends Nette\Object implements IRequest
36: {
37:
38: private $method;
39:
40:
41: private $url;
42:
43:
44: private $query;
45:
46:
47: private $post;
48:
49:
50: private $files;
51:
52:
53: private $cookies;
54:
55:
56: private ;
57:
58:
59: private $remoteAddress;
60:
61:
62: private $remoteHost;
63:
64:
65: public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
66: $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
67: {
68: $this->url = $url;
69: $this->url->freeze();
70: if ($query === NULL) {
71: parse_str($url->query, $this->query);
72: } else {
73: $this->query = (array) $query;
74: }
75: $this->post = (array) $post;
76: $this->files = (array) $files;
77: $this->cookies = (array) $cookies;
78: $this->headers = (array) $headers;
79: $this->method = $method;
80: $this->remoteAddress = $remoteAddress;
81: $this->remoteHost = $remoteHost;
82: }
83:
84:
85: 86: 87: 88:
89: final public function getUrl()
90: {
91: return $this->url;
92: }
93:
94:
95:
96: function getUri()
97: {
98: trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::getUrl() instead.', E_USER_WARNING);
99: return $this->getUrl();
100: }
101:
102:
103:
104:
105:
106: 107: 108: 109: 110: 111: 112:
113: final public function getQuery($key = NULL, $default = NULL)
114: {
115: if (func_num_args() === 0) {
116: return $this->query;
117:
118: } elseif (isset($this->query[$key])) {
119: return $this->query[$key];
120:
121: } else {
122: return $default;
123: }
124: }
125:
126:
127: 128: 129: 130: 131: 132: 133:
134: final public function getPost($key = NULL, $default = NULL)
135: {
136: if (func_num_args() === 0) {
137: return $this->post;
138:
139: } elseif (isset($this->post[$key])) {
140: return $this->post[$key];
141:
142: } else {
143: return $default;
144: }
145: }
146:
147:
148: 149: 150: 151: 152:
153: final public function getFile($key)
154: {
155: return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL);
156: }
157:
158:
159: 160: 161: 162:
163: final public function getFiles()
164: {
165: return $this->files;
166: }
167:
168:
169: 170: 171: 172: 173: 174:
175: final public function getCookie($key, $default = NULL)
176: {
177: if (func_num_args() === 0) {
178: return $this->cookies;
179:
180: } elseif (isset($this->cookies[$key])) {
181: return $this->cookies[$key];
182:
183: } else {
184: return $default;
185: }
186: }
187:
188:
189: 190: 191: 192:
193: final public function getCookies()
194: {
195: return $this->cookies;
196: }
197:
198:
199:
200:
201:
202: 203: 204: 205:
206: public function getMethod()
207: {
208: return $this->method;
209: }
210:
211:
212: 213: 214: 215: 216:
217: public function isMethod($method)
218: {
219: return strcasecmp($this->method, $method) === 0;
220: }
221:
222:
223: 224: 225: 226:
227: public function isPost()
228: {
229: return $this->isMethod('POST');
230: }
231:
232:
233: 234: 235: 236: 237: 238: 239:
240: final public function ($header, $default = NULL)
241: {
242: $header = strtolower($header);
243: if (isset($this->headers[$header])) {
244: return $this->headers[$header];
245: } else {
246: return $default;
247: }
248: }
249:
250:
251: 252: 253: 254:
255: public function ()
256: {
257: return $this->headers;
258: }
259:
260:
261: 262: 263: 264:
265: final public function getReferer()
266: {
267: return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
268: }
269:
270:
271: 272: 273: 274:
275: public function isSecured()
276: {
277: return $this->url->scheme === 'https';
278: }
279:
280:
281: 282: 283: 284:
285: public function isAjax()
286: {
287: return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
288: }
289:
290:
291: 292: 293: 294:
295: public function getRemoteAddress()
296: {
297: return $this->remoteAddress;
298: }
299:
300:
301: 302: 303: 304:
305: public function getRemoteHost()
306: {
307: if (!$this->remoteHost) {
308: $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
309: }
310: return $this->remoteHost;
311: }
312:
313:
314: 315: 316: 317: 318:
319: public function detectLanguage(array $langs)
320: {
321: $header = $this->getHeader('Accept-Language');
322: if (!$header) {
323: return NULL;
324: }
325:
326: $s = strtolower($header);
327: $s = strtr($s, '_', '-');
328: rsort($langs);
329: preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
330:
331: if (!$matches[0]) {
332: return NULL;
333: }
334:
335: $max = 0;
336: $lang = NULL;
337: foreach ($matches[1] as $key => $value) {
338: $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
339: if ($q > $max) {
340: $max = $q; $lang = $value;
341: }
342: }
343:
344: return $lang;
345: }
346:
347: }
348: