Compare commits

...

6 Commits

Author SHA1 Message Date
Flatlogic Bot
91e2347c7a 9 2025-09-17 11:16:22 +00:00
Flatlogic Bot
5323ad896f 5 2025-09-17 10:49:46 +00:00
Flatlogic Bot
8989de90b0 3 2025-09-17 10:37:57 +00:00
Flatlogic Bot
96ee530283 admin 2025-09-17 10:34:40 +00:00
Flatlogic Bot
415571db64 post editor 2025-09-17 09:59:23 +00:00
Flatlogic Bot
44a47c5d24 v1 2025-09-16 18:17:20 +00:00
12 changed files with 826 additions and 124 deletions

113
admin.php Normal file
View File

@ -0,0 +1,113 @@
<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
// Handle deletion
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
try {
$pdo = db();
$stmt = $pdo->prepare("DELETE FROM posts WHERE id = ?");
$stmt->execute([$_POST['delete_id']]);
header("Location: admin.php?deleted=true");
exit;
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
// In a real app, you'd have a more robust error handling system
die("Error deleting post. Check logs for details.");
}
}
$posts = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT id, title, created_at FROM posts ORDER BY created_at DESC");
$posts = $stmt->fetchAll();
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin - Manage Posts</title>
<meta name="robots" content="noindex, nofollow">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="/">My Awesome Blog</a>
<a href="admin.php" class="nav-link">Admin</a>
<a href="logout.php" class="nav-link">Logout</a>
</div>
</nav>
<main class="container my-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h2">Manage Posts</h1>
<a href="editor.php" class="btn btn-primary">Create New Post</a>
</div>
<?php if (isset($_GET['deleted'])): ?>
<div class="alert alert-success">Post deleted successfully.</div>
<?php endif; ?>
<?php if (isset($_GET['saved'])): ?>
<div class="alert alert-success">Post saved successfully.</div>
<?php endif; ?>
<?php if (empty($posts)): ?>
<div class="text-center py-5">
<p class="lead">No posts found.</p>
<a href="editor.php" class="btn btn-primary">Create your first post</a>
</div>
<?php else: ?>
<div class="card">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th scope="col">Title</th>
<th scope="col">Created At</th>
<th scope="col" class="text-end">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo htmlspecialchars($post['title']); ?></td>
<td><?php echo date("F j, Y, g:i a", strtotime($post['created_at'])); ?></td>
<td class="text-end">
<a href="editor.php?id=<?php echo $post['id']; ?>" class="btn btn-sm btn-outline-primary">Edit</a>
<form action="admin.php" method="POST" onsubmit="return confirm('Are you sure you want to delete this post?');" class="d-inline">
<input type="hidden" name="delete_id" value="<?php echo $post['id']; ?>">
<button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php endif; ?>
</main>
<footer class="footer mt-auto py-3 bg-white border-top">
<div class="container text-center">
<span class="text-muted">&copy; <?php echo date("Y"); ?> My Awesome Blog. All Rights Reserved.</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

78
assets/css/custom.css Normal file
View File

@ -0,0 +1,78 @@
/*
Palette:
- Primary: #3B82F6 (Blue)
- Secondary: #10B981 (Green)
- Background: #F9FAFB (Light Gray)
- Surface: #FFFFFF (White)
- Text: #1F2937 (Dark Gray)
- Subtle Text: #6B7280 (Lighter Gray)
*/
:root {
--primary-color: #3B82F6;
--secondary-color: #10B981;
--background-color: #F9FAFB;
--surface-color: #FFFFFF;
--text-color: #1F2937;
--subtle-text-color: #6B7280;
--heading-font: Georgia, 'Times New Roman', Times, serif;
--body-font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}
body {
background-color: var(--background-color);
color: var(--text-color);
font-family: var(--body-font);
}
h1, h2, h3, h4, h5, h6 {
font-family: var(--heading-font);
}
.navbar-brand {
font-family: var(--heading-font);
font-weight: bold;
}
.post-card {
background-color: var(--surface-color);
border: 1px solid #E5E7EB;
border-radius: 8px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
}
.post-card:hover {
transform: translateY(-4px);
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
}
.post-card img {
border-top-left-radius: 8px;
border-top-right-radius: 8px;
}
.btn-primary {
background-color: var(--primary-color);
border-color: var(--primary-color);
}
.btn-primary:hover {
background-color: #2563EB;
border-color: #2563EB;
}
.post-content {
line-height: 1.8;
}
.post-content img {
max-width: 100%;
height: auto;
border-radius: 8px;
}
.footer {
background-color: var(--surface-color);
}

0
assets/js/main.js Normal file
View File

14
db/migrate.php Normal file
View File

@ -0,0 +1,14 @@
<?php
$migrations_dir = __DIR__ . '/migrations';
$migration_files = glob($migrations_dir . '/*.php');
foreach ($migration_files as $file) {
require_once $file;
$function_name = 'migrate_' . basename($file, '.php');
if (function_exists($function_name)) {
$function_name();
} else {
echo "Migration function {$function_name} not found in {$file}.\n";
}
}

View File

@ -0,0 +1,23 @@
<?php
require_once __DIR__ . '/../config.php';
function migrate_001_create_users_table() {
try {
$pdo = db();
$sql = "
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
";
$pdo->exec($sql);
echo "Migration 001: Users table created successfully.\n";
} catch (PDOException $e) {
die("Migration 001 failed: " . $e->getMessage() . "\n");
}
}

View File

@ -0,0 +1,25 @@
<?php
require_once __DIR__ . '/../config.php';
function migrate_002_create_comments_table() {
try {
$pdo = db();
$sql = "
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`post_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`comment` text NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `post_id` (`post_id`),
CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
";
$pdo->exec($sql);
echo "Migration 002: Comments table created successfully.\n";
} catch (PDOException $e) {
die("Migration 002 failed: " . $e->getMessage() . "\n");
}
}

148
editor.php Normal file
View File

@ -0,0 +1,148 @@
<?php
session_start();
require_once 'db/config.php';
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$post = [
'id' => null,
'title' => '',
'content' => '',
'excerpt' => '',
'image_url' => ''
];
$pageTitle = 'Create New Post';
$action = 'editor.php';
// Edit mode
if (isset($_GET['id'])) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
$stmt->execute([$_GET['id']]);
$post = $stmt->fetch();
if (!$post) {
// Post not found, redirect or show error
header("Location: admin.php?error=notfound");
exit;
}
$pageTitle = 'Edit Post';
$action = 'editor.php?id=' . $_GET['id'];
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
die("Error fetching post. Check logs.");
}
}
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = $_POST['title'] ?? '';
$content = $_POST['content'] ?? '';
$excerpt = $_POST['excerpt'] ?? '';
$imageUrl = $_POST['image_url'] ?? '';
$id = $_POST['id'] ?? null;
// Basic validation
if (empty($title) || empty($content)) {
$error = "Title and Content are required.";
} else {
try {
$pdo = db();
if ($id) {
// Update
$stmt = $pdo->prepare("UPDATE posts SET title = ?, content = ?, excerpt = ?, image_url = ? WHERE id = ?");
$stmt->execute([$title, $content, $excerpt, $imageUrl, $id]);
} else {
// Insert
$stmt = $pdo->prepare("INSERT INTO posts (title, content, excerpt, image_url) VALUES (?, ?, ?, ?)");
$stmt->execute([$title, $content, $excerpt, $imageUrl]);
}
header("Location: admin.php?saved=true");
exit;
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
$error = "Error saving post. Check logs for details.";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $pageTitle; ?></title>
<meta name="robots" content="noindex, nofollow">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="/">My Awesome Blog</a>
<a href="admin.php" class="nav-link">Admin</a>
<a href="logout.php" class="nav-link">Logout</a>
</div>
</nav>
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<h1 class="h2 mb-4"><?php echo $pageTitle; ?></h1>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="card">
<div class="card-body">
<form action="<?php echo $action; ?>" method="POST">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($post['id'] ?? ''); ?>">
<div class="mb-3">
<label for="title" class="form-label">Title</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($post['title'] ?? ''); ?>" required>
</div>
<div class="mb-3">
<label for="content" class="form-label">Content</label>
<textarea class="form-control" id="content" name="content" rows="10" required><?php echo htmlspecialchars($post['content'] ?? ''); ?></textarea>
</div>
<div class="mb-3">
<label for="excerpt" class="form-label">Excerpt</label>
<textarea class="form-control" id="excerpt" name="excerpt" rows="3"><?php echo htmlspecialchars($post['excerpt'] ?? ''); ?></textarea>
<div class="form-text">A short summary of the post, shown on the main blog page.</div>
</div>
<div class="mb-3">
<label for="image_url" class="form-label">Image URL</label>
<input type="url" class="form-control" id="image_url" name="image_url" value="<?php echo htmlspecialchars($post['image_url'] ?? 'https://picsum.photos/seed/'.uniqid().'/800/600'); ?>">
<div class="form-text">URL for the post's main image. A new random placeholder is generated for you.</div>
</div>
<div class="d-flex justify-content-end">
<a href="admin.php" class="btn btn-secondary me-2">Cancel</a>
<button type="submit" class="btn btn-primary">Save Post</button>
</div>
</form>
</div>
</div>
</div>
</div>
</main>
<footer class="footer mt-auto py-3 bg-white border-top">
<div class="container text-center">
<span class="text-muted">&copy; <?php echo date("Y"); ?> My Awesome Blog. All Rights Reserved.</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

205
index.php
View File

@ -1,131 +1,88 @@
<?php
declare(strict_types=1);
@ini_set('display_errors', '1');
@error_reporting(E_ALL);
@date_default_timezone_set('UTC');
$phpVersion = PHP_VERSION;
$now = date('Y-m-d H:i:s');
session_start();
require_once 'db/config.php';
$posts = [];
try {
$pdo = db();
$stmt = $pdo->query("SELECT * FROM posts ORDER BY created_at DESC");
$posts = $stmt->fetchAll();
} catch (PDOException $e) {
// For a real app, you'd log this error and show a user-friendly message.
// For this demo, we'll just show the error.
error_log("DB Error: " . $e->getMessage());
}
?>
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>New Style</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=Inter:wght@400;700&display=swap" rel="stylesheet">
<style>
:root {
--bg-color-start: #6a11cb;
--bg-color-end: #2575fc;
--text-color: #ffffff;
--card-bg-color: rgba(255, 255, 255, 0.01);
--card-border-color: rgba(255, 255, 255, 0.1);
}
body {
margin: 0;
font-family: 'Inter', sans-serif;
background: linear-gradient(45deg, var(--bg-color-start), var(--bg-color-end));
color: var(--text-color);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
overflow: hidden;
position: relative;
}
body::before {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><path d="M-10 10L110 10M10 -10L10 110" stroke-width="1" stroke="rgba(255,255,255,0.05)"/></svg>');
animation: bg-pan 20s linear infinite;
z-index: -1;
}
@keyframes bg-pan {
0% { background-position: 0% 0%; }
100% { background-position: 100% 100%; }
}
main {
padding: 2rem;
}
.card {
background: var(--card-bg-color);
border: 1px solid var(--card-border-color);
border-radius: 16px;
padding: 2rem;
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
}
.loader {
margin: 1.25rem auto 1.25rem;
width: 48px;
height: 48px;
border: 3px solid rgba(255, 255, 255, 0.25);
border-top-color: #fff;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.hint {
opacity: 0.9;
}
.sr-only {
position: absolute;
width: 1px; height: 1px;
padding: 0; margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap; border: 0;
}
h1 {
font-size: 3rem;
font-weight: 700;
margin: 0 0 1rem;
letter-spacing: -1px;
}
p {
margin: 0.5rem 0;
font-size: 1.1rem;
}
code {
background: rgba(0,0,0,0.2);
padding: 2px 6px;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
footer {
position: absolute;
bottom: 1rem;
font-size: 0.8rem;
opacity: 0.7;
}
</style>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello world</title>
<meta name="description" content="A clean blog to publish updates and articles.">
<!-- Open Graph -->
<meta property="og:title" content="Hello world">
<meta property="og:description" content="A clean blog to publish updates and articles.">
<meta property="og:type" content="website">
<meta property="og:url" content="http://<?php echo $_SERVER['HTTP_HOST']; ?>">
<meta property="og:image" content="https://picsum.photos/seed/hero-1/1200/600">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<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=Inter:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<main>
<div class="card">
<h1>Analyzing your requirements and generating your website…</h1>
<div class="loader" role="status" aria-live="polite" aria-label="Applying initial changes">
<span class="sr-only">Loading…</span>
</div>
<p class="hint">Flatlogic AI is collecting your requirements and applying the first changes.</p>
<p class="hint">This page will update automatically as the plan is implemented.</p>
<p>Runtime: PHP <code><?= htmlspecialchars($phpVersion) ?></code> — UTC <code><?= htmlspecialchars($now) ?></code></p>
</div>
</main>
<footer>
Page updated: <?= htmlspecialchars($now) ?> (UTC)
</footer>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="/">Hello world</a>
</div>
</nav>
<main class="container my-5">
<div class="text-center mb-5">
<h1 class="display-4">Hello world</h1>
<p class="lead text-muted">The latest articles and updates, just for you.</p>
</div>
<?php if (empty($posts)): ?>
<div class="text-center">
<p>No posts found. Run the setup script to seed the database.</p>
<code>php db/setup.php</code>
</div>
<?php else: ?>
<div class="row g-4">
<?php foreach ($posts as $post): ?>
<div class="col-md-6 col-lg-4 d-flex align-items-stretch">
<div class="card post-card w-100">
<img src="<?php echo htmlspecialchars($post['image_url']); ?>" class="card-img-top" alt="Abstract placeholder image for the blog post titled '<?php echo htmlspecialchars($post['title']); ?>'">
<div class="card-body d-flex flex-column">
<h5 class="card-title"><?php echo htmlspecialchars($post['title']); ?></h5>
<p class="card-text text-muted flex-grow-1"><?php echo htmlspecialchars($post['excerpt']); ?></p>
<a href="post.php?id=<?php echo $post['id']; ?>" class="btn btn-primary mt-auto align-self-start">Read More</a>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
</main>
<footer class="footer mt-auto py-3 bg-white border-top">
<div class="container text-center">
<span class="text-muted">&copy; <?php echo date("Y"); ?> Hello world. All Rights Reserved. |
<?php if (isset($_SESSION['user_id'])): ?>
<a href="admin.php">Admin</a> | <a href="logout.php">Logout</a>
<?php else: ?>
<a href="login.php">Login</a>
<?php endif; ?>
</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>
</html>

89
login.php Normal file
View File

@ -0,0 +1,89 @@
<?php
session_start();
require_once 'db/config.php';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if (empty($username) || empty($password)) {
$error = 'Please fill in all fields.';
} else {
try {
$stmt = db()->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: admin.php");
exit;
} else {
$error = 'Invalid username or password.';
}
} catch (PDOException $e) {
$error = "Error: " . $e->getMessage();
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - My Blog</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Georgia&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="bg-light-gray">
<header class="bg-white shadow-md">
<div class="container mx-auto px-4 py-6">
<h1 class="text-3xl font-serif text-dark-gray"><a href="index.php">My Blog</a></h1>
</div>
</header>
<main class="container mx-auto px-4 py-12">
<div class="max-w-md mx-auto bg-white p-8 rounded-lg shadow-md">
<h2 class="text-2xl font-bold mb-6 text-center">Admin Login</h2>
<?php if ($error): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline"><?php echo htmlspecialchars($error); ?></span>
</div>
<?php endif; ?>
<form action="login.php" method="POST">
<div class="mb-4">
<label for="username" class="block text-gray-700 text-sm font-bold mb-2">Username</label>
<input type="text" id="username" name="username" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-6">
<label for="password" class="block text-gray-700 text-sm font-bold mb-2">Password</label>
<input type="password" id="password" name="password" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="flex items-center justify-between">
<button type="submit"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Sign In
</button>
<a href="register.php" class="inline-block align-baseline font-bold text-sm text-blue-500 hover:text-blue-800">
Register
</a>
</div>
</form>
</div>
</main>
<footer class="bg-white mt-12">
<div class="container mx-auto px-4 py-6 text-center text-gray-600">
<p>&copy; <?php echo date('Y'); ?> My Blog. All rights reserved.</p>
</div>
</footer>
</body>
</html>

6
logout.php Normal file
View File

@ -0,0 +1,6 @@
<?php
session_start();
session_unset();
session_destroy();
header("Location: login.php");
exit;

143
post.php Normal file
View File

@ -0,0 +1,143 @@
<?php
require_once 'db/config.php';
$post_id = $_GET['id'] ?? null;
if (!$post_id || !filter_var($post_id, FILTER_VALIDATE_INT)) {
header("Location: index.php");
exit;
}
// Handle comment submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_comment') {
$name = trim($_POST['name'] ?? '');
$comment_text = trim($_POST['comment'] ?? '');
if (!empty($name) && !empty($comment_text)) {
try {
$pdo = db();
$stmt = $pdo->prepare("INSERT INTO comments (post_id, name, comment) VALUES (?, ?, ?)");
$stmt->execute([$post_id, $name, $comment_text]);
header("Location: post.php?id=" . $post_id);
exit;
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
}
}
}
$post = null;
$comments = [];
try {
$pdo = db();
// Fetch post
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
$stmt->execute([$post_id]);
$post = $stmt->fetch();
// Fetch comments
$stmt = $pdo->prepare("SELECT * FROM comments WHERE post_id = ? ORDER BY created_at DESC");
$stmt->execute([$post_id]);
$comments = $stmt->fetchAll();
} catch (PDOException $e) {
error_log("DB Error: " . $e->getMessage());
}
// If no post is found, redirect to the homepage
if (!$post) {
header("Location: index.php");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo htmlspecialchars($post['title']); ?> - My Awesome Blog</title>
<meta name="description" content="<?php echo htmlspecialchars($post['excerpt']); ?>">
<!-- Open Graph -->
<meta property="og:title" content="<?php echo htmlspecialchars($post['title']); ?>">
<meta property="og:description" content="<?php echo htmlspecialchars($post['excerpt']); ?>">
<meta property="og:type" content="article">
<meta property="og:url" content="http://<?php echo $_SERVER['HTTP_HOST']; ?>/post.php?id=<?php echo $post['id']; ?>">
<meta property="og:image" content="<?php echo htmlspecialchars($post['image_url']); ?>">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<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=Inter:wght@400;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm">
<div class="container">
<a class="navbar-brand" href="/">My Awesome Blog</a>
</div>
</nav>
<main class="container my-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<article>
<header class="mb-4">
<h1 class="fw-bolder mb-1 display-5"><?php echo htmlspecialchars($post['title']); ?></h1>
<div class="text-muted fst-italic mb-2">Posted on <?php echo date('F j, Y', strtotime($post['created_at'])); ?></div>
</header>
<figure class="mb-4">
<img class="img-fluid rounded" src="<?php echo htmlspecialchars($post['image_url']); ?>" alt="Hero image for the blog post titled '<?php echo htmlspecialchars($post['title']); ?>'">
</figure>
<section class="mb-5 post-content fs-5">
<?php echo nl2br(htmlspecialchars($post['content'])); ?>
</section>
</article>
<section class="mb-5">
<div class="card bg-light">
<div class="card-body">
<!-- Comment form-->
<form class="mb-4" method="POST">
<input type="hidden" name="action" value="create_comment">
<div class="mb-3">
<label for="name" class="form-label">Your Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="comment" class="form-label">Your Comment</label>
<textarea class="form-control" rows="3" id="comment" name="comment" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit Comment</button>
</form>
<!-- Comments section-->
<?php foreach ($comments as $comment): ?>
<div class="d-flex mb-4">
<div class="flex-shrink-0"><img class="rounded-circle" src="https://dummyimage.com/50x50/ced4da/6c757d.jpg" alt="..." /></div>
<div class="ms-3">
<div class="fw-bold"><?php echo htmlspecialchars($comment['name']); ?></div>
<?php echo htmlspecialchars($comment['comment']); ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</section>
<a href="/" class="btn btn-outline-primary">&larr; Back to all posts</a>
</div>
</div>
</main>
<footer class="footer mt-auto py-3 bg-white border-top">
<div class="container text-center">
<span class="text-muted">&copy; <?php echo date("Y"); ?> My Awesome Blog. All Rights Reserved.</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
</body>
</html>

106
register.php Normal file
View File

@ -0,0 +1,106 @@
<?php
session_start();
require_once 'db/config.php';
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$password_confirm = $_POST['password_confirm'] ?? '';
if (empty($username) || empty($password) || empty($password_confirm)) {
$error = 'Please fill in all fields.';
} elseif ($password !== $password_confirm) {
$error = 'Passwords do not match.';
} else {
try {
$stmt = db()->prepare("SELECT id FROM users WHERE username = ?");
$stmt->execute([$username]);
if ($stmt->fetch()) {
$error = 'Username already exists.';
} else {
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
$stmt = db()->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->execute([$username, $hashed_password]);
$success = 'Registration successful! You can now <a href="login.php" class="font-bold text-blue-700">log in</a>.';
}
} catch (PDOException $e) {
if ($e->getCode() === '42S02') { // Base table not found
$error = 'The application has not been fully set up. Please run the database migrations.';
} else {
$error = "Error: " . $e->getMessage();
}
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - My Blog</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Georgia&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="bg-light-gray">
<header class="bg-white shadow-md">
<div class="container mx-auto px-4 py-6">
<h1 class="text-3xl font-serif text-dark-gray"><a href="index.php">My Blog</a></h1>
</div>
</header>
<main class="container mx-auto px-4 py-12">
<div class="max-w-md mx-auto bg-white p-8 rounded-lg shadow-md">
<h2 class="text-2xl font-bold mb-6 text-center">Create an Account</h2>
<?php if ($error): ?>
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline"><?php echo htmlspecialchars($error); ?></span>
</div>
<?php endif; ?>
<?php if ($success): ?>
<div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline"><?php echo $success; ?></span>
</div>
<?php else: ?>
<form action="register.php" method="POST">
<div class="mb-4">
<label for="username" class="block text-gray-700 text-sm font-bold mb-2">Username</label>
<input type="text" id="username" name="username" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-4">
<label for="password" class="block text-gray-700 text-sm font-bold mb-2">Password</label>
<input type="password" id="password" name="password" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-6">
<label for="password_confirm" class="block text-gray-700 text-sm font-bold mb-2">Confirm Password</label>
<input type="password" id="password_confirm" name="password_confirm" required
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 mb-3 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="flex items-center justify-between">
<button type="submit"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Register
</button>
<a href="login.php" class="inline-block align-baseline font-bold text-sm text-blue-500 hover:text-blue-800">
Login
</a>
</div>
</form>
<?php endif; ?>
</div>
</main>
<footer class="bg-white mt-12">
<div class="container mx-auto px-4 py-6 text-center text-gray-600">
<p>&copy; <?php echo date('Y'); ?> My Blog. All rights reserved.</p>
</div>
</footer>
</body>
</html>