Sindbad~EG File Manager

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

<?php
require_once '../config/config.php';

// Check if user is logged in and has admin or superuser role
if (!isLoggedIn() || (!hasRole('admin') && !hasRole('superuser'))) {
    redirect('login.php');
}

$db = new Database();
$conn = $db->getConnection();

// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$per_page = 50;
$offset = ($page - 1) * $per_page;

// Filters
$user_filter = isset($_GET['user']) ? (int)$_GET['user'] : '';
$action_filter = isset($_GET['action']) ? sanitizeInput($_GET['action']) : '';
$date_from = isset($_GET['date_from']) ? $_GET['date_from'] : '';
$date_to = isset($_GET['date_to']) ? $_GET['date_to'] : '';

// Build query conditions
$conditions = ['1=1'];
$params = [];

if ($user_filter) {
    $conditions[] = "al.user_id = ?";
    $params[] = $user_filter;
}

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

if ($date_from) {
    $conditions[] = "DATE(al.created_at) >= ?";
    $params[] = $date_from;
}

if ($date_to) {
    $conditions[] = "DATE(al.created_at) <= ?";
    $params[] = $date_to;
}

$where_clause = implode(' AND ', $conditions);

// Get total count
$count_query = "SELECT COUNT(*) as total FROM audit_logs al WHERE $where_clause";
$count_stmt = $conn->prepare($count_query);
$count_stmt->execute($params);
$total_records = $count_stmt->fetch()['total'];
$total_pages = ceil($total_records / $per_page);

// Get audit logs
$query = "SELECT al.*, u.full_name, u.username 
          FROM audit_logs al 
          LEFT JOIN users u ON al.user_id = u.id 
          WHERE $where_clause
          ORDER BY al.created_at DESC 
          LIMIT $per_page OFFSET $offset";
$stmt = $conn->prepare($query);
$stmt->execute($params);
$audit_logs = $stmt->fetchAll();

// Get users for filter
$user_query = "SELECT id, full_name, username FROM users WHERE is_active = 1 ORDER BY full_name";
$user_stmt = $conn->prepare($user_query);
$user_stmt->execute();
$users = $user_stmt->fetchAll();

// Get common actions for filter
$action_query = "SELECT DISTINCT action FROM audit_logs ORDER BY action";
$action_stmt = $conn->prepare($action_query);
$action_stmt->execute();
$actions = $action_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 Logs - Admin Panel</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#F59E0B',
                        accent: '#6B7280'
                    }
                }
            }
        }
    </script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #3B82F6 0%, #F59E0B 50%, #6B7280 100%);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Include Sidebar -->
    <?php include 'includes/sidebar.php'; ?>

    <!-- Main Content -->
    <div class="md:ml-64">
        <!-- Header -->
        <header class="bg-white shadow-sm border-b">
            <div class="px-6 py-4">
                <div class="flex items-center justify-between">
                    <h1 class="text-2xl font-bold text-gray-900">Audit Logs</h1>
                    <div class="flex items-center space-x-4">
                        <span class="text-sm text-gray-500">
                            Total Records: <?php echo number_format($total_records); ?>
                        </span>
                    </div>
                </div>
            </div>
        </header>

        <!-- Content -->
        <main class="p-6">
            <!-- Filters -->
            <div class="bg-white rounded-lg shadow p-6 mb-6">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">
                    <i class="fas fa-filter mr-2"></i>
                    Filter Audit Logs
                </h3>
                <form method="GET" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-5 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">User</label>
                        <select name="user" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary">
                            <option value="">All Users</option>
                            <?php foreach ($users as $user): ?>
                                <option value="<?php echo $user['id']; ?>" <?php echo ($user_filter == $user['id']) ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($user['full_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">Action</label>
                        <select name="action" class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary">
                            <option value="">All Actions</option>
                            <?php foreach ($actions as $action): ?>
                                <option value="<?php echo htmlspecialchars($action['action']); ?>" <?php echo ($action_filter == $action['action']) ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars(ucwords(str_replace('_', ' ', $action['action']))); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">From Date</label>
                        <input type="date" name="date_from" value="<?php echo $date_from; ?>" 
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">To Date</label>
                        <input type="date" name="date_to" value="<?php echo $date_to; ?>" 
                               class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-primary">
                    </div>
                    <div class="flex items-end">
                        <button type="submit" class="w-full bg-primary text-white px-4 py-2 rounded-md hover:bg-blue-700 transition duration-300">
                            <i class="fas fa-search mr-2"></i>
                            Filter
                        </button>
                    </div>
                </form>
                <?php if ($user_filter || $action_filter || $date_from || $date_to): ?>
                    <div class="mt-4">
                        <a href="audit.php" class="text-primary hover:text-blue-700 text-sm">
                            <i class="fas fa-times mr-1"></i>
                            Clear Filters
                        </a>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Audit Logs Table -->
            <div class="bg-white rounded-lg shadow overflow-hidden">
                <div class="px-6 py-4 border-b">
                    <h3 class="text-lg font-semibold text-gray-900">
                        Activity Log
                    </h3>
                </div>

                <?php if (empty($audit_logs)): ?>
                    <div class="text-center py-12">
                        <i class="fas fa-shield-alt text-6xl text-gray-400 mb-4"></i>
                        <h3 class="text-xl font-semibold text-gray-700 mb-2">No Audit Logs Found</h3>
                        <p class="text-gray-500">No activity matches your current filters.</p>
                    </div>
                <?php else: ?>
                    <div class="overflow-x-auto">
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Date/Time
                                    </th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        User
                                    </th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Action
                                    </th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        Details
                                    </th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
                                        IP Address
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                <?php foreach ($audit_logs as $log): ?>
                                    <tr class="hover:bg-gray-50">
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">
                                            <?php echo date('M j, Y', strtotime($log['created_at'])); ?><br>
                                            <span class="text-gray-500"><?php echo date('g:i:s A', strtotime($log['created_at'])); ?></span>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <?php if ($log['full_name']): ?>
                                                <div class="text-sm font-medium text-gray-900">
                                                    <?php echo htmlspecialchars($log['full_name']); ?>
                                                </div>
                                                <div class="text-sm text-gray-500">
                                                    @<?php echo htmlspecialchars($log['username']); ?>
                                                </div>
                                            <?php else: ?>
                                                <span class="text-gray-500 italic">System/Unknown</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap">
                                            <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
                                                <?php 
                                                $action_lower = strtolower($log['action']);
                                                if (strpos($action_lower, 'login') !== false) {
                                                    echo 'bg-green-100 text-green-800';
                                                } elseif (strpos($action_lower, 'delete') !== false) {
                                                    echo 'bg-red-100 text-red-800';
                                                } elseif (strpos($action_lower, 'create') !== false) {
                                                    echo 'bg-blue-100 text-blue-800';
                                                } elseif (strpos($action_lower, 'update') !== false) {
                                                    echo 'bg-yellow-100 text-yellow-800';
                                                } else {
                                                    echo 'bg-gray-100 text-gray-800';
                                                }
                                                ?>">
                                                <i class="fas <?php 
                                                if (strpos($action_lower, 'login') !== false) {
                                                    echo 'fa-sign-in-alt';
                                                } elseif (strpos($action_lower, 'logout') !== false) {
                                                    echo 'fa-sign-out-alt';
                                                } elseif (strpos($action_lower, 'delete') !== false) {
                                                    echo 'fa-trash';
                                                } elseif (strpos($action_lower, 'create') !== false) {
                                                    echo 'fa-plus';
                                                } elseif (strpos($action_lower, 'update') !== false) {
                                                    echo 'fa-edit';
                                                } else {
                                                    echo 'fa-cog';
                                                }
                                                ?> mr-1"></i>
                                                <?php echo htmlspecialchars(ucwords(str_replace('_', ' ', $log['action']))); ?>
                                            </span>
                                        </td>
                                        <td class="px-6 py-4">
                                            <div class="text-sm text-gray-900 max-w-xs truncate" title="<?php echo htmlspecialchars($log['details']); ?>">
                                                <?php echo htmlspecialchars($log['details'] ?: 'No details'); ?>
                                            </div>
                                        </td>
                                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                            <?php echo htmlspecialchars($log['ip_address'] ?: 'Unknown'); ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                        <div class="px-6 py-4 border-t">
                            <div class="flex items-center justify-between">
                                <div class="text-sm text-gray-700">
                                    Showing <?php echo (($page - 1) * $per_page) + 1; ?> to 
                                    <?php echo min($page * $per_page, $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; ?>&<?php echo http_build_query(array_filter($_GET, function($key) { return $key !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                           class="px-3 py-2 text-sm bg-gray-200 text-gray-700 rounded hover:bg-gray-300">
                                            Previous
                                        </a>
                                    <?php endif; ?>
                                    
                                    <?php for ($i = max(1, $page - 2); $i <= min($total_pages, $page + 2); $i++): ?>
                                        <a href="?page=<?php echo $i; ?>&<?php echo http_build_query(array_filter($_GET, function($key) { return $key !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                           class="px-3 py-2 text-sm <?php echo ($i == $page) ? 'bg-primary text-white' : 'bg-gray-200 text-gray-700 hover:bg-gray-300'; ?> rounded">
                                            <?php echo $i; ?>
                                        </a>
                                    <?php endfor; ?>
                                    
                                    <?php if ($page < $total_pages): ?>
                                        <a href="?page=<?php echo $page + 1; ?>&<?php echo http_build_query(array_filter($_GET, function($key) { return $key !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                           class="px-3 py-2 text-sm bg-gray-200 text-gray-700 rounded hover:bg-gray-300">
                                            Next
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>

            <!-- Security Summary -->
            <div class="mt-8 grid grid-cols-1 md:grid-cols-3 gap-6">
                <?php
                // Get security statistics
                $today = date('Y-m-d');
                $week_ago = date('Y-m-d', strtotime('-7 days'));
                
                // Login attempts today
                $login_query = "SELECT COUNT(*) as count FROM audit_logs WHERE action LIKE '%login%' AND DATE(created_at) = ?";
                $login_stmt = $conn->prepare($login_query);
                $login_stmt->execute([$today]);
                $login_today = $login_stmt->fetch()['count'];
                
                // Failed login attempts this week
                $failed_query = "SELECT COUNT(*) as count FROM audit_logs WHERE action = 'login_failed' AND DATE(created_at) >= ?";
                $failed_stmt = $conn->prepare($failed_query);
                $failed_stmt->execute([$week_ago]);
                $failed_logins = $failed_stmt->fetch()['count'];
                
                // Total activities this week
                $activity_query = "SELECT COUNT(*) as count FROM audit_logs WHERE DATE(created_at) >= ?";
                $activity_stmt = $conn->prepare($activity_query);
                $activity_stmt->execute([$week_ago]);
                $total_activities = $activity_stmt->fetch()['count'];
                ?>
                
                <div class="bg-white rounded-lg shadow p-6">
                    <div class="flex items-center">
                        <div class="p-2 bg-green-100 rounded-lg">
                            <i class="fas fa-sign-in-alt text-green-600 text-xl"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-sm font-medium text-gray-600">Logins Today</p>
                            <p class="text-2xl font-bold text-gray-900"><?php echo $login_today; ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-lg shadow p-6">
                    <div class="flex items-center">
                        <div class="p-2 bg-red-100 rounded-lg">
                            <i class="fas fa-exclamation-triangle text-red-600 text-xl"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-sm font-medium text-gray-600">Failed Logins (7 days)</p>
                            <p class="text-2xl font-bold text-gray-900"><?php echo $failed_logins; ?></p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-lg shadow p-6">
                    <div class="flex items-center">
                        <div class="p-2 bg-blue-100 rounded-lg">
                            <i class="fas fa-activity text-blue-600 text-xl"></i>
                        </div>
                        <div class="ml-4">
                            <p class="text-sm font-medium text-gray-600">Total Activities (7 days)</p>
                            <p class="text-2xl font-bold text-gray-900"><?php echo $total_activities; ?></p>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

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