summaryrefslogtreecommitdiffstats
path: root/modules-available/locations/inc/autolocation.inc.php
blob: cccfe9b1b17fb75f70e5ec2f38bcec996c0f1325 (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
<?php

class AutoLocation
{

	public static function rebuildAll()
	{
		if (Module::get('statistics') === false)
			return; // Nothing to do
		$res = Database::simpleQuery("SELECT machineuuid, clientip FROM machine");
		$updates = array();
		$nulls = array();
		while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
			$loc = Location::mapIpToLocation($row['clientip']);
			if ($loc === false) {
				$nulls[] = $row['machineuuid'];
			} else {
				if (!isset($updates[$loc])) {
					$updates[$loc] = array();
				}
				$updates[$loc][] = $row['machineuuid'];
			}
		}
		if (!empty($nulls)) {
			$qs = '?' . str_repeat(',?', count($nulls) - 1);
			Database::exec("UPDATE machine SET subnetlocationid = NULL WHERE machineuuid IN ($qs)", $nulls);
		}
		foreach ($updates as $lid => $machines) {
			$qs = '?' . str_repeat(',?', count($machines) - 1);
			$lid = (int)$lid;
			Database::exec("UPDATE machine SET subnetlocationid = $lid WHERE machineuuid IN ($qs)", $machines);
		}
	}

}