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:  * Current HTTP request factory.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Http
 20:  */
 21: class NHttpRequestFactory extends NObject
 22: {
 23:     /** @internal */
 24:     const NONCHARS = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
 25: 
 26:     /** @var array */
 27:     public $urlFilters = array(
 28:         'path' => array('#/{2,}#' => '/'), // '%20' => ''
 29:         'url' => array(), // '#[.,)]\z#' => ''
 30:     );
 31: 
 32:     /** @var string */
 33:     private $encoding;
 34: 
 35: 
 36: 
 37:     /**
 38:      * @param  string
 39:      * @return NHttpRequestFactory  provides a fluent interface
 40:      */
 41:     public function setEncoding($encoding)
 42:     {
 43:         $this->encoding = $encoding;
 44:         return $this;
 45:     }
 46: 
 47: 
 48: 
 49:     /**
 50:      * Creates current HttpRequest object.
 51:      * @return NHttpRequest
 52:      */
 53:     public function createHttpRequest()
 54:     {
 55:         // DETECTS URI, base path and script path of the request.
 56:         $url = new NUrlScript;
 57:         $url->scheme = !empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http';
 58:         $url->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
 59:         $url->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
 60: 
 61:         // host & port
 62:         if (isset($_SERVER['HTTP_HOST'])) {
 63:             $pair = explode(':', $_SERVER['HTTP_HOST']);
 64: 
 65:         } elseif (isset($_SERVER['SERVER_NAME'])) {
 66:             $pair = explode(':', $_SERVER['SERVER_NAME']);
 67: 
 68:         } else {
 69:             $pair = array('');
 70:         }
 71: 
 72:         $url->host = preg_match('#^[-._a-z0-9]+\z#', $pair[0]) ? $pair[0] : '';
 73: 
 74:         if (isset($pair[1])) {
 75:             $url->port = (int) $pair[1];
 76: 
 77:         } elseif (isset($_SERVER['SERVER_PORT'])) {
 78:             $url->port = (int) $_SERVER['SERVER_PORT'];
 79:         }
 80: 
 81:         // path & query
 82:         if (isset($_SERVER['REQUEST_URI'])) { // Apache, IIS 6.0
 83:             $requestUrl = $_SERVER['REQUEST_URI'];
 84: 
 85:         } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0 (PHP as CGI ?)
 86:             $requestUrl = $_SERVER['ORIG_PATH_INFO'];
 87:             if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
 88:                 $requestUrl .= '?' . $_SERVER['QUERY_STRING'];
 89:             }
 90:         } else {
 91:             $requestUrl = '';
 92:         }
 93: 
 94:         $requestUrl = NStrings::replace($requestUrl, $this->urlFilters['url']);
 95:         $tmp = explode('?', $requestUrl, 2);
 96:         $url->path = NStrings::replace($tmp[0], $this->urlFilters['path']);
 97:         $url->query = isset($tmp[1]) ? $tmp[1] : '';
 98: 
 99:         // normalized url
100:         $url->canonicalize();
101:         $url->path = NStrings::fixEncoding($url->path);
102: 
103:         // detect script path
104:         if (isset($_SERVER['SCRIPT_NAME'])) {
105:             $script = $_SERVER['SCRIPT_NAME'];
106:         } elseif (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'])
107:             && strncmp($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])) === 0
108:         ) {
109:             $script = '/' . ltrim(strtr(substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])), '\\', '/'), '/');
110:         } else {
111:             $script = '/';
112:         }
113: 
114:         $path = strtolower($url->path) . '/';
115:         $script = strtolower($script) . '/';
116:         $max = min(strlen($path), strlen($script));
117:         for ($i = 0; $i < $max; $i++) {
118:             if ($path[$i] !== $script[$i]) {
119:                 break;
120:             } elseif ($path[$i] === '/') {
121:                 $url->scriptPath = substr($url->path, 0, $i + 1);
122:             }
123:         }
124: 
125:         // GET, POST, COOKIE
126:         $useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags'));
127: 
128:         parse_str($url->query, $query);
129:         if (!$query) {
130:             $query = $useFilter ? filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) : (empty($_GET) ? array() : $_GET);
131:         }
132:         $post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? array() : $_POST);
133:         $cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? array() : $_COOKIE);
134: 
135:         $gpc = (bool) get_magic_quotes_gpc();
136:         $old = error_reporting(error_reporting() ^ E_NOTICE);
137: 
138:         // remove fucking quotes and check (and optionally convert) encoding
139:         if ($gpc || $this->encoding) {
140:             $utf = strcasecmp($this->encoding, 'UTF-8') === 0;
141:             $list = array(& $query, & $post, & $cookies);
142:             while (list($key, $val) = each($list)) {
143:                 foreach ($val as $k => $v) {
144:                     unset($list[$key][$k]);
145: 
146:                     if ($gpc) {
147:                         $k = stripslashes($k);
148:                     }
149: 
150:                     if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
151:                         // invalid key -> ignore
152: 
153:                     } elseif (is_array($v)) {
154:                         $list[$key][$k] = $v;
155:                         $list[] = & $list[$key][$k];
156: 
157:                     } else {
158:                         if ($gpc && !$useFilter) {
159:                             $v = stripSlashes($v);
160:                         }
161:                         if ($this->encoding) {
162:                             if ($utf) {
163:                                 $v = NStrings::fixEncoding($v);
164: 
165:                             } else {
166:                                 if (!NStrings::checkEncoding($v)) {
167:                                     $v = iconv($this->encoding, 'UTF-8//IGNORE', $v);
168:                                 }
169:                                 $v = html_entity_decode($v, ENT_QUOTES, 'UTF-8');
170:                             }
171:                             $v = preg_replace(self::NONCHARS, '', $v);
172:                         }
173:                         $list[$key][$k] = $v;
174:                     }
175:                 }
176:             }
177:             unset($list, $key, $val, $k, $v);
178:         }
179: 
180: 
181:         // FILES and create FileUpload objects
182:         $files = array();
183:         $list = array();
184:         if (!empty($_FILES)) {
185:             foreach ($_FILES as $k => $v) {
186:                 if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
187:                     continue;
188:                 }
189:                 $v['@'] = & $files[$k];
190:                 $list[] = $v;
191:             }
192:         }
193: 
194:         while (list(, $v) = each($list)) {
195:             if (!isset($v['name'])) {
196:                 continue;
197: 
198:             } elseif (!is_array($v['name'])) {
199:                 if ($gpc) {
200:                     $v['name'] = stripSlashes($v['name']);
201:                 }
202:                 if ($this->encoding) {
203:                     $v['name'] = preg_replace(self::NONCHARS, '', NStrings::fixEncoding($v['name']));
204:                 }
205:                 $v['@'] = new NHttpUploadedFile($v);
206:                 continue;
207:             }
208: 
209:             foreach ($v['name'] as $k => $foo) {
210:                 if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
211:                     continue;
212:                 }
213:                 $list[] = array(
214:                     'name' => $v['name'][$k],
215:                     'type' => $v['type'][$k],
216:                     'size' => $v['size'][$k],
217:                     'tmp_name' => $v['tmp_name'][$k],
218:                     'error' => $v['error'][$k],
219:                     '@' => & $v['@'][$k],
220:                 );
221:             }
222:         }
223: 
224:         error_reporting($old);
225: 
226: 
227:         // HEADERS
228:         if (function_exists('apache_request_headers')) {
229:             $headers = array_change_key_case(apache_request_headers(), CASE_LOWER);
230:         } else {
231:             $headers = array();
232:             foreach ($_SERVER as $k => $v) {
233:                 if (strncmp($k, 'HTTP_', 5) == 0) {
234:                     $k = substr($k, 5);
235:                 } elseif (strncmp($k, 'CONTENT_', 8)) {
236:                     continue;
237:                 }
238:                 $headers[ strtr(strtolower($k), '_', '-') ] = $v;
239:             }
240:         }
241: 
242:         return new NHttpRequest($url, $query, $post, $files, $cookies, $headers,
243:             isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : NULL,
244:             isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL,
245:             isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : NULL
246:         );
247:     }
248: 
249: }
250: 
Nette Framework 2.0.7 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0