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

This commit is contained in:
johnnyq
2026-10-18 22:36:25 -04:00
parent ad271aaa87
commit ea23a2bad5
38 changed files with 1169 additions and 316 deletions

View File

@@ -1,295 +1,269 @@
<?php
/*
* Admin > Audit Logs - who did what, when and from where: every change,
* sign-in, look at a secret and export logAudit() writes. Searched, filtered
* by date, who, client, type and action, and in views - changes, deletions,
* sign-ins, security and data out - each counted for the filters picked.
* Each entry links to the record it is about while that still exists, and
* opens a quick look with all of it (modals/audit_log/audit_log.php). What is
* shown can be exported (export_audit_logs). The work is in
* functions/audit_log.php.
*/
// Default Column Sortby Filter
$sort = "log_id";
$order = "DESC";
require_once "includes/inc_all_admin.php";
// User Filter
if (isset($_GET['user']) & !empty($_GET['user'])) {
$user_query = 'AND (log_user_id = ' . intval($_GET['user']) . ')';
$user_filter = intval($_GET['user']);
} else {
// Default - any
$user_query = '';
$user_filter = '';
// Tab title - the file is named for the table, the page for what it holds
$page_title = 'Audit Logs';
// Only these sort - an unknown column would take the page down
$sortable_columns = [
'log_id' => "log_id $order",
'log_created_at' => "log_created_at $order, log_id $order",
'user_name' => "user_name IS NULL, user_name $order, log_id DESC",
'client_name' => "client_name IS NULL, client_name $order, log_id DESC",
'log_type' => "log_type $order, log_action $order, log_id DESC",
'log_action' => "log_action $order, log_id DESC",
'log_ip' => "log_ip IS NULL, log_ip $order, log_id DESC",
];
if (!isset($sortable_columns[$sort])) {
$sort = 'log_id';
}
// Client Filter
if (isset($_GET['client']) & !empty($_GET['client'])) {
$client_query = 'AND (log_client_id = ' . intval($_GET['client']) . ')';
$client_filter = intval($_GET['client']);
} else {
// Default - any
$client_query = '';
$client_filter = '';
}
// Log Type Filter
if (isset($_GET['type']) & !empty($_GET['type'])) {
$log_type_query = "AND (log_type = '" . escapeSql($_GET['type']) . "')";
$type_filter = escapeHtml($_GET['type']);
} else {
// Default - any
$log_type_query = '';
$type_filter = '';
}
// Log Action Filter
if (isset($_GET['action']) & !empty($_GET['action'])) {
$log_action_query = "AND (log_action = '" . escapeSql($_GET['action']) . "')";
$action_filter = escapeHtml($_GET['action']);
} else {
// Default - any
$log_action_query = '';
$action_filter = '';
}
/*
* The date filter is a half-open range rather than DATE(log_created_at) BETWEEN
* ... - wrapping the column in a function makes the comparison non-sargable, so
* KEY log_created_at could never be used and every page view scanned the whole
* table. '< dtt + 1 day' keeps the end date inclusive, which BETWEEN on a bare
* DATETIME would not: '2026-08-28' alone means midnight, and would drop that
* day's rows.
*/
$sql = mysqli_query(
$mysqli,
"SELECT SQL_CALC_FOUND_ROWS client_id, client_name, log_action, log_created_at, log_description, log_entity_id, log_id,
log_ip, log_type, log_user_agent, user_id, user_name FROM logs
LEFT JOIN users ON log_user_id = user_id
LEFT JOIN clients ON log_client_id = client_id
WHERE (log_type LIKE '%$q%' OR log_action LIKE '%$q%' OR log_description LIKE '%$q%' OR log_ip LIKE '%$q%' OR log_user_agent LIKE '%$q%' OR user_name LIKE '%$q%' OR client_name LIKE '%$q%')
AND log_created_at >= '$dtf 00:00:00' AND log_created_at < DATE_ADD('$dtt', INTERVAL 1 DAY)
$user_query
$client_query
$log_type_query
$log_action_query
ORDER BY $sort $order LIMIT $record_from, $record_to"
);
$views = auditLogViews();
$view = isset($_GET['view']) && is_string($_GET['view']) && isset($views[$_GET['view']]) ? $_GET['view'] : '';
$filter_src = array_merge($_GET, ['dtf' => $dtf, 'dtt' => $dtt, 'view' => $view]);
$joins = auditLogJoinsSql();
$sql = mysqli_query($mysqli, "SELECT SQL_CALC_FOUND_ROWS 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_avatar, user_type, client_name
FROM logs $joins
WHERE " . auditLogFiltersSql($filter_src) . "
ORDER BY {$sortable_columns[$sort]}
LIMIT $record_from, $record_to");
$num_rows = mysqli_fetch_row(mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$rows = [];
while ($row = mysqli_fetch_assoc($sql)) {
$rows[] = $row;
}
$records = auditLogRecords($rows);
$kinds = auditLogKinds();
// Each view counted for the rest of the filters
$count_sql = [];
foreach ($views as $definition) {
$count_sql[] = 'COALESCE(SUM(' . ($definition['where'] === '' ? '1' : $definition['where']) . '), 0)';
}
$counts = array_combine(array_keys($views), array_map('intval', mysqli_fetch_row(mysqli_query($mysqli, "SELECT " . implode(', ', $count_sql) . "
FROM logs $joins WHERE " . auditLogFiltersSql(array_merge($filter_src, ['view' => '']))))));
$client_filter = is_scalar($_GET['client'] ?? null) ? intval($_GET['client']) : 0;
$user_filter = is_string($_GET['user'] ?? null) ? $_GET['user'] : '';
$type_filter = is_string($_GET['type'] ?? null) ? $_GET['type'] : '';
$action_filter = is_string($_GET['action'] ?? null) ? $_GET['action'] : '';
$entity_filter = is_scalar($_GET['entity'] ?? null) ? intval($_GET['entity']) : 0;
$more_filters = $action_filter !== '' || $entity_filter;
$any_filter = $q !== '' || $client_filter || $user_filter !== '' || $type_filter !== '' || $action_filter !== '' || $entity_filter || $dtf !== '1970-01-01' || $dtt !== '2099-12-31';
$sort_link = function ($column, $label) use ($url_query_strings_sort, $sort, $disp, $order_icon) {
return '<a class="text-reset text-decoration-none" href="?' . escapeHtml($url_query_strings_sort) . '&amp;sort=' . $column . '&amp;order=' . $disp . '">'
. $label . ($sort === $column ? " $order_icon" : '') . '</a>';
};
// A view keeps the filters, and starts from its first page
$view_url = function ($key) {
$query = $_GET;
unset($query['page']);
$query['view'] = $key;
return '?' . http_build_query(array_filter($query, function ($value) {
return $value !== '' && $value !== null && $value !== [];
}));
};
// When a view has nothing in it, and there are no filters to blame
$view_empty = [
'changes' => 'No changes yet',
'deletions' => 'Nothing deleted yet',
'signins' => 'No sign-ins yet',
'security' => 'Nothing to do with security yet',
'data' => 'Nothing looked at or taken out yet',
];
?>
<div class="card">
<div class="card-header bg-dark py-2">
<h3 class="card-title"><i class="fas fa-fw fa-history me-2"></i>Audit Logs</h3>
<div class="card">
<div class="card-header bg-dark py-2">
<h3 class="card-title mt-2"><i class="fas fa-fw fa-history me-2"></i>Audit Logs</h3>
<div class="card-tools">
<button type="button" class="btn btn-primary ajax-modal" data-modal-url="<?= buildExportModalUrl('modals/audit_log/audit_log_export.php', ['client', 'user', 'type', 'action', 'entity', 'q'], ['dtf' => $dtf, 'dtt' => $dtt, 'view' => $view]) ?>">
<i class="fas fa-fw fa-download"></i><span class="d-none d-lg-inline ms-2">Export</span>
</button>
</div>
<div class="card-header py-3">
<form autocomplete="off">
<div class="row g-2 align-items-end">
<div class="col-sm-4">
<div class="input-group">
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search audit logs">
<button class="btn btn-secondary" title="More filters" type="button" data-bs-toggle="collapse" data-bs-target="#advancedFilter"><i class="fas fa-filter"></i></button>
<button class="btn btn-primary" title="Search"><i class="fa fa-search"></i></button>
</div>
</div>
<div class="col-sm-2">
<div class="input-group">
<select class="form-select select2" name="client" onchange="this.form.submit()">
<option value="">- All Clients -</option>
<?php
$sql_clients_filter = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients ORDER BY client_name ASC");
while ($row = mysqli_fetch_assoc($sql_clients_filter)) {
$client_id = intval($row['client_id']);
$client_name = escapeHtml($row['client_name']);
?>
<option <?php if ($client_filter == $client_id) { echo "selected"; } ?> value="<?= $client_id ?>"<?= selectOptionClient($row['client_id'], $row['client_name'], $row['client_abbreviation'] ?? '') ?>><?= $client_name ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-sm-2">
<div class="input-group">
<select class="form-select select2" name="user" onchange="this.form.submit()">
<option value="">- All Users -</option>
<?php
$sql_users_filter = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar FROM users ORDER BY user_name ASC");
while ($row = mysqli_fetch_assoc($sql_users_filter)) {
$user_id = intval($row['user_id']);
$user_name = escapeHtml($row['user_name']);
?>
<option <?php if ($user_filter == $user_id) { echo "selected"; } ?> value="<?= $user_id ?>"<?= selectOptionUser($row['user_id'], $row['user_name'], $row['user_avatar'] ?? '') ?>><?= $user_name ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-sm-2">
<div class="input-group">
<select class="form-select select2" name="type" onchange="this.form.submit()">
<option value="">- All Types -</option>
<?php
$sql_types_filter = mysqli_query($mysqli, "SELECT DISTINCT log_type FROM logs ORDER BY log_type ASC");
while ($row = mysqli_fetch_assoc($sql_types_filter)) {
$log_type = escapeHtml($row['log_type']);
?>
<option <?php if ($type_filter == $log_type) { echo "selected"; } ?>><?= $log_type ?></option>
<?php
}
?>
</select>
</div>
</div>
<div class="col-sm-2">
<div class="input-group">
<select class="form-select select2" name="action" onchange="this.form.submit()">
<option value="">- All Actions -</option>
<?php
$sql_actions_filter = mysqli_query($mysqli, "SELECT DISTINCT log_action FROM logs ORDER BY log_action ASC");
while ($row = mysqli_fetch_assoc($sql_actions_filter)) {
$log_action = escapeHtml($row['log_action']);
?>
<option <?php if ($action_filter == $log_action) { echo "selected"; } ?>><?= $log_action ?></option>
<?php
}
?>
</select>
</div>
</div>
</div>
<div class="collapse mt-3 <?php if (isset($_GET['dtf']) && $_GET['dtf'] !== '1970-01-01') { echo"show"; } ?>" id="advancedFilter">
<div class="row g-3">
<div class="col-md-3">
<div>
<label class="form-label">Date range</label>
<input type="text" id="dateFilter" class="form-control" autocomplete="off">
<input type="hidden" name="canned_date" id="canned_date" value="<?= escapeHtml($_GET['canned_date']) ?? '' ?>">
<input type="hidden" name="dtf" id="dtf" value="<?= escapeHtml($dtf ?? '') ?>">
<input type="hidden" name="dtt" id="dtt" value="<?= escapeHtml($dtt ?? '') ?>">
</div>
</div>
</div>
</div>
</form>
</div>
<div class="table-responsive-sm">
<table class="table table-sm table-striped table-borderless table-hover mb-0">
<thead class="text-dark <?php if ($num_rows[0] == 0) { echo "d-none"; } ?> text-nowrap">
<tr>
<th class="ps-3">
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=log_created_at&order=<?= $disp ?>">
Timestamp <?php if ($sort == 'log_created_at') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=user_name&order=<?= $disp ?>">
User <?php if ($sort == 'user_name') { echo $order_icon; } ?>
</a>
</th>
<?php if (empty($client)) { ?>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=client_name&order=<?= $disp ?>">
Client <?php if ($sort == 'client_name') { echo $order_icon; } ?>
</a>
</th>
<?php } ?>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=log_type&order=<?= $disp ?>">
Type <?php if ($sort == 'log_type') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=log_action&order=<?= $disp ?>">
Action <?php if ($sort == 'log_action') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=log_description&order=<?= $disp ?>">
Description <?php if ($sort == 'log_description') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=log_ip&order=<?= $disp ?>">
IP Address <?php if ($sort == 'log_ip') { echo $order_icon; } ?>
</a>
</th>
<th>
<a class="text-dark" href="?<?= $url_query_strings_sort ?>&sort=log_user_agent&order=<?= $disp ?>">
User Agent <?php if ($sort == 'log_user_agent') { echo $order_icon; } ?>
</a>
</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($sql)) {
$log_id = intval($row['log_id']);
$log_type = escapeHtml($row['log_type']);
$log_action = escapeHtml($row['log_action']);
$log_description = escapeHtml($row['log_description']);
$log_ip = escapeHtml($row['log_ip']);
$log_user_agent = escapeHtml($row['log_user_agent']);
$log_user_os = getOS($log_user_agent);
$log_user_browser = getWebBrowser($log_user_agent);
$log_created_at = escapeHtml($row['log_created_at']);
$user_id = intval($row['user_id']);
$user_name = escapeHtml($row['user_name']);
if (empty($user_name)) {
$user_name_display = "-";
} else {
$user_name_display = $user_name;
}
$client_name = escapeHtml($row['client_name']);
$client_id = intval($row['client_id']);
if (empty($client_name)) {
$client_name_display = "-";
} else {
$client_name_display = "<a href='../agent/client_overview.php?client_id=$client_id'>$client_name</a>";
}
$log_entity_id = intval($row['log_entity_id']);
?>
<tr>
<td class="font-monospace ps-3"><?= $log_created_at ?></td>
<td><?= $user_name_display ?></td>
<?php if(empty($client)) { ?>
<td><?= $client_name_display ?></td>
<?php } ?>
<td><?= $log_type ?></td>
<td><?= $log_action ?></td>
<td><?= $log_description ?></td>
<td class="font-monospace"><?= $log_ip ?></td>
<td><?= "$log_user_os<div class='text-secondary'>$log_user_browser</div>" ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<?php
$empty_state = [
'thing' => 'audit logs',
'icon' => 'fa-history',
'text' => 'Who changed what, and when, shows here.',
];
?>
<?php require_once "../includes/filter_footer.php";
?>
</div>
<div class="card-header py-3">
<form autocomplete="off">
<?php if ($view !== '') { ?><input type="hidden" name="view" value="<?= escapeHtml($view) ?>"><?php } ?>
<div class="row g-2 align-items-center">
<div class="col-lg-4 col-md-6">
<div class="input-group">
<input type="search" class="form-control" name="q" value="<?php if (isset($q)) { echo stripslashes(escapeHtml($q)); } ?>" placeholder="Search the audit log" title="What was done, who did it, the client, the IP address or the browser">
<button class="btn btn-outline-secondary <?= $more_filters ? 'active' : '' ?>" title="More filters" type="button" data-bs-toggle="collapse" data-bs-target="#auditMoreFilters"><i class="fas fa-filter"></i></button>
<button class="btn btn-dark" title="Search"><i class="fa fa-search"></i></button>
</div>
</div>
<div class="col-lg-2 col-md-6">
<input type="text" id="dateFilter" class="form-control" autocomplete="off" title="When" aria-label="When">
<input type="hidden" name="canned_date" id="canned_date" value="<?= escapeHtml(is_string($_GET['canned_date'] ?? null) ? $_GET['canned_date'] : '') ?>">
<input type="hidden" name="dtf" id="dtf" value="<?= escapeHtml($dtf ?? '') ?>">
<input type="hidden" name="dtt" id="dtt" value="<?= escapeHtml($dtt ?? '') ?>">
</div>
<div class="col-lg-2 col-md-4">
<select class="form-select select2" name="user" onchange="this.form.submit()" aria-label="Who">
<option value="">- Anyone -</option>
<option value="system" <?= $user_filter === 'system' ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas fa-cog', 'description' => 'Cron, the API, the email parser']) ?>>ITFlow itself</option>
<?php
$sql_filter = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar, user_type FROM users WHERE EXISTS (SELECT 1 FROM logs WHERE log_user_id = user_id) ORDER BY user_type ASC, user_name ASC");
while ($row = mysqli_fetch_assoc($sql_filter)) {
$option_id = intval($row['user_id']); ?>
<option <?= $user_filter === (string) $option_id ? 'selected' : '' ?> value="<?= $option_id ?>"<?= selectOptionUser($row['user_id'], $row['user_name'], $row['user_avatar'] ?? '') ?><?= intval($row['user_type']) === 2 ? ' data-description="Portal"' : '' ?>><?= escapeHtml($row['user_name']) ?></option>
<?php } ?>
</select>
</div>
<div class="col-lg-2 col-md-4">
<select class="form-select select2" name="client" onchange="this.form.submit()" aria-label="Client">
<option value="">- Any client -</option>
<?php
$sql_filter = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE EXISTS (SELECT 1 FROM logs WHERE log_client_id = client_id) ORDER BY client_name ASC");
while ($row = mysqli_fetch_assoc($sql_filter)) {
$option_id = intval($row['client_id']); ?>
<option <?= $client_filter === $option_id ? 'selected' : '' ?> value="<?= $option_id ?>"<?= selectOptionClient($row['client_id'], $row['client_name'], $row['client_abbreviation'] ?? '') ?>><?= escapeHtml($row['client_name']) ?></option>
<?php } ?>
</select>
</div>
<div class="col-lg-2 col-md-4">
<select class="form-select select2" name="type" onchange="this.form.submit()" aria-label="Type">
<option value="">- Any type -</option>
<?php
$sql_filter = mysqli_query($mysqli, "SELECT DISTINCT log_type FROM logs ORDER BY log_type ASC");
while ($row = mysqli_fetch_row($sql_filter)) { ?>
<option <?= $type_filter === $row[0] ? 'selected' : '' ?> value="<?= escapeHtml($row[0]) ?>"<?= selectOptionData(['icon' => 'fas ' . auditLogTypeIcon($row[0])]) ?>><?= escapeHtml($row[0]) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="collapse <?= $more_filters ? 'show' : '' ?>" id="auditMoreFilters">
<div class="row g-2 mt-0 align-items-center">
<div class="col-lg-3 col-md-6">
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-bolt"></i></span>
<select class="form-select select2" name="action" onchange="this.form.submit()" aria-label="Action">
<option value="">- Any action -</option>
<?php
$sql_filter = mysqli_query($mysqli, "SELECT DISTINCT log_action FROM logs ORDER BY log_action ASC");
while ($row = mysqli_fetch_row($sql_filter)) { ?>
<option <?= $action_filter === $row[0] ? 'selected' : '' ?> value="<?= escapeHtml($row[0]) ?>"<?= selectOptionData(['icon' => 'fas ' . auditLogActionStyle($row[0])['icon']]) ?>><?= escapeHtml($row[0]) ?></option>
<?php } ?>
</select>
</div>
</div>
<?php if ($entity_filter) { ?>
<div class="col-lg-9 col-md-6 small" data-audit-entity="<?= $entity_filter ?>">
<input type="hidden" name="entity" value="<?= $entity_filter ?>">
<span class="text-body-secondary"><i class="fas fa-fw fa-filter me-1"></i>Just one record: <?= escapeHtml($type_filter !== '' ? $type_filter : 'record') ?> #<?= $entity_filter ?></span>
<a class="ms-2" href="<?= escapeHtml('?' . http_build_query(array_diff_key($_GET, ['entity' => 1, 'page' => 1]))) ?>">Every record</a>
</div>
<?php } ?>
</div>
</div>
</form>
<nav class="nav nav-pills itflow-asset-tabs flex-nowrap overflow-auto mt-3">
<?php foreach ($views as $key => $definition) { ?>
<a class="nav-link text-nowrap <?= $view === $key ? 'active' : '' ?>" href="<?= escapeHtml($view_url($key)) ?>" title="<?= escapeHtml($definition['title']) ?>" data-audit-view="<?= $key === '' ? 'all' : $key ?>">
<i class="fas fa-fw <?= $definition['icon'] ?> me-1"></i><?= $definition['label'] ?>
<span class="badge rounded-pill ms-1"><?= number_format($counts[$key]) ?></span>
</a>
<?php } ?>
</nav>
</div>
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="table-light text-nowrap <?php if ($num_rows[0] == 0) { echo "d-none"; } ?>">
<tr>
<th class="ps-3"><?= $sort_link('log_created_at', 'When') ?></th>
<th><?= $sort_link('user_name', 'Who') ?></th>
<th class="d-none d-md-table-cell"><?= $sort_link('client_name', 'Client') ?></th>
<th><?= $sort_link('log_type', 'What') ?></th>
<th>Description</th>
<th class="d-none d-xl-table-cell"><?= $sort_link('log_ip', 'From') ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($rows as $row) {
$log_id = intval($row['log_id']);
$who = auditLogWho($row);
$type = (string) $row['log_type'];
$link = auditLogRecordLink($row, $records);
$gone = $link === '' && intval($row['log_entity_id']) > 0 && isset($kinds[$type]['table']);
$device = auditLogDevice($row['log_user_agent']);
$from = implode(' · ', array_filter([(string) $row['log_ip'], $device]));
?>
<tr data-log-id="<?= $log_id ?>">
<td class="ps-3 text-nowrap small" title="<?= escapeHtml($row['log_created_at']) ?>">
<div><?= escapeHtml(date('M j, Y g:i A', strtotime($row['log_created_at']))) ?></div>
<div class="text-body-secondary"><?= escapeHtml(timeAgo($row['log_created_at'])) ?></div>
</td>
<td class="small" style="max-width: 14rem;"><?= $who['html'] ?></td>
<td class="d-none d-md-table-cell small">
<?php if ($row['client_name'] !== null) { ?>
<a class="text-reset" href="/agent/client_overview.php?client_id=<?= intval($row['log_client_id']) ?>"><?= escapeHtml($row['client_name']) ?></a>
<?php } else { ?>
<span class="text-body-secondary">-</span>
<?php } ?>
</td>
<td class="text-nowrap">
<div class="small mb-1"><i class="fas fa-fw <?= auditLogTypeIcon($type) ?> text-secondary me-1"></i><?= escapeHtml($type) ?></div>
<?= auditLogActionPill($row['log_action']) ?>
</td>
<td class="small" style="min-width: 16rem;">
<div class="text-break"><?= escapeHtml(auditLogDescription($row)) ?></div>
<?php if ($link !== '') { ?>
<div class="mt-1" data-audit-record><i class="fas fa-fw fa-external-link-alt text-secondary me-1"></i><?= $link ?></div>
<?php } elseif ($gone) { ?>
<div class="mt-1 text-body-secondary" data-audit-record-gone title="The <?= escapeHtml(strtolower($type)) ?> this is about has been deleted"><i class="fas fa-fw fa-ghost me-1"></i>No longer here</div>
<?php } ?>
<?php if ($from !== '') { ?>
<div class="d-xl-none text-body-secondary mt-1"><?= escapeHtml($from) ?></div>
<?php } ?>
</td>
<td class="d-none d-xl-table-cell small text-nowrap">
<div class="font-monospace"><?= escapeHtml((string) $row['log_ip']) ?></div>
<div class="text-body-secondary"><?= escapeHtml($device) ?></div>
</td>
<td class="text-end pe-3">
<button type="button" class="btn btn-sm btn-light ajax-modal" data-modal-size="lg" data-modal-url="modals/audit_log/audit_log.php?id=<?= $log_id ?>" title="All of it" aria-label="All of it"><i class="fas fa-fw fa-search-plus"></i></button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php
$empty_state = [
'thing' => 'audit log entries',
'icon' => 'fa-history',
'filtered' => $any_filter,
'views' => ['view'],
'view' => $view !== '' ? ['icon' => $views[$view]['icon'], 'title' => $view_empty[$view], 'text' => escapeHtml($views[$view]['title']) . ' show here.'] : null,
'text' => 'Who changed what, signed in, looked at a password or exported something - when, and from where - shows here.',
];
require_once "../includes/filter_footer.php";
?>
</div>
<?php
require_once "../includes/footer.php";

View File

@@ -0,0 +1,68 @@
<?php
/*
* ITFlow - Database update to version 3.3.1 (from 3.3.0)
* Included by admin/database_updates.php - do not access directly
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
// The audit log, made consistent so it can be filtered and each entry opened.
//
// 1. Entries that pointed at the wrong record: a client restored through the
// API was logged as a Contact with no record, editing or redacting a ticket
// reply pointed at the reply rather than its ticket, a document edited
// through the API at its old version, and paying back an agent at the
// reimbursement rather than the expense that recorded it. Moving an id
// twice would move it somewhere wrong, so this is done only while the
// log_entity_id index, added last below, is not there yet.
$itflow_logs_done = mysqli_query($mysqli, "SELECT 1 FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'logs' AND INDEX_NAME = 'log_entity_id' LIMIT 1");
if (!$itflow_logs_done || mysqli_num_rows($itflow_logs_done) === 0) {
mysqli_query($mysqli, "UPDATE `logs` SET `log_type` = 'Client', `log_entity_id` = `log_client_id`
WHERE `log_type` = 'Contact' AND `log_action` = 'Unarchive' AND `log_entity_id` = 0 AND `log_description` LIKE '% unarchived via API (%'");
mysqli_query($mysqli, "UPDATE `logs` LEFT JOIN `ticket_replies` ON `ticket_reply_id` = `log_entity_id`
SET `log_entity_id` = COALESCE(`ticket_reply_ticket_id`, 0)
WHERE `log_type` = 'Ticket' AND `log_action` = 'Reply' AND (`log_description` LIKE '% edited a reply on ticket %' OR `log_description` LIKE '% redacted a reply on ticket %')");
mysqli_query($mysqli, "UPDATE `logs` LEFT JOIN `document_versions` ON `document_version_id` = `log_entity_id`
SET `log_entity_id` = COALESCE(`document_version_document_id`, 0)
WHERE `log_type` = 'Document' AND `log_action` = 'Edit' AND `log_description` LIKE '% via API (%), previous version kept'");
mysqli_query($mysqli, "UPDATE `logs` LEFT JOIN `reimbursements` ON `reimbursement_id` = `log_entity_id`
SET `log_entity_id` = COALESCE(`reimbursement_expense_id`, 0)
WHERE `log_type` = 'Expense' AND `log_action` = 'Reimburse'");
mysqli_query($mysqli, "UPDATE `logs` SET `log_entity_id` = 0 WHERE `log_type` = 'Expense' AND `log_action` = 'Reimbursement Undo'");
}
// 2. One name for each thing done, whatever it was done to - the past-tense
// and API spellings become the verbs everything else uses, an agent
// turned away from a client says Denied rather than Access, and a saved
// payment method saved or deleted says so rather than Update
mysqli_query($mysqli, "UPDATE `logs` SET `log_type` = 'Client' WHERE BINARY `log_type` = 'client'");
foreach (['Unarchive' => 'Restore', 'Restored' => 'Restore', 'Deleted' => 'Delete', 'Merged' => 'Merge', 'Closed' => 'Close',
'Reopened' => 'Reopen', 'Resolved' => 'Resolve', 'Sent' => 'Send'] as $itflow_old_action => $itflow_new_action) {
mysqli_query($mysqli, "UPDATE `logs` SET `log_action` = '$itflow_new_action' WHERE `log_action` = '$itflow_old_action'");
}
mysqli_query($mysqli, "UPDATE `logs` SET `log_action` = 'Denied' WHERE `log_type` = 'Client' AND `log_action` = 'Access'");
mysqli_query($mysqli, "UPDATE `logs` SET `log_action` = 'Delete' WHERE `log_type` IN ('Payment Provider', 'Stripe') AND `log_action` = 'Update' AND `log_description` LIKE '% deleted %'");
mysqli_query($mysqli, "UPDATE `logs` SET `log_action` = 'Create' WHERE `log_type` = 'Stripe' AND `log_action` = 'Update' AND `log_description` LIKE '% saved payment method %'");
mysqli_query($mysqli, "UPDATE `logs` SET `log_action` = 'Edit' WHERE `log_type` = 'Contract Template' AND `log_action` = 'Update'");
// 3. Two indexes on logs - by client, for the client filter and a client's
// recent activity, and by record, for a record's history. logs takes a
// write per change, so these two and no more; the record's id leads
// because it is the selective column, its type trails.
$itflow_indexes = [
['logs', 'log_client_id', ['log_client_id', 'log_created_at']],
['logs', 'log_entity_id', ['log_entity_id', 'log_type']],
];
foreach ($itflow_indexes as $itflow_index) {
list($itflow_index_table, $itflow_index_name, $itflow_index_columns) = $itflow_index;
// No ADD INDEX IF NOT EXISTS - check first, so the step can run again
$itflow_index_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = '$itflow_index_table' AND INDEX_NAME = '$itflow_index_name' LIMIT 1");
if ($itflow_index_exists && mysqli_num_rows($itflow_index_exists) > 0) {
continue;
}
mysqli_query($mysqli, "ALTER TABLE `$itflow_index_table` ADD KEY `$itflow_index_name` (`" . implode('`, `', $itflow_index_columns) . "`)");
}
unset($itflow_logs_done, $itflow_indexes, $itflow_index, $itflow_index_table, $itflow_index_name, $itflow_index_columns, $itflow_index_exists, $itflow_old_action, $itflow_new_action);

View File

@@ -0,0 +1,196 @@
<?php
/*
* An audit log entry's quick look - all of it: when, who (and their email),
* the client, what kind of record and what was done, the whole description,
* the record while it still exists, the IP address and device with the
* browser's own user agent. Then the record's other entries - its history -
* and what the same person did in the quarter hour either side. Opened from
* Admin > Audit Logs.
*/
require_once '../../includes/modal_header.php';
$log_id = intval($_GET['id'] ?? 0);
$row = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT logs.*, user_name, user_email, user_avatar, user_type, client_name
FROM logs " . auditLogJoinsSql() . " WHERE log_id = $log_id"));
if (!$row) {
echo json_encode(['error' => 'That audit log entry no longer exists.']);
exit;
}
$type = (string) $row['log_type'];
$type_sql = escapeSql($type);
$entity_id = intval($row['log_entity_id']);
$user_id = intval($row['log_user_id']);
$kinds = auditLogKinds();
$who = auditLogWho($row);
$style = auditLogActionStyle($row['log_action']);
$records = auditLogRecords([$row]);
$link = auditLogRecordLink($row, $records);
$device = auditLogDevice($row['log_user_agent']);
$created = strtotime($row['log_created_at']);
$joins = auditLogJoinsSql();
// The record's other entries - its history here (an entry about no one record - a sign-in, a setting - has none)
$is_record = $entity_id > 0 && isset($kinds[$type]['table']);
$history = [];
$history_count = 0;
if ($is_record) {
$history_where = "log_entity_id = $entity_id AND log_type = '$type_sql' AND log_id <> $log_id";
$history_count = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM logs WHERE $history_where"))[0]);
$sql = mysqli_query($mysqli, "SELECT log_id, log_type, log_action, log_description, log_created_at, log_user_id, user_name, user_avatar, user_type
FROM logs $joins WHERE $history_where ORDER BY log_created_at DESC, log_id DESC LIMIT 8");
while ($history_row = mysqli_fetch_assoc($sql)) {
$history[] = $history_row;
}
}
// What the same person did around then - a quarter hour either side
$around = [];
$around_count = 0;
if ($user_id > 0) {
$around_where = "log_user_id = $user_id AND log_id <> $log_id
AND log_created_at >= '" . escapeSql($row['log_created_at']) . "' - INTERVAL 15 MINUTE AND log_created_at <= '" . escapeSql($row['log_created_at']) . "' + INTERVAL 15 MINUTE";
$around_count = intval(mysqli_fetch_row(mysqli_query($mysqli, "SELECT COUNT(*) FROM logs WHERE $around_where"))[0]);
$sql = mysqli_query($mysqli, "SELECT log_id, log_type, log_action, log_description, log_created_at, log_entity_id, log_user_id, user_name, user_avatar, user_type
FROM logs $joins WHERE $around_where ORDER BY log_created_at ASC, log_id ASC LIMIT 10");
while ($around_row = mysqli_fetch_assoc($sql)) {
$around[] = $around_row;
}
}
// One line of a list of entries - when, the action and what it says; each opens its own quick look
$entry_line = function ($entry, $show_when_as = 'date') {
$time = strtotime($entry['log_created_at']);
$when = $show_when_as === 'time' ? date('g:i A', $time) : date('M j, Y g:i A', $time);
return '<a href="#" class="list-group-item list-group-item-action d-flex align-items-start gap-2 small ajax-modal" data-modal-size="lg" data-modal-url="modals/audit_log/audit_log.php?id=' . intval($entry['log_id']) . '">'
. '<span class="text-body-secondary text-nowrap flex-shrink-0" style="min-width: 4.5rem;" title="' . escapeHtml($entry['log_created_at']) . '">' . escapeHtml($when) . '</span>'
. '<span class="flex-shrink-0">' . auditLogActionPill($entry['log_action']) . '</span>'
. '<span class="text-break">' . escapeHtml($entry['log_description']) . '</span>'
. '</a>';
};
ob_start();
?>
<div class="modal-header bg-dark" data-bs-theme="dark">
<div class="d-flex align-items-center gap-3 min-w-0 flex-grow-1">
<span class="itflow-asset-icon itflow-asset-icon-lg bg-<?= $style['tone'] ?>-subtle text-<?= $style['tone'] ?>-emphasis" aria-hidden="true"><i class="fas <?= auditLogTypeIcon($type) ?>"></i></span>
<div class="min-w-0 flex-grow-1">
<h5 class="modal-title fw-semibold text-break"><?= escapeHtml($type) ?> &middot; <?= escapeHtml($row['log_action']) ?></h5>
<div class="small text-body-secondary mt-1"><?= escapeHtml(date('D, M j, Y \a\t g:i:s A', $created)) ?> &middot; <?= escapeHtml(timeAgo($row['log_created_at'])) ?></div>
</div>
</div>
<button type="button" class="btn-close btn-close-white ms-3" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="p-3 rounded border bg-body-tertiary mb-3">
<div class="text-break" style="white-space: pre-line;" data-audit-full-description><?= escapeHtml($row['log_description']) ?></div>
<?php if ($link !== '') { ?>
<div class="small mt-2"><i class="fas fa-fw fa-external-link-alt text-secondary me-1"></i><?= $link ?></div>
<?php } elseif ($is_record) { ?>
<div class="small mt-2 text-body-secondary"><i class="fas fa-fw fa-ghost me-1"></i>The <?= escapeHtml(strtolower($type)) ?> this is about has since been deleted</div>
<?php } ?>
</div>
<div class="row g-2 itflow-asset-glance">
<div class="col-sm-6">
<div class="itflow-glance-item" title="Who">
<i class="fas fa-fw fa-user"></i>
<div class="min-w-0">
<?= $who['html'] ?>
<?php if ($user_id && $row['user_email'] !== null) { ?>
<div class="small text-body-secondary text-truncate"><?= escapeHtml($row['user_email']) ?><?= $who['kind'] === 'portal' ? ' - a client contact, in the portal' : '' ?></div>
<?php } elseif (!$user_id) { ?>
<div class="small text-body-secondary">Not a person - cron, the API or the email parser</div>
<?php } ?>
</div>
</div>
<div class="itflow-glance-item" title="Client">
<i class="fas fa-fw fa-building"></i>
<?php if ($row['client_name'] !== null) { ?>
<a class="text-truncate" href="/agent/client_overview.php?client_id=<?= intval($row['log_client_id']) ?>"><?= escapeHtml($row['client_name']) ?></a>
<?php } elseif (intval($row['log_client_id'])) { ?>
<span class="text-body-secondary">A client since deleted</span>
<?php } else { ?>
<span class="text-body-secondary">Not for a client</span>
<?php } ?>
</div>
<div class="itflow-glance-item" title="Entry">
<i class="fas fa-fw fa-hashtag"></i>
<span class="text-body-secondary">Entry <?= $log_id ?><?= $is_record ? ' &middot; ' . escapeHtml($type) . ' #' . $entity_id : '' ?></span>
</div>
</div>
<div class="col-sm-6">
<div class="itflow-glance-item" title="IP address">
<i class="fas fa-fw fa-network-wired"></i>
<?php if (trim((string) $row['log_ip']) !== '') { ?>
<span><span class="font-monospace"><?= escapeHtml($row['log_ip']) ?></span> <a class="small ms-1" href="/admin/audit_logs.php?q=<?= urlencode($row['log_ip']) ?>">Everything from it</a></span>
<?php } else { ?>
<span class="text-body-secondary">Not recorded</span>
<?php } ?>
</div>
<div class="itflow-glance-item" title="Device">
<i class="fas fa-fw fa-laptop"></i>
<span><?= $device !== '' ? escapeHtml($device) : '<span class="text-body-secondary">Not recorded</span>' ?></span>
</div>
</div>
</div>
<?php if (trim((string) $row['log_user_agent']) !== '') { ?>
<div class="small text-body-secondary font-monospace text-break mt-1 ps-4 ms-2" title="The browser's own user agent"><?= escapeHtml($row['log_user_agent']) ?></div>
<?php } ?>
<?php if ($is_record) { ?>
<div class="mt-4" data-audit-history>
<div class="d-flex align-items-center mb-2">
<div class="small fw-semibold text-uppercase text-body-secondary">This <?= escapeHtml(strtolower($type)) ?>'s other entries</div>
<?php if ($history_count > 0) { ?>
<a class="small ms-auto" href="/admin/audit_logs.php?type=<?= urlencode($type) ?>&amp;entity=<?= $entity_id ?>">All <?= $history_count + 1 ?> entries for it</a>
<?php } ?>
</div>
<?php if ($history) { ?>
<div class="list-group">
<?php foreach ($history as $entry) { echo $entry_line($entry); } ?>
</div>
<?php if ($history_count > count($history)) { ?>
<div class="small text-body-secondary mt-1">and <?= $history_count - count($history) ?> more</div>
<?php } ?>
<?php } else { ?>
<div class="small text-body-secondary">None - this is the only one.</div>
<?php } ?>
</div>
<?php } ?>
<?php if ($user_id > 0) { ?>
<div class="mt-4" data-audit-around>
<div class="d-flex align-items-center mb-2">
<div class="small fw-semibold text-uppercase text-body-secondary">What <?= escapeHtml($who['name']) ?> did around then</div>
<?php if ($around_count > 0) { ?>
<a class="small ms-auto" href="/admin/audit_logs.php?user=<?= $user_id ?>&amp;canned_date=custom&amp;dtf=<?= date('Y-m-d', $created) ?>&amp;dtt=<?= date('Y-m-d', $created) ?>">All of that day</a>
<?php } ?>
</div>
<?php if ($around) { ?>
<div class="list-group">
<?php foreach ($around as $entry) { echo $entry_line($entry, 'time'); } ?>
</div>
<?php if ($around_count > count($around)) { ?>
<div class="small text-body-secondary mt-1">and <?= $around_count - count($around) ?> more</div>
<?php } ?>
<?php } else { ?>
<div class="small text-body-secondary">Nothing else in the quarter hour either side.</div>
<?php } ?>
</div>
<?php } ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal"><i class="fa fa-times me-2"></i>Close</button>
</div>
<?php
require_once '../../../includes/modal_footer.php';

View File

@@ -0,0 +1,150 @@
<?php
/*
* Export the audit log to CSV or PDF - pre-filled from the list's filters and
* view, every one editable: the export runs what this form posts, not what
* the page happened to show (export_audit_logs, admin/post/audit_log.php).
*/
require_once '../../includes/modal_header.php';
$views = auditLogViews();
$q_filter = is_string($_GET['q'] ?? null) ? $_GET['q'] : '';
$view_filter = is_string($_GET['view'] ?? null) && isset($views[$_GET['view']]) ? $_GET['view'] : '';
$client_filter = is_scalar($_GET['client'] ?? null) ? intval($_GET['client']) : 0;
$user_filter = is_string($_GET['user'] ?? null) ? $_GET['user'] : '';
$type_filter = is_string($_GET['type'] ?? null) ? $_GET['type'] : '';
$action_filter = is_string($_GET['action'] ?? null) ? $_GET['action'] : '';
$entity_filter = is_scalar($_GET['entity'] ?? null) ? intval($_GET['entity']) : 0;
$date_from_filter = (is_string($_GET['dtf'] ?? null) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $_GET['dtf']) && $_GET['dtf'] !== '1970-01-01') ? $_GET['dtf'] : '';
$date_to_filter = (is_string($_GET['dtt'] ?? null) && preg_match('/^\d{4}-\d{2}-\d{2}$/', $_GET['dtt']) && $_GET['dtt'] !== '2099-12-31') ? $_GET['dtt'] : '';
ob_start();
?>
<div class="modal-header bg-dark">
<h5 class="modal-title"><i class="fas fa-fw fa-download me-2"></i>Export Audit Logs</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
</div>
<?php exportTabsNav(); ?>
<form action="post.php" method="post" autocomplete="off">
<input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>">
<?php if ($entity_filter) { ?><input type="hidden" name="entity" value="<?= $entity_filter ?>"><?php } ?>
<div class="modal-body">
<?php exportTabsFiltersOpen(); ?>
<?php if ($entity_filter) { ?>
<div class="alert alert-info small py-2"><i class="fas fa-fw fa-filter me-1"></i>Just one record: <?= escapeHtml($type_filter !== '' ? $type_filter : 'record') ?> #<?= $entity_filter ?></div>
<?php } ?>
<div class="mb-3">
<label class="form-label">Search</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-search"></i></span>
<input type="text" class="form-control" name="q" value="<?= escapeHtml($q_filter) ?>" placeholder="What was done, who, the client, the IP address or the browser">
</div>
</div>
<div class="mb-3">
<label class="form-label">Showing</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-layer-group"></i></span>
<select class="form-select select2" name="view">
<?php foreach ($views as $key => $definition) { ?>
<option value="<?= $key ?>" <?= $view_filter === $key ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas ' . $definition['icon'], 'description' => $definition['title']]) ?>><?= $key === '' ? 'Everything' : escapeHtml($definition['label']) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="row g-3 mb-3">
<div class="col-md-6">
<label class="form-label">Who</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-user"></i></span>
<select class="form-select select2" name="user">
<option value="">- Anyone -</option>
<option value="system" <?= $user_filter === 'system' ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas fa-cog']) ?>>ITFlow itself</option>
<?php
$sql = mysqli_query($mysqli, "SELECT user_id, user_name, user_avatar, user_type FROM users WHERE EXISTS (SELECT 1 FROM logs WHERE log_user_id = user_id) ORDER BY user_type ASC, user_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$option_id = intval($row['user_id']); ?>
<option value="<?= $option_id ?>" <?= $user_filter === (string) $option_id ? 'selected' : '' ?><?= selectOptionUser($row['user_id'], $row['user_name'], $row['user_avatar'] ?? '') ?><?= intval($row['user_type']) === 2 ? ' data-description="Portal"' : '' ?>><?= escapeHtml($row['user_name']) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<label class="form-label">Client</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-building"></i></span>
<select class="form-select select2" name="client">
<option value="">- Any client -</option>
<?php
$sql = mysqli_query($mysqli, "SELECT client_id, client_name, client_abbreviation FROM clients WHERE EXISTS (SELECT 1 FROM logs WHERE log_client_id = client_id) ORDER BY client_name ASC");
while ($row = mysqli_fetch_assoc($sql)) {
$option_id = intval($row['client_id']); ?>
<option value="<?= $option_id ?>" <?= $client_filter === $option_id ? 'selected' : '' ?><?= selectOptionClient($row['client_id'], $row['client_name'], $row['client_abbreviation'] ?? '') ?>><?= escapeHtml($row['client_name']) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<label class="form-label">Type</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-shapes"></i></span>
<select class="form-select select2" name="type">
<option value="">- Any type -</option>
<?php
$sql = mysqli_query($mysqli, "SELECT DISTINCT log_type FROM logs ORDER BY log_type ASC");
while ($row = mysqli_fetch_row($sql)) { ?>
<option value="<?= escapeHtml($row[0]) ?>" <?= $type_filter === $row[0] ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas ' . auditLogTypeIcon($row[0])]) ?>><?= escapeHtml($row[0]) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<label class="form-label">Action</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-bolt"></i></span>
<select class="form-select select2" name="action">
<option value="">- Any action -</option>
<?php
$sql = mysqli_query($mysqli, "SELECT DISTINCT log_action FROM logs ORDER BY log_action ASC");
while ($row = mysqli_fetch_row($sql)) { ?>
<option value="<?= escapeHtml($row[0]) ?>" <?= $action_filter === $row[0] ? 'selected' : '' ?><?= selectOptionData(['icon' => 'fas ' . auditLogActionStyle($row[0])['icon']]) ?>><?= escapeHtml($row[0]) ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="col-md-6">
<label class="form-label">From</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
<input type="date" class="form-control" name="dtf" value="<?= $date_from_filter ?>" max="2999-12-31">
</div>
</div>
<div class="col-md-6">
<label class="form-label">To</label>
<div class="input-group">
<span class="input-group-text"><i class="fa fa-fw fa-calendar"></i></span>
<input type="date" class="form-control" name="dtt" value="<?= $date_to_filter ?>" max="2999-12-31">
</div>
</div>
</div>
<?php exportTabsColumns('audit_logs'); ?>
</div>
<div class="modal-footer">
<?php renderExportButtons('export_audit_logs'); ?>
</div>
</form>
<?php
require_once '../../../includes/modal_footer.php';

71
admin/post/audit_log.php Normal file
View File

@@ -0,0 +1,71 @@
<?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();
}

View File

@@ -103,7 +103,7 @@ if (isset($_POST['edit_contract_template'])) {
");
// Log action
logAudit("Contract Template", "Update", "$session_name updated contract template $name", 0, $contract_template_id);
logAudit("Contract Template", "Edit", "$session_name updated contract template $name", 0, $contract_template_id);
// Flash + redirect
flashAlert("Contract Template <strong>$name</strong> updated");

View File

@@ -62,7 +62,7 @@ if (isset($_GET['delete_saved_payment'])) {
// SQL Cascade delete will Remove All Associated Auto Payment Methods on recurring invoices in the recurring payments table.
$payment_method_esc = escapeSql($payment_method);
logAudit("Payment Provider", "Update", "$session_name deleted saved payment method $saved_payment_description (PM: $payment_method_esc)", $client_id);
logAudit("Payment Provider", "Delete", "$session_name deleted saved payment method $saved_payment_description (PM: $payment_method_esc)", $client_id);
flashAlert("Payment method <strong>$saved_payment_description</strong> removed", 'error');

View File

@@ -39,7 +39,7 @@ if (isset($_GET['stripe_remove_pm'])) {
mysqli_query($mysqli, "DELETE FROM recurring_payments WHERE recurring_payment_method = 'Stripe' AND recurring_payment_recurring_invoice_id = $recurring_invoice_id");
}
logAudit("Stripe", "Update", "$session_name deleted saved Stripe payment method (PM: $payment_method)", $client_id);
logAudit("Stripe", "Delete", "$session_name deleted saved Stripe payment method (PM: $payment_method)", $client_id);
flashAlert("Payment method removed", 'error');

View File

@@ -78,7 +78,7 @@ if (isset($_GET['unarchive_sla'])) {
mysqli_query($mysqli, "UPDATE slas SET sla_archived_at = NULL WHERE sla_id = $sla_id");
logAudit("SLA", "Unarchive", "$session_name restored SLA ID $sla_id");
logAudit("SLA", "Restore", "$session_name restored SLA ID $sla_id");
flashAlert("SLA restored");

View File

@@ -163,7 +163,7 @@ if (isset($_POST['remove_demo_data'])) {
$clients_removed = demoDataRemove($mysqli);
if ($clients_removed) {
logAudit("Demo Data", "Deleted", "$session_name removed demo data - $clients_removed demo clients and all associated records");
logAudit("Demo Data", "Delete", "$session_name removed demo data - $clients_removed demo clients and all associated records");
flashAlert("Removed <strong>$clients_removed</strong> demo clients and everything associated with them", 'error');
} else {
flashAlert("No demo clients found - nothing to remove", 'info');