Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • 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

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 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:  * @property-read string $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 $query;
 42: 
 43:     /** @var array */
 44:     private $post;
 45: 
 46:     /** @var array */
 47:     private $files;
 48: 
 49:     /** @var array */
 50:     private $cookies;
 51: 
 52:     /** @var array */
 53:     private $headers;
 54: 
 55:     /** @var string */
 56:     private $remoteAddress;
 57: 
 58:     /** @var string */
 59:     private $remoteHost;
 60: 
 61:     /** @var string */
 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->query, $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) $headers;
 78:         $this->method = $method;
 79:         $this->remoteAddress = $remoteAddress;
 80:         $this->remoteHost = $remoteHost;
 81:     }
 82: 
 83: 
 84:     /**
 85:      * Returns URL object.
 86:      * @return UrlScript
 87:      */
 88:     public function getUrl()
 89:     {
 90:         return $this->url;
 91:     }
 92: 
 93: 
 94:     /********************* query, post, files & cookies ****************d*g**/
 95: 
 96: 
 97:     /**
 98:      * Returns variable provided to the script via URL query ($_GET).
 99:      * If no key is passed, returns the entire array.
100:      * @param  string key
101:      * @param  mixed  default value
102:      * @return mixed
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:      * Returns variable provided to the script via POST method ($_POST).
120:      * If no key is passed, returns the entire array.
121:      * @param  string key
122:      * @param  mixed  default value
123:      * @return mixed
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:      * Returns uploaded file.
141:      * @param  string key (or more keys)
142:      * @return FileUpload
143:      */
144:     public function getFile($key)
145:     {
146:         return Nette\Utils\Arrays::get($this->files, func_get_args(), NULL);
147:     }
148: 
149: 
150:     /**
151:      * Returns uploaded files.
152:      * @return array
153:      */
154:     public function getFiles()
155:     {
156:         return $this->files;
157:     }
158: 
159: 
160:     /**
161:      * Returns variable provided to the script via HTTP cookies.
162:      * @param  string key
163:      * @param  mixed  default value
164:      * @return mixed
165:      */
166:     public function getCookie($key, $default = NULL)
167:     {
168:         return isset($this->cookies[$key]) ? $this->cookies[$key] : $default;
169:     }
170: 
171: 
172:     /**
173:      * Returns variables provided to the script via HTTP cookies.
174:      * @return array
175:      */
176:     public function getCookies()
177:     {
178:         return $this->cookies;
179:     }
180: 
181: 
182:     /********************* method & headers ****************d*g**/
183: 
184: 
185:     /**
186:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
187:      * @return string
188:      */
189:     public function getMethod()
190:     {
191:         return $this->method;
192:     }
193: 
194: 
195:     /**
196:      * Checks if the request method is the given one.
197:      * @param  string
198:      * @return bool
199:      */
200:     public function isMethod($method)
201:     {
202:         return strcasecmp($this->method, $method) === 0;
203:     }
204: 
205: 
206:     /**
207:      * Checks if the request method is POST.
208:      * @return bool
209:      */
210:     public function isPost()
211:     {
212:         return $this->isMethod('POST');
213:     }
214: 
215: 
216:     /**
217:      * Return the value of the HTTP header. Pass the header name as the
218:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
219:      * @param  string
220:      * @param  mixed
221:      * @return mixed
222:      */
223:     public function getHeader($header, $default = NULL)
224:     {
225:         $header = strtolower($header);
226:         if (isset($this->headers[$header])) {
227:             return $this->headers[$header];
228:         } else {
229:             return $default;
230:         }
231:     }
232: 
233: 
234:     /**
235:      * Returns all HTTP headers.
236:      * @return array
237:      */
238:     public function getHeaders()
239:     {
240:         return $this->headers;
241:     }
242: 
243: 
244:     /**
245:      * Returns referrer.
246:      * @return Url|NULL
247:      */
248:     public function getReferer()
249:     {
250:         return isset($this->headers['referer']) ? new Url($this->headers['referer']) : NULL;
251:     }
252: 
253: 
254:     /**
255:      * Is the request is sent via secure channel (https).
256:      * @return bool
257:      */
258:     public function isSecured()
259:     {
260:         return $this->url->scheme === 'https';
261:     }
262: 
263: 
264:     /**
265:      * Is AJAX request?
266:      * @return bool
267:      */
268:     public function isAjax()
269:     {
270:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
271:     }
272: 
273: 
274:     /**
275:      * Returns the IP address of the remote client.
276:      * @return string
277:      */
278:     public function getRemoteAddress()
279:     {
280:         return $this->remoteAddress;
281:     }
282: 
283: 
284:     /**
285:      * Returns the host of the remote client.
286:      * @return string
287:      */
288:     public function getRemoteHost()
289:     {
290:         if (!$this->remoteHost) {
291:             $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
292:         }
293:         return $this->remoteHost;
294:     }
295: 
296: 
297:     /**
298:      * Returns raw content of HTTP request body.
299:      * @return string
300:      */
301:     public function getRawBody()
302:     {
303:         if (PHP_VERSION_ID >= 50600) {
304:             return file_get_contents('php://input');
305: 
306:         } elseif ($this->rawBody === NULL) { // can be read only once in PHP < 5.6
307:             $this->rawBody = (string) file_get_contents('php://input');
308:         }
309: 
310:         return $this->rawBody;
311:     }
312: 
313: 
314:     /**
315:      * Parse Accept-Language header and returns prefered language.
316:      * @param  array   Supported languages
317:      * @return string|null
318:      */
319:     public function detectLanguage(array $langs)
320:     {
321:         $header = $this->getHeader('Accept-Language');
322:         if (!$header) {
323:             return NULL;
324:         }
325: 
326:         $s = strtolower($header);  // case insensitive
327:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
328:         rsort($langs);             // first more specific
329:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
330: 
331:         if (!$matches[0]) {
332:             return NULL;
333:         }
334: 
335:         $max = 0;
336:         $lang = NULL;
337:         foreach ($matches[1] as $key => $value) {
338:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
339:             if ($q > $max) {
340:                 $max = $q; $lang = $value;
341:             }
342:         }
343: 
344:         return $lang;
345:     }
346: 
347: }
348: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0