示例#1
0
 /**
  * @return IHttpRequest
  */
 public function factory()
 {
     $useFilter = !in_array(ini_get('filter.default'), ['', 'unsafe_raw']) || ini_get('filter.default_flags');
     $post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? [] : $_POST);
     $cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? [] : $_COOKIE);
     $blacklist = '\\x09\\x0A\\x0D\\x20-\\x7E\\xA0-\\x{10FFFF}';
     $chars = '~^[' . $blacklist . ']*+\\z~u';
     if (!$this->binary) {
         $list = [&$post, &$cookies];
         while (list($key, $source) = each($list)) {
             foreach ($source as $k => $v) {
                 if (is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
                     unset($list[$key][$k]);
                 } else {
                     if (is_array($v)) {
                         $list[$key][$k] = $v;
                         $list[] =& $list[$key][$k];
                     } else {
                         $list[$key][$k] = (string) preg_replace('~[^' . $blacklist . ']+~u', '', $v);
                     }
                 }
             }
         }
         unset($list, $key, $source, $k, $v);
     }
     $uploadList = [];
     $list = [];
     if (!empty($_FILES)) {
         foreach ($_FILES as $k => $v) {
             if (!$this->binary && is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
                 continue;
             }
             $v['@'] =& $uploadList[$k];
             $list[] = $v;
         }
     }
     while (list(, $upload) = each($list)) {
         if (!isset($upload['name'])) {
             continue;
         } else {
             if (!is_array($upload['name'])) {
                 if (!$this->binary && (!preg_match($chars, $upload['name']) || preg_last_error())) {
                     $upload['name'] = '';
                 }
                 if ($upload['error'] !== UPLOAD_ERR_NO_FILE) {
                     $upload['@'] = new Upload($upload);
                 }
                 continue;
             }
         }
         foreach ($upload['name'] as $k => $foo) {
             if (!$this->binary && is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
                 continue;
             }
             $list[] = ['name' => $upload['name'][$k], 'type' => $upload['type'][$k], 'size' => $upload['size'][$k], 'tmp_name' => $upload['tmp_name'][$k], 'error' => $upload['error'][$k], '@' => &$upload['@'][$k]];
         }
     }
     $remoteAddress = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
     $remoteHost = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : null;
     $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null;
     if ($method === 'POST' && isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) && preg_match('#^[A-Z]+\\z#', $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
         $method = $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'];
     }
     $headers = [];
     foreach ($_SERVER as $k => $v) {
         if (strncmp($k, 'HTTP_', 5) == 0) {
             $k = substr($k, 5);
         } else {
             if (strncmp($k, 'CONTENT_', 8)) {
                 continue;
             }
         }
         $headers[strtr($k, '_', '-')] = $v;
     }
     $httpRequest = new HttpRequest();
     $httpRequest->setUrl(Url::getCurrent());
     $httpRequest->setMethod($method);
     $httpRequest->setPost($post);
     $httpRequest->setUploadList($uploadList);
     $httpRequest->setCookies($cookies);
     $httpRequest->setHeaders($headers);
     $httpRequest->setRemoteAddress($remoteAddress);
     $httpRequest->setRemoteHost($remoteHost);
     $httpRequest->setBody(file_get_contents('php://input'));
     return $httpRequest;
 }
示例#2
0
 public static function getCurrent()
 {
     $url = new Url();
     $url->setScheme(!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http');
     $url->setUser(isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : null);
     $url->setPassword(isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : null);
     if ((isset($_SERVER[$hostname = 'HTTP_HOST']) || isset($_SERVER[$hostname = 'SERVER_NAME'])) && preg_match('#^([a-z0-9_.-]+|\\[[a-f0-9:]+\\])(:\\d+)?\\z#i', $_SERVER[$hostname], $pair)) {
         $url->setHost(strtolower($pair[1]));
         if (isset($pair[2])) {
             $url->setPort(substr($pair[2], 1));
         } else {
             if (isset($_SERVER['SERVER_PORT'])) {
                 $url->setPort($_SERVER['SERVER_PORT']);
             }
         }
     }
     $requestUrl = StringUtils::replace(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/', ['~[.,)]\\z~' => '']);
     $tmp = explode('?', $requestUrl, 2);
     $path = Url::unescape($tmp[0], '%/?#');
     $url->setPath($path);
     $url->setQuery(isset($tmp[1]) ? $tmp[1] : null);
     $lpath = strtolower($path);
     $script = isset($_SERVER['SCRIPT_NAME']) ? strtolower($_SERVER['SCRIPT_NAME']) : null;
     if ($lpath !== $script) {
         $max = min(strlen($lpath), strlen($script));
         for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++) {
             /** no-op */
         }
         $path = $i ? substr($path, 0, strrpos($path, '/', $i - strlen($path) - 1) + 1) : '/';
     }
     $url->setScriptPath($path);
     $query = $url->getQueryParameters();
     $blacklist = '\\x09\\x0A\\x0D\\x20-\\x7E\\xA0-\\x{10FFFF}';
     $chars = '~^[' . $blacklist . ']*+\\z~u';
     $list = [&$query];
     while (list($key, $source) = each($list)) {
         foreach ($source as $k => $v) {
             if (is_string($k) && (!preg_match($chars, $k) || preg_last_error())) {
                 unset($list[$key][$k]);
             } else {
                 if (is_array($v)) {
                     $list[$key][$k] = $v;
                     $list[] =& $list[$key][$k];
                 } else {
                     $list[$key][$k] = (string) preg_replace('~[^' . $blacklist . ']+~u', '', $v);
                 }
             }
         }
     }
     unset($list, $key, $source, $k, $v);
     $url->setQuery($query);
     return $url;
 }