<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cloudkuimages - YouTube Downloader</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background-color: #121212;
            color: #e0f7fa;
            margin: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
        }
        h1 {
            margin-top: 20px;
            text-align: center;
            font-size: 2.5em;
        }
        .container {
            width: 90%;
            max-width: 800px;
            background-color: #1c1c1c;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
            margin: 20px 0;
        }
        input[type="text"], select, button {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border-radius: 5px;
            border: none;
            background: #004d66;
            color: #e0f7fa;
            font-size: 1em;
        }
        button {
            background-color: #00e5ff;
            color: #002233;
            cursor: pointer;
            font-weight: bold;
        }
        button:hover {
            background-color: #00b8d4;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #003344;
            border-radius: 5px;
            text-align: center;
            color: #e0f7fa;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
        }
        .result img {
            width: 120px;
            height: auto;
            border-radius: 5px;
            margin-bottom: 10px;
        }
        .result form {
            margin-top: 10px;
        }
        .result p {
            font-size: 1.1em;
            margin: 10px 0;
        }
        .download-link {
            color: #00e5ff;
            text-decoration: none;
            font-weight: bold;
        }
        .download-link:hover {
            text-decoration: underline;
        }
        .footer {
            text-align: center;
            background-color: #003344;
            color: #b2dfdb;
            padding: 10px 0;
            width: 100%;
            margin-top: auto;
        }
        .footer a {
            color: #00e5ff;
            text-decoration: none;
        }
        .footer a:hover {
            text-decoration: underline;
        }
        
        /* Checkmark animation */
        .checkmark-container {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 20px;
            text-align: center;
            color: #00e5ff;
            display: none;
            opacity: 0;
            animation: fadeIn 2s forwards;
        }

        .checkmark {
            font-size: 50px;
        }

        .checkmark-close {
            margin-top: 10px;
            cursor: pointer;
            color: #e0f7fa;
        }

        @keyframes fadeIn {
            0% { opacity: 0; }
            100% { opacity: 1; }
        }

    </style>
</head>
<body>

<div class="container">
    <h1>Cloudkuimages - YouTube Downloader</h1>
    <form action="" method="GET">
        <input type="text" name="query" placeholder="Enter video title or URL" required>
        <button type="submit">Search</button>
    </form>

    <?php
    $apiKey = 'AlfiDev';
    $searchApi = 'https://api.botcahx.eu.org/api/search/yts';
    $downloadApi = 'https://api.botcahx.eu.org/api/dowloader/yt';

    if (isset($_GET['query'])) {
        $query = urlencode($_GET['query']);
        $searchUrl = "$searchApi?query=$query&apikey=$apiKey";
        $response = file_get_contents($searchUrl);
        $data = json_decode($response, true);

        if (isset($data['result']) && is_array($data['result'])) {
            echo '<h2>Search Results:</h2>';
            foreach ($data['result'] as $video) {
                echo '<div class="result">';
                echo '<img src="' . $video['thumbnail'] . '" alt="Thumbnail">';
                echo '<p>' . $video['title'] . '</p>';
                echo '<form action="" method="GET">
                        <input type="hidden" name="url" value="' . $video['url'] . '">
                        <select name="type">
                            <option value="mp3">MP3</option>
                            <option value="mp4">MP4</option>
                        </select>
                        <button type="submit">Download</button>
                      </form>';
                echo '</div>';
            }
        } else {
            echo '<p>No results found.</p>';
        }
    }

    if (isset($_GET['url']) && isset($_GET['type'])) {
        $url = $_GET['url'];
        $type = $_GET['type'];
        $downloadUrl = "$downloadApi?url=" . urlencode($url) . "&apikey=$apiKey";
        $response = file_get_contents($downloadUrl);
        $data = json_decode($response, true);

        if ($data['status'] && isset($data['result'][$type])) {
            $fileUrl = $data['result'][$type];
            $ext = $type === 'mp3' ? 'mp3' : 'mp4';
            $uploadDir = $type === 'mp3' ? 'uploads/audios/' : 'uploads/videos/';

            if (!is_dir($uploadDir)) {
                mkdir($uploadDir, 0777, true);
            }

            // Create a clean hash for the filename
            $fileName = basename(parse_url($fileUrl, PHP_URL_PATH));
            $fileName = preg_replace('/[^a-zA-Z0-9-_\.]/', '', $fileName); // Remove unwanted characters

            // Hash the file name
            $fileName = md5($fileName) . '.' . $ext;

            $filePath = $uploadDir . $fileName;

            file_put_contents($filePath, file_get_contents($fileUrl));

            $serverFilePath = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $filePath;
            echo '<p>Download completed! <a href="' . $serverFilePath . '" target="_blank" class="download-link">Click here to download ' . $ext . '</a></p>';

            // Show checkmark animation
            echo '<div class="checkmark-container">
                    <div class="checkmark">✔</div>
                    <div>Download successful!</div>
                    <div class="checkmark-close" onclick="this.parentElement.style.display = \'none\';">Close</div>
                  </div>';
        } else {
            echo '<p>Download failed. Please try again.</p>';
        }
    }
    ?>
</div>

<div class="footer">
    <p>&copy; 2024 Cloudkuimages. Made for my friends ❤️</p>
    <p><a href="https://creativecommons.org/licenses/by/4.0/?ref=chooser-v1" target="_blank">Creative Commons Attribution 4.0 International License</a></p>
</div>

</body>
</html>
