<?php
error_reporting(0);
session_start();

function validateIP($ip) {
    return filter_var($ip, FILTER_VALIDATE_IP) !== false;
}

if (isset($_POST['block_user'])) {
    $userIP = $_POST['block_user'];

    if (validateIP($userIP)) {
        $blockedIPs = file_get_contents('blocked_ips.txt');
        if (strpos($blockedIPs, $userIP) === false) {
            file_put_contents('blocked_ips.txt', $userIP . "\n", FILE_APPEND);
            $user_id = $_POST['user_id'];
            $filePath = './get-panel/info-panel.txt';
            $originalWord = $user_id . "," . "Message Explain Page";
            $replacementWord = "This User Is Block !?". "," . "This User Is Block!?";
            $fileContent = file_get_contents($filePath);
            $modifiedContent = str_replace($originalWord, $replacementWord, $fileContent);
            file_put_contents($filePath, $modifiedContent);
            $_SESSION["message_panel"] = "This User Is Block - " . $userIP;
            header("Location: ./main_panel.php");
            exit();
        } else {
            header("Location: ./main_panel.php");
            $_SESSION["message_panel"] = "This User Is Already Block - " . $userIP;
            exit();
        }
    } else {
        echo "Invalid IP address.";
    }
}
header("Location: ./main_panel.php");
?>
