<?php
/**
* Kniha návštěv (Guestbook)
* Návštěvníci mohou psát zprávy, které se ukládají do souboru.
*/
$dataFile = __DIR__ . "/guestbook_data.json";
// Načtení existujících zpráv
$messages = [];
if (file_exists($dataFile)) {
$content = file_get_contents($dataFile);
$messages = json_decode($content, true) ?: [];
}
$error = "";
$success = false;
// Zpracování formuláře
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = trim($_POST["name"] ?? "");
$message = trim($_POST["message"] ?? "");
$emoji = $_POST["emoji"] ?? "👋";
if (strlen($name) < 2) {
$error = "Jméno musí mít alespoň 2 znaky.";
} elseif (strlen($message) < 3) {
$error = "Zpráva musí mít alespoň 3 znaky.";
} else {
// Přidání zprávy
array_unshift($messages, [
"name" => htmlspecialchars($name),
"message" => htmlspecialchars($message),
"emoji" => $emoji,
"date" => date("j.n.Y H:i"),
"ip" => substr(md5($_SERVER["REMOTE_ADDR"] ?? "x"), 0, 6)
]);
// Max 50 zpráv
$messages = array_slice($messages, 0, 50);
file_put_contents($dataFile, json_encode($messages, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
$success = true;
}
}
$totalMessages = count($messages);
?>
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kniha návštěv</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
min-height: 100vh;
font-family: "Segoe UI", system-ui, sans-serif;
background: linear-gradient(135deg, #0f0c29, #1a1a3e);
color: #e0e0e0;
padding: 2rem;
}
.container { max-width: 700px; margin: 0 auto; }
h1 {
text-align: center;
font-size: 2.2rem;
background: linear-gradient(to right, #00d4aa, #7b68ee);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 0.3rem;
}
.subtitle { text-align: center; color: #6b7280; margin-bottom: 2rem; }
.stats {
text-align: center;
margin-bottom: 2rem;
padding: 0.8rem;
background: rgba(0,212,170,0.08);
border-radius: 12px;
color: #00d4aa;
font-weight: 600;
}
/* Form */
.form-card {
background: rgba(255,255,255,0.04);
border: 1px solid rgba(255,255,255,0.08);
border-radius: 20px;
padding: 2rem;
margin-bottom: 2rem;
}
.form-card h2 { color: #00d4aa; margin-bottom: 1rem; font-size: 1.2rem; }
.form-row { margin-bottom: 1rem; }
.form-row label {
display: block;
color: #8892a4;
font-size: 0.85rem;
margin-bottom: 0.4rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.5px;
}
input[type="text"], textarea {
width: 100%;
padding: 0.8rem 1rem;
background: rgba(255,255,255,0.06);
border: 2px solid rgba(255,255,255,0.1);
border-radius: 12px;
color: #e0e0e0;
font-size: 1rem;
font-family: inherit;
outline: none;
transition: border-color 0.3s;
}
input:focus, textarea:focus { border-color: #7b68ee; }
textarea { min-height: 80px; resize: vertical; }
.emoji-picker {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
}
.emoji-option {
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.3rem;
border-radius: 10px;
border: 2px solid rgba(255,255,255,0.1);
background: rgba(255,255,255,0.04);
cursor: pointer;
transition: all 0.3s;
}
.emoji-option:hover, .emoji-option.selected {
border-color: #00d4aa;
background: rgba(0,212,170,0.15);
transform: scale(1.1);
}
.submit-btn {
width: 100%;
padding: 0.9rem;
background: linear-gradient(135deg, #00d4aa, #7b68ee);
color: white;
border: none;
border-radius: 12px;
font-size: 1.05rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s;
margin-top: 0.5rem;
}
.submit-btn:hover { transform: translateY(-2px); box-shadow: 0 8px 25px rgba(0,212,170,0.4); }
.error-msg { background: rgba(255,107,157,0.15); color: #ff6b9d; padding: 0.8rem; border-radius: 10px; margin-bottom: 1rem; }
.success-msg { background: rgba(0,212,170,0.15); color: #00d4aa; padding: 0.8rem; border-radius: 10px; margin-bottom: 1rem; }
/* Messages */
.messages h2 { color: #00d4aa; margin-bottom: 1rem; font-size: 1.2rem; }
.message {
background: rgba(255,255,255,0.03);
border: 1px solid rgba(255,255,255,0.06);
border-radius: 16px;
padding: 1.2rem;
margin-bottom: 1rem;
transition: all 0.3s;
animation: slideIn 0.3s ease;
}
.message:hover { border-color: rgba(0,212,170,0.2); }
.msg-header { display: flex; align-items: center; gap: 0.8rem; margin-bottom: 0.5rem; }
.msg-emoji { font-size: 1.5rem; }
.msg-name { font-weight: 600; color: #a78bfa; }
.msg-date { color: #555; font-size: 0.8rem; margin-left: auto; }
.msg-text { color: #c0c0c0; line-height: 1.6; }
.empty-state { text-align: center; color: #555; padding: 3rem; font-size: 1.1rem; }
@keyframes slideIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
@media (max-width: 600px) { body { padding: 1rem; } h1 { font-size: 1.8rem; } }
</style>
</head>
<body>
<div class="container">
<h1>📖 Kniha návštěv</h1>
<p class="subtitle">Zanech zprávu pro budoucí návštěvníky!</p>
<?php if ($totalMessages > 0): ?>
<div class="stats">💬 Celkem <?php echo $totalMessages; ?> zpráv</div>
<?php endif; ?>
<div class="form-card">
<h2>✍️ Napsat zprávu</h2>
<?php if ($error): ?>
<div class="error-msg"><?php echo $error; ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success-msg">✅ Zpráva uložena!</div>
<?php endif; ?>
<form method="POST">
<div class="form-row">
<label>Jméno</label>
<input type="text" name="name" placeholder="Vaše jméno" required>
</div>
<div class="form-row">
<label>Vyberte emoji</label>
<div class="emoji-picker">
<div class="emoji-option selected" onclick="selectEmoji(this, '👋')">👋</div>
<div class="emoji-option" onclick="selectEmoji(this, '😀')">😀</div>
<div class="emoji-option" onclick="selectEmoji(this, '🚀')">🚀</div>
<div class="emoji-option" onclick="selectEmoji(this, '❤️')">❤️</div>
<div class="emoji-option" onclick="selectEmoji(this, '🌟')">🌟</div>
<div class="emoji-option" onclick="selectEmoji(this, '🎉')">🎉</div>
<div class="emoji-option" onclick="selectEmoji(this, '💡')">💡</div>
<div class="emoji-option" onclick="selectEmoji(this, '🎮')">🎮</div>
</div>
<input type="hidden" name="emoji" id="selectedEmoji" value="👋">
</div>
<div class="form-row">
<label>Zpráva</label>
<textarea name="message" placeholder="Co byste chtěli sdělit?" required></textarea>
</div>
<button type="submit" class="submit-btn">📨 Odeslat zprávu</button>
</form>
</div>
<div class="messages">
<h2>💬 Zprávy</h2>
<?php if (empty($messages)): ?>
<div class="empty-state">💭 Zatím žádné zprávy. Buď první!</div>
<?php else: ?>
<?php foreach ($messages as $msg): ?>
<div class="message">
<div class="msg-header">
<span class="msg-emoji"><?php echo $msg["emoji"]; ?></span>
<span class="msg-name"><?php echo $msg["name"]; ?></span>
<span class="msg-date"><?php echo $msg["date"]; ?></span>
</div>
<div class="msg-text"><?php echo nl2br($msg["message"]); ?></div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<script>
function selectEmoji(el, emoji) {
document.querySelectorAll(".emoji-option").forEach(e => e.classList.remove("selected"));
el.classList.add("selected");
document.getElementById("selectedEmoji").value = emoji;
}
</script>
</body>
</html>