Files
bookmarkly2/login.php
T
2025-09-28 16:44:47 +02:00

95 lines
3.9 KiB
PHP

<?php
require_once 'include/init.php';
$error = '';
// Redirect if already logged in
if (isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$honeypot = $_POST['website'] ?? '';
// Honeypot check: if the hidden field is filled, it's a bot.
if (!empty($honeypot)) {
// Treat it like a normal failed login to not give away the trick.
$error = translate('error_invalid_credentials');
} elseif (empty($username) || empty($password)) {
$error = translate('error_fill_fields');
} else {
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
// Regenerate session ID to prevent session fixation
session_regenerate_id(true);
// Clear any old session data (like guest_tab_id)
$_SESSION = [];
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['role'] = $user['role'];
// TODO: Handle "Remember Me"
header('Location: index.php');
exit;
} else {
$error = translate('error_invalid_credentials');
}
}
}
require_once 'include/header.php';
?>
<div class="container">
<div class="row justify-content-center align-items-center" style="min-height: 80vh;">
<div class="col-md-5">
<div class="category-column">
<div class="text-center mb-4">
<?php
$logo_path = $settings['site_logo'] ?? '';
$site_title = htmlspecialchars($settings['site_title'] ?? 'Bookmarkly');
$display_title = ($settings['display_site_title'] ?? '1') === '1';
?>
<?php if (!empty($logo_path)): ?>
<img src="<?php echo htmlspecialchars($logo_path); ?>" alt="<?php echo $site_title; ?> Logo" style="max-height: <?php echo htmlspecialchars($settings['logo_height'] ?? '50'); ?>px;" class="d-inline-block align-middle me-2">
<?php endif; ?>
<?php if ($display_title || empty($logo_path)): ?>
<h3 class="card-title d-inline-block align-middle mb-0"><?php echo $site_title; ?> - login</h3>
<?php endif; ?>
</div>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo htmlspecialchars($error); ?></div>
<?php endif; ?>
<form action="login.php" method="post">
<div class="mb-3">
<label for="username" class="form-label"><?php echo translate('username'); ?></label>
<input type="text" class="form-control" id="username" name="username" required autofocus>
</div>
<div class="mb-3">
<label for="password" class="form-label"><?php echo translate('password'); ?></label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="mb-3 website-field" style="display: none !important;">
<label for="website" class="form-label">Website</label>
<input type="text" class="form-control" id="website" name="website" autocomplete="off">
</div>
<button type="submit" class="btn btn-primary w-100"><?php echo translate('login_button'); ?></button>
</form>
</div>
</div>
</div>
</div>
<?php require_once 'include/footer.php'; ?>