<?php
/**
 * Simple Antibot Protection
 * Blocks only obvious bots, allows all real browsers
 * Zero false positives for humans
 */

// Prevent direct access
if (!defined('APP_ACCESS')) {
    die('Direct access not permitted');
}

class SimpleAntibot {
    
    private $userAgent;
    private $redirectUrl = 'https://www.spotify.com';
    
    // List of obvious bot keywords
    private $botKeywords = [
        'bot', 'crawler', 'spider', 'scraper',
        'googlebot', 'bingbot', 'yandexbot', 'baiduspider',
        'slurp', 'duckduckbot', 'facebookexternalhit', 'twitterbot',
        'linkedinbot', 'whatsapp', 'telegram',
        'curl', 'wget', 'python-requests', 'python-urllib',
        'java/', 'apache-httpclient', 'go-http-client',
        'node-fetch', 'axios', 'okhttp', 'scrapy',
        'phantomjs', 'headless', 'selenium', 'webdriver',
        'postman', 'insomnia', 'httpie',
        'nmap', 'nikto', 'sqlmap', 'masscan', 'zmap',
        'shodan', 'censys', 'zgrab',
        'ahrefsbot', 'semrushbot', 'mj12bot', 'dotbot',
        'rogerbot', 'exabot', 'archive.org_bot',
        'ia_archiver', 'mediapartners-google',
        'adsbot-google', 'feedfetcher-google',
        'petalbot', 'yisouspider', 'sogou',
        'uptimerobot', 'pingdom', 'statuscake',
        'site24x7', 'newrelic', 'datadog'
    ];
    
    public function __construct() {
        $this->userAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
    }
    
    /**
     * Check if visitor is a bot
     */
    public function check() {
        // Always allow localhost
        if ($this->isLocalhost()) {
            return;
        }
        
        // Check for empty user agent
        if (empty($this->userAgent)) {
            $this->blockBot('Empty User Agent');
            return;
        }
        
        // Check for bot keywords in user agent
        foreach ($this->botKeywords as $keyword) {
            if (strpos($this->userAgent, $keyword) !== false) {
                $this->blockBot('Bot Keyword: ' . $keyword);
                return;
            }
        }
        
        // If we reach here, it's likely a real browser - allow access
    }
    
    /**
     * Check if request is from localhost
     */
    private function isLocalhost() {
        $ip = $_SERVER['REMOTE_ADDR'];
        
        // IPv4 localhost
        if ($ip === '127.0.0.1') {
            return true;
        }
        
        // IPv6 localhost
        if ($ip === '::1') {
            return true;
        }
        
        // Private network ranges
        if (strpos($ip, '192.168.') === 0) {
            return true;
        }
        
        if (strpos($ip, '10.') === 0) {
            return true;
        }
        
        if (strpos($ip, '172.') === 0) {
            $parts = explode('.', $ip);
            if (isset($parts[1]) && $parts[1] >= 16 && $parts[1] <= 31) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Block bot and redirect
     */
    private function blockBot($reason) {
        // Log the block
        $logMessage = sprintf(
            "[%s] BLOCKED - IP: %s | Reason: %s | UA: %s\n",
            date('Y-m-d H:i:s'),
            $_SERVER['REMOTE_ADDR'],
            $reason,
            substr($_SERVER['HTTP_USER_AGENT'], 0, 200)
        );
        
        if (defined('BOTS_LOG_FILE')) {
            @file_put_contents(BOTS_LOG_FILE, $logMessage, FILE_APPEND);
        }
        
        // Show 404 and redirect
        header('HTTP/1.1 404 Not Found');
        header('Location: ' . $this->redirectUrl);
        echo '<!DOCTYPE html>
<html>
<head>
    <title>404 Not Found</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .container {
            text-align: center;
            background: white;
            padding: 50px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            font-size: 72px;
            margin: 0;
            color: #e74c3c;
        }
        p {
            font-size: 18px;
            color: #666;
        }
        a {
            color: #3498db;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>404</h1>
        <p>Sorry, the page you are looking for does not exist.</p>
        <a href="/">Home</a>
    </div>
</body>
</html>';
        exit;
    }
}

// Auto-run if antibot is enabled
if (defined('ANTIBOT_ENABLED') && ANTIBOT_ENABLED === true) {
    $simpleAntibot = new SimpleAntibot();
    $simpleAntibot->check();
}

?>
