Portfolio_pero/admin/login.php
2026-09-03 13:27:06 +02:00

139 lines
5.1 KiB
PHP

<?php
/**
* BackOffice — Page de connexion avec protection brute force
* Portfolio Tony AVERTY
*/
require_once __DIR__ . '/../includes/Security.php';
Security::bootstrap(true, ['no_store' => true]);
if (!empty($_SESSION['admin_id'])) {
header('Location: dashboard.php');
exit;
}
// Déconnexion (redirige vers le handler dédié)
if (isset($_GET['logout'])) {
header('Location: /admin/login.php');
exit;
}
$error = '';
$username = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
Security::enforceSameOrigin();
Security::verifyCsrf($_POST['csrf_token'] ?? '');
require_once __DIR__ . '/../config/database.php';
$db = getDatabase();
$ip = Security::clientIp();
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
// Vérifier les tentatives récentes
$attempts = $db->prepare(
'SELECT COUNT(*) FROM login_attempts
WHERE ip = :ip AND created_at > NOW() - INTERVAL 15 MINUTE'
);
$attempts->execute(['ip' => $ip]);
$attemptCount = (int) $attempts->fetchColumn();
if ($attemptCount >= 5) {
$error = 'Trop de tentatives. Réessayez dans 15 minutes.';
} elseif (empty($username) || empty($password)) {
$error = 'Veuillez remplir tous les champs.';
} else {
try {
$stmt = $db->prepare(
'SELECT id, username, password_hash FROM admins WHERE username = :username LIMIT 1'
);
$stmt->execute(['username' => $username]);
$admin = $stmt->fetch();
if ($admin && password_verify($password, $admin['password_hash'])) {
// Connexion réussie
session_regenerate_id(true);
$_SESSION['admin_id'] = $admin['id'];
$_SESSION['admin_username'] = $admin['username'];
// Log succès
$db->prepare(
'INSERT INTO admin_logs (admin_id, username, action, ip, success)
VALUES (:id, :u, :a, :ip, 1)'
)->execute([
'id' => $admin['id'],
'u' => $admin['username'],
'a' => 'login',
'ip' => $ip,
]);
// Purger les tentatives de cette IP
$db->prepare('DELETE FROM login_attempts WHERE ip = :ip')->execute(['ip' => $ip]);
header('Location: dashboard.php');
exit;
} else {
// Échec — enregistrer la tentative
$db->prepare(
'INSERT INTO login_attempts (ip, username) VALUES (:ip, :u)'
)->execute(['ip' => $ip, 'u' => $username]);
// Log échec
$db->prepare(
'INSERT INTO admin_logs (username, action, ip, success)
VALUES (:u, :a, :ip, 0)'
)->execute(['u' => $username, 'a' => 'login_failed', 'ip' => $ip]);
$remaining = 5 - ($attemptCount + 1);
$error = 'Identifiants incorrects.' . ($remaining > 0 ? " ({$remaining} tentative(s) restante(s))" : '');
}
} catch (PDOException $e) {
error_log('Erreur login admin : ' . $e->getMessage());
$error = 'Une erreur est survenue. Veuillez réessayer.';
}
}
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<title>Connexion — BackOffice Tony AVERTY</title>
<link rel="stylesheet" href="assets/admin.css">
</head>
<body>
<div class="admin-login">
<div class="admin-login__card">
<h1 class="admin-login__title">Tony<span style="color:#6366f1">.</span> BackOffice</h1>
<?php if ($error): ?>
<div class="admin-alert admin-alert--error"><?= htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?></div>
<?php endif; ?>
<form method="post" action="">
<?= Security::csrfInput() ?>
<div class="admin-form__group">
<label for="username" class="admin-form__label">Identifiant</label>
<input type="text" id="username" name="username" class="admin-form__input"
required autofocus autocomplete="username"
value="<?= htmlspecialchars($username, ENT_QUOTES, 'UTF-8') ?>">
</div>
<div class="admin-form__group">
<label for="password" class="admin-form__label">Mot de passe</label>
<input type="password" id="password" name="password" class="admin-form__input"
required autocomplete="current-password">
</div>
<div class="admin-form__actions">
<button type="submit" class="admin-btn admin-btn--primary" style="width:100%;justify-content:center;">
Se connecter
</button>
</div>
</form>
</div>
</div>
</body>
</html>