<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Terabox Direct Download Generator</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            background: #f0f2f5;
            margin: 0;
            padding: 0;
        }

        .container {
            max-width: 700px;
            margin: 40px auto;
            padding: 30px;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        h2 {
            text-align: center;
            color: #333;
        }

        form {
            margin-top: 20px;
            text-align: center;
        }

        input[type="text"] {
            width: 80%;
            padding: 12px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 6px;
        }

        input[type="submit"] {
            padding: 12px 24px;
            margin-top: 10px;
            font-size: 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 30px;
        }

        .info-box {
            background-color: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
            border: 1px solid #ddd;
        }

        .info-box img {
            max-width: 100%;
            height: auto;
            margin-top: 10px;
            border-radius: 6px;
        }

        .download-link {
            display: inline-block;
            margin: 10px 5px 0 0;
            padding: 10px 20px;
            background-color: #28a745;
            color: white;
            text-decoration: none;
            border-radius: 6px;
        }

        .download-link:hover {
            background-color: #218838;
        }

        .error {
            color: red;
            font-weight: bold;
        }
        
        .video {
    margin-top: 15px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
}

    </style>
</head>
<body>

<div class="container">
    <h2>📥 Terabox Direct Download Link Generator</h2>

    <form method="get">
        <input type="text" name="url" placeholder="Enter Terabox File URL..." required>
        <br>
        <input type="submit" value="Generate Download Link">
    </form>

    <div class="result">
        <?php
        if (isset($_GET['url']) && !empty($_GET['url'])) {
            $inputUrl = $_GET['url'];

            if (filter_var($inputUrl, FILTER_VALIDATE_URL) && strpos($inputUrl, 'terabox') !== false) {
                $encodedUrl = urlencode($inputUrl);
                $curl = curl_init();

                curl_setopt_array($curl, [
                    CURLOPT_URL => "https://terabox-downloader-direct-download-link-generator2.p.rapidapi.com/url?url=$encodedUrl",
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_TIMEOUT => 15,
                    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                    CURLOPT_CUSTOMREQUEST => "GET",
                    CURLOPT_HTTPHEADER => [
                        "x-rapidapi-host: terabox-downloader-direct-download-link-generator2.p.rapidapi.com",
                        "x-rapidapi-key: c703eef4cdmsh37e41bd8403c00cp18db7ejsncfdd0fe5192d" // <-- replace with your real API key
                    ],
                ]);

                $response = curl_exec($curl);
                $err = curl_error($curl);
                curl_close($curl);

                if ($err) {
                    echo "<p class='error'>❌ Error: " . htmlspecialchars($err) . "</p>";
                } else {
                    $data = json_decode($response, true);

                    if (json_last_error() === JSON_ERROR_NONE && is_array($data) && count($data) > 0) {
                        $file = $data[0];

                        echo "<div class='info-box'>";
echo "<p><strong>📁 File Name:</strong> " . htmlspecialchars($file['file_name']) . "</p>";
echo "<p><strong>📦 File Size:</strong> " . htmlspecialchars($file['size']) . "</p>";
echo "<p><strong>✅ Direct Download:</strong></p>";
echo "<a class='download-link' href='" . htmlspecialchars($file['direct_link']) . "' target='_blank'>Download Now</a>";
echo "<a class='download-link' href='" . htmlspecialchars($file['link']) . "' target='_blank'>Backup Link</a>";

// Check if the file is a video
$videoExtensions = ['mp4', 'webm', 'ogg', 'mkv', 'mov'];
$fileExt = strtolower(pathinfo($file['file_name'], PATHINFO_EXTENSION));

if (in_array($fileExt, $videoExtensions)) {
    echo "<p><strong>📺 Video Preview:</strong></p>";
    echo "<video width='100%' controls poster='" . htmlspecialchars($file['thumb']) . "'>";
    echo "<source src='" . htmlspecialchars($file['direct_link']) . "' type='video/$fileExt'>";
    echo "Your browser does not support the video tag.";
    echo "</video>";
}

if (!empty($file['thumb'])) {
    echo "<p><strong>🎞️ Thumbnail Preview:</strong></p>";
    echo "<img src='" . htmlspecialchars($file['thumb']) . "' alt='Thumbnail'>";
}

echo "</div>";

                    } else {
                        echo "<p class='error'>⚠️ Could not parse or find valid data from API.</p>";
                    }
                }
            } else {
                echo "<p class='error'>⚠️ Please enter a valid Terabox URL.</p>";
            }
        }
        ?>
    </div>
</div>

</body>
</html>
