63 lines
1.9 KiB
PHP
63 lines
1.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class Http
|
|
{
|
|
public static function bootstrapGlobals(): void
|
|
{
|
|
if (PHP_SAPI !== 'cli') {
|
|
return;
|
|
}
|
|
$_SERVER['REQUEST_METHOD'] = strtoupper((string)(getenv('REQUEST_METHOD') ?: 'GET'));
|
|
foreach ([
|
|
'USER',
|
|
'USERNAME',
|
|
'SESSION_ID',
|
|
'REQUEST_URI',
|
|
'HTTP_HOST',
|
|
'SERVER_NAME',
|
|
'SERVER_PORT',
|
|
'REMOTE_ADDR',
|
|
'HTTP_X_FORWARDED_FOR',
|
|
'HTTP_X_FORWARDED_PROTO',
|
|
] 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 redirectNoStore(string $url): void
|
|
{
|
|
header('Cache-Control: no-store, private');
|
|
header('Pragma: no-cache');
|
|
header('Referrer-Policy: no-referrer');
|
|
header('X-Content-Type-Options: nosniff');
|
|
header('Location: ' . $url, true, 302);
|
|
exit;
|
|
}
|
|
|
|
public static function errorPage(string $message, int $status): void
|
|
{
|
|
http_response_code($status);
|
|
header('Content-Type: text/html; charset=UTF-8');
|
|
header('Cache-Control: no-store, private');
|
|
header('Referrer-Policy: no-referrer');
|
|
$safe = htmlspecialchars($message, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
echo '<!doctype html><html lang="pl"><head><meta charset="utf-8">';
|
|
echo '<meta name="viewport" content="width=device-width, initial-scale=1">';
|
|
echo '<title>Serwer poczty</title></head><body>';
|
|
echo '<h1>Serwer poczty</h1><p>' . $safe . '</p>';
|
|
echo '</body></html>';
|
|
exit;
|
|
}
|
|
}
|