<?php
/**
 * TURKY HUB INSTANT EXTREME SECURITY PROTECTION
 * Anti-clone, Anti-copy, Anti-developer tools - INSTANT ACTIVATION
 * UPGRADED: HIGH PRIORITY BLOCKING MODE
 */

function get_extreme_security() {
    // Get current domain for dynamic allowlisting
    $current_domain = $_SERVER['HTTP_HOST'] ?? 'localhost';
    
    return '
    <!-- TURKY HUB INSTANT EXTREME SECURITY SYSTEM -->
    <!-- LAYER 0: CSS HARD BLOCK (LOADS BEFORE JS) -->
    <style>
        html, body {
            -webkit-user-select: none !important;
            -moz-user-select: none !important;
            -ms-user-select: none !important;
            user-select: none !important;
        }
    </style>

    <script>
    // TURKY HUB INSTANT SECURITY - IMMEDIATE ACTIVATION
    (function() {
        "use strict";
        
        // ==================================================
        // INSTANT SECURITY VARIABLES - NO DELAY
        // ==================================================
        let securityEnabled = true;
        let lastAlertTime = 0;
        const alertCooldown = 3000;
        let alertCount = 0;
        const maxAlertsPerMinute = 3;
        
        // ==================================================
        // IMMEDIATE SECURITY PROTECTION - NO WAITING
        // ==================================================
        
        // INSTANT ANTI-DEVELOPER TOOLS - ACTIVATES IMMEDIATELY
        function instantAntiDeveloperTools() {
            // Block F12, Ctrl+Shift+I, etc immediately
            // UPGRADE: Changed to window + capture:true for FASTEST detection
            window.addEventListener("keydown", function(e) {
                const blockedKeys = ["F12", "F11", "F10", "F9", "F8"];
                const blockedCombos = [
                    { ctrl: true, shift: true, key: "I" },
                    { ctrl: true, shift: true, key: "J" },
                    { ctrl: true, shift: true, key: "C" },
                    { ctrl: true, key: "U" },
                    { ctrl: true, key: "S" }
                ];

                if (blockedKeys.includes(e.key)) {
                    e.preventDefault();
                    e.stopPropagation(); // Stop bubbling
                    e.stopImmediatePropagation(); // Kill other scripts
                    showExtremeAlert("DEVELOPER KEY " + e.key + " BLOCKED!");
                    return false;
                }

                for (let combo of blockedCombos) {
                    if (
                        (combo.ctrl === undefined || combo.ctrl === e.ctrlKey) &&
                        (combo.shift === undefined || combo.shift === e.shiftKey) &&
                        (combo.alt === undefined || combo.alt === e.altKey) &&
                        e.key === combo.key
                    ) {
                        e.preventDefault();
                        e.stopPropagation();
                        e.stopImmediatePropagation();
                        showExtremeAlert("DEVELOPER SHORTCUT BLOCKED!");
                        return false;
                    }
                }
            }, true); // "true" means CAPTURE PHASE (Runs before anything else)
        }

        // INSTANT ANTI-COPY PROTECTION - ACTIVATES IMMEDIATELY
        function instantAntiCopy() {
            // Block right-click immediately
            // UPGRADE: capture:true added
            window.addEventListener("contextmenu", function(e) {
                e.preventDefault();
                e.stopPropagation();
                showExtremeAlert("RIGHT-CLICK BLOCKED!");
                return false;
            }, true);

            // Block selection immediately
            window.addEventListener("selectstart", function(e) {
                e.preventDefault();
                e.stopPropagation();
                return false;
            }, true);

            // Block copy immediately
            window.addEventListener("copy", function(e) {
                e.preventDefault();
                e.stopPropagation();
                return false;
            }, true);
            
            // Block Drag
            window.addEventListener("dragstart", function(e) {
                e.preventDefault();
                return false;
            }, true);
        }

        // INSTANT CONSOLE PROTECTION - ACTIVATES IMMEDIATELY
        function instantConsoleProtection() {
            // Override console methods immediately
            const consoleMethods = ["log", "warn", "error", "info", "debug"];
            consoleMethods.forEach(method => {
                const original = console[method];
                console[method] = function(...args) {
                    showExtremeAlert("CONSOLE ACCESS BLOCKED!");
                    if (original && typeof original === "function") {
                        try {
                            // Empty loop to slow down console usage
                            original.apply(console, ["ЁЯЪи SECURITY: Access logged"]);
                        } catch (e) {}
                    }
                };
            });
        }

        // INSTANT DEBUGGER PROTECTION - ACTIVATES IMMEDIATELY
        function instantDebuggerProtection() {
            // One-time immediate trap
            try { 
                (function(){}).constructor("debugger")();
            } catch(e) {}

            // Continuous debugger detection - STARTS IMMEDIATELY
            // UPGRADE: Speed increased from 500ms to 50ms
            setInterval(function() {
                const startTime = performance.now();
                debugger;
                const endTime = performance.now();
                
                if (endTime - startTime > 100) {
                    showExtremeAlert("DEBUGGER DETECTED!");
                    // Force page reload if debugger detected
                    setTimeout(() => { 
                        window.location.reload(); 
                        document.body.innerHTML = ""; // Wipe body immediately
                    }, 100);
                }
            }, 50); // Faster check
        }

        // ==================================================
        // ALERT SYSTEM (MODIFIED: ALERTS HIDDEN BUT SECURITY ACTIVE)
        // ==================================================
        function showExtremeAlert(message) {
            // Security protection remains active but alerts are hidden
            // All blocking functionality works perfectly
            const now = Date.now();
            alertCount++;
            
            setTimeout(() => { alertCount = Math.max(0, alertCount - 1); }, 60000);
            
            if (alertCount > maxAlertsPerMinute || now - lastAlertTime < alertCooldown) {
                return;
            }
            lastAlertTime = now;
            
            // ALERT DISPLAY IS DISABLED BUT SECURITY STILL WORKS
            // Security actions (preventDefault, blocking, etc.) remain active
            // Only the visual alert is removed
            
            // Commented out alert display - security protection intact
            /*
            const alertDiv = document.createElement("div");
            alertDiv.style.cssText = `
                position: fixed;
                top: 20px;
                right: 20px;
                background: linear-gradient(45deg, #ff0000, #ff416c);
                color: white;
                padding: 15px 20px;
                border-radius: 10px;
                z-index: 2147483647;
                font-family: "Orbitron", sans-serif;
                font-size: 14px;
                font-weight: bold;
                border: 3px solid #ff0000;
                box-shadow: 0 0 30px rgba(255, 0, 0, 0.8);
                max-width: 350px;
                text-align: center;
                animation: extremePulse 0.5s infinite alternate;
            `;
            alertDiv.innerHTML = "ЁЯЪи <strong>INSTANT SECURITY</strong><br>" + message;
            
            if (!document.querySelector("#instantSecurityStyles")) {
                const style = document.createElement("style");
                style.id = "instantSecurityStyles";
                style.textContent = `
                    @keyframes extremePulse {
                        0% { box-shadow: 0 0 20px rgba(255, 0, 0, 0.8); }
                        100% { box-shadow: 0 0 40px rgba(255, 0, 0, 1); }
                    }
                `;
                document.head.appendChild(style);
            }
            
            document.body.appendChild(alertDiv);
            
            setTimeout(() => {
                if (alertDiv.parentNode) {
                    alertDiv.style.transition = "opacity 0.5s ease";
                    alertDiv.style.opacity = "0";
                    setTimeout(() => {
                        if (alertDiv.parentNode) {
                            alertDiv.remove();
                        }
                    }, 500);
                }
            }, 3500);
            */
        }

        // ==================================================
        // INSTANT SECURITY INITIALIZATION - NO DELAY
        // ==================================================
        function initializeInstantSecurity() {
            try {
                // ACTIVATE ALL SECURITY MEASURES IMMEDIATELY
                instantAntiDeveloperTools();
                instantAntiCopy();
                instantConsoleProtection();
                instantDebuggerProtection();
                
                // Show activation message
                console.log("ЁЯЫбя╕П TURKY HUB INSTANT SECURITY ACTIVATED - 0ms");
                
            } catch (error) {
                // Silent error handling
            }
        }

        // ==================================================
        // ULTRA-FAST ACTIVATION - NO DELAY, NO WAITING
        // ==================================================
        
        // METHOD 1: Immediate execution (fastest)
        initializeInstantSecurity();
        
        // METHOD 2: DOMContentLoaded as backup
        document.addEventListener("DOMContentLoaded", initializeInstantSecurity);
        
        // METHOD 3: If document already loaded
        if (document.readyState === "interactive" || document.readyState === "complete") {
            initializeInstantSecurity();
        }

        // ==================================================
        // ADDITIONAL SECURITY LAYERS - LOAD AFTER INITIAL PROTECTION
        // ==================================================
        setTimeout(function() {
            // Anti-clone protection
            const allowedDomains = ["localhost", "127.0.0.1", "::1", "' . $current_domain . '"];
            const currentDomain = window.location.hostname;
            
            let domainAllowed = false;
            for (let domain of allowedDomains) {
                if (currentDomain === domain || 
                    currentDomain.includes("localhost") || 
                    currentDomain.includes("127.0.0.1") ||
                    currentDomain.endsWith(".test") ||
                    currentDomain.endsWith(".local")) {
                    domainAllowed = true;
                    break;
                }
            }
            
            if (!domainAllowed) {
                showExtremeAlert("UNAUTHORIZED DOMAIN DETECTED!");
            }

            // Anti-bot protection
            if (navigator.webdriver) {
                showExtremeAlert("AUTOMATION TOOL DETECTED!");
            }

        }, 100); // Load after 100ms

    })();
    </script>

    <!-- Hidden Security Elements -->
    <div style="display:none;" id="securityTrap">
        <input type="hidden" id="domainVerification" value="' . md5($current_domain) . '">
        <div id="antiCloneFingerprint" data-fingerprint="' . uniqid() . '"></div>
    </div>
    ';
}

// CSRF protection remains same
function get_csrf_protection() {
    if (!function_exists('generate_csrf_token')) {
        return '';
    }
    
    return '
    <script>
    const csrfToken = "' . (function_exists('generate_csrf_token') ? generate_csrf_token() : '') . '";
    
    document.addEventListener("DOMContentLoaded", function() {
        const forms = document.querySelectorAll("form");
        forms.forEach(form => {
            if (!form.querySelector("input[name=\"csrf_token\"]")) {
                const csrfInput = document.createElement("input");
                csrfInput.type = "hidden";
                csrfInput.name = "csrf_token";
                csrfInput.value = csrfToken;
                form.appendChild(csrfInput);
            }
        });
    });
    </script>
    ';
}

function get_complete_security() {
    return get_extreme_security() . get_csrf_protection();
}
?>