diff options
Diffstat (limited to 'modules-available/syslog/inc/clientlog.inc.php')
-rw-r--r-- | modules-available/syslog/inc/clientlog.inc.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/modules-available/syslog/inc/clientlog.inc.php b/modules-available/syslog/inc/clientlog.inc.php new file mode 100644 index 00000000..b38c29fe --- /dev/null +++ b/modules-available/syslog/inc/clientlog.inc.php @@ -0,0 +1,47 @@ +<?php + +class ClientLog +{ + + public static function write(array $client, string $type, string $description, string $longDesc = ''): bool + { + if (!isset($client['machineuuid']) && !isset($client['clientip'])) { + error_log("Bad clientlog write call: " . json_encode($client)); + return false; + } + if (!isset($client['machineuuid'])) { + $res = Database::queryFirst("SELECT machineuuid FROM machine WHERE clientip = :ip + ORDER BY lastseen DESC LIMIT 1", ['ip' => $client['clientip']]); + if ($res === false) { + error_log("Invalid client IP for client log: " . $client['clientip']); + return false; + } + $client['machineuuid'] = $res['machineuuid']; + } + if (!isset($client['clientip'])) { + $res = Database::queryFirst("SELECT clientip FROM machine WHERE machineuuid = :uuid", + ['uuid' => $client['machineuuid']]); + if ($res === false) { + error_log("Invalid machine uuid for client log: " . $client['machineuuid']); + return false; + } + $client['clientip'] = $res['clientip']; + } + $data = [ + 'type' => $type, + 'clientip' => $client['clientip'], + 'description' => $description, + 'extra' => $longDesc, + 'machineuuid' => $client['machineuuid'], + ]; + $res = Database::exec('INSERT INTO clientlog (dateline, logtypeid, clientip, machineuuid, description, extra) + VALUES (UNIX_TIMESTAMP(), :type, :clientip, :machineuuid, :description, :extra)', $data, true); + if ($res === false) { + error_log("Constraint failed for client log from {$client['machineuuid']} for $type : $description"); + return false; + } + EventLog::applyFilterRules($type, $data + $client); + return true; + } + +}
\ No newline at end of file |