summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics/inc/statistics.inc.php
blob: 2500f16f03275a7aed642d557d7cec1cf46b9dcb (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php



class Statistics
{

	private static $machineFields = false;

	private static function initFields($returnData)
	{
		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 {
			Util::traceError('Invalid $returnData option passed');
		}
		return implode(',', array_keys(self::$machineFields));
	}

	/**
	 * @param string $machineuuid
	 * @param int $returnData What kind of data to return Machine::NO_DATA, Machine::RAW_DATA, ...
	 * @return \Machine|false
	 */
	public static function getMachine($machineuuid, $returnData)
	{
		$fields = self::initFields($returnData);

		$row = Database::queryFirst("SELECT $fields FROM machine WHERE machineuuid = :machineuuid", compact('machineuuid'));
		if ($row === false)
			return false;
		$m = new Machine();
		foreach ($row as $key => $val) {
			$m->{$key} = $val;
		}
		return $m;
	}

	/**
	 * @param string $ip
	 * @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($ip, $returnData, $sort = false)
	{
		$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();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$m = new Machine();
			foreach ($row as $key => $val) {
				$m->{$key} = $val;
			}
			$list[] = $m;
		}
		return $list;
	}

}