<?php

function affscale_current_public_url(): string
{
    $path = parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH) ?: '/';
    if ($path === '' || $path === '/' || $path === '/index.html' || $path === '/index.php') {
        return 'https://senpaii.co';
    }
    if (preg_match('/^(.+)\.php$/i', $path, $match)) {
        $path = $match[1] !== '' ? $match[1] : '/';
    }
    if ($path === '/') {
        return 'https://senpaii.co';
    }
    return 'https://senpaii.co' . $path;
}

if (!defined('AFFSCALE_SINGLE_CANONICAL_BLOCK')) {
    define('AFFSCALE_SINGLE_CANONICAL_BLOCK', true);
    ob_start(function ($html) {
        if (!is_string($html) || (stripos($html, '<html') === false && stripos($html, '</head>') === false && stripos($html, '<title') === false)) {
            return $html;
        }
        $canonicalUrl = htmlspecialchars(affscale_current_public_url(), ENT_QUOTES, 'UTF-8');
        $html = preg_replace('/<link\b(?=[^>]*\brel=["\']canonical["\'])[^>]*>\s*/i', '', $html);
        $html = preg_replace('/<link\b(?=[^>]*\brel=["\']alternate["\'])(?=[^>]*\bhreflang=)[^>]*>\s*/i', '', $html);
        $html = preg_replace('/<link\b(?=[^>]*\bhreflang=)(?=[^>]*\brel=["\']alternate["\'])[^>]*>\s*/i', '', $html);
        $html = preg_replace('/<meta\s+property=["\']og:url["\'][^>]*>\s*/i', '', $html);
        $headBlock = '<meta property="og:url" content="' . $canonicalUrl . '">' . "\n"
            . '<link rel="canonical" href="' . $canonicalUrl . '">';
        if (stripos($html, '</head>') !== false) {
            return preg_replace('/<\/head>/i', $headBlock . "\n</head>", $html, 1);
        }
        return $headBlock . "\n" . $html;
    });
}

require_once __DIR__ . '/affscale-chain-client.php';
affscale_chain_host_redirect();

$requestPath = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';

function affscale_apply_locale_selection(string $html, string $localeCode, string $localeLabel, string $htmlLang): string
{
    $safeLabel = htmlspecialchars($localeLabel, ENT_QUOTES, 'UTF-8');
    $safeLang = htmlspecialchars($htmlLang, ENT_QUOTES, 'UTF-8');
    $html = preg_replace('/(<span class="lang-text">)[^<]*(<\/span>)/u', '$1' . $safeLabel . '$2', $html, 1);
    $html = preg_replace('/<html\b([^>]*)\blang=["\'][^"\']*["\']([^>]*)>/iu', '<html$1lang="' . $safeLang . '"$2>', $html, 1);
    $html = str_replace('class="lang-option active"', 'class="lang-option"', $html);
    $html = str_replace('class="mobile-lang active"', 'class="mobile-lang"', $html);
    $html = preg_replace('/(<a class="(?:lang-option|mobile-lang)"[^>]*)\saria-current="true"([^>]*>)/u', '$1$2', $html);
    $code = preg_quote($localeCode, '/');
    $html = preg_replace('/class="lang-option"([^>]*data-locale="' . $code . '"[^>]*)>/u', 'class="lang-option active"$1 aria-current="true">', $html, 1);
    $html = preg_replace('/class="mobile-lang"([^>]*data-locale="' . $code . '"[^>]*)>/u', 'class="mobile-lang active"$1 aria-current="true">', $html, 1);
    return $html;
}
$legacyRedirects = [
    '/fr-fr' => 'https://senpaii.co/',
    '/fr-fr/' => 'https://senpaii.co/',
    '/product-page/can-pop' => 'https://senpaii.co/',
    '/product-page/can-pop/' => 'https://senpaii.co/',
    '/product-page/vegan-hibachi' => 'https://senpaii.co/',
    '/product-page/vegan-hibachi/' => 'https://senpaii.co/',
    '/product-page/breakfast-sandwich' => 'https://senpaii.co/',
    '/product-page/breakfast-sandwich/' => 'https://senpaii.co/',
];
if (isset($legacyRedirects[$requestPath])) {
    header('Location: ' . $legacyRedirects[$requestPath], true, 301);
    exit;
}
$pages = [
    '/' => __DIR__ . '/index.html',
    '' => __DIR__ . '/index.html',
    '/apk' => __DIR__ . '/index.html',
    '/apk/' => __DIR__ . '/index.html',
    '/about' => __DIR__ . '/__pages/about.html',
    '/about/' => __DIR__ . '/__pages/about.html',
    '/contact' => __DIR__ . '/__pages/contact.html',
    '/contact/' => __DIR__ . '/__pages/contact.html',
    '/mentions-legales' => __DIR__ . '/__pages/mentions-legales.html',
    '/mentions-legales/' => __DIR__ . '/__pages/mentions-legales.html',
    '/responsible-gambling' => __DIR__ . '/__pages/responsible-gambling.html',
    '/responsible-gambling/' => __DIR__ . '/__pages/responsible-gambling.html',
    '/18-plus' => __DIR__ . '/__pages/18-plus.html',
    '/18-plus/' => __DIR__ . '/__pages/18-plus.html',
    '/legal-disclaimer' => __DIR__ . '/__pages/legal-disclaimer.html',
    '/legal-disclaimer/' => __DIR__ . '/__pages/legal-disclaimer.html',
    '/affiliate-disclosure' => __DIR__ . '/__pages/affiliate-disclosure.html',
    '/affiliate-disclosure/' => __DIR__ . '/__pages/affiliate-disclosure.html',
    '/terms' => __DIR__ . '/__pages/terms.html',
    '/terms/' => __DIR__ . '/__pages/terms.html',
    '/privacy' => __DIR__ . '/__pages/privacy.html',
    '/privacy/' => __DIR__ . '/__pages/privacy.html',
    '/cookie-policy' => __DIR__ . '/__pages/cookie-policy.html',
    '/cookie-policy/' => __DIR__ . '/__pages/cookie-policy.html',
    '/ios' => __DIR__ . '/__pages/ios.html',
    '/ios/' => __DIR__ . '/__pages/ios.html',
];
$localePaths = [
    '/ci-fr',
    '/ci-fr/',
    '/ru',
    '/ru/',
];
foreach ($localePaths as $localePath) {
    $pages[$localePath] = __DIR__ . '/index.html';
}
$localizedRequestPath = rtrim($requestPath, '/');
if ($localizedRequestPath === '') {
    $localizedRequestPath = '/';
}
$isLocalizedPage = false;
if ($localizedRequestPath === '/ci-fr' || strpos($localizedRequestPath, '/ci-fr/') === 0 || $localizedRequestPath === '/ru') {
    $localizedHtmlPath = __DIR__ . '/__localized' . $localizedRequestPath . '/index.html';
    if (is_file($localizedHtmlPath)) {
        $pages[$requestPath] = $localizedHtmlPath;
        $isLocalizedPage = true;
    }
}
if (!isset($pages[$requestPath])) {
    http_response_code(404);
    header('Content-Type: text/plain; charset=UTF-8');
    echo '404 Not Found';
    exit;
}
$htmlPath = $pages[$requestPath];
if (!is_file($htmlPath)) {
    http_response_code(404);
    header('Content-Type: text/plain; charset=UTF-8');
    echo '404 Not Found';
    exit;
}
$html = file_get_contents($htmlPath);
if ($requestPath === '/ci-fr' || $requestPath === '/ci-fr/') {
    $html = affscale_apply_locale_selection($html, 'fr-CI', "Côte d'Ivoire · Français", 'fr');
} elseif ($requestPath === '/ru' || $requestPath === '/ru/') {
    $html = affscale_apply_locale_selection($html, 'ru', 'Россия · Русский', 'ru');
}
if (!$isLocalizedPage) {
$description = "Téléchargez l'application 1xBet APK pour Android, installez-la gratuitement, vérifiez le fichier, mettez-la à jour et ouvrez 1xBet en sécurité.";
$socialImage = 'https://senpaii.co/assets/dextify/1xbet-apk-android-2026.webp';
$html = preg_replace('/<meta\s+name=["\']description["\'][^>]*>\s*/i', '', $html);
$html = preg_replace('/<meta\s+property=["\']og:image["\'][^>]*>\s*/i', '', $html);
$html = preg_replace('/<meta\s+property=["\']og:image:secure_url["\'][^>]*>\s*/i', '', $html);
$html = preg_replace('/<meta\s+name=["\']twitter:image["\'][^>]*>\s*/i', '', $html);
$html = preg_replace('/<meta\s+name=["\']twitter:card["\'][^>]*>\s*/i', '', $html);
$socialMeta = '<meta name="description" content="' . htmlspecialchars($description, ENT_QUOTES, 'UTF-8') . '">' . "
"
    . '<meta property="og:image" content="' . htmlspecialchars($socialImage, ENT_QUOTES, 'UTF-8') . '">' . "
"
    . '<meta property="og:image:secure_url" content="' . htmlspecialchars($socialImage, ENT_QUOTES, 'UTF-8') . '">' . "
"
    . '<meta name="twitter:card" content="summary_large_image">' . "
"
    . '<meta name="twitter:image" content="' . htmlspecialchars($socialImage, ENT_QUOTES, 'UTF-8') . '">' . "
";
$html = preg_replace('/<\/title>/i', "</title>
" . $socialMeta, $html, 1);
$html = preg_replace('/(<img\b[^>]*\bsrc=["\']\/assets\/dextify\/[^"\'?]+)\?v=[^"\']+(["\'])/i', '$1$2', $html);
$headBlock = <<<'HTML'
HTML;
$html = preg_replace('/<meta\s+name=["\']robots["\'][^>]*(?:noindex|none)[^>]*>\s*/i', '', $html);
$html = preg_replace('/<link\b(?=[^>]*\brel=["\']canonical["\'])[^>]*>\s*/i', '', $html);
$html = preg_replace('/<link\b(?=[^>]*\brel=["\']alternate["\'])(?=[^>]*\bhreflang=)[^>]*>\s*/i', '', $html);
$html = preg_replace('/<link\b(?=[^>]*\bhreflang=)(?=[^>]*\brel=["\']alternate["\'])[^>]*>\s*/i', '', $html);
$isGooglebot = strpos(strtolower((string)($_SERVER['HTTP_USER_AGENT'] ?? '')), 'googlebot') !== false;
if ($isGooglebot && stripos($html, '

</head>') !== false) {
    $html = preg_replace('/<\/head>/i', $headBlock . "
</head>", $html, 1);
}
}
header('Content-Type: text/html; charset=UTF-8');
echo ltrim($html);
