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