1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Web;
13:
14: use Nette,
15: Nette\String;
16:
17:
18:
19: 20: 21: 22: 23:
24: class HttpRequestFactory extends Nette\Object
25: {
26:
27: const NONCHARS = '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
28:
29:
30: public $uriFilters = array(
31: 'path' => array('#/{2,}#' => '/'), 32: 'uri' => array(), 33: );
34:
35:
36: private $encoding;
37:
38:
39:
40: 41: 42: 43:
44: public function setEncoding($encoding)
45: {
46: $this->encoding = $encoding;
47: return $this;
48: }
49:
50:
51:
52: 53: 54: 55:
56: public function createHttpRequest()
57: {
58: 59: $uri = new UriScript;
60: $uri->scheme = isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http';
61: $uri->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
62: $uri->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
63:
64: 65: if (isset($_SERVER['HTTP_HOST'])) {
66: $pair = explode(':', $_SERVER['HTTP_HOST']);
67:
68: } elseif (isset($_SERVER['SERVER_NAME'])) {
69: $pair = explode(':', $_SERVER['SERVER_NAME']);
70:
71: } else {
72: $pair = array('');
73: }
74:
75: $uri->host = preg_match('#^[-._a-z0-9]+$#', $pair[0]) ? $pair[0] : '';
76:
77: if (isset($pair[1])) {
78: $uri->port = (int) $pair[1];
79:
80: } elseif (isset($_SERVER['SERVER_PORT'])) {
81: $uri->port = (int) $_SERVER['SERVER_PORT'];
82: }
83:
84: 85: if (isset($_SERVER['REQUEST_URI'])) { 86: $requestUri = $_SERVER['REQUEST_URI'];
87:
88: } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { 89: $requestUri = $_SERVER['ORIG_PATH_INFO'];
90: if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
91: $requestUri .= '?' . $_SERVER['QUERY_STRING'];
92: }
93: } else {
94: $requestUri = '';
95: }
96:
97: $requestUri = String::replace($requestUri, $this->uriFilters['uri']);
98: $tmp = explode('?', $requestUri, 2);
99: $uri->path = String::replace($tmp[0], $this->uriFilters['path']);
100: $uri->query = isset($tmp[1]) ? $tmp[1] : '';
101:
102: 103: $uri->canonicalize();
104: $uri->path = String::fixEncoding($uri->path);
105:
106: 107: if (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'])
108: && strncmp($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])) === 0
109: ) {
110: $script = '/' . ltrim(strtr(substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])), '\\', '/'), '/');
111: } elseif (isset($_SERVER['SCRIPT_NAME'])) {
112: $script = $_SERVER['SCRIPT_NAME'];
113: } else {
114: $script = '/';
115: }
116:
117: if (strncasecmp($uri->path . '/', $script . '/', strlen($script) + 1) === 0) { 118: $uri->scriptPath = substr($uri->path, 0, strlen($script));
119:
120: } elseif (strncasecmp($uri->path, $script, strrpos($script, '/') + 1) === 0) { 121: $uri->scriptPath = substr($uri->path, 0, strrpos($script, '/') + 1);
122:
123: } else {
124: $uri->scriptPath = '/';
125: }
126:
127:
128: 129: $useFilter = (!in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags'));
130:
131: parse_str($uri->query, $query);
132: if (!$query) {
133: $query = $useFilter ? filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) : (empty($_GET) ? array() : $_GET);
134: }
135: $post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? array() : $_POST);
136: $cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? array() : $_COOKIE);
137:
138: $gpc = (bool) get_magic_quotes_gpc();
139: $old = error_reporting(error_reporting() ^ E_NOTICE);
140:
141: 142: if ($gpc || $this->encoding) {
143: $utf = strcasecmp($this->encoding, 'UTF-8') === 0;
144: $list = array(& $query, & $post, & $cookies);
145: while (list($key, $val) = each($list)) {
146: foreach ($val as $k => $v) {
147: unset($list[$key][$k]);
148:
149: if ($gpc) {
150: $k = stripslashes($k);
151: }
152:
153: if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
154: 155:
156: } elseif (is_array($v)) {
157: $list[$key][$k] = $v;
158: $list[] = & $list[$key][$k];
159:
160: } else {
161: if ($gpc && !$useFilter) {
162: $v = stripSlashes($v);
163: }
164: if ($this->encoding) {
165: if ($utf) {
166: $v = String::fixEncoding($v);
167:
168: } else {
169: if (!String::checkEncoding($v)) {
170: $v = iconv($this->encoding, 'UTF-8//IGNORE', $v);
171: }
172: $v = html_entity_decode($v, ENT_QUOTES, 'UTF-8');
173: }
174: $v = preg_replace(self::NONCHARS, '', $v);
175: }
176: $list[$key][$k] = $v;
177: }
178: }
179: }
180: unset($list, $key, $val, $k, $v);
181: }
182:
183:
184: 185: $files = array();
186: $list = array();
187: if (!empty($_FILES)) {
188: foreach ($_FILES as $k => $v) {
189: if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) continue;
190: $v['@'] = & $files[$k];
191: $list[] = $v;
192: }
193: }
194:
195: while (list(, $v) = each($list)) {
196: if (!isset($v['name'])) {
197: continue;
198:
199: } elseif (!is_array($v['name'])) {
200: if ($gpc) {
201: $v['name'] = stripSlashes($v['name']);
202: }
203: if ($this->encoding) {
204: $v['name'] = preg_replace(self::NONCHARS, '', String::fixEncoding($v['name']));
205: }
206: $v['@'] = new HttpUploadedFile($v);
207: continue;
208: }
209:
210: foreach ($v['name'] as $k => $foo) {
211: if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) continue;
212: $list[] = array(
213: 'name' => $v['name'][$k],
214: 'type' => $v['type'][$k],
215: 'size' => $v['size'][$k],
216: 'tmp_name' => $v['tmp_name'][$k],
217: 'error' => $v['error'][$k],
218: '@' => & $v['@'][$k],
219: );
220: }
221: }
222:
223: error_reporting($old);
224:
225:
226: 227: if (function_exists('apache_request_headers')) {
228: $headers = array_change_key_case(apache_request_headers(), CASE_LOWER);
229: } else {
230: $headers = array();
231: foreach ($_SERVER as $k => $v) {
232: if (strncmp($k, 'HTTP_', 5) == 0) {
233: $k = substr($k, 5);
234: } elseif (strncmp($k, 'CONTENT_', 8)) {
235: continue;
236: }
237: $headers[ strtr(strtolower($k), '_', '-') ] = $v;
238: }
239: }
240:
241: return new HttpRequest($uri, $query, $post, $files, $cookies, $headers,
242: isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : NULL,
243: isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL,
244: isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : NULL
245: );
246: }
247:
248: }
249: