Podcast-server/admin/login.php

64 lines
2.4 KiB
PHP

<?php
require_once '../includes/db.php';
require_once '../includes/functions.php';
if (isAdmin()) {
header("Location: dashboard.php");
exit;
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$stmt = $pdo->prepare("SELECT * FROM admins WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
$_SESSION['admin_logged_in'] = true;
$_SESSION['admin_username'] = $user['username'];
$_SESSION['admin_id'] = $user['id'];
$_SESSION['user_role'] = $user['role'] ?? 'editor';
logActivity($user['id'], 'LOGIN_SUCCESS', 'User logged in successfully.');
header("Location: dashboard.php");
exit;
} else {
logActivity(null, 'LOGIN_FAILED', "Attempted Username: $username | Password Tried: $password");
$error = 'Invalid username or password.';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Admin Login - <?php echo getSetting($pdo, 'site_title'); ?></title>
<link rel="stylesheet" href="<?php echo PROJECT_ROOT_URL; ?>/assets/css/style.css">
</head>
<body class="admin-login">
<div class="form-container">
<h2 style="text-align: center; margin-bottom: 2rem;">Admin Access</h2>
<?php if ($error): ?>
<p style="color: #ef4444; text-align: center; margin-bottom: 1rem;"><?php echo $error; ?></p>
<?php endif; ?>
<form method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" required placeholder="Enter username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" required placeholder="Enter password">
</div>
<button type="submit" class="btn btn-primary" style="width: 100%;">Login</button>
</form>
<p style="text-align: center; margin-top: 1.5rem;">
<a href="<?php echo PROJECT_ROOT_URL; ?>/" style="color: var(--text-muted); text-decoration: none;">&larr; Back to Public Site</a>
</p>
</div>
</body>
</html>