blob: ae4d29243799c09f042baa821f8f89cac04ae636 (
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
|
<?php
if (!Dnbd3::isEnabled()) return;
// Locations from closest to furthest (order)
$locations = ConfigHolder::get('SLX_LOCATIONS');
if ($locations === false) {
$locationIds = [0];
} else {
$locationIds = explode(' ', $locations);
if (empty($locationIds)) {
$locationIds[] = 0;
}
}
$res = Database::simpleQuery('SELECT s.fixedip, m.clientip, sxl.locationid FROM dnbd3_server s
LEFT JOIN machine m USING (machineuuid)
LEFT JOIN dnbd3_server_x_location sxl USING (serverid)
WHERE sxl.locationid IS NULL OR sxl.locationid IN (:lids)', array('lids' => $locationIds));
// Lookup of priority - first index (0) will be closest location in chain
$locationsAssoc = array_flip($locationIds);
$servers = array();
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
if ($row['fixedip'] === '<self>') {
$row['fixedip'] = Property::getServerIp();
$defPrio = 2000;
} else {
$defPrio = 1000;
}
$ip = $row['clientip'] ? $row['clientip'] : $row['fixedip'];
if ($defPrio === 1000 && is_null($row['locationid'])) {
$serverLoc = Location::getFromIp($ip);
if ($serverLoc !== false) {
$row['locationid'] = $serverLoc;
}
}
$old = isset($servers[$ip]) ? $servers[$ip] : $defPrio;
if (is_null($row['locationid']) || !isset($locationsAssoc[$row['locationid']])) {
$servers[$ip] = min($defPrio, $old);
} else {
$servers[$ip] = min($locationsAssoc[$row['locationid']], $old);
}
}
asort($servers, SORT_NUMERIC | SORT_ASC);
ConfigHolder::add('SLX_DNBD3_SERVERS', implode(' ', array_keys($servers)));
|