summaryrefslogblamecommitdiffstats
path: root/modules-available/dnbd3/baseconfig/getconfig.inc.php
blob: ae4d29243799c09f042baa821f8f89cac04ae636 (plain) (tree)













































                                                                                                           
<?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)));