<?php

echo <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>~* Guestbook *~</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Comic+Neue:ital,wght@0,300;0,400;0,700;1,300;1,400;1,700&display=swap" rel="stylesheet">
    <style>
        body {
            color: #4b2e83;
            font-family: 'Comic Sans MS', 'Comic Sans', 'Comic Neue', cursive, sans-serif;
            margin: 0;
            padding: 0;
        }
        .container {
            max-width: 700px;
            margin: 40px auto;
            background: rgba(255,255,255,0.95);
            border: 4px double #ffb6c1;
            border-radius: 18px;
            box-shadow: 0 0 24px #ffb6c1;
            padding: 32px 24px;
        }
        h1 {
            text-align: center;
            font-size: 2.5em;
            color: #ff69b4;
            text-shadow: 2px 2px 0 #fff, 0 0 8px #ffb6c1;
            margin-bottom: 0.5em;
        }
        .catgirl {
            display: block;
            margin: 0 auto 1em auto;
            width: 120px;
            filter: drop-shadow(0 0 8px #ffb6c1);
        }
        .entry {
            margin-bottom: 1.5em;
            border-bottom: 2px dashed #ffb6c1;
            padding-bottom: 1em;
            background: rgba(255,182,193,0.1);
            border-radius: 10px;
        }
        .entry strong {
            color: #d72660;
            font-size: 1.2em;
        }
        .entry em {
            color: #7e57c2;
            font-size: 0.95em;
        }
        .nyan {
            font-size: 1.1em;
            color: #ff69b4;
            font-weight: bold;
            margin-bottom: 1em;
            text-align: center;
        }
        .paw {
            font-size: 1.3em;
            color: #ffb6c1;
            margin-right: 0.3em;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>~* Guestbook *~</h1>
 <div class="nyan">Welcome to my guestbook!<span class="paw">&#x1F43E;</span></div>
HTML;

function censor_bad_words($text, $badWords)
{
    foreach ($badWords as $word) {
        $pattern = '/\b' . preg_quote($word, '/') . '\b/i'; // whole word match, case-insensitive
        $replacement = str_repeat('*', strlen($word));
        $text = preg_replace($pattern, $replacement, $text);
    }
    return $text;
}
$badWordsFile = __DIR__ . '/data/en.txt';
//echo it html to see if the file exists

$badWords = file_exists($badWordsFile)
    ? array_filter(array_map('trim', file($badWordsFile)))
    : [];


$lines = @file("data/guestbook.json", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!$lines) {
    echo "<div class='nyan'>No entries yet, nya~ Be the first to leave your pawprint! &#x1F43E;</div>";

} else {
    foreach (array_reverse($lines) as $line) {
        $entry = json_decode($line, true);
        if ($entry) {
            // Censor message before displaying
            $cleanMessage = censor_bad_words($entry['message'], $badWords);
            $cleanMessage = nl2br(htmlspecialchars($cleanMessage));

            echo "<div class='entry'>";
            echo "<span class='paw'>&#x1F43E;</span> <strong>" . htmlspecialchars($entry['name']) . "</strong> ";
            echo "<em>(" . $entry['timestamp'] . ")</em><br>";
            echo $cleanMessage . "<br>";
            echo "</div>";
        }
    }
}

echo <<<HTML
    </div>
</body>
</html>
HTML;
?>
