auditoria.php 2.85 KB
<?php

declare(strict_types=1);

require __DIR__ . '/../src/bootstrap.php';
Auth::requireAdmin();

$perPage = 50;
$page = max(1, (int) ($_GET['p'] ?? 1));
$offset = ($page - 1) * $perPage;

$total = (int) Database::pdo()->query('SELECT COUNT(*) FROM audit_log')->fetchColumn();

$stmt = Database::pdo()->prepare(
    'SELECT * FROM audit_log ORDER BY criado_em DESC, id DESC LIMIT :limit OFFSET :offset'
);
$stmt->bindValue('limit', $perPage, PDO::PARAM_INT);
$stmt->bindValue('offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$logs = $stmt->fetchAll();

$totalPages = max(1, (int) ceil($total / $perPage));

$acaoLabels = [
    'login'                 => 'Login',
    'login_negado'          => 'Login negado (sem permissão)',
    'login_falhou'          => 'Login falhou (usuário/senha)',
    'logout'                => 'Logoff',
    'habilitou_aluno'       => 'Habilitou aluno',
    'desabilitou_aluno'     => 'Desabilitou aluno',
    'alterou_senha_aluno'   => 'Alterou senha de aluno',
    'adicionou_admin'       => 'Adicionou administrador',
    'desativou_admin'       => 'Desativou administrador',
    'reativou_admin'        => 'Reativou administrador',
    'alterou_papel_admin'   => 'Alterou papel de administrador',
    'atualizou_settings'    => 'Atualizou configurações',
];

View::header('Auditoria');
?>
<div class="card">
    <h1>Auditoria</h1>
    <p style="color:#6b7280;font-size:13px">
        Histórico de logins (inclusive tentativas com senha incorreta), alterações em
        alunos, administradores e configurações. Mostrando <?= count($logs) ?> de <?= $total ?> registros.
    </p>

    <table>
        <tr>
            <th>Data/hora</th>
            <th>Usuário</th>
            <th>Ação</th>
            <th>Alvo</th>
            <th>Detalhe</th>
            <th>IP</th>
        </tr>
        <?php foreach ($logs as $log): ?>
        <tr>
            <td><?= View::escape($log['criado_em']) ?></td>
            <td><?= View::escape($log['usuario']) ?></td>
            <td><?= View::escape($acaoLabels[$log['acao']] ?? $log['acao']) ?></td>
            <td><?= View::escape($log['alvo'] ?? '') ?></td>
            <td><?= View::escape($log['detalhe'] ?? '') ?></td>
            <td><?= View::escape($log['ip'] ?? '') ?></td>
        </tr>
        <?php endforeach; ?>
        <?php if (!$logs): ?>
        <tr><td colspan="6">Nenhum registro ainda.</td></tr>
        <?php endif; ?>
    </table>

    <?php if ($totalPages > 1): ?>
    <p style="margin-top:12px">
        <?php for ($p = 1; $p <= $totalPages; $p++): ?>
            <?php if ($p === $page): ?>
                <strong><?= $p ?></strong>
            <?php else: ?>
                <a href="auditoria.php?p=<?= $p ?>"><?= $p ?></a>
            <?php endif; ?>
            <?php if ($p < $totalPages): ?> | <?php endif; ?>
        <?php endfor; ?>
    </p>
    <?php endif; ?>
</div>
<?php
View::footer();