Sindbad~EG File Manager

Current Path : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/
Upload File :
Current File : /home/copmadinaarea/thecopmadinaarea.org/conference/admin/audit.php

<?php
session_start();
require_once '../includes/functions.php';

// Check if user is logged in and has appropriate role
if (!isLoggedIn()) {
    header('Location: ../login.php');
    exit();
}

$user = getCurrentUser();
if (!hasRole(['superuser'])) {
    header('Location: index.php');
    exit();
}

// Get audit logs with pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$limit = 50;
$offset = ($page - 1) * $limit;

$search = isset($_GET['search']) ? trim($_GET['search']) : '';
$action_filter = isset($_GET['action']) ? $_GET['action'] : '';
$table_filter = isset($_GET['table']) ? $_GET['table'] : '';

// Build query
$where_conditions = [];
$params = [];

if ($search) {
    $where_conditions[] = "(u.first_name LIKE ? OR u.last_name LIKE ? OR al.table_name LIKE ? OR al.record_id LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

if ($action_filter) {
    $where_conditions[] = "al.action = ?";
    $params[] = $action_filter;
}

if ($table_filter) {
    $where_conditions[] = "al.table_name = ?";
    $params[] = $table_filter;
}

$where_clause = !empty($where_conditions) ? 'WHERE ' . implode(' AND ', $where_conditions) : '';

$query = "
    SELECT al.*, u.first_name, u.last_name, u.email
    FROM audit_log al
    LEFT JOIN users u ON al.user_id = u.id
    $where_clause
    ORDER BY al.created_at DESC
    LIMIT $limit OFFSET $offset
";

$stmt = executeQuery($query, $params);
$audit_logs = $stmt ? $stmt->fetchAll() : [];

// Get total count for pagination
$count_query = "
    SELECT COUNT(*) as total
    FROM audit_log al
    LEFT JOIN users u ON al.user_id = u.id
    $where_clause
";
$count_stmt = executeQuery($count_query, $params);
$total_records = $count_stmt ? $count_stmt->fetch()['total'] : 0;
$total_pages = ceil($total_records / $limit);

// Get unique actions and tables for filters
$actions_stmt = executeQuery("SELECT DISTINCT action FROM audit_log ORDER BY action");
$actions = $actions_stmt ? $actions_stmt->fetchAll() : [];

$tables_stmt = executeQuery("SELECT DISTINCT table_name FROM audit_log ORDER BY table_name");
$tables = $tables_stmt ? $tables_stmt->fetchAll() : [];
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audit Log - COP Madina Conference</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gradient-to-br from-slate-50 to-blue-50 min-h-screen">
    <div class="flex h-screen">
        <!-- Sidebar -->
        <?php include 'includes/admin_sidebar.php'; ?>
        
        <!-- Main Content -->
        <div class="flex-1 flex flex-col overflow-hidden ml-72">
            <!-- Header -->
            <?php include 'includes/admin_header.php'; ?>

            <!-- Content -->
            <main class="flex-1 overflow-y-auto p-8">
                <!-- Filters -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 p-6 mb-8">
                    <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Search</label>
                            <input type="text" name="search" value="<?php echo htmlspecialchars($search); ?>"
                                   placeholder="Search users, tables, or records..."
                                   class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500">
                        </div>
                        
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Action</label>
                            <select name="action" class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500">
                                <option value="">All Actions</option>
                                <?php foreach ($actions as $action): ?>
                                <option value="<?php echo $action['action']; ?>" <?php echo $action_filter === $action['action'] ? 'selected' : ''; ?>>
                                    <?php echo ucfirst($action['action']); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div>
                            <label class="block text-sm font-semibold text-slate-700 mb-2">Table</label>
                            <select name="table" class="w-full px-4 py-3 border border-slate-300 rounded-xl focus:outline-none focus:ring-2 focus:ring-amber-500">
                                <option value="">All Tables</option>
                                <?php foreach ($tables as $table): ?>
                                <option value="<?php echo $table['table_name']; ?>" <?php echo $table_filter === $table['table_name'] ? 'selected' : ''; ?>>
                                    <?php echo ucfirst($table['table_name']); ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="flex items-end">
                            <button type="submit" class="w-full px-6 py-3 bg-gradient-to-r from-amber-600 to-orange-600 hover:from-amber-700 hover:to-orange-700 text-white font-medium rounded-xl transition-all duration-200">
                                <i class="fas fa-search mr-2"></i>
                                Filter
                            </button>
                        </div>
                    </form>
                </div>

                <!-- Audit Log Table -->
                <div class="bg-white/70 backdrop-blur-sm rounded-2xl shadow-xl border border-slate-200/50 overflow-hidden">
                    <div class="p-6 border-b border-slate-200/50">
                        <h2 class="text-xl font-bold text-slate-800">Audit Log</h2>
                        <p class="text-slate-600 mt-1">System activity and changes tracking</p>
                    </div>
                    
                    <div class="overflow-x-auto">
                        <table class="w-full">
                            <thead class="bg-slate-50">
                                <tr>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">User</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Action</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Table</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Record ID</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">Date/Time</th>
                                    <th class="px-6 py-4 text-left text-xs font-semibold text-slate-600 uppercase tracking-wider">IP Address</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-slate-200">
                                <?php if (empty($audit_logs)): ?>
                                <tr>
                                    <td colspan="6" class="px-6 py-12 text-center text-slate-500">
                                        <i class="fas fa-history text-4xl mb-4"></i>
                                        <p>No audit logs found</p>
                                    </td>
                                </tr>
                                <?php else: ?>
                                <?php foreach ($audit_logs as $log): ?>
                                <tr class="hover:bg-slate-50">
                                    <td class="px-6 py-4">
                                        <div class="flex items-center">
                                            <div class="w-8 h-8 rounded-full bg-gradient-to-br from-blue-500 to-purple-600 flex items-center justify-center mr-3">
                                                <span class="text-white font-semibold text-xs">
                                                    <?php echo strtoupper(substr($log['first_name'] ?: 'S', 0, 1) . substr($log['last_name'] ?: 'Y', 0, 1)); ?>
                                                </span>
                                            </div>
                                            <div>
                                                <p class="text-sm font-medium text-slate-900">
                                                    <?php echo htmlspecialchars(($log['first_name'] ?: 'System') . ' ' . ($log['last_name'] ?: '')); ?>
                                                </p>
                                                <?php if ($log['email']): ?>
                                                <p class="text-xs text-slate-500"><?php echo htmlspecialchars($log['email']); ?></p>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="px-6 py-4">
                                        <?php
                                        $action_colors = [
                                            'create' => 'bg-green-100 text-green-800',
                                            'update' => 'bg-blue-100 text-blue-800',
                                            'delete' => 'bg-red-100 text-red-800',
                                            'login' => 'bg-purple-100 text-purple-800',
                                            'logout' => 'bg-gray-100 text-gray-800'
                                        ];
                                        $color_class = $action_colors[$log['action']] ?? 'bg-slate-100 text-slate-800';
                                        ?>
                                        <span class="inline-flex px-2 py-1 text-xs font-semibold rounded-full <?php echo $color_class; ?>">
                                            <?php echo ucfirst($log['action']); ?>
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-slate-900">
                                        <?php echo htmlspecialchars($log['table_name']); ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-slate-900">
                                        <?php echo htmlspecialchars($log['record_id']); ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-slate-900">
                                        <?php echo date('M j, Y g:i A', strtotime($log['created_at'])); ?>
                                    </td>
                                    <td class="px-6 py-4 text-sm text-slate-900">
                                        <?php echo htmlspecialchars($log['ip_address'] ?: 'N/A'); ?>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                    <div class="px-6 py-4 border-t border-slate-200/50 flex items-center justify-between">
                        <div class="text-sm text-slate-700">
                            Showing <?php echo (($page - 1) * $limit) + 1; ?> to <?php echo min($page * $limit, $total_records); ?> of <?php echo $total_records; ?> results
                        </div>
                        <div class="flex space-x-2">
                            <?php if ($page > 1): ?>
                            <a href="?page=<?php echo $page - 1; ?>&search=<?php echo urlencode($search); ?>&action=<?php echo urlencode($action_filter); ?>&table=<?php echo urlencode($table_filter); ?>" 
                               class="px-3 py-2 text-sm bg-white border border-slate-300 rounded-lg hover:bg-slate-50">
                                Previous
                            </a>
                            <?php endif; ?>
                            
                            <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                            <a href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>&action=<?php echo urlencode($action_filter); ?>&table=<?php echo urlencode($table_filter); ?>" 
                               class="px-3 py-2 text-sm <?php echo $i === $page ? 'bg-amber-600 text-white' : 'bg-white border border-slate-300 hover:bg-slate-50'; ?> rounded-lg">
                                <?php echo $i; ?>
                            </a>
                            <?php endfor; ?>
                            
                            <?php if ($page < $total_pages): ?>
                            <a href="?page=<?php echo $page + 1; ?>&search=<?php echo urlencode($search); ?>&action=<?php echo urlencode($action_filter); ?>&table=<?php echo urlencode($table_filter); ?>" 
                               class="px-3 py-2 text-sm bg-white border border-slate-300 rounded-lg hover:bg-slate-50">
                                Next
                            </a>
                            <?php endif; ?>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </main>
        </div>
    </div>
</body>
</html>

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists