Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • None
  • PHP

Classes

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

Interfaces

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