1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Web;
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 HttpRequest extends Nette\Object implements IHttpRequest
36: {
37:
38: private $method;
39:
40:
41: private $uri;
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(UriScript $uri, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
67: $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
68: {
69: $this->uri = $uri;
70: $this->uri->freeze();
71: if ($query === NULL) {
72: parse_str($uri->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 getUri()
92: {
93: return $this->uri;
94: }
95:
96:
97:
98:
99:
100:
101:
102: 103: 104: 105: 106: 107: 108:
109: final public function getQuery($key = NULL, $default = NULL)
110: {
111: if (func_num_args() === 0) {
112: return $this->query;
113:
114: } elseif (isset($this->query[$key])) {
115: return $this->query[$key];
116:
117: } else {
118: return $default;
119: }
120: }
121:
122:
123:
124: 125: 126: 127: 128: 129: 130:
131: final public function getPost($key = NULL, $default = NULL)
132: {
133: if (func_num_args() === 0) {
134: return $this->post;
135:
136: } elseif (isset($this->post[$key])) {
137: return $this->post[$key];
138:
139: } else {
140: return $default;
141: }
142: }
143:
144:
145:
146: 147: 148: 149: 150:
151: final public function getFile($key)
152: {
153: $args = func_get_args();
154: return Nette\ArrayTools::get($this->files, $args);
155: }
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:
176: final public function getCookie($key, $default = NULL)
177: {
178: if (func_num_args() === 0) {
179: return $this->cookies;
180:
181: } elseif (isset($this->cookies[$key])) {
182: return $this->cookies[$key];
183:
184: } else {
185: return $default;
186: }
187: }
188:
189:
190:
191: 192: 193: 194:
195: final public function getCookies()
196: {
197: return $this->cookies;
198: }
199:
200:
201:
202:
203:
204:
205:
206: 207: 208: 209:
210: public function getMethod()
211: {
212: return $this->method;
213: }
214:
215:
216:
217: 218: 219: 220: 221:
222: public function isMethod($method)
223: {
224: return strcasecmp($this->method, $method) === 0;
225: }
226:
227:
228:
229: 230: 231: 232:
233: public function isPost()
234: {
235: return $this->isMethod('POST');
236: }
237:
238:
239:
240: 241: 242: 243: 244: 245: 246:
247: final public function getHeader($header, $default = NULL)
248: {
249: $header = strtolower($header);
250: if (isset($this->headers[$header])) {
251: return $this->headers[$header];
252: } else {
253: return $default;
254: }
255: }
256:
257:
258:
259: 260: 261: 262:
263: public function getHeaders()
264: {
265: return $this->headers;
266: }
267:
268:
269:
270: 271: 272: 273:
274: final public function getReferer()
275: {
276: return isset($this->headers['referer']) ? new Uri($this->headers['referer']) : NULL;
277: }
278:
279:
280:
281: 282: 283: 284:
285: public function isSecured()
286: {
287: return $this->uri->scheme === 'https';
288: }
289:
290:
291:
292: 293: 294: 295:
296: public function isAjax()
297: {
298: return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
299: }
300:
301:
302:
303: 304: 305: 306:
307: public function getRemoteAddress()
308: {
309: return $this->remoteAddress;
310: }
311:
312:
313:
314: 315: 316: 317:
318: public function getRemoteHost()
319: {
320: if (!$this->remoteHost) {
321: $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
322: }
323: return $this->remoteHost;
324: }
325:
326:
327:
328: 329: 330: 331: 332:
333: public function detectLanguage(array $langs)
334: {
335: $header = $this->getHeader('Accept-Language');
336: if (!$header) {
337: return NULL;
338: }
339:
340: $s = strtolower($header); 341: $s = strtr($s, '_', '-'); 342: rsort($langs); 343: preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
344:
345: if (!$matches[0]) {
346: return NULL;
347: }
348:
349: $max = 0;
350: $lang = NULL;
351: foreach ($matches[1] as $key => $value) {
352: $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
353: if ($q > $max) {
354: $max = $q; $lang = $value;
355: }
356: }
357:
358: return $lang;
359: }
360:
361: }
362: