/** * Create the Request from env. * * @param Environment $env * @return self */ public static function createFromEnvironment(Environment $env) { $method = $env->get('REQUEST_METHOD'); $uri = Uri::createFromEnvironment($env); $header = Headers::createFromEnvironment($env); $cookies = Cookies::parseHeader($header->get('Cookie', array())); $uploadedFile = UploadedFile::createFromEnvironment(); $body = new RequestBody(); return new static($method, $uri, $header, $cookies, $body, $uploadedFile); }
/** * Auth invoke. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response) { // Check CORS(Cross-Origin Resource Sharing) and add allow headers $env = new Environment($_SERVER); $origin = $env->get('HTTP_ORIGIN'); $uri = $request->getUri(); if (!is_null($origin) && $origin != $uri->getScheme() . '://' . $uri->getHost()) { if (method_exists($response, 'withHeader')) { // TODO: Add configure to set allow origin sites for AJAX-CORS $response = $response->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Access-Control-Allow-Credentials', 'true')->withHeader('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')->withHeader('Access-Control-Max-Age', '60')->withHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,Accept'); } } return $response; }
/** * Create new Uri from environment. * * @param Environment $env * @return self */ public static function createFromEnvironment(Environment $env) { $isSecure = $env->get('HTTPS'); $scheme = empty($isSecure) || $isSecure === 'off' ? 'http' : 'https'; $username = $env->get('PHP_AUTH_USER', ''); $password = $env->get('PHP_AUTH_PW', ''); if ($env->has('HTTP_HOST')) { $host = $env->get('HTTP_HOST'); } else { $host = $env->get('SERVER_NAME'); } $port = (int) $env->get('SERVER_PORT', 80); // Path $requestScriptName = parse_url($env->get('SCRIPT_NAME'), PHP_URL_PATH); $requestScriptDir = dirname($requestScriptName); // parse_url() requires a full URL. As we don't extract the domain name or scheme, // we use a stand-in. $requestUri = parse_url('http://example.com' . $env->get('REQUEST_URI'), PHP_URL_PATH); $basePath = ''; $virtualPath = $requestUri; if (stripos($requestUri, $requestScriptName) === 0) { $basePath = $requestScriptName; } elseif ($requestScriptDir !== '/' && stripos($requestUri, $requestScriptDir) === 0) { $basePath = $requestScriptDir; } if ($basePath) { $virtualPath = ltrim(substr($requestUri, strlen($basePath)), '/'); } // Query string $queryString = $env->get('QUERY_STRING', ''); $fragment = ''; $uri = new static($scheme, $host, $port, $virtualPath, $queryString, $fragment, $username, $password); if ($basePath) { $uri = $uri->withBasePath($basePath); } return $uri; }
/** * Get the current App name. * * @param Environment $env * @return string App name * @throws AppException if app not found for the host */ protected function getCurrentApp(Environment $env) { global $CONFIG; $serverName = $env->get('HTTP_HOST'); if ($CONFIG->hostToApp && isset($CONFIG->hostToApp[$serverName]) && $CONFIG->hostToApp[$serverName]) { return (string) $CONFIG->hostToApp[$serverName]; } throw new AppException(sprintf('App not found for %s, please check $CONFIG->hostToApp.', $serverName)); }