<?php
$day = date("j");
$month = date("m");
$today = date("j");
$suff = "th";

if ($month != 12) {
    http_response_code(410);
    include($_SERVER["DOCUMENT_ROOT"] . "/markustegelane/admin/410.php");
    exit();
}

if (isset($_GET["day"])) {
    $day = $_GET["day"];
    if (!is_numeric($_GET["day"]) || ($day > $today) || ($day < 1)) {
        http_response_code(400);
        include($_SERVER["DOCUMENT_ROOT"] . "/markustegelane/admin/400.php");
        exit();
    }
}
if ($day > 25) {
    $day = 25;
}
if (($day > 10) and ($day < 20)) {
    // ignore
} else if (str_ends_with($day, "1")) {
    $suff = "st";
} else if (str_ends_with($day, "2")) {
    $suff = "nd";
} else if (str_ends_with($day, "3")) {
    $suff = "rd";
}

include_once "../maintenance.php";
if ((!empty($_COOKIE["cookie_ok"])) && ($_COOKIE["cookie_ok"] == "true")) {
    welcomeback();
} else if (empty($_COOKIE["lang"])) {
    include($_SERVER["DOCUMENT_ROOT"] . "/markustegelane/common/config/cookie/setcookie.php");
    //echo '<meta http-equiv="Refresh" content="0; URL=/markustegelane/common/config/cookie">';
} else if ((!empty($_COOKIE["theme"])) && (!(($_COOKIE["theme"] == "dark") || ($_COOKIE["theme"] == "light") || ($_COOKIE["theme"] == "blue")))) {
    if (empty($_COOKIE["old_theme"]) || ($_COOKIE["old_theme"] != "true")) {
        welcomeback();
    }
}
if (!empty($_COOKIE["lang"])) {
$lang = $_COOKIE["lang"];
}
$theme = "blue";
if (!empty($_COOKIE["theme"])) {
$theme = $_COOKIE["theme"];
}
$whitelist = array(
'127.0.0.1',
'::1',
'localhost',
'markustegelane.local',
'staging.markustegelane.eu'
);
$remote = !in_array($_SERVER['REMOTE_ADDR'], $whitelist) && !str_starts_with($_SERVER["REMOTE_ADDR"], "192.");
$hash = md5("MT_174A1111_$day.$month");
?>

<!DOCTYPE html>
<html lang="<?php if ($lang == "et-EE") { echo "et"; } else { echo "en"; } ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="<?php if ($lang == "et-EE") { echo "Ametlik avalehekülg MarkusTegelane kanalitele."; } else { echo "Official home page for MarkusTegelane channels."; }?>">
    <meta name="keywords" content="MarkusTegelane,MarkusTegelane+,MarkusTegelane++,MarkusTegelane kogukond,Press any key to continue...,Markuse asjad,Markuse tarkvara,Markuse videod,Kanalite andmebaas,Channel database,otseülekanded,live streams">
    <meta name="author" content="Markus Maal">
    <title>MarkusTegelane</title>
    <style>
        body {
        <?php
        switch ($theme) {
            case "light":
                echo 'background: #bbb 70%;';
                echo 'color: #000;';
                echo 'background-color: #fff;';
                break;
            case "dark":
                echo 'background: #222;';
                echo 'color: #fff;';
                echo 'background-color: #222;';
                break;
            default:
                echo 'background: linear-gradient(#000, #008);';
                echo 'color: #fff;';
                echo 'background-color: #008;';
                break;
        }
        ?>
            background-size: 100%;
            min-height: 100%;
            background-repeat: no-repeat;
            background-attachment: fixed;
            font-family: "Segoe UI", "Microsoft Sans Serif", "Ubuntu", "Sans";
        }
        h1 {
            font-style: normal;
            font-weight: normal;
            text-align: center;
        }

        h2 {
            font-style: normal;
            font-weight: normal;
            text-align: center;
        }

        .cont {
            margin-left: auto;
            margin-right: auto;
            vertical-align: bottom;
            padding-top: 3em;
        }
        table {
            border-collapse: collapse;
            margin:0 auto;
        }
        .thumb {
            border: 0.15em solid;
            transition: 0.25s;
            /*border-color: #fff #888 #888 #fff;*/
            margin: 0em;
            padding: 0em;
            width: 96px;
            height: 96px;
        }

        .thumb:active {
            border: 0.15em solid;
            /*border-color: #888 #fff #fff #888;*/
            margin-top: -1em;
        }

        .thumb:hover {
            margin-top: -0.5em;
        }

        img {
            margin: 0em;
            padding: 0em;
            image-rendering: pixelated;
        }

        .desc {
            text-align: center;
        }

        td {
            margin: 1.5em;
            text-align: center;
            padding: 0.5em;
        }

        a {
            cursor: pointer;
            text-decoration: none;
            transition: 0.25s;
        <?php
            if ($theme == "light") {
                echo 'color: #088;';
            } else {
                echo 'color: #8bb;';
            }
        ?>
        }

        a:hover {
            color: #0ff;
        }

        p.copy {
            color: #0aa;
            text-align: center;
        }

        p.copy:hover, p.copy:focus {
            animation: hover 1s linear infinite;
            cursor: default;
            outline: none;
        }

        .blink {
            animation: blink 1s linear infinite;
        }
        .blink-soft {
            animation: teeter 1s linear infinite;
            color: #f00;
        }
        .sizable {
            animation: zoom 20s linear infinite;
        }
        .colorful {
            animation: colors 5s linear infinite;
        }
        .unlogic {
            position: fixed;
            font-family: "Comic Sans MS", "Segoe UI", "Microsoft Sans Serif", "Ubuntu", "Sans";
            animation: fun 4s linear infinite;
        }

        @keyframes fun {
            0%{ font-size: 1em; left: 0em; top: 0em; color: #ff0; }
            15%{ font-size: 8em; left: 1.4em; top: 0em; -webkit-transform: rotate(-45deg); -moz-transform: rotate(-45deg); color: #0ff; }
            50%{ font-size: 0.5em; left: 18em; top: 20em; -webkit-transform: rotate(80deg); -moz-transform: rotate(80deg); color: #f0f; }
            75%{ font-size: 8em; left: 0em; top: 4em; -webkit-transform: rotate(-100deg); -moz-transform: rotate(-100deg); color: #f00; }
            100%{ font-size: 1em; left: 0em; top: 0em; -webkit-transform: rotate(0deg); -moz-transform: rotate(0deg); color: #ff0; }
        }

        @keyframes zoom{
            0%{
                font-size: 0.5em;
            }
            50%{
                font-size: 8em;
            }
            100%{
                font-size: 0.5em;
            }
        }
        @keyframes colors{
            0%{
                color: #f00;
            }
            16%{
                color: #ff0;
            }
            32%{
                color: #0f0;
            }
            49%{
                color: #0ff;
            }
            66%{
                color: #00f;
            }
            84%{
                color: #f0f;
            }
            100%{
                color: #f00;
            }
        }
        @keyframes blink{
            0%{
                opacity: 1;
            }
            49%{
                opacity: 1;
            }
            50%{
                opacity: 0;
            }
            99%{
                opacity: 0;
            }
            100%{
                opacity: 1;
            }
        }
        @keyframes hover {
            50%{
                color: #0cc;
            }
            100%{
                color: #0aa;
            }
        }

        @keyframes teeter{
            0%{
                opacity: 1;
            }
            50%{
                opacity: 0;
            }
            100%{
                opacity: 1;
            }
        }

        .flex-me {
            display: flex;
            justify-content: center;
            flex-wrap: wrap;
            align-content: space-between;
            gap: 1em;
        }

        img[alt="www.000webhost.com"]
        {
            display:none;
        }

        .disclaimer
        {
            display:none;
        }

        .social {
            opacity: 75% !important;
        }

        .social:hover {
            opacity: 100% !important;
        }
    </style>

    <script>
        function copyEgg(press = false) {
            var current = document.getElementById("copy").innerHTML;
            <?php
            if ("$lang" == "et-EE") {
                echo "var e1 = \"&copy;" . date("Y") . " Markus Maal\";";
                echo "var e2 = \"Ma ei ole ühendatud\";";
                echo "var e3 = \"Vajuta soovitud linki, et alustada!\";";
                echo "var e4 = \"<span style=\\\"color: blue\\\">Seda teksti on väga raske näha</span>\";";
                echo "var e5 = \"<span class=\\\"blink\\\">Vilkuv tekst</span>\";";
                echo "var e6 = press ? \"Lõpeta nupule vajutamine...\" : \"Lõpeta klikkimine...\";";
                echo "var e7 = \"<h2 class=\\\"blink-soft\\\">HÄIRE! HÄIRE! HÄIRE!</h2>\";";
                echo "var e8 = \"<span class=\\\"sizable\\\">Ma muudan suurust</span>\";";
                echo "var e9 = \"<span class=\\\"colorful\\\">Värviline</span>\";";
                echo "var e10 = \"<span class=\\\"unlogic\\\">Püüa mind kinni!</span>\";";
            } else {
                echo "var e1 = \"&copy;" . date("Y") . " Markus Maal\";";
                echo "var e2 = \"I am not connected\";";
                echo "var e3 = \"Click on the desired link to start!\";";
                echo "var e4 = \"<span style=\\\"color: blue\\\">This text is hard to see</span>\";";
                echo "var e5 = \"<span class=\\\"blink\\\">Blinking text</span>\";";
                echo "var e6 = press ? \"Stop pressing the button...\" : \"Stop clicking...\";";
                echo "var e7 = \"<h2 class=\\\"blink-soft\\\">ALERT! ALERT! ALERT!</h2>\";";
                echo "var e8 = \"<span class=\\\"sizable\\\">I am changing size</span>\";";
                echo "var e9 = \"<span class=\\\"colorful\\\">Colorful</span>\";";
                echo "var e10 = \"<span class=\\\"unlogic\\\">Catch me!</span>\";";
            }
            ?>
            switch (current) {
                case e1:
                    document.getElementById("copy").innerHTML = e2;
                    break;
                case e2:
                    document.getElementById("copy").innerHTML = e3;
                    break;
                case e3:
                    document.getElementById("copy").innerHTML = e4;
                    break;
                case e4:
                    document.getElementById("copy").innerHTML = e5;
                    break;
                case e5:
                    document.getElementById("copy").innerHTML = e6;
                    break;
                case e6:
                    document.getElementById("copy").innerHTML = e7;
                    break;
                case e7:
                    document.getElementById("copy").innerHTML = e8;
                    break;
                case e8:
                    document.getElementById("copy").innerHTML = e9;
                    break;
                case e9:
                    document.getElementById("copy").innerHTML = e10;
                    break;
                case e10:
                    document.getElementById("copy").innerHTML = e1;
                    break;
                default:
                    document.getElementById("copy").innerHTML = e2;
                    break;
            }
        }
    </script>
</head>
<body>
    <h1><?= ($lang == "et-EE") ? "Advendikalender" : "Advent calendar" ?></h1>
    <div style="display: flex; justify-content: center; flex-direction: column; text-align: center; align-items: center; gap: 1em;">
        <?php if (!isset($_GET["cal"]) && !isset($_GET["soft"])) : ?>
        <p><?= ($lang == "et-EE") ? "Iga päev enne jõule üks üllatus" : "One surprise every day before Christmas" ?>.</p>
        <p><?= $day ?><?= ($lang == "et-EE") ? ". detsember" : $suff . " December " . date("Y") ?></p>
        <?php if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/advent/$hash.png")) : ?>
            <a href="/advent/<?= $hash ?>.png"><img style="width: 512px;" src="<?= $hash ?>.png" alt="<?= $day ?>. <?= ($lang == "et-EE") ? "detsember" : "December" ?>"></a>
        <?php elseif (file_exists($_SERVER["DOCUMENT_ROOT"] . "/advent/$hash.webm")) : ?>
            <a href="#" onclick="replayVideo();">
                <video id="funstuff" width="512" height="512" autoplay>
                    <source src="<?= $hash ?>.webm" type="video/webm">
                    [WEBM]
                </video>
            </a>
            <script>
                function replayVideo() {
                    let stream = document.querySelector("#funstuff");
                    if (stream.paused) {
                        document.querySelector("#funstuff").play();
                    } else {
                        document.querySelector("#funstuff").pause();
                    }
                }
            </script>
        <?php else : ?>
            <a href="/advent/<?= $hash ?>.gif"><img style="width: 512px;" src="<?= $hash ?>.gif" alt="<?= $day ?>. <?= ($lang == "et-EE") ? "detsember" : "December" ?>"></a>
        <?php endif; ?>
        <div style="display: flex; gap: 1em; justify-content: center;">
            <?php if ($day - 1 > 0) : ?>
                <a href="?day=<?= ($day - 1) ?>">&lt;&nbsp;<?= ($lang == "et-EE") ? "Eelmine" : "Previous" ?></a>
            <?php endif; ?>
            <?php if ($today < 26) : ?>
            <a href="/advent"><?= ($lang == "et-EE") ? "Täna" : "Today" ?></a>
            <?php endif; ?>
            <?php if (($day < 25) && ($day + 1 <= $today)) : ?>
                <a href="?day=<?= ($day + 1) ?>"><?= ($lang == "et-EE") ? "Järgmine" : "Next" ?>&nbsp;&gt;</a>
            <?php endif; ?>
        </div>
        <a href="/advent?cal">&#10052;&#65039; <?= ($lang == "et-EE") ? "Kalender" : "Calendar" ?> &#10052;&#65039;</a>
        <?php else : ?>
            <p><?= ($lang == "et-EE") ? "detsember 2024" : "December 2024" ?></p>
            <table>
                <thead>
                    <tr>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "E" : "Mon" ?></th>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "T" : "Tue" ?></th>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "K" : "Wed" ?></th>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "N" : "Thu" ?></th>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "R" : "Fri" ?></th>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "L" : "Sat" ?></th>
                        <th style="padding: 0.4em; font-weight: normal;"><?= ($lang == "et-EE") ? "P" : "Sun" ?></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td colspan="6"></td>
                        <td><a href="/advent?day=1">1</a></td>
                    </tr>
                    <tr>
                        <?php for ($i = 2; $i < 9; $i++) : ?>
                            <td>
                                <?php if ($i < $today) : ?>
                                    <a href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php elseif ($i = $today) : ?>
                                    <a style="font-weight: bold;" href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php else: ?>
                                    <?= $i ?>
                                <?php endif; ?>
                            </td>
                        <?php endfor; ?>
                    </tr>
                    <tr>
                        <?php for ($i = 9; $i < 16; $i++) : ?>
                            <td>
                                <?php if ($i < $today) : ?>
                                    <a href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php elseif ($i == $today) : ?>
                                    <a style="font-weight: bold;" href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php else: ?>
                                    <?= $i ?>
                                <?php endif; ?>
                            </td>
                        <?php endfor; ?>
                    </tr>
                    <tr>
                        <?php for ($i = 16; $i < 23; $i++) : ?>
                            <td>
                                <?php if ($i < $today) : ?>
                                    <a href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php elseif ($i == $today) : ?>
                                    <a style="font-weight: bold;" href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php else: ?>
                                    <?= $i ?>
                                <?php endif; ?>
                            </td>
                        <?php endfor; ?>
                    </tr>
                    <tr>
                        <?php for ($i = 23; $i < 30; $i++) : ?>
                            <td>
                                <?php if (($i < $today) && ($i < 26)) : ?>
                                    <a href="/advent?day=<?= $i ?>"><?= $i ?></a>
                                <?php elseif ($i == $today) : ?>
                                    <?php if ($today < 26) : ?>
                                    <a href="/advent?day=<?= $i ?>">
                                    <?php endif; ?>
                                    <span style="font-weight: bold;"><?= $i ?></span>
                                    <?php if ($today < 26) : ?>
                                    </a>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <?= $i ?>
                                <?php endif; ?>
                            </td>
                        <?php endfor; ?>
                    </tr>
                    <tr>
                        <?php for ($i = 30; $i < 32; $i++) : ?>
                            <td>
                                <?= $i ?>
                            </td>
                        <?php endfor; ?>
                    </tr>
                </tbody>
            </table>
            <?php if ($today < 26) : ?>
            <a href="/advent">&#10052;&#65039; <?= ($lang == "et-EE") ? "Täna" : "Today" ?> &#10052;&#65039;</a>
            <?php endif; ?>
        <?php endif ;?>

        <?php
            $getsuff = "?";
            foreach ($_GET as $key => $value) {
                $getsuff .= $key . "=" . $value . "&";
            }
            $getsuff = substr($getsuff, 0, -1);
        ?>
        <div style="display: flex; flex-direction: row; gap: 1em;">
            <a href="/"><?= ($lang == "et-EE") ? "Tagasi avalehele" : "Go back" ?></a>
            <a href="/markustegelane/common/config/chlang.php?redir2=/advent<?= $getsuff ?>"><?php if ($lang == "et-EE") { echo "in English"; } else { echo "eesti keeles"; } ?></a>
        </div>
        <?php if ($today > 25) : ?>
            <p><?= ($lang == "et-EE") ? "Advendikalender suletakse 1. jaanuaril" : "The advent calendar will be closed on January 1st" ?> <?= date("Y") + 1 ?></p>
        <?php endif; ?>
    </div>
</body>
</html>