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