summaryrefslogtreecommitdiffstats
path: root/modules-available/syslog/inc/clientlog.inc.php
blob: 5b34f87ee884f6b9ba01c49f462be4fe28aa3976 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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'];
		}
		if (mb_strlen($description) > 255) {
			$description = mb_substr($description, 0, 255);
		}
		if (mb_strlen($longDesc) > 65535) {
			$longDesc = mb_substr($longDesc, 0, 65535);
		}
		if (mb_strlen($type) > 30) {
			$type = mb_substr($type, 0, 30);
		}
		$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;
	}

}