http://nette.org/admin/script.php/pathinfo/?name=param#fragment
                \_______________/\________/
                       |              |
                  scriptPath       pathInfo
- scriptPath: /admin/script.php (or simply /admin/ when script is directory index) - pathInfo: /pathinfo/ (additional path information)
Inheritance: extends Url
 /**
  * @param string|NULL
  */
 public function renderIn($backlink)
 {
     $httpRequest = $this->getHttpRequest();
     $referer = NULL;
     if ($httpRequest instanceof \Nette\Http\Request) {
         $referer = $httpRequest->getReferer();
     }
     if (!$backlink && $referer && $referer->getHost() == $httpRequest->getUrl()->getHost()) {
         $url = new UrlScript($referer);
         $url->setScriptPath($httpRequest->getUrl()->getScriptPath());
         $tmp = new Request($url);
         $req = $this->router->match($tmp);
         if (!$req) {
             return;
         }
         if (isset($req->parameters[static::SIGNAL_KEY])) {
             $params = $req->parameters;
             unset($params[static::SIGNAL_KEY]);
             $req->setParameters($params);
         }
         if ($req->getPresenterName() != $this->getName()) {
             $session = $this->getSession('Nette.Application/requests');
             do {
                 $key = Strings::random(5);
             } while (isset($session[$key]));
             $session[$key] = array($this->getUser()->getId(), $req);
             $session->setExpiration('+ 10 minutes', $key);
             $this->params['backlink'] = $key;
         }
     }
 }
Esempio n. 2
0
 /**
  * @return \Nette\Http\Request
  */
 public function createHttpRequest()
 {
     if ($this->fakeUrl === NULL || PHP_SAPI !== 'cli' || !empty($_SERVER['REMOTE_HOST'])) {
         return parent::createHttpRequest();
     }
     parse_str($this->fakeUrl->getQuery(), $query);
     return new Nette\Http\Request($this->fakeUrl, $query, array(), array(), array(), array(), PHP_SAPI, '127.0.0.1', '127.0.0.1');
 }
 public function testDefaultFormat()
 {
     $route = new RestRoute('Api');
     $url = new UrlScript();
     $url->setPath('/api/foo');
     $request = new Request($url);
     $format = $this->runDetectFormatMethod($route, $request);
     $this->assertEquals('json', $format);
 }
 /**
  * @expectedException \Nette\InvalidStateException
  */
 public function testOverrideMethodWithInvalidMethod()
 {
     $method = 'invalid';
     $route = new RestRoute('Api');
     $url = new UrlScript();
     $url->setPath('/api/foo');
     $url->setQuery(['__method' => $method]);
     $request = new Request($url, NULL, NULL, NULL, NULL, NULL, 'POST');
     $appRequest = $route->match($request);
 }
Esempio n. 5
0
 /**
  * Build the URL for given domain alias, path and parameters.
  *
  * @param string $name The name of the domain
  * @param string $path Optional path (without a leading slash)
  * @param array $params Optional query parameters
  *
  * @return UrlScript The URL for the given parameters
  */
 public function createUrl($name, $path = NULL, $params = array())
 {
     if (preg_match('~^https?://([^.]+\\.)?github\\.com/~', trim($path))) {
         $url = new UrlScript($path);
     } else {
         $url = new UrlScript($this->domains[$name]);
         $url->path .= ltrim($path, '/');
     }
     $url->appendQuery(array_map(function ($param) {
         return $param instanceof UrlScript ? (string) $param : $param;
     }, $params));
     return $url;
 }
Esempio n. 6
0
 protected function buildClient($query = [])
 {
     // Please do not abuse this
     $this->config = new IPub\Twitter\Configuration('123', 'abc');
     $url = new Http\UrlScript('http://www.ipublikuj.eu');
     $url->setQuery($query);
     $httpRequest = new Http\Request($url);
     $session = new Http\Session($httpRequest, new Http\Response());
     $session->setStorage(new ArraySessionStorage($session));
     $this->session = new IPub\Twitter\SessionStorage($session, $this->config);
     $this->httpClient = new ApiClientMock();
     $consumer = new OAuth\Consumer('123', 'abc');
     return new IPub\Twitter\Client($consumer, $this->httpClient, $this->config, $this->session, $httpRequest);
 }
Esempio n. 7
0
 /**
  * Build the URL for given domain alias, path and parameters.
  *
  * @param string $name The name of the domain
  * @param string $path Optional path (without a leading slash)
  * @param array $params Optional query parameters
  *
  * @return Http\UrlScript The URL for the given parameters
  */
 public function createUrl($name, $path = NULL, $params = [])
 {
     if (preg_match('~^https?://([^.]+\\.)?twitter\\.com/~', trim($path))) {
         $url = new Http\UrlScript($path);
     } else {
         $url = new Http\UrlScript($this->domains[$name]);
         $path = $url->getPath() . ltrim($path, '/');
         $url->setPath($path);
     }
     $url->appendQuery(array_map(function ($param) {
         return $param instanceof Http\UrlScript ? (string) $param : $param;
     }, $params));
     return $url;
 }
Esempio n. 8
0
 /**
  * @param bool $hasDb
  * @param Provider $provider
  * @param Request $request
  * @param Response $response
  */
 public function __construct($hasDb, Provider $provider, Request $request, Response $response)
 {
     $this->provider = $provider;
     $this->url = $request->getUrl();
     $this->hasDb = $hasDb;
     $this->request = $request;
     if ($this->url->getQueryParameter('debug-import-parameters')) {
         $provider->import();
         $this->redirectBack();
     }
     if ($this->url->getQueryParameter('debug-parameters-cache')) {
         $provider->cleanParametersCache();
         $this->redirectBack();
     }
 }
 /**
  * @dataProvider getActions
  */
 public function testDefault($method, $path, $action, $id = null, $associations = null)
 {
     $route = new RestRoute();
     $url = new UrlScript();
     $url->setPath($path);
     $request = new Request($url, NULL, NULL, NULL, NULL, NULL, $method);
     $appRequest = $route->match($request);
     $this->assertEquals('Foo', $appRequest->getPresenterName());
     $this->assertEquals($action, $appRequest->parameters['action']);
     if ($id) {
         $this->assertEquals($id, $appRequest->parameters['id']);
     }
     if ($associations) {
         $this->assertSame($associations, $appRequest->parameters['associations']);
     }
 }
Esempio n. 10
0
 /**
  * @param string $from
  * @param string $to
  * @param string $subject
  * @param string $link
  */
 private function sendEmail($from, $to, $subject, $link)
 {
     $latte = new Latte\Engine();
     $parameters = array('subject' => $subject, 'link' => $link, 'baseUri' => $this->urlScript->getHostUrl(), 'host' => $this->urlScript->getHost());
     $email = new Message();
     $email->setFrom($from)->addTo($to)->setSubject($subject)->setHtmlBody($latte->renderToString($this->appDir . '/presenters/templates/emails/registration.latte', $parameters));
     $this->mailer->send($email);
 }
Esempio n. 11
0
 public function __construct(UrlScript $url, $query = NULL, $post = NULL, $files = NULL, $cookies = NULL, $headers = NULL, $method = NULL, $remoteAddress = NULL, $remoteHost = NULL)
 {
     $this->url = $url;
     $this->url->freeze();
     if ($query === NULL) {
         parse_str($url->query, $this->query);
     } else {
         $this->query = (array) $query;
     }
     $this->post = (array) $post;
     $this->files = (array) $files;
     $this->cookies = (array) $cookies;
     $this->headers = (array) $headers;
     $this->method = $method;
     $this->remoteAddress = $remoteAddress;
     $this->remoteHost = $remoteHost;
 }
Esempio n. 12
0
 public function __invoke($application, $response)
 {
     if ($response instanceof JsonResponse && ($payload = $response->getPayload()) instanceof \stdClass) {
         if (!$this->forwardHasHappened && isset($payload->redirect)) {
             $url = new Http\UrlScript($payload->redirect);
             $url->setScriptPath($this->httpRequest->url->scriptPath);
             $httpRequest = new Http\Request($url);
             if ($this->router->match($httpRequest) !== NULL) {
                 $prop = new Property($application, 'httpRequest');
                 $prop->setAccessible(TRUE);
                 $prop->setValue($application, $httpRequest);
                 $application->run();
                 exit;
             }
         } elseif ($this->forwardHasHappened && !isset($payload->redirect)) {
             $payload->redirect = $application->getPresenter()->link('this');
         }
     }
 }
 public function __invoke($application, $response)
 {
     if ($response instanceof JsonResponse && ($payload = $response->getPayload()) instanceof \stdClass) {
         if (!$this->forwardHasHappened && isset($payload->redirect)) {
             if (($fragmentPos = strpos($payload->redirect, '#')) !== FALSE) {
                 $this->fragment = substr($payload->redirect, $fragmentPos);
             }
             $url = new Http\UrlScript($payload->redirect);
             $url->setScriptPath($this->httpRequest->url->scriptPath);
             $httpRequest = new Http\Request($url);
             if ($this->router->match($httpRequest) !== NULL) {
                 $prop = new Property('Nette\\Application\\Application', 'httpRequest');
                 $prop->setAccessible(TRUE);
                 $prop->setValue($application, $httpRequest);
                 $application->run();
                 exit;
             }
         } elseif ($this->forwardHasHappened && !isset($payload->redirect)) {
             $payload->redirect = $application->getPresenter()->link('this', $application->getPresenter()->getParameters()) . $this->fragment;
             $this->fragment = '';
         }
     }
 }
Esempio n. 14
0
 /**
  * Creates current HttpRequest object.
  * @return Request
  */
 public function createHttpRequest()
 {
     // DETECTS URI, base path and script path of the request.
     $url = new UrlScript();
     $url->setScheme(!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http');
     $url->setUser(isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '');
     $url->setPassword(isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '');
     // host & port
     if ((isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME'])) && preg_match('#^([a-z0-9_.-]+|\\[[a-f0-9:]+\\])(:\\d+)?\\z#i', $_SERVER[$tmp], $pair)) {
         $url->setHost(strtolower($pair[1]));
         if (isset($pair[2])) {
             $url->setPort(substr($pair[2], 1));
         } elseif (isset($_SERVER['SERVER_PORT'])) {
             $url->setPort($_SERVER['SERVER_PORT']);
         }
     }
     // path & query
     $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
     $requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
     $tmp = explode('?', $requestUrl, 2);
     $path = Url::unescape($tmp[0], '%/?#');
     $path = Strings::fixEncoding(Strings::replace($path, $this->urlFilters['path']));
     $url->setPath($path);
     $url->setQuery(isset($tmp[1]) ? $tmp[1] : '');
     // detect script path
     $lpath = strtolower($path);
     $script = isset($_SERVER['SCRIPT_NAME']) ? strtolower($_SERVER['SCRIPT_NAME']) : '';
     if ($lpath !== $script) {
         $max = min(strlen($lpath), strlen($script));
         for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++) {
         }
         $path = $i ? substr($path, 0, strrpos($path, '/', $i - strlen($path) - 1) + 1) : '/';
     }
     $url->setScriptPath($path);
     // GET, POST, COOKIE
     $useFilter = !in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags');
     $query = $url->getQueryParameters();
     $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);
     if (get_magic_quotes_gpc()) {
         $post = Helpers::stripslashes($post, $useFilter);
         $cookies = Helpers::stripslashes($cookies, $useFilter);
     }
     // remove invalid characters
     $reChars = '#^[' . self::CHARS . ']*+\\z#u';
     if (!$this->binary) {
         $list = array(&$query, &$post, &$cookies);
         while (list($key, $val) = each($list)) {
             foreach ($val as $k => $v) {
                 if (is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
                     unset($list[$key][$k]);
                 } elseif (is_array($v)) {
                     $list[$key][$k] = $v;
                     $list[] =& $list[$key][$k];
                 } else {
                     $list[$key][$k] = (string) preg_replace('#[^' . self::CHARS . ']+#u', '', $v);
                 }
             }
         }
         unset($list, $key, $val, $k, $v);
     }
     $url->setQuery($query);
     // FILES and create FileUpload objects
     $files = array();
     $list = array();
     if (!empty($_FILES)) {
         foreach ($_FILES as $k => $v) {
             if (!$this->binary && is_string($k) && (!preg_match($reChars, $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 (get_magic_quotes_gpc()) {
                 $v['name'] = stripSlashes($v['name']);
             }
             if (!$this->binary && (!preg_match($reChars, $v['name']) || preg_last_error())) {
                 $v['name'] = '';
             }
             if ($v['error'] !== UPLOAD_ERR_NO_FILE) {
                 $v['@'] = new FileUpload($v);
             }
             continue;
         }
         foreach ($v['name'] as $k => $foo) {
             if (!$this->binary && is_string($k) && (!preg_match($reChars, $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]);
         }
     }
     // HEADERS
     if (function_exists('apache_request_headers')) {
         $headers = apache_request_headers();
     } 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($k, '_', '-')] = $v;
         }
     }
     $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL;
     $remoteHost = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : NULL;
     // proxy
     foreach ($this->proxies as $proxy) {
         if (Helpers::ipMatch($remoteAddr, $proxy)) {
             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                 $remoteAddr = trim(current(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])));
             }
             if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
                 $remoteHost = trim(current(explode(',', $_SERVER['HTTP_X_FORWARDED_HOST'])));
             }
             break;
         }
     }
     $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'];
     }
     // raw body
     $rawBodyCallback = function () {
         static $rawBody;
         if (PHP_VERSION_ID >= 50600) {
             return file_get_contents('php://input');
         } elseif ($rawBody === NULL) {
             // can be read only once in PHP < 5.6
             $rawBody = (string) file_get_contents('php://input');
         }
         return $rawBody;
     };
     return new Request($url, NULL, $post, $files, $cookies, $headers, $method, $remoteAddr, $remoteHost, $rawBodyCallback);
 }
Esempio n. 15
0
 /**
  * Makes an HTTP request. This method can be overridden by subclasses if
  * developers want to do fancier things or use something other than curl to
  * make the request.
  *
  * @param string $url The URL to make the request to
  * @param array $params The parameters to use for the POST body
  * @param resource $ch Initialized curl handle
  *
  * @throws Facebook\FacebookApiException
  * @return string The response text
  */
 protected function makeRequest($url, array $params, $ch = NULL)
 {
     if (isset($this->cache[$cacheKey = md5(serialize(array($url, $params)))])) {
         return $this->cache[$cacheKey];
     }
     $url = new UrlScript($url);
     $method = strtoupper(isset($params['method']) ? $params['method'] : 'GET');
     $this->onRequest((string) $url, $params);
     $ch = $ch ?: curl_init();
     $opts = $this->curlOptions;
     if ($this->fb->config->graphVersion !== '' && $this->fb->config->graphVersion !== 'v1.0') {
         // v2.0 or later
         unset($params['method']);
         if ($method === 'GET') {
             $url->appendQuery($params);
             $params = array();
         }
         if ($method === 'DELETE' || $method === 'PUT') {
             $opts[CURLOPT_CUSTOMREQUEST] = $method;
         }
         if ($method !== 'GET') {
             $opts[CURLOPT_POSTFIELDS] = $params;
         }
         $opts[CURLOPT_HTTPHEADER][] = 'Accept-Encoding: *';
     } else {
         // BC
         $opts[CURLOPT_POSTFIELDS] = $this->fb->config->fileUploadSupport ? $params : http_build_query($params, NULL, '&');
         // disable the 'Expect: 100-continue' behaviour. This causes CURL to wait
         // for 2 seconds if the server does not support this header.
         $opts[CURLOPT_HTTPHEADER][] = 'Expect:';
     }
     $opts[CURLOPT_URL] = (string) $url;
     // execute request
     curl_setopt_array($ch, $opts);
     $result = curl_exec($ch);
     // provide certificate if needed
     if (curl_errno($ch) == CURLE_SSL_CACERT || curl_errno($ch) === CURLE_SSL_CACERT_BADFILE) {
         Debugger::log('Invalid or no certificate authority found, using bundled information', 'facebook');
         $this->curlOptions[CURLOPT_CAINFO] = CertificateHelper::getCaInfoFile();
         curl_setopt($ch, CURLOPT_CAINFO, CertificateHelper::getCaInfoFile());
         $result = curl_exec($ch);
     }
     // With dual stacked DNS responses, it's possible for a server to
     // have IPv6 enabled but not have IPv6 connectivity.  If this is
     // the case, curl will try IPv4 first and if that fails, then it will
     // fall back to IPv6 and the error EHOSTUNREACH is returned by the
     // operating system.
     if ($result === FALSE && empty($opts[CURLOPT_IPRESOLVE])) {
         $matches = array();
         if (preg_match('/Failed to connect to ([^:].*): Network is unreachable/', curl_error($ch), $matches)) {
             if (strlen(@inet_pton($matches[1])) === 16) {
                 Debugger::log('Invalid IPv6 configuration on server, Please disable or get native IPv6 on your server.', 'facebook');
                 $this->curlOptions[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
                 curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
                 $result = curl_exec($ch);
             }
         }
     }
     $info = curl_getinfo($ch);
     if (isset($info['request_header'])) {
         list($info['request_header']) = self::parseHeaders($info['request_header']);
     }
     $info['method'] = $method;
     if ($result === FALSE) {
         $e = new Facebook\FacebookApiException(array('error_code' => curl_errno($ch), 'error' => array('message' => curl_error($ch), 'type' => 'CurlException')));
         curl_close($ch);
         $this->onError($e, $info);
         throw $e;
     }
     if (!$result && isset($info['redirect_url'])) {
         $result = Json::encode(array('url' => $info['redirect_url']));
     }
     $info['headers'] = self::parseHeaders(substr($result, 0, $info['header_size']));
     $result = trim(substr($result, $info['header_size']));
     $this->onSuccess($result, $info);
     curl_close($ch);
     return $this->cache[$cacheKey] = $result;
 }
 /**
  * @param React\Http\Request $reactRequest
  * @return Nette\Http\Request
  */
 private function createNetteHttpRequest(React\Http\Request $reactRequest)
 {
     $url = new Nette\Http\UrlScript('http://' . $reactRequest->getHeaders()['Host'] . $reactRequest->getPath());
     $url->setQuery($reactRequest->getQuery());
     return new Nette\Http\Request($url, NULL, NULL, NULL, NULL, $reactRequest->getHeaders(), $reactRequest->getMethod(), $reactRequest->remoteAddress, NULL, NULL);
 }
Esempio n. 17
0
 /**
  * Creates current HttpRequest object.
  * @return Request
  */
 public function createHttpRequest()
 {
     // DETECTS URI, base path and script path of the request.
     $url = new UrlScript();
     $url->setScheme(!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http');
     $url->setUser(isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '');
     $url->setPassword(isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '');
     // host & port
     if ((isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME'])) && preg_match('#^([a-z0-9_.-]+|\\[[a-f0-9:]+\\])(:\\d+)?\\z#i', $_SERVER[$tmp], $pair)) {
         $url->setHost(strtolower($pair[1]));
         if (isset($pair[2])) {
             $url->setPort((int) substr($pair[2], 1));
         } elseif (isset($_SERVER['SERVER_PORT'])) {
             $url->setPort((int) $_SERVER['SERVER_PORT']);
         }
     }
     // path & query
     $requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
     $requestUrl = preg_replace('#^\\w++://[^/]++#', '', $requestUrl);
     $requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
     $tmp = explode('?', $requestUrl, 2);
     $path = Url::unescape($tmp[0], '%/?#');
     $path = Strings::fixEncoding(Strings::replace($path, $this->urlFilters['path']));
     $url->setPath($path);
     $url->setQuery(isset($tmp[1]) ? $tmp[1] : '');
     // detect script path
     $lpath = strtolower($path);
     $script = isset($_SERVER['SCRIPT_NAME']) ? strtolower($_SERVER['SCRIPT_NAME']) : '';
     if ($lpath !== $script) {
         $max = min(strlen($lpath), strlen($script));
         for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++) {
         }
         $path = $i ? substr($path, 0, strrpos($path, '/', $i - strlen($path) - 1) + 1) : '/';
     }
     $url->setScriptPath($path);
     // GET, POST, COOKIE
     $useFilter = !in_array(ini_get('filter.default'), ['', 'unsafe_raw']) || ini_get('filter.default_flags');
     $query = $url->getQueryParameters();
     $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);
     // remove invalid characters
     $reChars = '#^[' . self::CHARS . ']*+\\z#u';
     if (!$this->binary) {
         $list = [&$query, &$post, &$cookies];
         while (list($key, $val) = each($list)) {
             foreach ($val as $k => $v) {
                 if (is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
                     unset($list[$key][$k]);
                 } elseif (is_array($v)) {
                     $list[$key][$k] = $v;
                     $list[] =& $list[$key][$k];
                 } else {
                     $list[$key][$k] = (string) preg_replace('#[^' . self::CHARS . ']+#u', '', $v);
                 }
             }
         }
         unset($list, $key, $val, $k, $v);
     }
     $url->setQuery($query);
     // FILES and create FileUpload objects
     $files = [];
     $list = [];
     if (!empty($_FILES)) {
         foreach ($_FILES as $k => $v) {
             if (!$this->binary && is_string($k) && (!preg_match($reChars, $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 (!$this->binary && (!preg_match($reChars, $v['name']) || preg_last_error())) {
                 $v['name'] = '';
             }
             if ($v['error'] !== UPLOAD_ERR_NO_FILE) {
                 $v['@'] = new FileUpload($v);
             }
             continue;
         }
         foreach ($v['name'] as $k => $foo) {
             if (!$this->binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
                 continue;
             }
             $list[] = ['name' => $v['name'][$k], 'type' => $v['type'][$k], 'size' => $v['size'][$k], 'tmp_name' => $v['tmp_name'][$k], 'error' => $v['error'][$k], '@' => &$v['@'][$k]];
         }
     }
     // HEADERS
     if (function_exists('apache_request_headers')) {
         $headers = apache_request_headers();
     } else {
         $headers = [];
         foreach ($_SERVER as $k => $v) {
             if (strncmp($k, 'HTTP_', 5) == 0) {
                 $k = substr($k, 5);
             } elseif (strncmp($k, 'CONTENT_', 8)) {
                 continue;
             }
             $headers[strtr($k, '_', '-')] = $v;
         }
     }
     $remoteAddr = !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL;
     $remoteHost = !empty($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : NULL;
     // use real client address and host if trusted proxy is used
     $usingTrustedProxy = $remoteAddr && array_filter($this->proxies, function ($proxy) use($remoteAddr) {
         return Helpers::ipMatch($remoteAddr, $proxy);
     });
     if ($usingTrustedProxy) {
         if (!empty($_SERVER['HTTP_FORWARDED'])) {
             $forwardParams = preg_split('/[,;]/', $_SERVER['HTTP_FORWARDED']);
             foreach ($forwardParams as $forwardParam) {
                 list($key, $value) = explode('=', $forwardParam, 2) + [1 => NULL];
                 $proxyParams[strtolower(trim($key))][] = trim($value, " \t\"");
             }
             if (isset($proxyParams['for'])) {
                 $address = $proxyParams['for'][0];
                 if (strpos($address, '[') === FALSE) {
                     //IPv4
                     $remoteAddr = explode(':', $address)[0];
                 } else {
                     //IPv6
                     $remoteAddr = substr($address, 1, strpos($address, ']') - 1);
                 }
             }
             if (isset($proxyParams['host']) && count($proxyParams['host']) === 1) {
                 $host = $proxyParams['host'][0];
                 $startingDelimiterPosition = strpos($host, '[');
                 if ($startingDelimiterPosition === FALSE) {
                     //IPv4
                     $remoteHostArr = explode(':', $host);
                     $remoteHost = $remoteHostArr[0];
                     if (isset($remoteHostArr[1])) {
                         $url->setPort((int) $remoteHostArr[1]);
                     }
                 } else {
                     //IPv6
                     $endingDelimiterPosition = strpos($host, ']');
                     $remoteHost = substr($host, strpos($host, '[') + 1, $endingDelimiterPosition - 1);
                     $remoteHostArr = explode(':', substr($host, $endingDelimiterPosition));
                     if (isset($remoteHostArr[1])) {
                         $url->setPort((int) $remoteHostArr[1]);
                     }
                 }
             }
             $scheme = isset($proxyParams['scheme']) && count($proxyParams['scheme']) === 1 ? $proxyParams['scheme'][0] : 'http';
             $url->setScheme(strcasecmp($scheme, 'https') === 0 ? 'https' : 'http');
         } else {
             if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO'])) {
                 $url->setScheme(strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 ? 'https' : 'http');
             }
             if (!empty($_SERVER['HTTP_X_FORWARDED_PORT'])) {
                 $url->setPort((int) $_SERVER['HTTP_X_FORWARDED_PORT']);
             }
             if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                 $xForwardedForWithoutProxies = array_filter(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']), function ($ip) {
                     return !array_filter($this->proxies, function ($proxy) use($ip) {
                         return Helpers::ipMatch(trim($ip), $proxy);
                     });
                 });
                 $remoteAddr = trim(end($xForwardedForWithoutProxies));
                 $xForwardedForRealIpKey = key($xForwardedForWithoutProxies);
             }
             if (isset($xForwardedForRealIpKey) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
                 $xForwardedHost = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
                 if (isset($xForwardedHost[$xForwardedForRealIpKey])) {
                     $remoteHost = trim($xForwardedHost[$xForwardedForRealIpKey]);
                 }
             }
         }
     }
     // method, eg. GET, PUT, ...
     $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'];
     }
     // raw body
     $rawBodyCallback = function () {
         return file_get_contents('php://input');
     };
     return new Request($url, NULL, $post, $files, $cookies, $headers, $method, $remoteAddr, $remoteHost, $rawBodyCallback);
 }
Esempio n. 18
0
 /**
  * @dataProvider getVersions
  */
 public function testModuleVersioning($module, $path, $expectedPresenterName, $expectedUrl)
 {
     $route = new RestRoute($module);
     $route->useURLModuleVersioning(RestRoute::MODULE_VERSION_PATH_PREFIX_PATTERN, [NULL => 'V1', 'v1' => 'V1', 'v2' => 'V2']);
     $url = new UrlScript();
     $url->setPath($path);
     $request = new Request($url, NULL, NULL, NULL, NULL, NULL, 'GET');
     $appRequest = $route->match($request);
     $this->assertEquals($expectedPresenterName, $appRequest->getPresenterName());
     $refUrl = new Url('http://localhost');
     $url = $route->constructUrl($appRequest, $refUrl);
     $this->assertEquals($expectedUrl, $url);
 }
Esempio n. 19
0
 /**
  * Creates current HttpRequest object.
  * @return Request
  */
 public function createHttpRequest()
 {
     // DETECTS URI, base path and script path of the request.
     $url = new UrlScript();
     $url->setScheme(!empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http');
     $url->setUser(isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '');
     $url->setPassword(isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '');
     // host & port
     if ((isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME'])) && preg_match('#^([a-z0-9_.-]+|\\[[a-f0-9:]+\\])(:\\d+)?\\z#i', $_SERVER[$tmp], $pair)) {
         $url->setHost(strtolower($pair[1]));
         if (isset($pair[2])) {
             $url->setPort(substr($pair[2], 1));
         } elseif (isset($_SERVER['SERVER_PORT'])) {
             $url->setPort($_SERVER['SERVER_PORT']);
         }
     }
     // path & query
     if (isset($_SERVER['REQUEST_URI'])) {
         // Apache, IIS 6.0
         $requestUrl = $_SERVER['REQUEST_URI'];
     } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
         // IIS 5.0 (PHP as CGI ?)
         $requestUrl = $_SERVER['ORIG_PATH_INFO'];
         if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
             $requestUrl .= '?' . $_SERVER['QUERY_STRING'];
         }
     } else {
         $requestUrl = '';
     }
     $requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
     $tmp = explode('?', $requestUrl, 2);
     $url->setPath(Strings::replace($tmp[0], $this->urlFilters['path']));
     $url->setQuery(isset($tmp[1]) ? $tmp[1] : '');
     // normalized url
     $url->canonicalize();
     $url->setPath(Strings::fixEncoding($url->getPath()));
     // detect script path
     if (isset($_SERVER['SCRIPT_NAME'])) {
         $script = $_SERVER['SCRIPT_NAME'];
     } elseif (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME']) && strncmp($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])) === 0) {
         $script = '/' . ltrim(strtr(substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])), '\\', '/'), '/');
     } else {
         $script = '/';
     }
     $path = strtolower($url->getPath()) . '/';
     $script = strtolower($script) . '/';
     $max = min(strlen($path), strlen($script));
     for ($i = 0; $i < $max; $i++) {
         if ($path[$i] !== $script[$i]) {
             break;
         } elseif ($path[$i] === '/') {
             $url->setScriptPath(substr($url->getPath(), 0, $i + 1));
         }
     }
     // GET, POST, COOKIE
     $useFilter = !in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags');
     parse_str($url->getQuery(), $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();
     // remove f*****g quotes, control characters and check encoding
     if ($gpc || !$this->binary) {
         $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->binary && is_string($k) && (!preg_match(self::CHARS, $k) || preg_last_error())) {
                     // invalid key -> ignore
                 } elseif (is_array($v)) {
                     $list[$key][$k] = $v;
                     $list[] =& $list[$key][$k];
                 } else {
                     if ($gpc && !$useFilter) {
                         $v = stripSlashes($v);
                     }
                     if (!$this->binary && (!preg_match(self::CHARS, $v) || preg_last_error())) {
                         $v = '';
                     }
                     $list[$key][$k] = $v;
                 }
             }
         }
         unset($list, $key, $val, $k, $v);
     }
     // FILES and create FileUpload objects
     $files = array();
     $list = array();
     if (!empty($_FILES)) {
         foreach ($_FILES as $k => $v) {
             if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $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->binary && (!preg_match(self::CHARS, $v['name']) || preg_last_error())) {
                 $v['name'] = '';
             }
             if ($v['error'] !== UPLOAD_ERR_NO_FILE) {
                 $v['@'] = new FileUpload($v);
             }
             continue;
         }
         foreach ($v['name'] as $k => $foo) {
             if (!$this->binary && is_string($k) && (!preg_match(self::CHARS, $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]);
         }
     }
     // HEADERS
     if (function_exists('apache_request_headers')) {
         $headers = apache_request_headers();
     } 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($k, '_', '-')] = $v;
         }
     }
     $remoteAddr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL;
     $remoteHost = isset($_SERVER['REMOTE_HOST']) ? $_SERVER['REMOTE_HOST'] : NULL;
     // proxy
     foreach ($this->proxies as $proxy) {
         if (Helpers::ipMatch($remoteAddr, $proxy)) {
             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                 $remoteAddr = trim(current(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])));
             }
             if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
                 $remoteHost = trim(current(explode(',', $_SERVER['HTTP_X_FORWARDED_HOST'])));
             }
             break;
         }
     }
     $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'];
     }
     return new Request($url, $query, $post, $files, $cookies, $headers, $method, $remoteAddr, $remoteHost);
 }
Esempio n. 20
0
 /**
  * Creates current HttpRequest object.
  * @return Request
  */
 public function createHttpRequest()
 {
     // DETECTS URI, base path and script path of the request.
     $url = new UrlScript();
     $url->scheme = !empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http';
     $url->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
     $url->password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
     // host & port
     if ((isset($_SERVER[$tmp = 'HTTP_HOST']) || isset($_SERVER[$tmp = 'SERVER_NAME'])) && preg_match('#^([a-z0-9_.-]+|\\[[a-fA-F0-9:]+\\])(:\\d+)?\\z#', $_SERVER[$tmp], $pair)) {
         $url->host = strtolower($pair[1]);
         if (isset($pair[2])) {
             $url->port = (int) substr($pair[2], 1);
         } elseif (isset($_SERVER['SERVER_PORT'])) {
             $url->port = (int) $_SERVER['SERVER_PORT'];
         }
     }
     // path & query
     if (isset($_SERVER['REQUEST_URI'])) {
         // Apache, IIS 6.0
         $requestUrl = $_SERVER['REQUEST_URI'];
     } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
         // IIS 5.0 (PHP as CGI ?)
         $requestUrl = $_SERVER['ORIG_PATH_INFO'];
         if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
             $requestUrl .= '?' . $_SERVER['QUERY_STRING'];
         }
     } else {
         $requestUrl = '';
     }
     $requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
     $tmp = explode('?', $requestUrl, 2);
     $url->path = Strings::replace($tmp[0], $this->urlFilters['path']);
     $url->query = isset($tmp[1]) ? $tmp[1] : '';
     // normalized url
     $url->canonicalize();
     $url->path = Strings::fixEncoding($url->path);
     // detect script path
     if (isset($_SERVER['SCRIPT_NAME'])) {
         $script = $_SERVER['SCRIPT_NAME'];
     } elseif (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME']) && strncmp($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])) === 0) {
         $script = '/' . ltrim(strtr(substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])), '\\', '/'), '/');
     } else {
         $script = '/';
     }
     $path = strtolower($url->path) . '/';
     $script = strtolower($script) . '/';
     $max = min(strlen($path), strlen($script));
     for ($i = 0; $i < $max; $i++) {
         if ($path[$i] !== $script[$i]) {
             break;
         } elseif ($path[$i] === '/') {
             $url->scriptPath = substr($url->path, 0, $i + 1);
         }
     }
     // GET, POST, COOKIE
     $useFilter = !in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags');
     parse_str($url->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);
     // remove f*****g quotes and check (and optionally convert) encoding
     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())) {
                     // invalid key -> ignore
                 } 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 = Strings::fixEncoding($v);
                         } else {
                             if (!Strings::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 and create FileUpload objects
     $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, '', Strings::fixEncoding($v['name']));
             }
             $v['@'] = new FileUpload($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);
     // HEADERS
     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 Request($url, $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);
 }
Esempio n. 21
0
 /**
  * Is the request is sent via secure channel (https).
  * @return bool
  */
 public function isSecured()
 {
     return $this->url->getScheme() === 'https';
 }
Esempio n. 22
0
 public function update(Items\Base $item = NULL, $data)
 {
     try {
         $actualUrl = $this->request->getUrl();
         $urlScript = new Nette\Http\UrlScript($data);
         $urlScript->setScriptPath($actualUrl->getScriptPath());
         if ($urlScript->getHost() == $actualUrl->getHost()) {
             $request = new Nette\Http\Request($urlScript);
             $appRequest = $this->router->match($request);
             if (!is_null($appRequest)) {
                 $data = $appRequest->getPresenterName();
                 $data .= ':' . $appRequest->getParameter('action');
                 $fragment = $urlScript->getFragment();
                 if ($fragment != '') {
                     $data .= '#' . $fragment;
                 }
                 $parameters = $appRequest->getParameters();
                 unset($parameters['action']);
                 if (count($parameters) > 0) {
                     $data .= ', ' . Nette\Utils\Json::encode($parameters);
                 }
             }
         }
     } catch (Nette\InvalidArgumentException $e) {
     }
     return $data;
 }
Esempio n. 23
0
 public function __construct(Nette\Application\IRouter $router, Nette\Http\IRequest $httpRequest)
 {
     $this->router = $router;
     $this->refUrl = $httpRequest->getUrl();
     $this->refUrlHost = $this->refUrl->getHostUrl();
 }
Esempio n. 24
0
 function createHttpRequest()
 {
     $url = new UrlScript();
     $url->scheme = isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https' : 'http';
     $url->user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
     $url->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('');
     }
     $url->host = preg_match('#^[-._a-z0-9]+$#', $pair[0]) ? $pair[0] : '';
     if (isset($pair[1])) {
         $url->port = (int) $pair[1];
     } elseif (isset($_SERVER['SERVER_PORT'])) {
         $url->port = (int) $_SERVER['SERVER_PORT'];
     }
     if (isset($_SERVER['REQUEST_URI'])) {
         $requestUrl = $_SERVER['REQUEST_URI'];
     } elseif (isset($_SERVER['ORIG_PATH_INFO'])) {
         $requestUrl = $_SERVER['ORIG_PATH_INFO'];
         if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING'] != '') {
             $requestUrl .= '?' . $_SERVER['QUERY_STRING'];
         }
     } else {
         $requestUrl = '';
     }
     $requestUrl = Strings::replace($requestUrl, $this->urlFilters['url']);
     $tmp = explode('?', $requestUrl, 2);
     $url->path = Strings::replace($tmp[0], $this->urlFilters['path']);
     $url->query = isset($tmp[1]) ? $tmp[1] : '';
     $url->canonicalize();
     $url->path = Strings::fixEncoding($url->path);
     if (isset($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME']) && strncmp($_SERVER['DOCUMENT_ROOT'], $_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])) === 0) {
         $script = '/' . ltrim(strtr(substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])), '\\', '/'), '/');
     } elseif (isset($_SERVER['SCRIPT_NAME'])) {
         $script = $_SERVER['SCRIPT_NAME'];
     } else {
         $script = '/';
     }
     if (strncasecmp($url->path . '/', $script . '/', strlen($script) + 1) === 0) {
         $url->scriptPath = substr($url->path, 0, strlen($script));
     } elseif (strncasecmp($url->path, $script, strrpos($script, '/') + 1) === 0) {
         $url->scriptPath = substr($url->path, 0, strrpos($script, '/') + 1);
     } else {
         $url->scriptPath = '/';
     }
     $useFilter = !in_array(ini_get('filter.default'), array('', 'unsafe_raw')) || ini_get('filter.default_flags');
     parse_str($url->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 = Strings::fixEncoding($v);
                         } else {
                             if (!Strings::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, '', Strings::fixEncoding($v['name']));
             }
             $v['@'] = new FileUpload($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 Request($url, $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);
 }