188 lines
8.1 KiB
PHP
188 lines
8.1 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
// Hardcoded credentials
|
|
$valid_username = 'admin';
|
|
$valid_password = 'password';
|
|
|
|
$is_logged_in = isset($_SESSION['loggedin']) && $_SESSION['loggedin'] === true;
|
|
$error_message = '';
|
|
$pdo = db();
|
|
$editing_fact = null;
|
|
|
|
// Handle Login/Logout
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if (isset($_POST['logout'])) {
|
|
$_SESSION = [];
|
|
session_destroy();
|
|
header("Location: admin.php");
|
|
exit;
|
|
}
|
|
|
|
if (isset($_POST['login'])) {
|
|
$username = $_POST['username'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
if ($username === $valid_username && $password === $valid_password) {
|
|
$_SESSION['loggedin'] = true;
|
|
header("Location: admin.php");
|
|
exit;
|
|
} else {
|
|
$error_message = "Invalid username or password.";
|
|
}
|
|
}
|
|
}
|
|
|
|
// If logged in, handle CRUD actions
|
|
if ($is_logged_in && $pdo) {
|
|
// Handle Delete
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_id'])) {
|
|
$stmt = $pdo->prepare('DELETE FROM nokia_facts WHERE id = ?');
|
|
$stmt->execute([$_POST['delete_id']]);
|
|
header("Location: admin.php");
|
|
exit;
|
|
}
|
|
|
|
// Handle Add/Update
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['fact_text'])) {
|
|
$fact_text = trim($_POST['fact_text']);
|
|
$fact_id = $_POST['fact_id'] ?? null;
|
|
|
|
if (!empty($fact_text)) {
|
|
if ($fact_id) { // Update
|
|
$stmt = $pdo->prepare('UPDATE nokia_facts SET fact = ? WHERE id = ?');
|
|
$stmt->execute([$fact_text, $fact_id]);
|
|
} else { // Add
|
|
$stmt = $pdo->prepare('INSERT INTO nokia_facts (fact) VALUES (?)');
|
|
$stmt->execute([$fact_text]);
|
|
}
|
|
}
|
|
header("Location: admin.php");
|
|
exit;
|
|
}
|
|
|
|
// Handle Edit - Fetch fact for form
|
|
if (isset($_GET['edit_id'])) {
|
|
$stmt = $pdo->prepare('SELECT id, fact FROM nokia_facts WHERE id = ?');
|
|
$stmt->execute([$_GET['edit_id']]);
|
|
$editing_fact = $stmt->fetch();
|
|
}
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Admin - LAMP Demo</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body class="bg-light">
|
|
|
|
<?php include 'navbar.php'; ?>
|
|
|
|
<main class="container my-5">
|
|
<?php if (!$is_logged_in): ?>
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-6">
|
|
<div class="card shadow-sm mb-4">
|
|
<div class="card-body p-4">
|
|
<h1 class="h3 mb-3 fw-normal text-center">Admin Login</h1>
|
|
<?php if ($error_message): ?>
|
|
<div class="alert alert-danger"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
<form method="POST" action="admin.php">
|
|
<div class="form-floating mb-3">
|
|
<input type="text" class="form-control" id="username" name="username" placeholder="Username" required>
|
|
<label for="username">Username</label>
|
|
</div>
|
|
<div class="form-floating mb-3">
|
|
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
|
|
<label for="password">Password</label>
|
|
</div>
|
|
<button class="w-100 btn btn-lg btn-primary" type="submit" name="login">Sign in</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="card shadow-sm">
|
|
<div class="card-header d-flex justify-content-between align-items-center">
|
|
<h2 class="h4 mb-0">Nokia Facts Management</h2>
|
|
<form method="POST" action="admin.php" class="mb-0">
|
|
<button type="submit" name="logout" class="btn btn-sm btn-outline-secondary">Logout <i class="bi bi-box-arrow-right"></i></button>
|
|
</form>
|
|
</div>
|
|
<div class="card-body">
|
|
|
|
<div class="mb-4 p-3 border rounded bg-light">
|
|
<h3 class="h5"><?php echo $editing_fact ? 'Edit Fact' : 'Add New Fact'; ?></h3>
|
|
<form method="POST" action="admin.php">
|
|
<input type="hidden" name="fact_id" value="<?php echo $editing_fact['id'] ?? ''; ?>">
|
|
<div class="input-group">
|
|
<textarea class="form-control" name="fact_text" rows="2" placeholder="Enter a new Nokia fact..."><?php echo htmlspecialchars($editing_fact['fact'] ?? ''); ?></textarea>
|
|
<button class="btn btn-<?php echo $editing_fact ? 'primary' : 'success'; ?>" type="submit"><?php echo $editing_fact ? 'Update Fact' : 'Add Fact'; ?></button>
|
|
<?php if ($editing_fact): ?>
|
|
<a href="admin.php" class="btn btn-secondary">Cancel</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table table-striped table-hover">
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">Fact</th>
|
|
<th scope="col" class="text-end">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php
|
|
if ($pdo) {
|
|
$stmt = $pdo->query('SELECT id, fact, created_at FROM nokia_facts ORDER BY id DESC');
|
|
$facts = $stmt->fetchAll();
|
|
foreach ($facts as $fact):
|
|
?>
|
|
<tr>
|
|
<td><?php echo htmlspecialchars($fact['fact']); ?></td>
|
|
<td class="text-end">
|
|
<a href="admin.php?edit_id=<?php echo $fact['id']; ?>" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i> Edit</a>
|
|
<form method="POST" action="admin.php" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this fact?');">
|
|
<input type="hidden" name="delete_id" value="<?php echo $fact['id']; ?>">
|
|
<button type="submit" class="btn btn-sm btn-outline-danger"><i class="bi bi-trash"></i> Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php
|
|
endforeach;
|
|
if (empty($facts)) {
|
|
echo "<tr><td colspan='2'>No facts found. Add one above!</td></tr>";
|
|
}
|
|
} else {
|
|
echo "<tr><td colspan='2'>Database connection not available.</td></tr>";
|
|
}
|
|
?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</main>
|
|
|
|
<footer class="bg-white text-center text-muted py-4 mt-5 border-top">
|
|
<div class="container">
|
|
<p class="mb-0">© 2025 LAMP Demo. All rights reserved.</p>
|
|
</div>
|
|
</footer>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js"></script>
|
|
</body>
|
|
</html>
|