Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Context
  • FileUpload
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Http;
 13: 
 14: use Nette;
 15: 
 16: 
 17: 
 18: /**
 19:  * HttpRequest provides access scheme for request sent via HTTP.
 20:  *
 21:  * @author     David Grudl
 22:  *
 23:  * @property-read UrlScript $url
 24:  * @property-read mixed $query
 25:  * @property-read bool $post
 26:  * @property-read array $files
 27:  * @property-read array $cookies
 28:  * @property-read string $method
 29:  * @property-read array $headers
 30:  * @property-read Url|NULL $referer
 31:  * @property-read bool $secured
 32:  * @property-read bool $ajax
 33:  * @property-read string $remoteAddress
 34:  * @property-read string $remoteHost
 35:  */
 36: class Request extends Nette\Object implements IRequest
 37: {
 38:     /** @var string */
 39:     private $method;
 40: 
 41:     /** @var UrlScript */
 42:     private $url;
 43: 
 44:     /** @var array */
 45:     private $query;
 46: 
 47:     /** @var array */
 48:     private $post;
 49: 
 50:     /** @var array */
 51:     private $files;
 52: 
 53:     /** @var array */
 54:     private $cookies;
 55: 
 56:     /** @var array */
 57:     private $headers;
 58: 
 59:     /** @var string */
 60:     private $remoteAddress;
 61: 
 62:     /** @var string */
 63:     private $remoteHost;
 64: 
 65: 
 66: 
 67:     public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
 68:         $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
 69:     {
 70:         $this->url = $url;
 71:         $this->url->freeze();
 72:         if ($query === NULL) {
 73:             parse_str($url->query, $this->query);
 74:         } else {
 75:             $this->query = (array) $query;
 76:         }
 77:         $this->post = (array) $post;
 78:         $this->files = (array) $files;
 79:         $this->cookies = (array) $cookies;
 80:         $this->headers = (array) $headers;
 81:         $this->method = $method;
 82:         $this->remoteAddress = $remoteAddress;
 83:         $this->remoteHost = $remoteHost;
 84:     }
 85: 
 86: 
 87: 
 88:     /**
 89:      * Returns URL object.
 90:      * @return UrlScript
 91:      */
 92:     final public function getUrl()
 93:     {
 94:         return $this->url;
 95:     }
 96: 
 97: 
 98: 
 99:     /** @deprecated */
100:     function getUri()
101:     {
102:         trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::getUrl() instead.', E_USER_WARNING);
103:         return $this->getUrl();
104:     }
105: 
106: 
107: 
108:     /********************* query, post, files & cookies ****************d*g**/
109: 
110: 
111: 
112:     /**
113:      * Returns variable provided to the script via URL query ($_GET).
114:      * If no key is passed, returns the entire array.
115:      * @param  string key
116:      * @param  mixed  default value
117:      * @return mixed
118:      */
119:     final public function getQuery($key = NULL, $default = NULL)
120:     {
121:         if (func_num_args() === 0) {
122:             return $this->query;
123: 
124:         } elseif (isset($this->query[$key])) {
125:             return $this->query[$key];
126: 
127:         } else {
128:             return $default;
129:         }
130:     }
131: 
132: 
133: 
134:     /**
135:      * Returns variable provided to the script via POST method ($_POST).
136:      * If no key is passed, returns the entire array.
137:      * @param  string key
138:      * @param  mixed  default value
139:      * @return mixed
140:      */
141:     final public function getPost($key = NULL, $default = NULL)
142:     {
143:         if (func_num_args() === 0) {
144:             return $this->post;
145: 
146:         } elseif (isset($this->post[$key])) {
147:             return $this->post[$key];
148: 
149:         } else {
150:             return $default;
151:         }
152:     }
153: 
154: 
155: 
156:     /**
157:      * Returns uploaded file.
158:      * @param  string key (or more keys)
159:      * @return FileUpload
160:      */
161:     final public function getFile($key)
162:     {
163:         $args = func_get_args();
164:         return Nette\Utils\Arrays::get($this->files, $args, NULL);
165:     }
166: 
167: 
168: 
169:     /**
170:      * Returns uploaded files.
171:      * @return array
172:      */
173:     final public function getFiles()
174:     {
175:         return $this->files;
176:     }
177: 
178: 
179: 
180:     /**
181:      * Returns variable provided to the script via HTTP cookies.
182:      * @param  string key
183:      * @param  mixed  default value
184:      * @return mixed
185:      */
186:     final public function getCookie($key, $default = NULL)
187:     {
188:         if (func_num_args() === 0) {
189:             return $this->cookies;
190: 
191:         } elseif (isset($this->cookies[$key])) {
192:             return $this->cookies[$key];
193: 
194:         } else {
195:             return $default;
196:         }
197:     }
198: 
199: 
200: 
201:     /**
202:      * Returns variables provided to the script via HTTP cookies.
203:      * @return array
204:      */
205:     final public function getCookies()
206:     {
207:         return $this->cookies;
208:     }
209: 
210: 
211: 
212:     /********************* method & headers ****************d*g**/
213: 
214: 
215: 
216:     /**
217:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
218:      * @return string
219:      */
220:     public function getMethod()
221:     {
222:         return $this->method;
223:     }
224: 
225: 
226: 
227:     /**
228:      * Checks if the request method is the given one.
229:      * @param  string
230:      * @return bool
231:      */
232:     public function isMethod($method)
233:     {
234:         return strcasecmp($this->method, $method) === 0;
235:     }
236: 
237: 
238: 
239:     /**
240:      * Checks if the request method is POST.
241:      * @return bool
242:      */
243:     public function isPost()
244:     {
245:         return $this->isMethod('POST');
246:     }
247: 
248: 
249: 
250:     /**
251:      * Return the value of the HTTP header. Pass the header name as the
252:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
253:      * @param  string
254:      * @param  mixed
255:      * @return mixed
256:      */
257:     final public function getHeader($header, $default = NULL)
258:     {
259:         $header = strtolower($header);
260:         if (isset($this->headers[$header])) {
261:             return $this->headers[$header];
262:         } else {
263:             return $default;
264:         }
265:     }
266: 
267: 
268: 
269:     /**
270:      * Returns all HTTP headers.
271:      * @return array
272:      */
273:     public function getHeaders()
274:     {
275:         return $this->headers;
276:     }
277: 
278: 
279: 
280:     /**
281:      * Returns referrer.
282:      * @return Url|NULL
283:      */
284:     final public function getReferer()
285:     {
286:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
287:     }
288: 
289: 
290: 
291:     /**
292:      * Is the request is sent via secure channel (https).
293:      * @return bool
294:      */
295:     public function isSecured()
296:     {
297:         return $this->url->scheme === 'https';
298:     }
299: 
300: 
301: 
302:     /**
303:      * Is AJAX request?
304:      * @return bool
305:      */
306:     public function isAjax()
307:     {
308:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
309:     }
310: 
311: 
312: 
313:     /**
314:      * Returns the IP address of the remote client.
315:      * @return string
316:      */
317:     public function getRemoteAddress()
318:     {
319:         return $this->remoteAddress;
320:     }
321: 
322: 
323: 
324:     /**
325:      * Returns the host of the remote client.
326:      * @return string
327:      */
328:     public function getRemoteHost()
329:     {
330:         if (!$this->remoteHost) {
331:             $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
332:         }
333:         return $this->remoteHost;
334:     }
335: 
336: 
337: 
338:     /**
339:      * Parse Accept-Language header and returns prefered language.
340:      * @param  array   Supported languages
341:      * @return string
342:      */
343:     public function detectLanguage(array $langs)
344:     {
345:         $header = $this->getHeader('Accept-Language');
346:         if (!$header) {
347:             return NULL;
348:         }
349: 
350:         $s = strtolower($header);  // case insensitive
351:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
352:         rsort($langs);             // first more specific
353:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
354: 
355:         if (!$matches[0]) {
356:             return NULL;
357:         }
358: 
359:         $max = 0;
360:         $lang = NULL;
361:         foreach ($matches[1] as $key => $value) {
362:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
363:             if ($q > $max) {
364:                 $max = $q; $lang = $value;
365:             }
366:         }
367: 
368:         return $lang;
369:     }
370: 
371: }
372: 
Nette Framework 2.0.10 API API documentation generated by ApiGen 2.8.0