Sindbad~EG File Manager

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

<?php
require_once '../config/config.php';
require_admin();

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

// Pagination
$page = max(1, intval($_GET['page'] ?? 1));
$limit = 20;
$offset = ($page - 1) * $limit;

// Filters
$user_filter = $_GET['user'] ?? '';
$action_filter = $_GET['action'] ?? '';
$table_filter = $_GET['table'] ?? '';
$date_from = $_GET['date_from'] ?? '';
$date_to = $_GET['date_to'] ?? '';

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

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

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

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

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

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

// Get audit logs
$query = "SELECT al.*, u.name as user_name, u.username
          FROM audit_logs al
          LEFT JOIN users u ON al.user_id = u.id";

if (!empty($conditions)) {
    $query .= " WHERE " . implode(' AND ', $conditions);
}

$query .= " ORDER BY al.created_at DESC LIMIT ? OFFSET ?";
$params[] = $limit;
$params[] = $offset;

$stmt = $conn->prepare($query);
$stmt->execute($params);
$audit_logs = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Get total count for pagination
$count_query = "SELECT COUNT(*) as total FROM audit_logs al 
                LEFT JOIN users u ON al.user_id = u.id";

if (!empty($conditions)) {
    $count_params = array_slice($params, 0, -2);
    $count_query .= " WHERE " . implode(' AND ', $conditions);
    $count_stmt = $conn->prepare($count_query);
    $count_stmt->execute($count_params);
} else {
    $count_stmt = $conn->prepare($count_query);
    $count_stmt->execute();
}

$total_records = $count_stmt->fetch(PDO::FETCH_ASSOC)['total'];
$total_pages = ceil($total_records / $limit);

// Get filter options
$users_query = "SELECT DISTINCT u.id, u.name FROM audit_logs al 
                JOIN users u ON al.user_id = u.id ORDER BY u.name";
$users_stmt = $conn->prepare($users_query);
$users_stmt->execute();
$users = $users_stmt->fetchAll(PDO::FETCH_ASSOC);

$actions_query = "SELECT DISTINCT action FROM audit_logs ORDER BY action";
$actions_stmt = $conn->prepare($actions_query);
$actions_stmt->execute();
$actions = $actions_stmt->fetchAll(PDO::FETCH_ASSOC);

$tables_query = "SELECT DISTINCT table_name FROM audit_logs ORDER BY table_name";
$tables_stmt = $conn->prepare($tables_query);
$tables_stmt->execute();
$tables = $tables_stmt->fetchAll(PDO::FETCH_ASSOC);

$flash = get_flash_message();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Audit Logs - COP News Portal</title>
    <link rel="stylesheet" href="../assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
<body>
    <header class="header">
        <nav class="navbar">
            <a href="../dashboard.php" class="logo">
                <i class="fas fa-church"></i>
                COP News Portal
            </a>
            <ul class="nav-links">
                <li><a href="../dashboard.php"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
                <li><a href="../news/index.php"><i class="fas fa-newspaper"></i> News</a></li>
                <li><a href="index.php"><i class="fas fa-cog"></i> Admin</a></li>
                <li><a href="../profile.php"><i class="fas fa-user"></i> Profile</a></li>
                <li><a href="../logout.php"><i class="fas fa-sign-out-alt"></i> Logout</a></li>
            </ul>
        </nav>
    </header>

    <main class="container" style="margin-top: 2rem;">
        <?php if ($flash): ?>
            <div class="alert alert-<?php echo $flash['type']; ?>">
                <i class="fas fa-info-circle"></i> <?php echo $flash['message']; ?>
            </div>
        <?php endif; ?>

        <div class="card">
            <div class="card-header">
                <h1><i class="fas fa-history"></i> Audit Logs</h1>
            </div>
            <div class="card-body">
                <!-- Filters -->
                <form method="GET" class="mb-4">
                    <div class="grid grid-3">
                        <div class="form-group">
                            <label for="user" class="form-label">User</label>
                            <select id="user" name="user" class="form-control form-select">
                                <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['name']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="action" class="form-label">Action</label>
                            <select id="action" name="action" class="form-control form-select">
                                <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(strtolower($action['action'])); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="table" class="form-label">Table</label>
                            <select id="table" name="table" class="form-control form-select">
                                <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="form-group">
                            <label for="date_from" class="form-label">Date From</label>
                            <input type="date" id="date_from" name="date_from" class="form-control" 
                                   value="<?php echo htmlspecialchars($date_from); ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="date_to" class="form-label">Date To</label>
                            <input type="date" id="date_to" name="date_to" class="form-control" 
                                   value="<?php echo htmlspecialchars($date_to); ?>">
                        </div>
                        
                        <div class="form-group" style="display: flex; align-items: end; gap: 1rem;">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> Filter
                            </button>
                            <a href="audit.php" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Clear
                            </a>
                        </div>
                    </div>
                </form>

                <!-- Audit Logs Table -->
                <?php if (empty($audit_logs)): ?>
                    <div class="text-center p-4">
                        <i class="fas fa-history" style="font-size: 4rem; color: var(--light-grey); margin-bottom: 1rem;"></i>
                        <h3>No audit logs found</h3>
                        <p style="color: var(--primary-grey);">
                            <?php if ($user_filter || $action_filter || $table_filter || $date_from || $date_to): ?>
                                Try adjusting your filter criteria.
                            <?php else: ?>
                                System activities will appear here as they occur.
                            <?php endif; ?>
                        </p>
                    </div>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Date/Time</th>
                                    <th>User</th>
                                    <th>Action</th>
                                    <th>Table</th>
                                    <th>Record ID</th>
                                    <th>IP Address</th>
                                    <th>Details</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($audit_logs as $log): ?>
                                    <tr>
                                        <td>
                                            <small><?php echo date('M j, Y', strtotime($log['created_at'])); ?></small><br>
                                            <small style="color: var(--primary-grey);"><?php echo date('g:i A', strtotime($log['created_at'])); ?></small>
                                        </td>
                                        <td>
                                            <?php if ($log['user_name']): ?>
                                                <strong><?php echo htmlspecialchars($log['user_name']); ?></strong><br>
                                                <small style="color: var(--primary-grey);">@<?php echo htmlspecialchars($log['username']); ?></small>
                                            <?php else: ?>
                                                <em style="color: var(--primary-grey);">System</em>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <span class="badge badge-action-<?php echo strtolower($log['action']); ?>">
                                                <?php echo $log['action']; ?>
                                            </span>
                                        </td>
                                        <td><?php echo ucfirst($log['table_name']); ?></td>
                                        <td><?php echo $log['record_id'] ?? '-'; ?></td>
                                        <td><small><?php echo $log['ip_address']; ?></small></td>
                                        <td>
                                            <?php if ($log['old_values'] || $log['new_values']): ?>
                                                <button class="btn btn-sm btn-outline" 
                                                        onclick="showDetails(<?php echo $log['id']; ?>)">
                                                    <i class="fas fa-eye"></i> View
                                                </button>
                                            <?php else: ?>
                                                -
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    <?php if ($total_pages > 1): ?>
                        <div class="pagination-wrapper text-center mt-4">
                            <div class="pagination">
                                <?php if ($page > 1): ?>
                                    <a href="?page=<?php echo $page - 1; ?>&<?php echo http_build_query(array_filter($_GET, function($k) { return $k !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                       class="btn btn-secondary btn-sm">
                                        <i class="fas fa-chevron-left"></i> Previous
                                    </a>
                                <?php endif; ?>
                                
                                <span class="pagination-info">
                                    Page <?php echo $page; ?> of <?php echo $total_pages; ?> 
                                    (<?php echo $total_records; ?> total logs)
                                </span>
                                
                                <?php if ($page < $total_pages): ?>
                                    <a href="?page=<?php echo $page + 1; ?>&<?php echo http_build_query(array_filter($_GET, function($k) { return $k !== 'page'; }, ARRAY_FILTER_USE_KEY)); ?>" 
                                       class="btn btn-secondary btn-sm">
                                        Next <i class="fas fa-chevron-right"></i>
                                    </a>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <!-- Details Modal -->
    <div id="detailsModal" class="modal" style="display: none;">
        <div class="modal-content">
            <div class="modal-header">
                <h3>Audit Log Details</h3>
                <button onclick="closeModal()" class="btn btn-sm btn-secondary">
                    <i class="fas fa-times"></i>
                </button>
            </div>
            <div class="modal-body" id="modalBody">
                <!-- Content will be loaded here -->
            </div>
        </div>
    </div>

    <style>
        .badge-action-create {
            background: var(--success);
            color: white;
        }
        .badge-action-update {
            background: var(--warning);
            color: white;
        }
        .badge-action-delete {
            background: var(--error);
            color: white;
        }
        .badge-action-login {
            background: var(--info);
            color: white;
        }
        .modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .modal-content {
            background: white;
            border-radius: 12px;
            max-width: 600px;
            width: 90%;
            max-height: 80vh;
            overflow-y: auto;
        }
        .modal-header {
            padding: 1.5rem;
            border-bottom: 1px solid var(--light-grey);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .modal-body {
            padding: 1.5rem;
        }
        .json-viewer {
            background: var(--light-grey);
            padding: 1rem;
            border-radius: 8px;
            font-family: monospace;
            font-size: 0.9rem;
            white-space: pre-wrap;
            margin: 1rem 0;
        }
    </style>

    <script>
        function showDetails(logId) {
            // In a real implementation, you would fetch the details via AJAX
            // For now, we'll show a placeholder
            document.getElementById('modalBody').innerHTML = `
                <h4>Audit Log ID: ${logId}</h4>
                <p>Detailed information about the changes would be displayed here.</p>
                <div class="json-viewer">
                    Loading details...
                </div>
            `;
            document.getElementById('detailsModal').style.display = 'flex';
        }

        function closeModal() {
            document.getElementById('detailsModal').style.display = 'none';
        }

        // Close modal when clicking outside
        document.getElementById('detailsModal').addEventListener('click', function(e) {
            if (e.target === this) {
                closeModal();
            }
        });
    </script>
</body>
</html>

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