diff --git a/admin/auth.php b/admin/auth.php new file mode 100644 index 0000000..fb04e2d --- /dev/null +++ b/admin/auth.php @@ -0,0 +1,7 @@ +prepare("INSERT INTO posts (user_id, title, content, slug) VALUES (?, ?, ?, ?)"); + $stmt->execute([$_SESSION['user_id'], $title, $content, $slug]); + + header('Location: posts.php'); + exit; +} +?> + +

Create Post

+ +
+
+ + +
+
+ + +
+ +
+ + diff --git a/admin/delete_post.php b/admin/delete_post.php new file mode 100644 index 0000000..ef221c0 --- /dev/null +++ b/admin/delete_post.php @@ -0,0 +1,21 @@ +prepare("SELECT * FROM posts WHERE id = ? AND user_id = ?"); +$stmt->execute([$id, $_SESSION['user_id']]); +$post = $stmt->fetch(); + +if ($post) { + $stmt = db()->prepare("DELETE FROM posts WHERE id = ?"); + $stmt->execute([$id]); +} + +header('Location: posts.php'); +exit; diff --git a/admin/edit_post.php b/admin/edit_post.php new file mode 100644 index 0000000..0e720d7 --- /dev/null +++ b/admin/edit_post.php @@ -0,0 +1,48 @@ +prepare("SELECT * FROM posts WHERE id = ? AND user_id = ?"); +$stmt->execute([$id, $_SESSION['user_id']]); +$post = $stmt->fetch(); + +if (!$post) { + header('Location: posts.php'); + exit; +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $title = $_POST['title']; + $content = $_POST['content']; + $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $title))); + + $stmt = db()->prepare("UPDATE posts SET title = ?, content = ?, slug = ? WHERE id = ?"); + $stmt->execute([$title, $content, $slug, $id]); + + header('Location: posts.php'); + exit; +} +?> + +

Edit Post

+ +
+
+ + +
+
+ + +
+ +
+ + diff --git a/admin/index.php b/admin/index.php new file mode 100644 index 0000000..dd4baf2 --- /dev/null +++ b/admin/index.php @@ -0,0 +1,11 @@ + + +

Admin Dashboard

+

Welcome, !

+ +Manage Posts + + diff --git a/admin/posts.php b/admin/posts.php new file mode 100644 index 0000000..ec4d21a --- /dev/null +++ b/admin/posts.php @@ -0,0 +1,36 @@ +prepare("SELECT * FROM posts WHERE user_id = ? ORDER BY created_at DESC"); +$stmt->execute([$_SESSION['user_id']]); +$posts = $stmt->fetchAll(); +?> + +

Manage Posts

+Create Post + + + + + + + + + + + + + + + + + + +
TitleCreated AtActions
+ Edit + Delete +
+ + diff --git a/db/migrate.php b/db/migrate.php new file mode 100644 index 0000000..6c09352 --- /dev/null +++ b/db/migrate.php @@ -0,0 +1,12 @@ +exec($sql); + echo "Migration successful!\n"; +} catch (PDOException $e) { + die("Migration failed: " . $e->getMessage() . "\n"); +} + diff --git a/db/migrations/001_create_users_and_posts_tables.sql b/db/migrations/001_create_users_and_posts_tables.sql new file mode 100644 index 0000000..84a6d9d --- /dev/null +++ b/db/migrations/001_create_users_and_posts_tables.sql @@ -0,0 +1,23 @@ +CREATE TABLE IF NOT EXISTS `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `username` varchar(50) NOT NULL, + `password` varchar(255) NOT NULL, + `email` varchar(100) NOT NULL, + `created_at` datetime DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `username` (`username`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS `posts` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `title` varchar(255) NOT NULL, + `slug` varchar(255) NOT NULL, + `content` text NOT NULL, + `created_at` datetime DEFAULT CURRENT_TIMESTAMP, + `updated_at` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `slug` (`slug`), + KEY `user_id` (`user_id`), + CONSTRAINT `posts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/includes/footer.php b/includes/footer.php new file mode 100644 index 0000000..5c46507 --- /dev/null +++ b/includes/footer.php @@ -0,0 +1,4 @@ + + + + diff --git a/includes/header.php b/includes/header.php new file mode 100644 index 0000000..8b3541e --- /dev/null +++ b/includes/header.php @@ -0,0 +1,38 @@ + + + + + + + My Blog + + + + +
\ No newline at end of file diff --git a/index.php b/index.php index 7205f3d..420a032 100644 --- a/index.php +++ b/index.php @@ -1,150 +1,33 @@ query("SELECT id FROM users LIMIT 1"); +if ($stmt->rowCount() === 0) { + header('Location: register.php'); + exit; +} + +require_once 'includes/header.php'; + +$stmt = db()->query("SELECT posts.*, users.username FROM posts JOIN users ON posts.user_id = users.id ORDER BY posts.created_at DESC"); +$posts = $stmt->fetchAll(); ?> - - - - - - New Style - - - - - - - - - - - - - - - - - - - - - -
-
-

Analyzing your requirements and generating your website…

-
- Loading… -
-

AI is collecting your requirements and applying the first changes.

-

This page will update automatically as the plan is implemented.

-

Runtime: PHP — UTC

-
-
- - - + +

Blog

+ +
+ +
+
+
+
+

By on

+ Read More +
+
+
+ +
+ + diff --git a/login.php b/login.php new file mode 100644 index 0000000..2727f7b --- /dev/null +++ b/login.php @@ -0,0 +1,49 @@ +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'); + exit; + } else { + $error = 'Invalid credentials'; + } +} +?> + +
+
+
+
Login
+
+ +
+ +
+
+ + +
+
+ + +
+ +

Don't have an account? Register here.

+
+
+
+
+
+ + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..30b3767 --- /dev/null +++ b/logout.php @@ -0,0 +1,5 @@ +prepare("SELECT posts.*, users.username FROM posts JOIN users ON posts.user_id = users.id WHERE posts.slug = ?"); +$stmt->execute([$slug]); +$post = $stmt->fetch(); + +if (!$post) { + http_response_code(404); + echo "Post not found"; + exit; +} +?> + +

+

By on

+ +
+ +
+ + diff --git a/register.php b/register.php new file mode 100644 index 0000000..2e5e6e3 --- /dev/null +++ b/register.php @@ -0,0 +1,54 @@ +prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); + $stmt->execute([$username, $email, $password]); + header('Location: login.php'); + exit; + } catch (PDOException $e) { + if ($e->errorInfo[1] == 1062) { + $error = 'Username or email already exists'; + } else { + $error = 'Something went wrong. Please try again.'; + } + } +} +?> + +
+
+
+
Register
+
+ +
+ +
+
+ + +
+
+ + +
+
+ + +
+ +

Already have an account? Login here.

+
+
+
+
+
+ +