<?php

declare(strict_types=1);

$file = __DIR__ . '/index.html';
if (!is_file($file)) { http_response_code(500); exit('index.html bulunamadı'); }

$html = file_get_contents($file);
if ($html === false) { http_response_code(500); exit('index.html okunamadı'); }

$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
      || (($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? '') === 'https');
$host  = $_SERVER['HTTP_HOST'] ?? 'www.muhammedazadi.com';

if (!preg_match('/^[a-z0-9.\-:]+$/i', $host)) $host = 'www.muhammedazadi.com';
$base = ($https ? 'https://' : 'http://') . $host;

const PROD = 'https://www.muhammedazadi.com';

if (stripos($base, PROD) !== 0) {
    $html = preg_replace_callback(
        '~(<meta\s+(?:property|name)="(?:og:image|twitter:image)"\s+content=")' . preg_quote(PROD, '~') . '(/[^"]*")~i',
        static fn(array $m): string => $m[1] . $base . $m[2],
        $html
    ) ?? $html;
}

header('Content-Type: text/html; charset=utf-8');
header('X-Content-Type-Options: nosniff');
header('Cache-Control: public, max-age=0, must-revalidate');
echo $html;
