54 lines
2.0 KiB
PHP
54 lines
2.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http;
|
|
|
|
use function App\Support\view;
|
|
|
|
final class Response
|
|
{
|
|
public static function securityHeaders(bool $https): void
|
|
{
|
|
header('X-Content-Type-Options: nosniff');
|
|
header('Referrer-Policy: no-referrer');
|
|
header('Cache-Control: no-store, private');
|
|
header("Content-Security-Policy: default-src 'self'; script-src 'self' https://developer.eitaa.com; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'self'");
|
|
if ($https) {
|
|
header('Strict-Transport-Security: max-age=31536000');
|
|
}
|
|
// Native Eitaa can embed the page: no frame-blocking header.
|
|
}
|
|
|
|
public static function json(array $data, int $status = 200): void
|
|
{
|
|
http_response_code($status);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE);
|
|
}
|
|
|
|
public static function error(\Throwable $error, bool $json = true): void
|
|
{
|
|
$status = $error instanceof HttpException ? $error->status : 500;
|
|
$message = $error instanceof HttpException
|
|
? $error->getMessage()
|
|
: 'خطای داخلی سرور. تنظیمات و دسترسی پوشهٔ ذخیرهسازی را بررسی کنید.';
|
|
header('Cache-Control: no-store');
|
|
if ($json) {
|
|
self::json(['ok' => false, 'error' => $message], $status);
|
|
return;
|
|
}
|
|
http_response_code($status);
|
|
header('Content-Type: text/plain; charset=utf-8');
|
|
echo $message;
|
|
}
|
|
|
|
public static function postcard(): void
|
|
{
|
|
header('Content-Type: image/svg+xml; charset=utf-8');
|
|
header('Content-Disposition: attachment; filename="madar-postcard.svg"');
|
|
header('X-Content-Type-Options: nosniff');
|
|
header("Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox");
|
|
view('postcard');
|
|
}
|
|
}
|