<?php
function showSource() {
    if (isset($_GET['show_source']) && $_GET['show_source'] === '1') {
        highlight_file(__FILE__);
        exit;
    }
}

showSource();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $jsonInput = $_POST['cookieInput'] ?? '';
    $cookies = json_decode($jsonInput, true);

    if (json_last_error() !== JSON_ERROR_NONE) {
        $error = "Fehler: Ungültiges JSON-Format - " . json_last_error_msg();
    } else {
        $output = "# Netscape HTTP Cookie File\n";
        $output .= "# This file is generated by Cookie-Helper. Do not edit.\n\n";

        foreach ($cookies as $cookie) {
            if (!isset($cookie['domain'], $cookie['name'], $cookie['value'], $cookie['path'], $cookie['secure'])) {
                continue; 
            }

            $domain = $cookie['domain'];
            $flag = strpos($domain, '.') === 0 ? 'TRUE' : 'FALSE';
            $path = $cookie['path'];
            $secure = $cookie['secure'] ? 'TRUE' : 'FALSE';
            $expiration = $cookie['expirationDate'] ?? 0;

            $name = preg_replace('/[\[\]]/', '_', $cookie['name']);
            $value = $cookie['value'];

            $output .= "$domain\t$flag\t$path\t$secure\t$expiration\t$name\t$value\n";
        }

        $netscapeCookies = $output;
    }
}
?>

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cookie-Helper</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            padding: 20px;
            background-color: #121212;
            color: #e0e0e0;
        }
        textarea {
            width: 100%;
            height: 200px;
            margin-bottom: 20px;
            font-family: monospace;
            font-size: 14px;
            background-color: #1e1e1e;
            color: #e0e0e0;
            border: 1px solid #333;
        }
        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #0056b3;
        }
        label {
            color: #e0e0e0;
        }
        footer {
            margin-top: 20px;
            text-align: center;
        }
        footer a {
            color: #007BFF;
            text-decoration: none;
        }
        footer a:hover {
            text-decoration: underline;
        }
    </style>
    <script>
        function downloadFile(content, filename) {
            const blob = new Blob([content], { type: 'text/plain' });
            const link = document.createElement('a');
            link.href = URL.createObjectURL(blob);
            link.download = filename;
            link.click();
        }

        function copyToClipboard(content) {
            navigator.clipboard.writeText(content).then(() => {
                alert('In die Zwischenablage kopiert!');
            }).catch(err => {
                alert('Kopieren fehlgeschlagen: ' + err);
            });
        }
    </script>
</head>
<body>
    <h1>Cookie-Helper</h1>
    <form method="POST">
        <label for="cookieInput">Füge hier dein JSON-Cookie-Export ein (z.B. von EditThisCookies):</label>
        <textarea name="cookieInput" id="cookieInput" placeholder="JSON hier einfügen..."><?php echo htmlspecialchars($_POST['cookieInput'] ?? ''); ?></textarea>
        <button type="submit">Konvertieren</button>
    </form>

    <?php if (isset($error)): ?>
        <p style="color: red;"><?php echo $error; ?></p>
    <?php elseif (isset($netscapeCookies)): ?>
        <p></p>
        <label for="cookieOutput">Netscape-Cookies:</label>
        <textarea id="cookieOutput" readonly><?php echo htmlspecialchars($netscapeCookies); ?></textarea>
        <button onclick="downloadFile(document.getElementById('cookieOutput').value, 'cookies.txt')">Als Datei herunterladen</button>
        <button onclick="copyToClipboard(document.getElementById('cookieOutput').value)">In die Zwischenablage kopieren</button>
    <?php endif; ?>

    <footer>
        <a href="?show_source=1">Quelltext anzeigen</a>
    </footer>
</body>
</html>