summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics/pages/replace.inc.php
blob: 50bfd6cf601c318900beb933abc09f3a91b5acf5 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

class SubPage
{

	public static function doPreprocess()
	{
		User::assertPermission('replace');
		$action = Request::post('action', false, 'string');
		if ($action === 'replace') {
			self::handleReplace();
		}
		if (Request::isPost()) {
			Util::redirect('?do=statistics&show=replace');
		}
	}

	private static function handleReplace()
	{
		$replace = Request::post('replace', false, 'array');
		if (empty($replace)) {
			Message::addError('main.parameter-empty', 'replace');
			return;
		}
		$list = [];
		$allowed = User::getAllowedLocations('replace');
		// Loop through passed machines, filter out unsuited pairs (both in use) and those without permission
		foreach ($replace as $p) {
			$split = explode('x', $p);
			if (count($split) !== 2) {
				Message::addError('invalid-replace-format', $p);
				continue;
			}
			$entry = ['old' => $split[0], 'new' => $split[1]];
			$old = Database::queryFirst('SELECT locationid, lastseen FROM machine WHERE machineuuid = :old',
				['old' => $entry['old']]);
			if ($old === false) {
				Message::addError('unknown-machine', $entry['old']);
				continue;
			}
			$new = Database::queryFirst('SELECT locationid, firstseen FROM machine WHERE machineuuid = :new',
				['new' => $entry['new']]);
			if ($new === false) {
				Message::addError('unknown-machine', $entry['new']);
				continue;
			}
			if ($old['lastseen'] - 86400*7 > $new['firstseen']) {
				Message::addWarning('ignored-both-in-use', $entry['old'], $entry['new']);
				continue;
			}
			if (!in_array(0, $allowed)) {
				if (!in_array($old['locationid'], $allowed)) {
					Message::addWarning('ignored-no-permission', $entry['old']);
					continue;
				}
				if (!in_array($new['locationid'], $allowed)) {
					Message::addWarning('ignored-no-permission', $entry['new']);
					continue;
				}
			}
			$entry['datelimit'] = min($new['firstseen'], $old['lastseen']);
			$list[] = $entry;
		}
		if (empty($list)) {
			Message::addError('main.parameter-empty', 'replace');
			return;
		}

		// First handle module internal tables
		foreach ($list as $entry) {
			Database::exec('UPDATE statistic SET machineuuid = :new WHERE machineuuid = :old AND dateline < :datelimit', $entry);
		}

		// Let other modules do their thing
		$fun = function($file, $list) {
			include $file;
		};
		foreach (Hook::load('statistics/machine-replace') as $hook) {
			$fun($hook->file, $list);
		}

		// Finalize by updating machine table
		foreach ($list as $entry) {
			unset($entry['datelimit']);
			Database::exec("UPDATE machine old, machine new SET
				new.fixedlocationid = old.fixedlocationid,
				new.position = old.position,
				old.position = NULL,
				old.subnetlocationid = NULL,
				old.fixedlocationid = NULL,
				new.notes = old.notes,
				old.notes = NULL,
				old.lastseen = new.firstseen,
				old.clientip = '0.0.0.0'
				WHERE old.machineuuid = :old AND new.machineuuid = :new", $entry);
		}
		Message::addSuccess('x-machines-replaced', count($list));
	}

	public static function doRender()
	{
		self::listSuggestions();
	}

	private static function listSuggestions()
	{
		if (Request::get('debug', false) !== false) {
			$oldCutoff = time() - 86400 * 180;
			$newCutoff = time() - 86400 * 180;
		} else {
			$oldCutoff = time() - 86400 * 90;
			$newCutoff = time() - 86400 * 30;
		}
		$res = Database::simpleQuery("SELECT
				old.machineuuid AS olduuid, old.locationid AS oldlid, old.hostname AS oldhost,
				old.clientip AS oldip, old.macaddr AS oldmac, old.lastseen AS oldlastseen, old.systemmodel AS oldmodel,
				new.machineuuid AS newuuid, new.locationid AS newlid, new.hostname AS newhost,
				new.clientip AS newip, new.macaddr AS newmac, new.firstseen AS newfirstseen, new.systemmodel AS newmodel
				FROM machine old INNER JOIN machine new ON (old.clientip = new.clientip AND old.lastseen < new.firstseen AND old.lastseen > $oldCutoff AND new.firstseen > $newCutoff)
				ORDER BY oldhost ASC, oldip ASC");
		$list = [];
		$allowed = User::getAllowedLocations('replace');
		foreach ($res as $row) {
			if (!in_array(0, $allowed) && (!in_array($row['oldlid'], $allowed) || !in_array($row['newlid'], $allowed)))
				continue;
			$row['oldlastseen_s'] = Util::prettyTime($row['oldlastseen']);
			$row['newfirstseen_s'] = Util::prettyTime($row['newfirstseen']);
			$list[] = $row;
		}
		$data = array('pairs' => $list);
		Render::addTemplate('page-replace', $data);
		if (empty($list)) {
			Message::addInfo('no-replacement-matches');
		}
	}

}