<?php
/**
 * AlixDeal Pro Backup System - SQL to TXT for WhatsApp
 */

// --- 1. WEBHOOK CHECK ---
$input = file_get_contents('php://input');
$webhook_data = json_decode($input, true);

if (isset($webhook_data['message'])) {
    $msg_received = strtolower(trim($webhook_data['message']));
    if ($msg_received !== 'mybckp') { exit("Ignored"); }
}

// --- 2. CONFIGURATION ---
$db_host = 'localhost';
$db_user = 'alixdealu_yourdb';
$db_pass = 'alixdealu_yourdb'; 
$db_name = 'alixdealu_yourdb';

$api_key   = "o6YBBpdG7dnLQgT5ZQX7YDtcBtdsgo"; 
$sender_no = "5730758877"; 
$target_no = "918273147063"; 
$api_url   = "https://ads.alixdeal.info/public/send-media"; 

// --- 3. FOLDER & FILE LOGIC ---
$today_folder = "backups/" . date("d-M-Y");
if (!file_exists($today_folder)) { mkdir($today_folder, 0755, true); }

// Hum file ko .txt extension ke sath banayenge taaki WhatsApp block na kare
$filename = "SMM_DB_BACKUP_" . date("H-i-s") . ".txt"; 
$file_path = $today_folder . '/' . $filename;

// --- 4. DATABASE DUMP ---
// Dump command ko direct .txt file me save kar rahe hain
$command = "mysqldump --opt -h $db_host -u $db_user -p'$db_pass' $db_name > $file_path 2>&1";
exec($command);

sleep(2); // File save hone ka waqt

// --- 5. SEND TO WHATSAPP ---
if (file_exists($file_path) && filesize($file_path) > 0) {
    
    $file_url = "https://yourdomain.com/backup/" . $file_path; 

    $data = [
        "api_key"    => $api_key,
        "sender"     => $sender_no,
        "number"     => $target_no,
        "media_type" => "document",
        "url"        => $file_url,
        "filename"   => $filename,
        "caption"    => "📂 *SMM DATABASE BACKUP (TXT)*\n\n✅ *Status:* SUCCESS\n📦 *File:* $filename\n⚠️ *Note:* Your 2029BCKP.\n\n💻 alixdeal.shop"
    ];

    $ch = curl_init($api_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    
    $response = curl_exec($ch);
    curl_close($ch);

    // Browser Display
    if (!isset($webhook_data['message'])) {
        echo "<body style='background:#000; color:#0f0; font-family:monospace; padding:20px;'>";
        echo "<h2>[✔] BACKUP GENERATED AS .TXT</h2>";
        echo "WhatsApp Response: $response";
        echo "</body>";
    }
} else {
    echo "Backup Failed!";
}
?>