Files
itflow/admin/post/audit_log.php
johnnyq ea23a2bad5 Audit logs stage 1: Admin > Audit Logs rebuilt on the standard list with search, sortable columns and paging, filters for date, who (agent, portal contact or ITFlow itself), client, type and action, and counted views for Changes, Deletions, Sign-ins, Security and Data out; each entry shows who with their avatar, the client, the type and action as a coloured label, the description without the name in front, the IP and device in words, and links to its record while it exists or says No longer here; a quick look with the full entry, the raw user agent, everything from that IP, the record's other entries and what the same person did 15 minutes either side; CSV/PDF export with the page's filters, view and picked columns, itself audited; new functions/audit_log.php, admin/modals/audit_log/audit_log.php and audit_log_export.php, admin/post/audit_log.php and audit_logs export columns; agent/files.php takes ?open= for a file's quick look; database 3.3.1 adds log_client_id and log_entity_id keys on logs, gives each action one name (Unarchive/Restored to Restore, Deleted, Merged, Closed, Reopened, Resolved and Sent to Delete, Merge, Close, Reopen, Resolve and Send, Client Access to Denied, saved card and payment provider Updates to Create or Delete, contract template Update to Edit, type client to Client) and repoints entries at the right record (reply edits and redactions to the ticket, API document edits to the document, reimbursements to the recording expense, API client restores to the client), guarded so it can run again; logAudit call sites in agent, admin, api, client, cron and functions use the same names and record ids
2026-10-18 22:36:25 -04:00

72 lines
2.5 KiB
PHP

<?php
/*
* ITFlow - POST handler for the audit log: exporting it to CSV or PDF, with
* the filters and view the export window posts - the same ones the list
* uses (auditLogFiltersSql(), functions/audit_log.php). The export is itself
* logged, as data taken out.
*/
defined('FROM_POST_HANDLER') || die("Direct file access is not allowed");
if (isExportRequest('export_audit_logs')) {
validateCSRFToken();
enforceAdminPermission();
$format = resolveExportFormat($_POST['export_audit_logs']);
$filter_summary = [];
$where = auditLogFiltersSql($_POST, $filter_summary);
$joins = auditLogJoinsSql();
$num_rows = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM logs $joins WHERE $where"))[0]);
if ($num_rows > 0) {
guardExportPdfRowCount($format, $num_rows);
$export = beginExport('audit_logs', $format, "$session_company_name-Audit-Logs", 'Audit Logs', summarizeExportFilters($filter_summary));
// Written 500 at a time - each lot's records looked up together, one query per kind
$sql = mysqli_query($mysqli, "SELECT log_id, log_type, log_action, log_description, log_ip, log_user_agent, log_created_at, log_client_id, log_user_id, log_entity_id,
user_name, user_email, user_avatar, user_type, client_name
FROM logs $joins
WHERE $where
ORDER BY log_created_at DESC, log_id DESC");
$write = function (array $chunk) use (&$export) {
$records = auditLogRecords($chunk);
foreach ($chunk as $row) {
$who = auditLogWho($row);
$row['audit_who'] = $who['name'] . ($who['kind'] === 'portal' ? ' (portal)' : '');
$row['audit_record'] = $records[$row['log_type'] . '|' . intval($row['log_entity_id'])]['name'] ?? '';
$row['audit_device'] = auditLogDevice($row['log_user_agent']);
addExportRow($export, $row);
}
};
$chunk = [];
while ($row = mysqli_fetch_assoc($sql)) {
$chunk[] = $row;
if (count($chunk) === 500) {
$write($chunk);
$chunk = [];
}
}
if ($chunk) {
$write($chunk);
}
finishExport($export);
logAudit("Audit Log", "Export", "$session_name exported $num_rows audit log " . ($num_rows === 1 ? 'entry' : 'entries') . " to a " . strtoupper($format) . " file");
exit;
}
flashAlert('No audit log entries match - nothing to export.', 'error');
redirect();
}