64 lines
3.0 KiB
PHP
64 lines
3.0 KiB
PHP
<?php
|
|
require_once '../config.php';
|
|
header('Content-Type: application/json');
|
|
|
|
if (!isLoggedIn() || !isAdmin()) {
|
|
echo json_encode(['data' => []]);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$sql = "SELECT l.id, l.action, l.details, l.ip_address, l.created_at, u.name as user_name, u.role as user_role
|
|
FROM activity_logs l
|
|
LEFT JOIN users u ON l.user_id = u.id
|
|
ORDER BY l.created_at DESC LIMIT 1000"; // limit to prevent huge payloads
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
$stmt->execute();
|
|
$logs = $stmt->fetchAll();
|
|
|
|
$data = [];
|
|
foreach ($logs as $log) {
|
|
$actor = '';
|
|
if ($log['user_name']) {
|
|
$badge = $log['user_role'] === 'admin' ? '<span class="bg-purple-100 text-purple-800 px-2 py-0.5 rounded text-xs ml-1">Admin</span>' : '';
|
|
$actor = '<div><strong>' . htmlspecialchars($log['user_name']) . '</strong>' . $badge . '</div>';
|
|
} else {
|
|
// System or failed login
|
|
if (strpos($log['action'], 'CRON') !== false) {
|
|
$actor = '<span class="text-gray-500"><i class="fas fa-robot mr-1"></i> System (Cron)</span>';
|
|
} else {
|
|
$actor = '<span class="text-gray-500"><i class="fas fa-user-secret mr-1"></i> Guest / System</span>';
|
|
}
|
|
}
|
|
|
|
// Format action nicely
|
|
$action_html = htmlspecialchars($log['action']);
|
|
if (strpos($log['action'], 'LOGIN') !== false) {
|
|
$action_html = '<span class="text-blue-600 font-semibold"><i class="fas fa-sign-in-alt mr-1"></i> ' . $action_html . '</span>';
|
|
} elseif (strpos($log['action'], 'CREATE') !== false) {
|
|
$action_html = '<span class="text-green-600 font-semibold"><i class="fas fa-plus-circle mr-1"></i> ' . $action_html . '</span>';
|
|
} elseif (strpos($log['action'], 'UPDATE') !== false) {
|
|
$action_html = '<span class="text-orange-600 font-semibold"><i class="fas fa-edit mr-1"></i> ' . $action_html . '</span>';
|
|
} elseif (strpos($log['action'], 'DELETE') !== false) {
|
|
$action_html = '<span class="text-red-600 font-semibold"><i class="fas fa-trash-alt mr-1"></i> ' . $action_html . '</span>';
|
|
} elseif (strpos($log['action'], 'CRON') !== false) {
|
|
$action_html = '<span class="text-indigo-600 font-semibold"><i class="fas fa-paper-plane mr-1"></i> ' . $action_html . '</span>';
|
|
}
|
|
|
|
$data[] = [
|
|
'id' => $log['id'],
|
|
'created_at' => date('d/m/Y H:i:s', strtotime($log['created_at'])),
|
|
'user' => $actor,
|
|
'action' => $action_html,
|
|
'details' => '<div class="text-xs text-gray-600 break-words max-w-md">' . htmlspecialchars($log['details']) . '</div>',
|
|
'ip_address' => '<span class="text-xs text-gray-500 font-mono">' . htmlspecialchars($log['ip_address']) . '</span>'
|
|
];
|
|
}
|
|
|
|
echo json_encode(['data' => $data]);
|
|
} catch (Exception $e) {
|
|
echo json_encode(['error' => $e->getMessage(), 'data' => []]);
|
|
}
|
|
?>
|