Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9671081d9c |
153
add_book.php
Normal file
153
add_book.php
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
if (!isset($_SESSION["user_id"])) {
|
||||||
|
header("Location: login.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once 'db/config.php';
|
||||||
|
require_once 'header.php';
|
||||||
|
|
||||||
|
$user_id = $_SESSION["user_id"];
|
||||||
|
$message = '';
|
||||||
|
$search_results = [];
|
||||||
|
$search_query = '';
|
||||||
|
|
||||||
|
// Handle Google Books Search
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['search'])) {
|
||||||
|
$search_query = trim($_GET['search']);
|
||||||
|
if (!empty($search_query) && defined('GOOGLE_BOOKS_API_KEY') && GOOGLE_BOOKS_API_KEY != 'YOUR_GOOGLE_BOOKS_API_KEY') {
|
||||||
|
$api_url = "https://www.googleapis.com/books/v1/volumes?q=" . urlencode($search_query) . "&key=" . GOOGLE_BOOKS_API_KEY;
|
||||||
|
$response = @file_get_contents($api_url);
|
||||||
|
if ($response) {
|
||||||
|
$data = json_decode($response, true);
|
||||||
|
if (isset($data['items'])) {
|
||||||
|
$search_results = $data['items'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Could not connect to Google Books API.</div>';
|
||||||
|
}
|
||||||
|
} elseif (empty($search_query)) {
|
||||||
|
$message = '<div class="alert alert-warning">Please enter a search term.</div>';
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Google Books API key is not configured. Please ask the administrator to set it up.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle Add Book from Google Books
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_google_book'])) {
|
||||||
|
$title = trim($_POST['title']);
|
||||||
|
$author_name = trim($_POST['author']);
|
||||||
|
$google_books_id = trim($_POST['google_books_id']);
|
||||||
|
$description = trim($_POST['description']);
|
||||||
|
$cover_image_url = trim($_POST['cover_image_url']);
|
||||||
|
|
||||||
|
if (!empty($title) && !empty($author_name)) {
|
||||||
|
try {
|
||||||
|
// Check if book with this Google ID already exists
|
||||||
|
$stmt = db()->prepare("SELECT id FROM books WHERE google_books_id = :google_books_id");
|
||||||
|
$stmt->execute(['google_books_id' => $google_books_id]);
|
||||||
|
$book = $stmt->fetch();
|
||||||
|
|
||||||
|
if ($book) {
|
||||||
|
$book_id = $book['id'];
|
||||||
|
} else {
|
||||||
|
// Insert new book
|
||||||
|
$stmt = db()->prepare(
|
||||||
|
"INSERT INTO books (title, author_name, description, cover_image_url, google_books_id, added_by_user_id)
|
||||||
|
VALUES (:title, :author_name, :description, :cover_image_url, :google_books_id, :user_id)"
|
||||||
|
);
|
||||||
|
$stmt->execute([
|
||||||
|
'title' => $title,
|
||||||
|
'author_name' => $author_name,
|
||||||
|
'description' => $description,
|
||||||
|
'cover_image_url' => $cover_image_url,
|
||||||
|
'google_books_id' => $google_books_id,
|
||||||
|
'user_id' => $user_id
|
||||||
|
]);
|
||||||
|
$book_id = db()->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the book is already in the user's library
|
||||||
|
$stmt = db()->prepare("SELECT * FROM user_libraries WHERE user_id = :user_id AND book_id = :book_id");
|
||||||
|
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
|
||||||
|
if ($stmt->fetch()) {
|
||||||
|
$message = '<div class="alert alert-warning">This book is already in your library.</div>';
|
||||||
|
} else {
|
||||||
|
// Add book to user's library
|
||||||
|
$stmt = db()->prepare("INSERT INTO user_libraries (user_id, book_id) VALUES (:user_id, :book_id)");
|
||||||
|
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
|
||||||
|
$message = '<div class="alert alert-success">Book added to your library! <a href="library.php">View your library</a>.</div>';
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$message = '<div class="alert alert-danger">Database Error: ' . $e->getMessage() . '</div>';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Title and Author are required.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="container mt-5">
|
||||||
|
<h2>Add Book from Google</h2>
|
||||||
|
<p>Search for a book to add it to your library automatically.</p>
|
||||||
|
|
||||||
|
<?php echo $message; ?>
|
||||||
|
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-header">Search Google Books</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<form action="add_book.php" method="get">
|
||||||
|
<div class="input-group mb-3">
|
||||||
|
<input type="text" class="form-control" placeholder="Search by title, author, or ISBN..." name="search" value="<?php echo htmlspecialchars($search_query); ?>">
|
||||||
|
<button class="btn btn-primary" type="submit">Search</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php if (!empty($search_results)): ?>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Search Results</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<div class="list-group">
|
||||||
|
<?php foreach ($search_results as $item): ?>
|
||||||
|
<?php
|
||||||
|
$volumeInfo = $item['volumeInfo'];
|
||||||
|
$title = $volumeInfo['title'] ?? 'N/A';
|
||||||
|
$authors = isset($volumeInfo['authors']) ? implode(', ', $volumeInfo['authors']) : 'N/A';
|
||||||
|
$description = $volumeInfo['description'] ?? '';
|
||||||
|
$cover_image = $volumeInfo['imageLinks']['thumbnail'] ?? 'https://via.placeholder.com/128x192.png?text=No+Cover';
|
||||||
|
$google_id = $item['id'];
|
||||||
|
?>
|
||||||
|
<div class="list-group-item">
|
||||||
|
<div class="row g-3">
|
||||||
|
<div class="col-md-2">
|
||||||
|
<img src="<?php echo htmlspecialchars($cover_image); ?>" class="img-fluid rounded" alt="Cover for <?php echo htmlspecialchars($title); ?>">
|
||||||
|
</div>
|
||||||
|
<div class="col-md-10">
|
||||||
|
<h5 class="mb-1"><?php echo htmlspecialchars($title); ?></h5>
|
||||||
|
<p class="mb-1">by <?php echo htmlspecialchars($authors); ?></p>
|
||||||
|
<p class="mb-1 text-muted small"><?php echo htmlspecialchars(substr($description, 0, 200)); ?>...</p>
|
||||||
|
<form action="add_book.php" method="post" class="mt-2">
|
||||||
|
<input type="hidden" name="title" value="<?php echo htmlspecialchars($title); ?>">
|
||||||
|
<input type="hidden" name="author" value="<?php echo htmlspecialchars($authors); ?>">
|
||||||
|
<input type="hidden" name="google_books_id" value="<?php echo htmlspecialchars($google_id); ?>">
|
||||||
|
<input type="hidden" name="description" value="<?php echo htmlspecialchars($description); ?>">
|
||||||
|
<input type="hidden" name="cover_image_url" value="<?php echo htmlspecialchars($cover_image); ?>">
|
||||||
|
<button type="submit" name="add_google_book" class="btn btn-sm btn-success">Add to Library</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
93
admin.php
Normal file
93
admin.php
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
// Ensure user is admin
|
||||||
|
if (!isset($_SESSION['user_id']) || !isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'admin') {
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
$message = '';
|
||||||
|
|
||||||
|
// Handle approve/reject actions
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['application_id'])) {
|
||||||
|
$application_id = $_POST['application_id'];
|
||||||
|
$user_id = $_POST['user_id'];
|
||||||
|
|
||||||
|
if (isset($_POST['approve'])) {
|
||||||
|
// Update application status
|
||||||
|
$stmt = $pdo->prepare("UPDATE writer_applications SET status = 'approved' WHERE id = ?");
|
||||||
|
$stmt->execute([$application_id]);
|
||||||
|
|
||||||
|
// Update user role
|
||||||
|
$stmt = $pdo->prepare("UPDATE users SET role = 'writer' WHERE id = ?");
|
||||||
|
$stmt->execute([$user_id]);
|
||||||
|
|
||||||
|
$message = '<div class="alert alert-success">Application approved. User is now a writer.</div>';
|
||||||
|
} elseif (isset($_POST['reject'])) {
|
||||||
|
// Update application status
|
||||||
|
$stmt = $pdo->prepare("UPDATE writer_applications SET status = 'rejected' WHERE id = ?");
|
||||||
|
$stmt->execute([$application_id]);
|
||||||
|
|
||||||
|
$message = '<div class="alert alert-info">Application rejected.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch pending applications
|
||||||
|
$stmt = $pdo->prepare("SELECT wa.*, u.username FROM writer_applications wa JOIN users u ON wa.user_id = u.id WHERE wa.status = 'pending'");
|
||||||
|
$stmt->execute();
|
||||||
|
$applications = $stmt->fetchAll();
|
||||||
|
|
||||||
|
?>
|
||||||
|
<?php require_once 'header.php'; ?>
|
||||||
|
|
||||||
|
<h2>Admin Panel - Writer Applications</h2>
|
||||||
|
|
||||||
|
<?php echo $message; ?>
|
||||||
|
|
||||||
|
<p>To test this page, you need to manually set a user's role to 'admin' in your database. For example: <br><code>UPDATE users SET role = 'admin' WHERE id = 1;</code></p>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Pending Applications</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Username</th>
|
||||||
|
<th>Bio</th>
|
||||||
|
<th>Action</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<?php if (empty($applications)): ?>
|
||||||
|
<tr>
|
||||||
|
<td colspan="3">No pending applications.</td>
|
||||||
|
</tr>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php foreach ($applications as $app): ?>
|
||||||
|
<tr>
|
||||||
|
<td><?php echo htmlspecialchars($app['username']); ?></td>
|
||||||
|
<td><?php echo htmlspecialchars($app['bio']); ?></td>
|
||||||
|
<td>
|
||||||
|
<form action="admin.php" method="post" style="display: inline-block;">
|
||||||
|
<input type="hidden" name="application_id" value="<?php echo $app['id']; ?>">
|
||||||
|
<input type="hidden" name="user_id" value="<?php echo $app['user_id']; ?>">
|
||||||
|
<button type="submit" name="approve" class="btn btn-success btn-sm">Approve</button>
|
||||||
|
</form>
|
||||||
|
<form action="admin.php" method="post" style="display: inline-block;">
|
||||||
|
<input type="hidden" name="application_id" value="<?php echo $app['id']; ?>">
|
||||||
|
<input type="hidden" name="user_id" value="<?php echo $app['user_id']; ?>">
|
||||||
|
<button type="submit" name="reject" class="btn btn-danger btn-sm">Reject</button>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
79
apply_writer.php
Normal file
79
apply_writer.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
if (!isset($_SESSION["user_id"])) {
|
||||||
|
header("Location: login.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once 'db/config.php';
|
||||||
|
require_once 'header.php';
|
||||||
|
|
||||||
|
$user_id = $_SESSION["user_id"];
|
||||||
|
$message = '';
|
||||||
|
|
||||||
|
// Check if user is already a writer or has a pending application
|
||||||
|
$stmt = db()->prepare("SELECT is_writer FROM users WHERE id = :user_id");
|
||||||
|
$stmt->execute(['user_id' => $user_id]);
|
||||||
|
$is_writer = $stmt->fetchColumn();
|
||||||
|
|
||||||
|
$stmt = db()->prepare("SELECT status FROM writer_applications WHERE user_id = :user_id");
|
||||||
|
$stmt->execute(['user_id' => $user_id]);
|
||||||
|
$application = $stmt->fetch();
|
||||||
|
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['apply'])) {
|
||||||
|
if (!$is_writer && (!$application || $application['status'] == 'rejected')) {
|
||||||
|
$bio = trim($_POST['bio']);
|
||||||
|
if (!empty($bio)) {
|
||||||
|
try {
|
||||||
|
$stmt = db()->prepare("INSERT INTO writer_applications (user_id, bio, status) VALUES (:user_id, :bio, 'pending') ON DUPLICATE KEY UPDATE bio = :bio, status = 'pending'");
|
||||||
|
$stmt->execute(['user_id' => $user_id, 'bio' => $bio]);
|
||||||
|
$message = '<div class="alert alert-success">Your application has been submitted for review.</div>';
|
||||||
|
// Refresh application status
|
||||||
|
$stmt = db()->prepare("SELECT status FROM writer_applications WHERE user_id = :user_id");
|
||||||
|
$stmt->execute(['user_id' => $user_id]);
|
||||||
|
$application = $stmt->fetch();
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Please provide a short bio or reason for your application.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="container mt-5">
|
||||||
|
<h2>Become a Writer</h2>
|
||||||
|
|
||||||
|
<?php echo $message; ?>
|
||||||
|
|
||||||
|
<?php if ($is_writer): ?>
|
||||||
|
<div class="alert alert-info">You are already a writer.</div>
|
||||||
|
<?php elseif ($application && $application['status'] == 'pending'): ?>
|
||||||
|
<div class="alert alert-info">You have a pending application. Please wait for an admin to review it.</div>
|
||||||
|
<?php elseif ($application && $application['status'] == 'approved'): ?>
|
||||||
|
<div class="alert alert-success">Your application was approved! You are now a writer.</div>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php if ($application && $application['status'] == 'rejected'): ?>
|
||||||
|
<div class="alert alert-warning">Your previous application was rejected. You may reapply with more information.</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
Writer Application
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<p>Tell us a bit about yourself and why you'd like to be a writer on our platform.</p>
|
||||||
|
<form action="apply_writer.php" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="bio" class="form-label">Your Bio & Motivation</label>
|
||||||
|
<textarea class="form-control" id="bio" name="bio" rows="5" required></textarea>
|
||||||
|
</div>
|
||||||
|
<button type="submit" name="apply" class="btn btn-primary">Submit Application</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
BIN
assets/pasted-20251108-120139-9f8bf321.png
Normal file
BIN
assets/pasted-20251108-120139-9f8bf321.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 593 KiB |
69
author.php
Normal file
69
author.php
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
require_once 'header.php';
|
||||||
|
|
||||||
|
// Get author ID from query string
|
||||||
|
$author_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||||
|
|
||||||
|
if (!$author_id) {
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
|
||||||
|
// Fetch author details
|
||||||
|
// This assumes the bio is stored in the writer_applications table.
|
||||||
|
// A better approach would be to have a 'bio' column in the users table.
|
||||||
|
$stmt = $pdo->prepare("SELECT u.username, wa.bio FROM users u LEFT JOIN writer_applications wa ON u.id = wa.user_id WHERE u.id = ? AND u.role = 'writer'");
|
||||||
|
$stmt->execute([$author_id]);
|
||||||
|
$author = $stmt->fetch();
|
||||||
|
|
||||||
|
if (!$author) {
|
||||||
|
// Redirect if not a writer or doesn't exist
|
||||||
|
// You might want to show a generic "user not found" page instead
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch author's books, assuming the user who added the book is the author
|
||||||
|
$stmt = $pdo->prepare("SELECT id, title, author_name, description FROM books WHERE added_by_user_id = ? ORDER BY created_at DESC");
|
||||||
|
$stmt->execute([$author_id]);
|
||||||
|
$books = $stmt->fetchAll();
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="container mt-5">
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-body">
|
||||||
|
<h1 class="card-title"><?php echo htmlspecialchars($author['username']); ?></h1>
|
||||||
|
<p class="card-text"><?php echo nl2br(htmlspecialchars($author['bio'] ?? 'This author has not provided a bio yet.')); ?></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<h3>Books by <?php echo htmlspecialchars($author['username']); ?></h3>
|
||||||
|
|
||||||
|
<div class="row">
|
||||||
|
<?php if (empty($books)): ?>
|
||||||
|
<div class="col">
|
||||||
|
<p>This author has not published any books yet.</p>
|
||||||
|
</div>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php foreach ($books as $book): ?>
|
||||||
|
<div class="col-md-4 mb-4">
|
||||||
|
<div class="card h-100">
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title"><a href="book.php?id=<?php echo $book['id']; ?>"><?php echo htmlspecialchars($book['title']); ?></a></h5>
|
||||||
|
<p class="card-text"><?php echo htmlspecialchars(substr($book['description'] ?? '', 0, 100)); ?>...</p>
|
||||||
|
</div>
|
||||||
|
<div class="card-footer">
|
||||||
|
<a href="book.php?id=<?php echo $book['id']; ?>" class="btn btn-primary">View Details</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
36
book.php
Normal file
36
book.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
// Get book ID from query string
|
||||||
|
$book_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
||||||
|
|
||||||
|
if (!$book_id) {
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
|
||||||
|
// Fetch book details
|
||||||
|
$stmt = $pdo->prepare("SELECT b.*, u.username FROM books b JOIN users u ON b.user_id = u.id WHERE b.id = ?");
|
||||||
|
$stmt->execute([$book_id]);
|
||||||
|
$book = $stmt->fetch();
|
||||||
|
|
||||||
|
if (!$book) {
|
||||||
|
// Redirect if book doesn't exist
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
<?php require_once 'header.php'; ?>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-body">
|
||||||
|
<h1 class="card-title"><?php echo htmlspecialchars($book['title']); ?></h1>
|
||||||
|
<h6 class="card-subtitle mb-2 text-muted">by <a href="/author.php?id=<?php echo $book['user_id']; ?>"><?php echo htmlspecialchars($book['username']); ?></a></h6>
|
||||||
|
<p class="card-text"><?php echo nl2br(htmlspecialchars($book['description'])); ?></p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
@ -5,6 +5,10 @@ define('DB_NAME', 'app_35570');
|
|||||||
define('DB_USER', 'app_35570');
|
define('DB_USER', 'app_35570');
|
||||||
define('DB_PASS', 'ea95d2df-6283-4025-8109-636b56cb9418');
|
define('DB_PASS', 'ea95d2df-6283-4025-8109-636b56cb9418');
|
||||||
|
|
||||||
|
// Define your Google Books API key here.
|
||||||
|
// You can obtain a key from the Google Cloud Console: https://console.cloud.google.com/apis/library/books.googleapis.com
|
||||||
|
define('GOOGLE_BOOKS_API_KEY', 'YOUR_GOOGLE_BOOKS_API_KEY');
|
||||||
|
|
||||||
function db() {
|
function db() {
|
||||||
static $pdo;
|
static $pdo;
|
||||||
if (!$pdo) {
|
if (!$pdo) {
|
||||||
@ -14,4 +18,4 @@ function db() {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
return $pdo;
|
return $pdo;
|
||||||
}
|
}
|
||||||
12
db/migrate.php
Normal file
12
db/migrate.php
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/config.php';
|
||||||
|
|
||||||
|
try {
|
||||||
|
$pdo = db();
|
||||||
|
$sql = file_get_contents(__DIR__ . '/migrations/001_initial_schema.sql');
|
||||||
|
$pdo->exec($sql);
|
||||||
|
echo "Database migration completed successfully.\n";
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
die("Database migration failed: " . $e->getMessage() . "\n");
|
||||||
|
}
|
||||||
|
|
||||||
72
db/migrations/001_initial_schema.sql
Normal file
72
db/migrations/001_initial_schema.sql
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
-- 001_initial_schema.sql
|
||||||
|
|
||||||
|
-- Users table for members and admins
|
||||||
|
CREATE TABLE IF NOT EXISTS `users` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`username` VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
`email` VARCHAR(255) NOT NULL UNIQUE,
|
||||||
|
`password_hash` VARCHAR(255) NOT NULL,
|
||||||
|
`role` ENUM('member', 'admin', 'writer') NOT NULL DEFAULT 'member',
|
||||||
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
-- Writer applications table
|
||||||
|
CREATE TABLE IF NOT EXISTS `writer_applications` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`user_id` INT NOT NULL,
|
||||||
|
`gov_id_path` VARCHAR(255) NOT NULL,
|
||||||
|
`work_proof_path` VARCHAR(255) NOT NULL,
|
||||||
|
`status` ENUM('pending', 'approved', 'rejected') NOT NULL DEFAULT 'pending',
|
||||||
|
`requested_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`reviewed_at` TIMESTAMP NULL DEFAULT NULL,
|
||||||
|
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
-- Books table
|
||||||
|
CREATE TABLE IF NOT EXISTS `books` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`title` VARCHAR(255) NOT NULL,
|
||||||
|
`author_name` VARCHAR(255) NOT NULL,
|
||||||
|
`description` TEXT,
|
||||||
|
`cover_image_url` VARCHAR(255),
|
||||||
|
`google_books_id` VARCHAR(255) UNIQUE,
|
||||||
|
`open_library_id` VARCHAR(255) UNIQUE,
|
||||||
|
`added_by_user_id` INT,
|
||||||
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (`added_by_user_id`) REFERENCES `users`(`id`) ON DELETE SET NULL
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
-- User libraries table (connecting users and books)
|
||||||
|
CREATE TABLE IF NOT EXISTS `user_libraries` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`user_id` INT NOT NULL,
|
||||||
|
`book_id` INT NOT NULL,
|
||||||
|
`review` TEXT,
|
||||||
|
`character_sketches` TEXT,
|
||||||
|
`added_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (`book_id`) REFERENCES `books`(`id`) ON DELETE CASCADE,
|
||||||
|
UNIQUE KEY `user_book_unique` (`user_id`, `book_id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
-- Author followers table
|
||||||
|
CREATE TABLE IF NOT EXISTS `author_followers` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`follower_id` INT NOT NULL,
|
||||||
|
`author_id` INT NOT NULL,
|
||||||
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (`follower_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (`author_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
UNIQUE KEY `follower_author_unique` (`follower_id`, `author_id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
|
||||||
|
-- Library likes table
|
||||||
|
CREATE TABLE IF NOT EXISTS `library_likes` (
|
||||||
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
|
`liker_id` INT NOT NULL,
|
||||||
|
`library_owner_id` INT NOT NULL,
|
||||||
|
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (`liker_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
FOREIGN KEY (`library_owner_id`) REFERENCES `users`(`id`) ON DELETE CASCADE,
|
||||||
|
UNIQUE KEY `liker_owner_unique` (`liker_id`, `library_owner_id`)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
1
db/migrations/002_add_role_to_users.sql
Normal file
1
db/migrations/002_add_role_to_users.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE users ADD COLUMN role VARCHAR(255) NOT NULL DEFAULT 'user';
|
||||||
1
db/migrations/003_add_description_to_books.sql
Normal file
1
db/migrations/003_add_description_to_books.sql
Normal file
@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE books ADD COLUMN description TEXT;
|
||||||
4
footer.php
Normal file
4
footer.php
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
</div>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
52
header.php
Normal file
52
header.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php session_start(); ?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Virtual Library</title>
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||||
|
<div class="container-fluid">
|
||||||
|
<a class="navbar-brand" href="/">Virtual Library</a>
|
||||||
|
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||||
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div class="collapse navbar-collapse" id="navbarNav">
|
||||||
|
<ul class="navbar-nav ms-auto">
|
||||||
|
<?php if (isset($_SESSION['user_id'])): ?>
|
||||||
|
<?php if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'admin'): ?>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/admin.php">Admin</a>
|
||||||
|
</li>
|
||||||
|
<?php endif; ?>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/library.php">My Library</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/add_book.php">Add Book</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/apply_writer.php">Become a Writer</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<span class="navbar-text me-2">Welcome, <?php echo htmlspecialchars($_SESSION['username']); ?>!</span>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/logout.php">Logout</a>
|
||||||
|
</li>
|
||||||
|
<?php else: ?>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/signup.php">Sign Up</a>
|
||||||
|
</li>
|
||||||
|
<li class="nav-item">
|
||||||
|
<a class="nav-link" href="/login.php">Login</a>
|
||||||
|
</li>
|
||||||
|
<?php endif; ?>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
<div class="container mt-4">
|
||||||
191
index.php
191
index.php
@ -1,150 +1,49 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
require_once 'db/config.php';
|
||||||
@ini_set('display_errors', '1');
|
require_once 'header.php';
|
||||||
@error_reporting(E_ALL);
|
|
||||||
@date_default_timezone_set('UTC');
|
// Fetch recent books and their authors
|
||||||
|
$stmt = db()->query("SELECT b.id, b.title, b.author_name, b.added_by_user_id, u.username FROM books b LEFT JOIN users u ON b.added_by_user_id = u.id ORDER BY b.created_at DESC LIMIT 5");
|
||||||
|
$recent_books = $stmt->fetchAll();
|
||||||
|
|
||||||
$phpVersion = PHP_VERSION;
|
|
||||||
$now = date('Y-m-d H:i:s');
|
|
||||||
?>
|
?>
|
||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
<div class="container my-5">
|
||||||
<head>
|
<div class="p-5 text-center bg-light rounded-3">
|
||||||
<meta charset="utf-8" />
|
<h1 class="display-4">Welcome to the Virtual Library</h1>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
<p class="lead">Your personal space to discover, organize, and share books.</p>
|
||||||
<title>New Style</title>
|
<?php if (!isset($_SESSION['user_id'])): ?>
|
||||||
<?php
|
<a class="btn btn-primary btn-lg" href="signup.php" role="button">Get Started</a>
|
||||||
// Read project preview data from environment
|
<?php else: ?>
|
||||||
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? '';
|
<a class="btn btn-primary btn-lg" href="library.php" role="button">Go to Your Library</a>
|
||||||
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
|
<?php endif; ?>
|
||||||
?>
|
|
||||||
<?php if ($projectDescription): ?>
|
|
||||||
<!-- Meta description -->
|
|
||||||
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
|
|
||||||
<!-- Open Graph meta tags -->
|
|
||||||
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
||||||
<!-- Twitter meta tags -->
|
|
||||||
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
|
|
||||||
<?php endif; ?>
|
|
||||||
<?php if ($projectImageUrl): ?>
|
|
||||||
<!-- Open Graph image -->
|
|
||||||
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
|
|
||||||
<!-- Twitter image -->
|
|
||||||
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
|
|
||||||
<?php endif; ?>
|
|
||||||
<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>
|
|
||||||
</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"><?= ($_SERVER['HTTP_HOST'] ?? '') === 'appwizzy.com' ? 'AppWizzy' : '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>
|
</div>
|
||||||
</main>
|
|
||||||
<footer>
|
<div class="row mt-5">
|
||||||
Page updated: <?= htmlspecialchars($now) ?> (UTC)
|
<div class="col">
|
||||||
</footer>
|
<h2 class="text-center mb-4">Recently Added Books</h2>
|
||||||
</body>
|
<?php if (count($recent_books) > 0): ?>
|
||||||
</html>
|
<div class="list-group">
|
||||||
|
<?php foreach ($recent_books as $book): ?>
|
||||||
|
<a href="book.php?id=<?php echo $book['id']; ?>" class="list-group-item list-group-item-action flex-column align-items-start">
|
||||||
|
<div class="d-flex w-100 justify-content-between">
|
||||||
|
<h5 class="mb-1"><?php echo htmlspecialchars($book['title']); ?></h5>
|
||||||
|
</div>
|
||||||
|
<p class="mb-1">by
|
||||||
|
<?php if ($book['added_by_user_id'] && $book['username']): ?>
|
||||||
|
<a href="author.php?id=<?php echo $book['added_by_user_id']; ?>"><?php echo htmlspecialchars($book['username']); ?></a>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php echo htmlspecialchars($book['author_name']); ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
</p>
|
||||||
|
</a>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</div>
|
||||||
|
<?php else: ?>
|
||||||
|
<p class="text-center">No books have been added yet. Be the first!</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
|
|||||||
108
library.php
Normal file
108
library.php
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
if (!isset($_SESSION["user_id"])) {
|
||||||
|
header("Location: login.php");
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once 'db/config.php';
|
||||||
|
require_once 'header.php';
|
||||||
|
|
||||||
|
$user_id = $_SESSION["user_id"];
|
||||||
|
$message = '';
|
||||||
|
|
||||||
|
// Handle Add Book Form Submission
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_book'])) {
|
||||||
|
$title = trim($_POST['title']);
|
||||||
|
$author_name = trim($_POST['author']);
|
||||||
|
|
||||||
|
if (!empty($title) && !empty($author_name)) {
|
||||||
|
try {
|
||||||
|
// Check if the book already exists
|
||||||
|
$stmt = db()->prepare("SELECT id FROM books WHERE title = :title AND author_name = :author_name");
|
||||||
|
$stmt->execute(['title' => $title, 'author_name' => $author_name]);
|
||||||
|
$book = $stmt->fetch();
|
||||||
|
|
||||||
|
if ($book) {
|
||||||
|
$book_id = $book['id'];
|
||||||
|
} else {
|
||||||
|
// Insert new book
|
||||||
|
$stmt = db()->prepare("INSERT INTO books (title, author_name, added_by_user_id) VALUES (:title, :author_name, :user_id)");
|
||||||
|
$stmt->execute(['title' => $title, 'author_name' => $author_name, 'user_id' => $user_id]);
|
||||||
|
$book_id = db()->lastInsertId();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the book is already in the user's library
|
||||||
|
$stmt = db()->prepare("SELECT * FROM user_libraries WHERE user_id = :user_id AND book_id = :book_id");
|
||||||
|
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
|
||||||
|
if ($stmt->fetch()) {
|
||||||
|
$message = '<div class="alert alert-warning">This book is already in your library.</div>';
|
||||||
|
} else {
|
||||||
|
// Add book to user's library
|
||||||
|
$stmt = db()->prepare("INSERT INTO user_libraries (user_id, book_id) VALUES (:user_id, :book_id)");
|
||||||
|
$stmt->execute(['user_id' => $user_id, 'book_id' => $book_id]);
|
||||||
|
$message = '<div class="alert alert-success">Book added to your library!</div>';
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
$message = '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Please enter both title and author.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch user's books
|
||||||
|
$stmt = db()->prepare("SELECT b.id, b.title, b.author_name FROM books b JOIN user_libraries ul ON b.id = ul.book_id WHERE ul.user_id = :user_id ORDER BY b.title");
|
||||||
|
$stmt->execute(['user_id' => $user_id]);
|
||||||
|
$user_books = $stmt->fetchAll();
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
||||||
|
<div class="container mt-5">
|
||||||
|
<h2>My Library</h2>
|
||||||
|
<p>Welcome, <?php echo htmlspecialchars($_SESSION["username"]); ?>!</p>
|
||||||
|
|
||||||
|
<?php echo $message; ?>
|
||||||
|
|
||||||
|
<div class="card mb-4">
|
||||||
|
<div class="card-header">
|
||||||
|
Add a New Book Manually
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<p>You can also <a href="add_book.php">search and add books automatically</a>.</p>
|
||||||
|
<form action="library.php" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="title" class="form-label">Title</label>
|
||||||
|
<input type="text" class="form-control" id="title" name="title" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="author" class="form-label">Author</label>
|
||||||
|
<input type="text" class="form-control" id="author" name="author" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit" name="add_book" class="btn btn-primary">Add Book</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">
|
||||||
|
Your Books
|
||||||
|
</div>
|
||||||
|
<div class="card-body">
|
||||||
|
<?php if (count($user_books) > 0): ?>
|
||||||
|
<ul class="list-group">
|
||||||
|
<?php foreach ($user_books as $book): ?>
|
||||||
|
<li class="list-group-item">
|
||||||
|
<a href="book.php?id=<?php echo $book['id']; ?>"><?php echo htmlspecialchars($book['title']); ?></a>
|
||||||
|
by <?php echo htmlspecialchars($book['author_name']); ?>
|
||||||
|
</li>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</ul>
|
||||||
|
<?php else: ?>
|
||||||
|
<p>You haven't added any books yet.</p>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
44
login.php
Normal file
44
login.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
$message = '';
|
||||||
|
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||||
|
$email = $_POST['email'];
|
||||||
|
$password = $_POST['password'];
|
||||||
|
|
||||||
|
$pdo = db();
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
||||||
|
$stmt->execute([$email]);
|
||||||
|
$user = $stmt->fetch();
|
||||||
|
|
||||||
|
if ($user && password_verify($password, $user['password_hash'])) {
|
||||||
|
$_SESSION['user_id'] = $user['id'];
|
||||||
|
$_SESSION['username'] = $user['username'];
|
||||||
|
$_SESSION['user_role'] = $user['role'];
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Invalid email or password.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<?php require_once 'header.php'; ?>
|
||||||
|
|
||||||
|
<h2>Login</h2>
|
||||||
|
<?php echo $message; ?>
|
||||||
|
<form action="login.php" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="email" class="form-label">Email address</label>
|
||||||
|
<input type="email" class="form-control" id="email" name="email" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Password</label>
|
||||||
|
<input type="password" class="form-control" id="password" name="password" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Login</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
7
logout.php
Normal file
7
logout.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
session_start();
|
||||||
|
session_unset();
|
||||||
|
session_destroy();
|
||||||
|
header("Location: index.php");
|
||||||
|
exit;
|
||||||
|
?>
|
||||||
58
signup.php
Normal file
58
signup.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
require_once 'db/config.php';
|
||||||
|
|
||||||
|
$message = '';
|
||||||
|
|
||||||
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
||||||
|
$username = $_POST['username'];
|
||||||
|
$email = $_POST['email'];
|
||||||
|
$password = $_POST['password'];
|
||||||
|
$confirm_password = $_POST['confirm_password'];
|
||||||
|
|
||||||
|
if ($password !== $confirm_password) {
|
||||||
|
$message = '<div class="alert alert-danger">Passwords do not match.</div>';
|
||||||
|
} else {
|
||||||
|
$pdo = db();
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
||||||
|
$stmt->execute([$email]);
|
||||||
|
if ($stmt->fetch()) {
|
||||||
|
$message = '<div class="alert alert-danger">Email already registered.</div>';
|
||||||
|
} else {
|
||||||
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
|
$stmt = $pdo->prepare("INSERT INTO users (username, email, password_hash, role) VALUES (?, ?, ?, 'member')");
|
||||||
|
if ($stmt->execute([$username, $email, $hashed_password])) {
|
||||||
|
$message = '<div class="alert alert-success">Registration successful! You can now <a href="login.php">login</a>.</div>';
|
||||||
|
} else {
|
||||||
|
$message = '<div class="alert alert-danger">Registration failed. Please try again.</div>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<?php require_once 'header.php'; ?>
|
||||||
|
|
||||||
|
<h2>Sign Up</h2>
|
||||||
|
<?php echo $message; ?>
|
||||||
|
<form action="signup.php" method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="username" class="form-label">Username</label>
|
||||||
|
<input type="text" class="form-control" id="username" name="username" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="email" class="form-label">Email address</label>
|
||||||
|
<input type="email" class="form-control" id="email" name="email" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="password" class="form-label">Password</label>
|
||||||
|
<input type="password" class="form-control" id="password" name="password" required>
|
||||||
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="confirm_password" class="form-label">Confirm Password</label>
|
||||||
|
<input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Sign Up</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<?php require_once 'footer.php'; ?>
|
||||||
Loading…
x
Reference in New Issue
Block a user