Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • Context
  • FileUpload
  • Helpers
  • 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:  * 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 array $query
 20:  * @property-read array $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|NULL $remoteAddress
 29:  * @property-read string|NULL $remoteHost
 30:  * @property-read string|NULL $rawBody
 31:  */
 32: class Request extends Nette\Object implements IRequest
 33: {
 34:     /** @var string */
 35:     private $method;
 36: 
 37:     /** @var UrlScript */
 38:     private $url;
 39: 
 40:     /** @var array */
 41:     private $post;
 42: 
 43:     /** @var array */
 44:     private $files;
 45: 
 46:     /** @var array */
 47:     private $cookies;
 48: 
 49:     /** @var array */
 50:     private $headers;
 51: 
 52:     /** @var string|NULL */
 53:     private $remoteAddress;
 54: 
 55:     /** @var string|NULL */
 56:     private $remoteHost;
 57: 
 58:     /** @var callable|NULL */
 59:     private $rawBodyCallback;
 60: 
 61: 
 62:     public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
 63:         $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL, $rawBodyCallback = NULL)
 64:     {
 65:         $this->url = $url;
 66:         if ($query !== NULL) {
 67:             trigger_error('Nette\Http\Request::__construct(): parameter $query is deprecated.', E_USER_DEPRECATED);
 68:             $url->setQuery($query);
 69:         }
 70:         $this->post = (array) $post;
 71:         $this->files = (array) $files;
 72:         $this->cookies = (array) $cookies;
 73:         $this->headers = array_change_key_case((array) $headers, CASE_LOWER);
 74:         $this->method = $method ?: 'GET';
 75:         $this->remoteAddress = $remoteAddress;
 76:         $this->remoteHost = $remoteHost;
 77:         $this->rawBodyCallback = $rawBodyCallback;
 78:     }
 79: 
 80: 
 81:     /**
 82:      * Returns URL object.
 83:      * @return UrlScript
 84:      */
 85:     public function getUrl()
 86:     {
 87:         return clone $this->url;
 88:     }
 89: 
 90: 
 91:     /********************* query, post, files & cookies ****************d*g**/
 92: 
 93: 
 94:     /**
 95:      * Returns variable provided to the script via URL query ($_GET).
 96:      * If no key is passed, returns the entire array.
 97:      * @param  string key
 98:      * @param  mixed  default value
 99:      * @return mixed
100:      */
101:     public function getQuery($key = NULL, $default = NULL)
102:     {
103:         if (func_num_args() === 0) {
104:             return $this->url->getQueryParameters();
105:         } else {
106:             return $this->url->getQueryParameter($key, $default);
107:         }
108:     }
109: 
110: 
111:     /**
112:      * Returns variable provided to the script via POST method ($_POST).
113:      * If no key is passed, returns the entire array.
114:      * @param  string key
115:      * @param  mixed  default value
116:      * @return mixed
117:      */
118:     public function getPost($key = NULL, $default = NULL)
119:     {
120:         if (func_num_args() === 0) {
121:             return $this->post;
122: 
123:         } elseif (isset($this->post[$key])) {
124:             return $this->post[$key];
125: 
126:         } else {
127:             return $default;
128:         }
129:     }
130: 
131: 
132:     /**
133:      * Returns uploaded file.
134:      * @param  string key
135:      * @return FileUpload|NULL
136:      */
137:     public function getFile($key)
138:     {
139:         if (func_num_args() > 1) {
140:             trigger_error('Calling getFile() with multiple keys is deprecated.', E_USER_DEPRECATED);
141:             return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL);
142:         }
143: 
144:         return isset($this->files[$key]) ? $this->files[$key] : NULL;
145:     }
146: 
147: 
148:     /**
149:      * Returns uploaded files.
150:      * @return array
151:      */
152:     public function getFiles()
153:     {
154:         return $this->files;
155:     }
156: 
157: 
158:     /**
159:      * Returns variable provided to the script via HTTP cookies.
160:      * @param  string key
161:      * @param  mixed  default value
162:      * @return mixed
163:      */
164:     public function getCookie($key, $default = NULL)
165:     {
166:         return isset($this->cookies[$key]) ? $this->cookies[$key] : $default;
167:     }
168: 
169: 
170:     /**
171:      * Returns variables provided to the script via HTTP cookies.
172:      * @return array
173:      */
174:     public function getCookies()
175:     {
176:         return $this->cookies;
177:     }
178: 
179: 
180:     /********************* method & headers ****************d*g**/
181: 
182: 
183:     /**
184:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
185:      * @return string
186:      */
187:     public function getMethod()
188:     {
189:         return $this->method;
190:     }
191: 
192: 
193:     /**
194:      * Checks if the request method is the given one.
195:      * @param  string
196:      * @return bool
197:      */
198:     public function isMethod($method)
199:     {
200:         return strcasecmp($this->method, $method) === 0;
201:     }
202: 
203: 
204:     /**
205:      * @deprecated
206:      */
207:     public function isPost()
208:     {
209:         return $this->isMethod('POST');
210:     }
211: 
212: 
213:     /**
214:      * Return the value of the HTTP header. Pass the header name as the
215:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
216:      * @param  string
217:      * @param  mixed
218:      * @return mixed
219:      */
220:     public function getHeader($header, $default = NULL)
221:     {
222:         $header = strtolower($header);
223:         return isset($this->headers[$header]) ? $this->headers[$header] : $default;
224:     }
225: 
226: 
227:     /**
228:      * Returns all HTTP headers.
229:      * @return array
230:      */
231:     public function getHeaders()
232:     {
233:         return $this->headers;
234:     }
235: 
236: 
237:     /**
238:      * Returns referrer.
239:      * @return Url|NULL
240:      */
241:     public function getReferer()
242:     {
243:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
244:     }
245: 
246: 
247:     /**
248:      * Is the request is sent via secure channel (https).
249:      * @return bool
250:      */
251:     public function isSecured()
252:     {
253:         return $this->url->getScheme() === 'https';
254:     }
255: 
256: 
257:     /**
258:      * Is AJAX request?
259:      * @return bool
260:      */
261:     public function isAjax()
262:     {
263:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
264:     }
265: 
266: 
267:     /**
268:      * Returns the IP address of the remote client.
269:      * @return string|NULL
270:      */
271:     public function getRemoteAddress()
272:     {
273:         return $this->remoteAddress;
274:     }
275: 
276: 
277:     /**
278:      * Returns the host of the remote client.
279:      * @return string|NULL
280:      */
281:     public function getRemoteHost()
282:     {
283:         if ($this->remoteHost === NULL && $this->remoteAddress !== NULL) {
284:             $this->remoteHost = getHostByAddr($this->remoteAddress);
285:         }
286:         return $this->remoteHost;
287:     }
288: 
289: 
290:     /**
291:      * Returns raw content of HTTP request body.
292:      * @return string|NULL
293:      */
294:     public function getRawBody()
295:     {
296:         return $this->rawBodyCallback ? call_user_func($this->rawBodyCallback) : NULL;
297:     }
298: 
299: 
300:     /**
301:      * Parse Accept-Language header and returns preferred language.
302:      * @param  string[] supported languages
303:      * @return string|NULL
304:      */
305:     public function detectLanguage(array $langs)
306:     {
307:         $header = $this->getHeader('Accept-Language');
308:         if (!$header) {
309:             return NULL;
310:         }
311: 
312:         $s = strtolower($header);  // case insensitive
313:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
314:         rsort($langs);             // first more specific
315:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
316: 
317:         if (!$matches[0]) {
318:             return NULL;
319:         }
320: 
321:         $max = 0;
322:         $lang = NULL;
323:         foreach ($matches[1] as $key => $value) {
324:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
325:             if ($q > $max) {
326:                 $max = $q; $lang = $value;
327:             }
328:         }
329: 
330:         return $lang;
331:     }
332: 
333: }
334: 
Nette 2.3.1 API API documentation generated by ApiGen 2.8.0