Beispiel #1
0
 function createHttpRequest()
 {
     $uri = new NUriScript();
     $uri->scheme = isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http';
     $uri->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
     $uri->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
     if (isset($_SERVER['HTTP_HOST'])) {
         $pair = explode(':', $_SERVER['HTTP_HOST']);
     } elseif (isset($_SERVER['SERVER_NAME'])) {
         $pair = explode(':', $_SERVER['SERVER_NAME']);
     } else {
         $pair = array('');
     }
     $uri->host = preg_match('#^[-.a-z0-9]+$#', $pair[0]) ? $pair[0] : '';
     if (isset($pair[1])) {
         $uri->port = (int) $pair[1];
     } elseif (isset($_SERVER['SERVER_PORT'])) {
         $uri->port = (int) $_SERVER['SERVER_PORT'];
     }
     if (isset($_SERVER['REQUEST_URI'])) {
         $requestUri = $_SERVER['REQUEST_URI'];
     } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
         $requestUri = $_SERVER['ORIG_PATH_INFO'];
         if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
             $requestUri .= '?' . $_SERVER['QUERY_STRING'];
         }
     } else {
         $requestUri = '';
     }
     $requestUri = NString::replace($requestUri, $this->uriFilters['uri']);
     $tmp = explode('?', $requestUri, 2);
     $uri->path = NString::replace($tmp[0], $this->uriFilters['path']);
     $uri->query = isset($tmp[1]) ? $tmp[1] : '';
     $uri->canonicalize();
     $uri->path = NString::fixEncoding($uri->path);
     $uri->scriptPath = '/';
     if (isset($_SERVER['SCRIPT_NAME'])) {
         $script = $_SERVER['SCRIPT_NAME'];
         if (strncmp($uri->path . '/', $script . '/', strlen($script) + 1) === 0) {
             $uri->scriptPath = $script;
         } elseif (strncmp($uri->path, $script, strrpos($script, '/') + 1) === 0) {
             $uri->scriptPath = substr($script, 0, strrpos($script, '/') + 1);
         }
     }
     $useFilter = !in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags');
     parse_str($uri->query, $query);
     if (!$query) {
         $query = $useFilter ? filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) : (empty($_GET) ? array() : $_GET);
     }
     $post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? array() : $_POST);
     $cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? array() : $_COOKIE);
     $gpc = (bool) get_magic_quotes_gpc();
     $old = error_reporting(error_reporting() ^ E_NOTICE);
     if ($gpc || $this->encoding) {
         $utf = strcasecmp($this->encoding, 'UTF-8') === 0;
         $list = array(&$query, &$post, &$cookies);
         while (list($key, $val) = each($list)) {
             foreach ($val as $k => $v) {
                 unset($list[$key][$k]);
                 if ($gpc) {
                     $k = stripslashes($k);
                 }
                 if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
                 } elseif (is_array($v)) {
                     $list[$key][$k] = $v;
                     $list[] =& $list[$key][$k];
                 } else {
                     if ($gpc && !$useFilter) {
                         $v = stripSlashes($v);
                     }
                     if ($this->encoding) {
                         if ($utf) {
                             $v = NString::fixEncoding($v);
                         } else {
                             if (!NString::checkEncoding($v)) {
                                 $v = iconv($this->encoding, 'UTF-8//IGNORE', $v);
                             }
                             $v = html_entity_decode($v, ENT_QUOTES, 'UTF-8');
                         }
                         $v = preg_replace(self::NONCHARS, '', $v);
                     }
                     $list[$key][$k] = $v;
                 }
             }
         }
         unset($list, $key, $val, $k, $v);
     }
     $files = array();
     $list = array();
     if (!empty($_FILES)) {
         foreach ($_FILES as $k => $v) {
             if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
                 continue;
             }
             $v['@'] =& $files[$k];
             $list[] = $v;
         }
     }
     while (list(, $v) = each($list)) {
         if (!isset($v['name'])) {
             continue;
         } elseif (!is_array($v['name'])) {
             if ($gpc) {
                 $v['name'] = stripSlashes($v['name']);
             }
             if ($this->encoding) {
                 $v['name'] = preg_replace(self::NONCHARS, '', NString::fixEncoding($v['name']));
             }
             $v['@'] = new NHttpUploadedFile($v);
             continue;
         }
         foreach ($v['name'] as $k => $foo) {
             if ($this->encoding && is_string($k) && (preg_match(self::NONCHARS, $k) || preg_last_error())) {
                 continue;
             }
             $list[] = array('name' => $v['name'][$k], 'type' => $v['type'][$k], 'size' => $v['size'][$k], 'tmp_name' => $v['tmp_name'][$k], 'error' => $v['error'][$k], '@' => &$v['@'][$k]);
         }
     }
     error_reporting($old);
     if (function_exists('apache_request_headers')) {
         $headers = array_change_key_case(apache_request_headers(), CASE_LOWER);
     } else {
         $headers = array();
         foreach ($_SERVER as $k => $v) {
             if (strncmp($k, 'HTTP_', 5) == 0) {
                 $k = substr($k, 5);
             } elseif (strncmp($k, 'CONTENT_', 8)) {
                 continue;
             }
             $headers[strtr(strtolower($k), '_', '-')] = $v;
         }
     }
     return new NHttpRequest($uri, $query, $post, $files, $cookies, $headers, isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : NULL, isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL, isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : NULL);
 }
Beispiel #2
0
 protected function detectUri()
 {
     $uri = $this->uri = new NUriScript();
     $uri->scheme = $this->isSecured() ? 'https' : 'http';
     $uri->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
     $uri->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
     if (isset($_SERVER['HTTP_HOST'])) {
         $pair = explode(':', $_SERVER['HTTP_HOST']);
     } elseif (isset($_SERVER['SERVER_NAME'])) {
         $pair = explode(':', $_SERVER['SERVER_NAME']);
     } else {
         $pair = array('');
     }
     $uri->host = $pair[0];
     if (isset($pair[1])) {
         $uri->port = (int) $pair[1];
     } elseif (isset($_SERVER['SERVER_PORT'])) {
         $uri->port = (int) $_SERVER['SERVER_PORT'];
     }
     if (isset($_SERVER['REQUEST_URI'])) {
         $requestUri = $_SERVER['REQUEST_URI'];
     } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
         $requestUri = $_SERVER['ORIG_PATH_INFO'];
         if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
             $requestUri .= '?' . $_SERVER['QUERY_STRING'];
         }
     } else {
         $requestUri = '';
     }
     $tmp = explode('?', $requestUri, 2);
     $this->originalUri = new NUri($uri);
     $this->originalUri->path = $tmp[0];
     $this->originalUri->query = isset($tmp[1]) ? $tmp[1] : '';
     $this->originalUri->freeze();
     $requestUri = NString::replace($requestUri, $this->uriFilter[0]);
     $tmp = explode('?', $requestUri, 2);
     $uri->path = NString::replace($tmp[0], $this->uriFilter[PHP_URL_PATH]);
     $uri->path = NString::fixEncoding($uri->path);
     $uri->query = isset($tmp[1]) ? $tmp[1] : '';
     $uri->canonicalize();
     $filename = isset($_SERVER['SCRIPT_FILENAME']) ? basename($_SERVER['SCRIPT_FILENAME']) : NULL;
     $scriptPath = '';
     if (isset($_SERVER['SCRIPT_NAME']) && basename($_SERVER['SCRIPT_NAME']) === $filename) {
         $scriptPath = rtrim($_SERVER['SCRIPT_NAME'], '/');
     } elseif (isset($_SERVER['PHP_SELF']) && basename($_SERVER['PHP_SELF']) === $filename) {
         $scriptPath = $_SERVER['PHP_SELF'];
     } elseif (isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME']) === $filename) {
         $scriptPath = $_SERVER['ORIG_SCRIPT_NAME'];
     } elseif (isset($_SERVER['PHP_SELF'], $_SERVER['SCRIPT_FILENAME'])) {
         $path = $_SERVER['PHP_SELF'];
         $segs = explode('/', trim($_SERVER['SCRIPT_FILENAME'], '/'));
         $segs = array_reverse($segs);
         $index = 0;
         $last = count($segs);
         do {
             $seg = $segs[$index];
             $scriptPath = '/' . $seg . $scriptPath;
             $index++;
         } while ($last > $index && FALSE !== ($pos = strpos($path, $scriptPath)) && 0 != $pos);
     }
     if (strncmp($uri->path, $scriptPath, strlen($scriptPath)) === 0) {
         $uri->scriptPath = $scriptPath;
     } elseif (strncmp($uri->path, $scriptPath, strrpos($scriptPath, '/') + 1) === 0) {
         $uri->scriptPath = substr($scriptPath, 0, strrpos($scriptPath, '/') + 1);
     } elseif (strpos($uri->path, basename($scriptPath)) === FALSE) {
         $uri->scriptPath = '/';
     } elseif (strlen($uri->path) >= strlen($scriptPath) && (FALSE !== ($pos = strpos($uri->path, $scriptPath)) && $pos !== 0)) {
         $uri->scriptPath = substr($uri->path, 0, $pos + strlen($scriptPath));
     } else {
         $uri->scriptPath = $scriptPath;
     }
     $uri->freeze();
 }