/** * If HTTP_AUTHORIZATION does not exist tries to get it from * getallheaders() when available. * * @param Environment $environment The Slim application Environment * * @return Environment */ public static function determineAuthorization(Environment $environment) { $authorization = $environment->get('HTTP_AUTHORIZATION'); if (null === $authorization && is_callable('getallheaders')) { $headers = getallheaders(); $headers = array_change_key_case($headers, CASE_LOWER); if (isset($headers['authorization'])) { $environment->set('HTTP_AUTHORIZATION', $headers['authorization']); } } return $environment; }
/** * Create new Uri from environment. * * @param Environment $env * * @return self */ public static function createFromEnvironment(Environment $env) { // Scheme $isSecure = $env->get('HTTPS'); $scheme = empty($isSecure) || $isSecure === 'off' ? 'http' : 'https'; // Authority: Username and password $username = $env->get('PHP_AUTH_USER', ''); $password = $env->get('PHP_AUTH_PW', ''); // Authority: Host if ($env->has('HTTP_HOST')) { $host = $env->get('HTTP_HOST'); } else { $host = $env->get('SERVER_NAME'); } // Authority: Port $port = (int) $env->get('SERVER_PORT', 80); if (preg_match('/^(\\[[a-fA-F0-9:.]+\\])(:\\d+)?\\z/', $host, $matches)) { $host = $matches[1]; if ($matches[2]) { $port = (int) substr($matches[2], 1); } } else { $pos = strpos($host, ':'); if ($pos !== false) { $port = (int) substr($host, $pos + 1); $host = strstr($host, ':', true); } } // 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 $fragment = ''; // Build Uri $uri = new static($scheme, $host, $port, $virtualPath, $queryString, $fragment, $username, $password); if ($basePath) { $uri = $uri->withBasePath($basePath); } return $uri; }
/** * Create new Uri from environment. * * @param Environment $env * * @return self */ public static function createFromEnvironment(Environment $env) { // Scheme if ($env->has('HTTP_X_FORWARDED_PROTO')) { $scheme = $env->get('HTTP_X_FORWARDED_PROTO'); // Will be "http" or "https" } else { $isSecure = $env->get('HTTPS'); $scheme = empty($isSecure) || $isSecure === 'off' ? 'http' : 'https'; } // Authority: Username and password $username = $env->get('PHP_AUTH_USER', ''); $password = $env->get('PHP_AUTH_PW', ''); // Authority: Host if ($env->has('HTTP_X_FORWARDED_HOST')) { $host = trim(current(explode(',', $env->get('HTTP_X_FORWARDED_HOST')))); } elseif ($env->has('HTTP_HOST')) { $host = $env->get('HTTP_HOST'); } else { $host = $env->get('SERVER_NAME'); } // Authority: Port $pos = strpos($host, ':'); if ($pos !== false) { $port = (int) substr($host, $pos + 1); $host = strstr($host, ':', true); } else { $port = (int) $env->get('SERVER_PORT', 80); } // Path $requestScriptName = parse_url($env->get('SCRIPT_NAME'), PHP_URL_PATH); $requestScriptDir = dirname($requestScriptName); $requestUri = parse_url($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 $fragment = ''; // Build Uri $uri = new static($scheme, $host, $port, $virtualPath, $queryString, $fragment, $username, $password); if ($basePath) { $uri = $uri->withBasePath($basePath); } return $uri; }