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

Classes

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

Interfaces

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