true]);
Security::requireAdmin();
$db = getDatabase();
$feedback = '';
$feedbackType = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
Security::verifyCsrf($_POST['csrf_token'] ?? '');
$action = (string) ($_POST['action'] ?? '');
if ($action === 'change_password') {
$current = (string) ($_POST['current_password'] ?? '');
$new = (string) ($_POST['new_password'] ?? '');
$confirm = (string) ($_POST['confirm_password'] ?? '');
if ($current === '' || $new === '' || $confirm === '') {
$feedback = 'Tous les champs sont requis.';
$feedbackType = 'error';
} elseif ($new !== $confirm) {
$feedback = 'Les nouveaux mots de passe ne correspondent pas.';
$feedbackType = 'error';
} elseif (strlen($new) < 12 || !preg_match('/[A-Z]/', $new) || !preg_match('/[a-z]/', $new) || !preg_match('/\d/', $new)) {
$feedback = 'Le mot de passe doit contenir au moins 12 caracteres, une majuscule, une minuscule et un chiffre.';
$feedbackType = 'error';
} else {
$stmt = $db->prepare('SELECT password_hash FROM admins WHERE id = :id');
$stmt->execute(['id' => $_SESSION['admin_id']]);
$hash = (string) $stmt->fetchColumn();
if ($hash === '' || !password_verify($current, $hash)) {
$feedback = 'Mot de passe actuel incorrect.';
$feedbackType = 'error';
} else {
$newHash = password_hash($new, PASSWORD_DEFAULT);
$db->prepare('UPDATE admins SET password_hash = :hash WHERE id = :id')
->execute([
'hash' => $newHash,
'id' => $_SESSION['admin_id'],
]);
$db->prepare(
'INSERT INTO admin_logs (admin_id, username, action, ip, success)
VALUES (:admin_id, :username, :action, :ip, 1)'
)->execute([
'admin_id' => $_SESSION['admin_id'],
'username' => $_SESSION['admin_username'],
'action' => 'change_password',
'ip' => Security::clientIp(),
]);
$feedback = 'Mot de passe modifie avec succes.';
$feedbackType = 'success';
}
}
} elseif ($action === 'create_admin') {
$username = trim((string) ($_POST['new_username'] ?? ''));
$password = (string) ($_POST['new_admin_password'] ?? '');
if (!preg_match('/^[a-zA-Z0-9._-]{3,50}$/', $username)) {
$feedback = 'Identifiant invalide. Utilisez 3 a 50 caracteres alphanumeriques.';
$feedbackType = 'error';
} elseif (strlen($password) < 12 || !preg_match('/[A-Z]/', $password) || !preg_match('/[a-z]/', $password) || !preg_match('/\d/', $password)) {
$feedback = 'Le mot de passe doit contenir au moins 12 caracteres, une majuscule, une minuscule et un chiffre.';
$feedbackType = 'error';
} else {
try {
$db->prepare('INSERT INTO admins (username, password_hash) VALUES (:username, :hash)')
->execute([
'username' => $username,
'hash' => password_hash($password, PASSWORD_DEFAULT),
]);
$db->prepare(
'INSERT INTO admin_logs (admin_id, username, action, detail, ip, success)
VALUES (:admin_id, :username, :action, :detail, :ip, 1)'
)->execute([
'admin_id' => $_SESSION['admin_id'],
'username' => $_SESSION['admin_username'],
'action' => 'create_admin',
'detail' => 'Cree : ' . $username,
'ip' => Security::clientIp(),
]);
$feedback = 'Compte admin cree avec succes.';
$feedbackType = 'success';
} catch (PDOException) {
$feedback = 'Cet identifiant existe deja.';
$feedbackType = 'error';
}
}
} elseif ($action === 'delete_admin') {
$adminId = (int) ($_POST['admin_id'] ?? 0);
if ($adminId === (int) $_SESSION['admin_id']) {
$feedback = 'Vous ne pouvez pas supprimer votre propre compte.';
$feedbackType = 'error';
} elseif ($adminId > 0) {
$db->prepare('DELETE FROM admins WHERE id = :id')->execute(['id' => $adminId]);
$feedback = 'Compte admin supprime.';
$feedbackType = 'success';
}
} elseif ($action === 'purge_logs') {
$db->exec('DELETE FROM admin_logs WHERE created_at < NOW() - INTERVAL 90 DAY');
$db->exec('DELETE FROM login_attempts WHERE created_at < NOW() - INTERVAL 7 DAY');
$feedback = 'Logs anciens purges.';
$feedbackType = 'success';
} elseif ($action === 'unblock_ip') {
$ip = trim((string) ($_POST['ip'] ?? ''));
if ($ip !== '') {
$db->prepare('DELETE FROM login_attempts WHERE ip = :ip')->execute(['ip' => $ip]);
$feedback = 'IP debloquee.';
$feedbackType = 'success';
}
}
}
$admins = $db->query('SELECT id, username, created_at FROM admins ORDER BY created_at ASC')->fetchAll();
$logs = $db->query(
'SELECT * FROM admin_logs ORDER BY created_at DESC LIMIT 50'
)->fetchAll();
$blockedIps = $db->query(
"SELECT ip, COUNT(*) AS attempts, MAX(created_at) AS last_attempt
FROM login_attempts
WHERE created_at > NOW() - INTERVAL 15 MINUTE
GROUP BY ip
HAVING attempts >= 5
ORDER BY attempts DESC"
)->fetchAll();
$totalAttempts = (int) $db->query(
'SELECT COUNT(*) FROM login_attempts WHERE created_at > NOW() - INTERVAL 24 HOUR'
)->fetchColumn();
?>
Securite - BackOffice Tony AVERTY
Securite et admins
= Security::e($feedback) ?>