2018-07-17 19:17:57 +02:00
|
|
|
<?php
|
|
|
|
use Misuzu\Database;
|
|
|
|
use Misuzu\Net\IPAddress;
|
|
|
|
|
|
|
|
function audit_log(
|
|
|
|
string $action,
|
|
|
|
int $userId = 0,
|
|
|
|
array $params = [],
|
|
|
|
IPAddress $ipAddress = null
|
|
|
|
): void {
|
|
|
|
$ipAddress = $ipAddress ?? IPAddress::remote();
|
|
|
|
|
|
|
|
for ($i = 0; $i < count($params); $i++) {
|
2018-07-23 15:29:57 +02:00
|
|
|
if (preg_match('#^(-?[0-9]+)$#', $params[$i])) {
|
2018-07-17 19:17:57 +02:00
|
|
|
$params[$i] = (int)$params[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$addLog = Database::prepare('
|
|
|
|
INSERT INTO `msz_audit_log`
|
2018-07-23 15:29:57 +02:00
|
|
|
(`log_action`, `user_id`, `log_params`, `log_ip`, `log_country`)
|
2018-07-17 19:17:57 +02:00
|
|
|
VALUES
|
2018-07-23 15:29:57 +02:00
|
|
|
(:action, :user, :params, :ip, :country)
|
2018-07-17 19:17:57 +02:00
|
|
|
');
|
|
|
|
$addLog->bindValue('action', $action);
|
|
|
|
$addLog->bindValue('user', $userId < 1 ? null : $userId);
|
|
|
|
$addLog->bindValue('params', json_encode($params));
|
2018-07-23 15:29:57 +02:00
|
|
|
$addLog->bindValue('ip', $ipAddress->getRaw());
|
|
|
|
$addLog->bindValue('country', $ipAddress->getCountryCode());
|
2018-07-17 19:17:57 +02:00
|
|
|
$addLog->execute();
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:29:57 +02:00
|
|
|
function audit_log_count($userId = 0): int
|
|
|
|
{
|
|
|
|
$getCount = Database::prepare(sprintf('
|
|
|
|
SELECT COUNT(`log_id`)
|
|
|
|
FROM `msz_audit_log`
|
|
|
|
WHERE %s
|
|
|
|
', $userId < 1 ? '1' : '`user_id` = :user_id'));
|
|
|
|
|
|
|
|
if ($userId >= 1) {
|
|
|
|
$getCount->bindValue('user_id', $userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $getCount->execute() ? (int)$getCount->fetchColumn() : 0;
|
|
|
|
}
|
|
|
|
|
2018-07-17 19:17:57 +02:00
|
|
|
function audit_log_list(int $offset, int $take, int $userId = 0): array
|
|
|
|
{
|
|
|
|
$offset = max(0, $offset);
|
|
|
|
$take = max(1, $take);
|
|
|
|
|
|
|
|
$getLogs = Database::prepare(sprintf('
|
|
|
|
SELECT
|
2018-07-23 15:29:57 +02:00
|
|
|
l.`log_id`, l.`log_action`, l.`log_params`, l.`log_created`, l.`log_country`,
|
2018-07-17 19:17:57 +02:00
|
|
|
u.`user_id`, u.`username`,
|
|
|
|
INET6_NTOA(l.`log_ip`) as `log_ip`,
|
2018-07-18 18:01:17 +02:00
|
|
|
COALESCE(u.`user_colour`, r.`role_colour`) as `user_colour`
|
2018-07-17 19:17:57 +02:00
|
|
|
FROM `msz_audit_log` as l
|
|
|
|
LEFT JOIN `msz_users` as u
|
|
|
|
ON u.`user_id` = l.`user_id`
|
|
|
|
LEFT JOIN `msz_roles` as r
|
|
|
|
ON r.`role_id` = u.`display_role`
|
|
|
|
WHERE %s
|
|
|
|
ORDER BY l.`log_id` DESC
|
|
|
|
LIMIT :offset, :take
|
|
|
|
', $userId < 1 ? '1' : 'l.`user_id` = :user_id'));
|
|
|
|
|
|
|
|
if ($userId >= 1) {
|
2018-07-23 15:29:57 +02:00
|
|
|
$getLogs->bindValue('user_id', $userId);
|
2018-07-17 19:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$getLogs->bindValue('offset', $offset);
|
|
|
|
$getLogs->bindValue('take', $take);
|
|
|
|
|
|
|
|
return $getLogs->execute() ? $getLogs->fetchAll(PDO::FETCH_ASSOC) : [];
|
|
|
|
}
|