Packages

  • 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

  • HttpContext
  • HttpRequest
  • HttpRequestFactory
  • HttpResponse
  • HttpUploadedFile
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

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