36407-vm/index.php
2025-11-28 00:36:51 +00:00

111 lines
4.2 KiB
PHP

<?php
// db/config.php is expected to be included and provide a db() function.
require_once 'db/config.php';
// --- Database and App Logic ---
try {
$pdo = db();
// Idempotent table creation
$pdo->exec("CREATE TABLE IF NOT EXISTS messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);");
// Handle new message submission (Post/Redirect/Get pattern)
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty(trim($_POST['message']))) {
$stmt = $pdo->prepare("INSERT INTO messages (message) VALUES (?)");
$stmt->execute([$_POST['message']]);
// Prevent form resubmission on refresh
header("Location: index.php");
exit;
}
// Fetch all messages for display
$messages = $pdo->query("SELECT * FROM messages ORDER BY created_at ASC")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
// For a real app, you'd want to log this and show a user-friendly error page.
error_log("Database error: " . $e->getMessage());
// Keep the page from crashing, show an error inline.
$dbError = "Could not connect to the database or run the query. Please check server logs.";
}
// --- Dynamic Meta Tags & SEO --- (Platform-managed)
$project_name = htmlspecialchars($_SERVER['PROJECT_NAME'] ?? 'Telegram App');
$project_description = htmlspecialchars($_SERVER['PROJECT_DESCRIPTION'] ?? 'A simple messaging app.');
$project_image_url = htmlspecialchars($_SERVER['PROJECT_IMAGE_URL'] ?? '');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $project_name ?></title>
<meta name="description" content="<?= $project_description ?>">
<!-- Open Graph / Facebook -->
<meta property="og:type" content="website">
<meta property="og:title" content="<?= $project_name ?>">
<meta property="og:description" content="<?= $project_description ?>">
<?php if ($project_image_url): ?>
<meta property="og:image" content="<?= $project_image_url ?>">
<?php endif; ?>
<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:title" content="<?= $project_name ?>">
<meta property="twitter:description" content="<?= $project_description ?>">
<?php if ($project_image_url): ?>
<meta property="twitter:image" content="<?= $project_image_url ?>">
<?php endif; ?>
<!-- Libs -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom App Styles -->
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<div class="chat-container">
<header class="chat-header">
Public Chat
</header>
<main class="chat-messages" id="chat-messages">
<?php if (isset($dbError)): ?>
<div class="alert alert-danger mx-auto">Error: <?= htmlspecialchars($dbError) ?></div>
<?php elseif (empty($messages)): ?>
<div class="text-center text-muted mt-5">No messages yet. Be the first to post!</div>
<?php else: ?>
<?php foreach ($messages as $msg): ?>
<div class="message-bubble sent">
<div><?= htmlspecialchars($msg['message']) ?></div>
<div class="message-time"><?= date('h:i A', strtotime($msg['created_at'])) ?></div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</main>
<footer class="chat-form">
<form action="index.php" method="POST" autocomplete="off">
<input type="text" name="message" placeholder="Type a message..." autofocus>
<button type="submit" aria-label="Send">&#10148;</button>
</form>
</footer>
</div>
<script>
// Auto-scroll to the latest message
window.addEventListener('load', () => {
const chatMessages = document.getElementById('chat-messages');
chatMessages.scrollTop = chatMessages.scrollHeight;
});
</script>
</body>
</html>