115 lines
3.5 KiB
PHP
115 lines
3.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class Http
|
|
{
|
|
public static function bootstrapGlobals(): void
|
|
{
|
|
if (PHP_SAPI !== 'cli') {
|
|
return;
|
|
}
|
|
|
|
$query = getenv('QUERY_STRING') ?: '';
|
|
$method = strtoupper((string)(getenv('REQUEST_METHOD') ?: 'GET'));
|
|
|
|
$_GET = [];
|
|
if ($query !== '') {
|
|
parse_str($query, $_GET);
|
|
}
|
|
|
|
$_POST = [];
|
|
if ($method === 'POST') {
|
|
$rawPost = (string)(getenv('POST') ?: '');
|
|
if ($rawPost === '') {
|
|
$rawPost = (string)(getenv('HTTP_POST') ?: '');
|
|
}
|
|
|
|
$contentType = strtolower((string)(getenv('CONTENT_TYPE') ?: ''));
|
|
$isUrlEncoded = ($contentType === '' || str_contains($contentType, 'application/x-www-form-urlencoded'));
|
|
if ($rawPost === '' && $isUrlEncoded) {
|
|
$stdin = @file_get_contents('php://stdin');
|
|
if (is_string($stdin) && $stdin !== '') {
|
|
$rawPost = $stdin;
|
|
}
|
|
}
|
|
|
|
if ($rawPost !== '') {
|
|
parse_str($rawPost, $_POST);
|
|
}
|
|
}
|
|
|
|
$_REQUEST = array_merge($_GET, $_POST);
|
|
$_SERVER['REQUEST_METHOD'] = $method;
|
|
$_SERVER['QUERY_STRING'] = $query;
|
|
|
|
foreach ([
|
|
'USER',
|
|
'USERNAME',
|
|
'HOME',
|
|
'LANGUAGE',
|
|
'SESSION_ID',
|
|
'REQUEST_URI',
|
|
'HTTP_HOST',
|
|
'HTTPS',
|
|
'HTTP_USER_AGENT',
|
|
'HTTP_X_FORWARDED_PROTO',
|
|
'HTTP_FRONT_END_HTTPS',
|
|
'REQUEST_SCHEME',
|
|
'SERVER_NAME',
|
|
'SERVER_PORT',
|
|
'REMOTE_ADDR',
|
|
'SKIN',
|
|
] as $key) {
|
|
if (!isset($_SERVER[$key])) {
|
|
$value = getenv($key);
|
|
if ($value !== false) {
|
|
$_SERVER[$key] = $value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function method(): string
|
|
{
|
|
return strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET'));
|
|
}
|
|
|
|
public static function isPost(): bool
|
|
{
|
|
return self::method() === 'POST';
|
|
}
|
|
|
|
/**
|
|
* @param array<string,mixed> $source
|
|
*/
|
|
public static function getString(array $source, string $key, string $default = ''): string
|
|
{
|
|
$value = $source[$key] ?? $default;
|
|
return is_scalar($value) ? trim((string)$value) : $default;
|
|
}
|
|
|
|
public static function redirect(string $url): void
|
|
{
|
|
if (PHP_SAPI === 'cli') {
|
|
header('Content-Type: text/html; charset=UTF-8');
|
|
echo self::redirectDocument($url);
|
|
exit;
|
|
}
|
|
header('Location: ' . $url, true, 302);
|
|
exit;
|
|
}
|
|
|
|
public static function redirectDocument(string $url): string
|
|
{
|
|
$safeUrl = htmlspecialchars($url, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
$jsonUrl = json_encode($url, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT);
|
|
return '<!doctype html><html lang="pl"><head><meta charset="utf-8">' .
|
|
'<meta name="viewport" content="width=device-width, initial-scale=1">' .
|
|
'<meta http-equiv="refresh" content="0;url=' . $safeUrl . '">' .
|
|
'<title>Przekierowanie</title></head><body>' .
|
|
'<p>Przekierowanie... <a href="' . $safeUrl . '">Kontynuuj</a></p>' .
|
|
'<script>window.location.replace(' . $jsonUrl . ');</script>' .
|
|
'</body></html>';
|
|
}
|
|
}
|