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