intégration de la base de la gestion des logs

This commit is contained in:
Esenjin 2025-01-09 12:21:06 +01:00
parent e6cf9b40ae
commit eb06ff5ed0
4 changed files with 267 additions and 0 deletions

View File

@ -159,6 +159,23 @@ function showAdminInterface() {
<p>Personnalisez le titre et la description de votre galerie.</p> <p>Personnalisez le titre et la description de votre galerie.</p>
</div> </div>
</a> </a>
<?php if ($_SESSION['admin_id'] == $firstId): ?>
<a href="logs.php" class="admin-menu-item">
<div class="menu-icon">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<line x1="16" y1="13" x2="8" y2="13"></line>
<line x1="16" y1="17" x2="8" y2="17"></line>
<polyline points="10 9 9 9 8 9"></polyline>
</svg>
</div>
<div class="menu-content">
<h2>Logs système</h2>
<p>Consultez l'historique des actions des administrateurs.</p>
</div>
</a>
<?php endif; ?>
<?php <?php
$updateStatus = checkUpdate(); $updateStatus = checkUpdate();
$updateAvailable = $updateStatus && $updateStatus['available']; $updateAvailable = $updateStatus && $updateStatus['available'];

View File

@ -379,6 +379,34 @@ function cleanExpiredShareKeys() {
return $db->changes(); return $db->changes();
} }
/**
* Enregistre une action d'administrateur dans les logs
*/
function logAdminAction($adminId, $actionType, $description, $targetPath = null) {
$db = new SQLite3('database.sqlite');
$stmt = $db->prepare('INSERT INTO admin_logs (admin_id, action_type, action_description, target_path)
VALUES (:admin_id, :action_type, :description, :target_path)');
$stmt->bindValue(':admin_id', $adminId, SQLITE3_INTEGER);
$stmt->bindValue(':action_type', $actionType, SQLITE3_TEXT);
$stmt->bindValue(':description', $description, SQLITE3_TEXT);
$stmt->bindValue(':target_path', $targetPath, SQLITE3_TEXT);
return $stmt->execute();
}
/**
* Récupère le nom d'utilisateur d'un admin
*/
function getAdminUsername($adminId) {
$db = new SQLite3('database.sqlite');
$stmt = $db->prepare('SELECT username FROM admins WHERE id = :id');
$stmt->bindValue(':id', $adminId, SQLITE3_INTEGER);
$result = $stmt->execute();
if ($row = $result->fetchArray()) {
return $row['username'];
}
return 'Inconnu';
}
/** /**
* Récupère la version actuelle du projet * Récupère la version actuelle du projet
* @return string La version du projet * @return string La version du projet

187
logs.php Normal file
View File

@ -0,0 +1,187 @@
<?php
require_once 'fonctions.php';
session_start();
if (!isset($_SESSION['admin_id'])) {
header('Location: admin.php?action=login');
exit;
}
// Vérifier que c'est bien le premier administrateur
$db = new SQLite3('database.sqlite');
$stmt = $db->prepare('SELECT MIN(id) as first_id FROM admins');
$result = $stmt->execute();
$firstId = $result->fetchArray()['first_id'];
if ($_SESSION['admin_id'] != $firstId) {
$_SESSION['error_message'] = "Accès non autorisé. Seul le premier administrateur peut consulter les logs.";
header('Location: admin.php');
exit;
}
// Pagination
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$perPage = 50;
$offset = ($page - 1) * $perPage;
// Filtres
$actionType = isset($_GET['action_type']) ? $_GET['action_type'] : '';
$adminFilter = isset($_GET['admin']) ? intval($_GET['admin']) : 0;
// Construction de la requête
$whereClause = [];
$params = [];
if ($actionType) {
$whereClause[] = 'action_type = :action_type';
$params[':action_type'] = $actionType;
}
if ($adminFilter) {
$whereClause[] = 'admin_id = :admin_id';
$params[':admin_id'] = $adminFilter;
}
$whereSQL = !empty($whereClause) ? 'WHERE ' . implode(' AND ', $whereClause) : '';
// Récupérer le nombre total de logs
$countQuery = "SELECT COUNT(*) as total FROM admin_logs $whereSQL";
$stmt = $db->prepare($countQuery);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$total = $stmt->execute()->fetchArray()['total'];
$totalPages = ceil($total / $perPage);
// Récupérer les logs
$query = "SELECT l.*, a.username
FROM admin_logs l
LEFT JOIN admins a ON l.admin_id = a.id
$whereSQL
ORDER BY l.created_at DESC
LIMIT :limit OFFSET :offset";
$stmt = $db->prepare($query);
$stmt->bindValue(':limit', $perPage, SQLITE3_INTEGER);
$stmt->bindValue(':offset', $offset, SQLITE3_INTEGER);
foreach ($params as $key => $value) {
$stmt->bindValue($key, $value);
}
$logs = [];
$result = $stmt->execute();
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$logs[] = $row;
}
// Récupérer la liste des admins pour le filtre
$admins = [];
$result = $db->query('SELECT id, username FROM admins ORDER BY username');
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$admins[] = $row;
}
// Récupérer les types d'actions uniques pour le filtre
$actionTypes = [];
$result = $db->query('SELECT DISTINCT action_type FROM admin_logs ORDER BY action_type');
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$actionTypes[] = $row['action_type'];
}
$config = getSiteConfig();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Logs administrateurs - <?php echo htmlspecialchars($config['site_title']); ?></title>
<link rel="icon" type="image/png" href="favicon.png">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="styles-admin.css">
</head>
<body class="admin-page">
<div class="admin-header">
<h1>Logs administrateurs</h1>
<div class="admin-actions">
<a href="admin.php" class="action-button action-button-secondary">Retour</a>
</div>
</div>
<div class="admin-content">
<!-- Filtres -->
<div class="filters">
<form method="get" class="filter-form">
<div class="filter-group">
<label for="action_type">Type d'action :</label>
<select name="action_type" id="action_type" class="form-select">
<option value="">Toutes les actions</option>
<?php foreach($actionTypes as $type): ?>
<option value="<?php echo htmlspecialchars($type); ?>"
<?php echo $actionType === $type ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($type); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="filter-group">
<label for="admin">Administrateur :</label>
<select name="admin" id="admin" class="form-select">
<option value="">Tous les administrateurs</option>
<?php foreach($admins as $admin): ?>
<option value="<?php echo $admin['id']; ?>"
<?php echo $adminFilter === $admin['id'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($admin['username']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="action-button">Filtrer</button>
</form>
</div>
<!-- Tableau des logs -->
<div class="logs-list">
<table class="admin-table">
<thead>
<tr>
<th>Date</th>
<th>Administrateur</th>
<th>Action</th>
<th>Description</th>
<th>Chemin</th>
</tr>
</thead>
<tbody>
<?php foreach($logs as $log): ?>
<tr>
<td><?php echo date('d/m/Y H:i:s', strtotime($log['created_at'])); ?></td>
<td><?php echo htmlspecialchars($log['username']); ?></td>
<td><?php echo htmlspecialchars($log['action_type']); ?></td>
<td><?php echo htmlspecialchars($log['action_description']); ?></td>
<td><?php echo htmlspecialchars($log['target_path'] ?? ''); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- Pagination -->
<?php if ($totalPages > 1): ?>
<div class="pagination">
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
<a href="?page=<?php echo $i; ?>&action_type=<?php echo urlencode($actionType); ?>&admin=<?php echo $adminFilter; ?>"
class="pagination-link <?php echo $page === $i ? 'active' : ''; ?>">
<?php echo $i; ?>
</a>
<?php endfor; ?>
</div>
<?php endif; ?>
</div>
<?php include 'footer.php'; ?>
</body>
</html>

View File

@ -1019,6 +1019,41 @@ body[data-page="carrousel"] .admin-header {
display: none; display: none;
} }
/* Styles pour les logs */
.filter-form {
display: flex;
gap: 1rem;
align-items: flex-end;
}
.pagination {
margin-top: 2rem;
display: flex;
justify-content: center;
gap: 0.5rem;
}
.pagination-link {
padding: 0.5rem 1rem;
background-color: #2a2a2a;
color: white;
text-decoration: none;
border-radius: 0.25rem;
transition: all 0.3s ease;
}
.pagination-link:hover {
background-color: #3a3a3a;
}
.pagination-link.active {
background-color: #2196f3;
}
.logs-list {
margin-top: 2rem;
}
/* Media Queries */ /* Media Queries */
@media (max-width: 768px) { @media (max-width: 768px) {
.admin-page { .admin-page {