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

  • NHttpContext
  • NHttpRequest
  • NHttpRequestFactory
  • NHttpResponse
  • NHttpUploadedFile
  • NSession
  • NSessionSection
  • NUrl
  • NUrlScript
  • NUserStorage

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 NUrlScript $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 NUrl|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 NHttpRequest extends NObject implements IHttpRequest
 35: {
 36:     /** @var string */
 37:     private $method;
 38: 
 39:     /** @var NUrlScript */
 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:     public function __construct(NUrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL,
 65:         $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
 66:     {
 67:         $this->url = $url;
 68:         $this->url->freeze();
 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 NUrlScript
 87:      */
 88:     final public function getUrl()
 89:     {
 90:         return $this->url;
 91:     }
 92: 
 93: 
 94:     /** @deprecated */
 95:     function getUri()
 96:     {
 97:         trigger_error(__METHOD__ . '() is deprecated; use ' . __CLASS__ . '::getUrl() instead.', E_USER_WARNING);
 98:         return $this->getUrl();
 99:     }
100: 
101: 
102:     /********************* query, post, files & cookies ****************d*g**/
103: 
104: 
105:     /**
106:      * Returns variable provided to the script via URL query ($_GET).
107:      * If no key is passed, returns the entire array.
108:      * @param  string key
109:      * @param  mixed  default value
110:      * @return mixed
111:      */
112:     final public function getQuery($key = NULL, $default = NULL)
113:     {
114:         if (func_num_args() === 0) {
115:             return $this->query;
116: 
117:         } elseif (isset($this->query[$key])) {
118:             return $this->query[$key];
119: 
120:         } else {
121:             return $default;
122:         }
123:     }
124: 
125: 
126:     /**
127:      * Returns variable provided to the script via POST method ($_POST).
128:      * If no key is passed, returns the entire array.
129:      * @param  string key
130:      * @param  mixed  default value
131:      * @return mixed
132:      */
133:     final public function getPost($key = NULL, $default = NULL)
134:     {
135:         if (func_num_args() === 0) {
136:             return $this->post;
137: 
138:         } elseif (isset($this->post[$key])) {
139:             return $this->post[$key];
140: 
141:         } else {
142:             return $default;
143:         }
144:     }
145: 
146: 
147:     /**
148:      * Returns uploaded file.
149:      * @param  string key (or more keys)
150:      * @return NHttpUploadedFile
151:      */
152:     final public function getFile($key)
153:     {
154:         $_args=func_get_args(); return NArrays::get($this->files, $_args, NULL);
155:     }
156: 
157: 
158:     /**
159:      * Returns uploaded files.
160:      * @return array
161:      */
162:     final public function getFiles()
163:     {
164:         return $this->files;
165:     }
166: 
167: 
168:     /**
169:      * Returns variable provided to the script via HTTP cookies.
170:      * @param  string key
171:      * @param  mixed  default value
172:      * @return mixed
173:      */
174:     final public function getCookie($key, $default = NULL)
175:     {
176:         if (func_num_args() === 0) {
177:             return $this->cookies;
178: 
179:         } elseif (isset($this->cookies[$key])) {
180:             return $this->cookies[$key];
181: 
182:         } else {
183:             return $default;
184:         }
185:     }
186: 
187: 
188:     /**
189:      * Returns variables provided to the script via HTTP cookies.
190:      * @return array
191:      */
192:     final public function getCookies()
193:     {
194:         return $this->cookies;
195:     }
196: 
197: 
198:     /********************* method & headers ****************d*g**/
199: 
200: 
201:     /**
202:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
203:      * @return string
204:      */
205:     public function getMethod()
206:     {
207:         return $this->method;
208:     }
209: 
210: 
211:     /**
212:      * Checks if the request method is the given one.
213:      * @param  string
214:      * @return bool
215:      */
216:     public function isMethod($method)
217:     {
218:         return strcasecmp($this->method, $method) === 0;
219:     }
220: 
221: 
222:     /**
223:      * Checks if the request method is POST.
224:      * @return bool
225:      */
226:     public function isPost()
227:     {
228:         return $this->isMethod('POST');
229:     }
230: 
231: 
232:     /**
233:      * Return the value of the HTTP header. Pass the header name as the
234:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
235:      * @param  string
236:      * @param  mixed
237:      * @return mixed
238:      */
239:     final public function getHeader($header, $default = NULL)
240:     {
241:         $header = strtolower($header);
242:         if (isset($this->headers[$header])) {
243:             return $this->headers[$header];
244:         } else {
245:             return $default;
246:         }
247:     }
248: 
249: 
250:     /**
251:      * Returns all HTTP headers.
252:      * @return array
253:      */
254:     public function getHeaders()
255:     {
256:         return $this->headers;
257:     }
258: 
259: 
260:     /**
261:      * Returns referrer.
262:      * @return NUrl|NULL
263:      */
264:     final public function getReferer()
265:     {
266:         return isset($this->headers['referer']) ? new NUrl($this->headers['referer']) : NULL;
267:     }
268: 
269: 
270:     /**
271:      * Is the request is sent via secure channel (https).
272:      * @return bool
273:      */
274:     public function isSecured()
275:     {
276:         return $this->url->scheme === 'https';
277:     }
278: 
279: 
280:     /**
281:      * Is AJAX request?
282:      * @return bool
283:      */
284:     public function isAjax()
285:     {
286:         return $this->getHeader('X-Requested-With') === 'XMLHttpRequest';
287:     }
288: 
289: 
290:     /**
291:      * Returns the IP address of the remote client.
292:      * @return string
293:      */
294:     public function getRemoteAddress()
295:     {
296:         return $this->remoteAddress;
297:     }
298: 
299: 
300:     /**
301:      * Returns the host of the remote client.
302:      * @return string
303:      */
304:     public function getRemoteHost()
305:     {
306:         if (!$this->remoteHost) {
307:             $this->remoteHost = $this->remoteAddress ? getHostByAddr($this->remoteAddress) : NULL;
308:         }
309:         return $this->remoteHost;
310:     }
311: 
312: 
313:     /**
314:      * Parse Accept-Language header and returns prefered language.
315:      * @param  array   Supported languages
316:      * @return string
317:      */
318:     public function detectLanguage(array $langs)
319:     {
320:         $header = $this->getHeader('Accept-Language');
321:         if (!$header) {
322:             return NULL;
323:         }
324: 
325:         $s = strtolower($header);  // case insensitive
326:         $s = strtr($s, '_', '-');  // cs_CZ means cs-CZ
327:         rsort($langs);             // first more specific
328:         preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches);
329: 
330:         if (!$matches[0]) {
331:             return NULL;
332:         }
333: 
334:         $max = 0;
335:         $lang = NULL;
336:         foreach ($matches[1] as $key => $value) {
337:             $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key];
338:             if ($q > $max) {
339:                 $max = $q; $lang = $value;
340:             }
341:         }
342: 
343:         return $lang;
344:     }
345: 
346: }
347: 
Nette Framework 2.0.12 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0