<?php
/**
 * Fully Server-Driven Client License Checker (No Hardcoded Text/Numbers)
 * Server: https://license.websitebazaar.top
 */

if (!defined("CENTRAL_LICENSE_API")) {
    define("CENTRAL_LICENSE_API", "https://license.websitebazaar.top/api/verify.php");
}
if (!defined("CLIENT_LICENSE_KEY")) {
    define("CLIENT_LICENSE_KEY", "YOUR_LICENSE_KEY_HERE");
}
if (!defined("PRODUCT_NAME")) {
    define("PRODUCT_NAME", "Rakib");
}
if (!defined("CACHE_TIME_SECONDS")) {
    define("CACHE_TIME_SECONDS", 10);
}

if (session_status() === PHP_SESSION_NONE) {
    @session_start();
}

if (!function_exists("get_current_client_domain")) {
    function get_current_client_domain() {
        $host = $_SERVER["HTTP_HOST"] ?? $_SERVER["SERVER_NAME"] ?? "localhost";
        $host = strtolower(trim($host));
        $host = strtok($host, ":");
        return preg_replace("/^www\./", "", $host);
    }
}

if (!function_exists("make_license_api_request")) {
    function make_license_api_request($url, $data) {
        $post_data = http_build_query($data);

        if (function_exists("curl_init")) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 4);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_USERAGENT, "LicenseChecker/2.0");
            
            $response = curl_exec($ch);
            curl_close($ch);
            
            if ($response !== false && !empty($response)) {
                return $response;
            }
        }

        $options = [
            "http" => [
                "header"  => "Content-type: application/x-www-form-urlencoded\r\n" .
                             "User-Agent: LicenseChecker/2.0\r\n",
                "method"  => "POST",
                "content" => $post_data,
                "timeout" => 4
            ],
            "ssl" => [
                "verify_peer"      => false,
                "verify_peer_name" => false,
            ]
        ];
        
        $context = @stream_context_create($options);
        return @file_get_contents($url, false, $context);
    }
}

if (!function_exists("check_client_site_license")) {
    function check_client_site_license() {
        $current_time = time();

        if (
            isset($_SESSION["license_checked_status"]) && 
            $_SESSION["license_checked_status"] === "active" && 
            isset($_SESSION["license_last_check"]) && 
            ($current_time - $_SESSION["license_last_check"] < CACHE_TIME_SECONDS)
        ) {
            return true;
        }

        $license_key  = CLIENT_LICENSE_KEY;
        $product_name = PRODUCT_NAME;
        $domain_name  = get_current_client_domain();

        $post_fields = [
            "license_key" => $license_key,
            "product"     => $product_name,
            "domain"      => $domain_name
        ];

        $response = make_license_api_request(CENTRAL_LICENSE_API, $post_fields);

        if (empty($response)) {
            return true; 
        }

        $data = json_decode($response, true);

        if (isset($data["valid"]) && $data["valid"] === true) {
            $_SESSION["license_checked_status"] = "active";
            $_SESSION["license_last_check"]     = $current_time;
            return true;
        }

        unset($_SESSION["license_checked_status"]);
        unset($_SESSION["license_last_check"]);
        
        if (session_status() === PHP_SESSION_ACTIVE) {
            @session_unset();
            @session_destroy();
        }

        $reason       = $data["message"] ?? "Service temporarily suspended.";
        $whatsapp_num = $data["whatsapp"] ?? "";

        render_suspended_ui($reason, $whatsapp_num, $domain_name);
    }
}

if (!function_exists("render_suspended_ui")) {
    function render_suspended_ui($reason_message, $whatsapp_num, $domain_name) {
        $clean_wa     = preg_replace("/[^0-9]/", "", $whatsapp_num);
        $wa_text      = urlencode("Hello, my website ($domain_name) is suspended. Reason: $reason_message. Please reactivate.");
        $whatsapp_url = !empty($clean_wa) ? "https://wa.me/{$clean_wa}?text={$wa_text}" : "#";

        if (ob_get_length()) @ob_clean();

        header("HTTP/1.1 503 Service Temporarily Unavailable");
        ?>
        <!DOCTYPE html>
        <html lang="bn">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Service Suspended</title>
            <style>
                * { box-sizing: border-box; margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
                body { background: #f3f4f6; color: #1f2937; display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
                .card { background: #ffffff; border-radius: 16px; box-shadow: 0 10px 25px rgba(0,0,0,0.08); max-width: 460px; width: 100%; text-align: center; padding: 32px 24px; border-top: 6px solid #ef4444; }
                .icon { background: #fee2e2; color: #ef4444; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; margin: 0 auto 20px; font-size: 28px; font-weight: bold; }
                h1 { font-size: 22px; font-weight: 700; color: #111827; margin-bottom: 10px; }
                p { font-size: 15px; color: #4b5563; line-height: 1.5; margin-bottom: 20px; }
                .badge { background: #fef2f2; border: 1px solid #fecaca; color: #991b1b; padding: 12px; border-radius: 8px; font-size: 14px; font-weight: 600; margin-bottom: 24px; word-break: break-word; }
                .btn-wa { display: inline-block; background: #25D366; color: #ffffff; text-decoration: none; font-size: 16px; font-weight: 600; padding: 12px 24px; border-radius: 8px; width: 100%; transition: background 0.2s; box-shadow: 0 4px 10px rgba(37, 211, 102, 0.25); }
                .btn-wa:hover { background: #1ebe57; }
                .footer { font-size: 12px; color: #9ca3af; margin-top: 20px; }
            </style>
        </head>
        <body>
            <div class="card">
                <div class="icon">!</div>
                <h1>Website Service Suspended</h1>
                <p>This website's service is temporarily inactive. Please contact the administrator to restore access.</p>
                <div class="badge">Reason: <?php echo htmlspecialchars($reason_message, ENT_QUOTES, "UTF-8"); ?></div>
                <?php if (!empty($clean_wa)): ?>
                    <a href="<?php echo htmlspecialchars($whatsapp_url, ENT_QUOTES, "UTF-8"); ?>" class="btn-wa" target="_blank" rel="noopener noreferrer">Contact on WhatsApp</a>
                <?php endif; ?>
                <div class="footer">Domain: <strong><?php echo htmlspecialchars($domain_name, ENT_QUOTES, "UTF-8"); ?></strong></div>
            </div>
        </body>
        </html>
        <?php
        exit;
    }
}

check_client_site_license();
