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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
<?php
class Statistics
{
private static $machineFields = false;
private static function initFields(int $returnData): string
{
if (self::$machineFields === false) {
$r = new ReflectionClass('Machine');
$props = $r->getProperties(ReflectionProperty::IS_PUBLIC);
self::$machineFields = array_flip(array_map(function(/* @var ReflectionProperty $e */ $e) { return $e->getName(); }, $props));
}
if ($returnData === Machine::NO_DATA) {
unset(self::$machineFields['data']);
} elseif ($returnData === Machine::RAW_DATA) {
self::$machineFields['data'] = true;
} else {
ErrorHandler::traceError('Invalid $returnData option passed');
}
return implode(',', array_keys(self::$machineFields));
}
/**
* @param int $returnData What kind of data to return Machine::NO_DATA, Machine::RAW_DATA, ...
*/
public static function getMachine(string $machineuuid, int $returnData): ?Machine
{
$fields = self::initFields($returnData);
$row = Database::queryFirst("SELECT $fields FROM machine WHERE machineuuid = :machineuuid", compact('machineuuid'));
if ($row === false)
return null;
$m = new Machine();
foreach ($row as $key => $val) {
$m->{$key} = $val;
}
return $m;
}
/**
* @param int $returnData What kind of data to return Machine::NO_DATA, Machine::RAW_DATA, ...
* @param string $sort something like 'lastseen ASC' - not sanitized, don't pass user input!
* @return Machine[] list of matches
*/
public static function getMachinesByIp(string $ip, int $returnData, bool $sort = false): array
{
$fields = self::initFields($returnData);
if ($sort === false) {
$sort = '';
} else {
$sort = "ORDER BY $sort";
}
$res = Database::simpleQuery("SELECT $fields FROM machine WHERE clientip = :ip $sort", compact('ip'));
$list = array();
foreach ($res as $row) {
$m = new Machine();
foreach ($row as $key => $val) {
$m->{$key} = $val;
}
$list[] = $m;
}
return $list;
}
const SESSION_LENGTH = '~session-length';
const OFFLINE_LENGTH = '~offline-length';
const SUSPEND_LENGTH = '~suspend-length';
public static function logMachineState(string $uuid, string $ip, string $type, int $start, int $length, string $username = ''): int
{
return Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
. " VALUES (:start, :type, :uuid, :clientip, :username, :length)", array(
'start' => $start,
'type' => $type,
'uuid' => $uuid,
'clientip' => $ip,
'username' => $username,
'length' => $length,
));
}
}
|