diff options
Diffstat (limited to 'modules-available/dnbd3/inc/dnbd3util.inc.php')
-rw-r--r-- | modules-available/dnbd3/inc/dnbd3util.inc.php | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/modules-available/dnbd3/inc/dnbd3util.inc.php b/modules-available/dnbd3/inc/dnbd3util.inc.php index 8e355370..314c44fe 100644 --- a/modules-available/dnbd3/inc/dnbd3util.inc.php +++ b/modules-available/dnbd3/inc/dnbd3util.inc.php @@ -12,7 +12,7 @@ class Dnbd3Util { $res = Database::simpleQuery('SELECT s.serverid, s.machineuuid, s.fixedip, s.lastup, s.lastdown, m.clientip FROM dnbd3_server s LEFT JOIN machine m USING (machineuuid)'); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { if (!empty($row['machineuuid'])) { $allUuids[$row['machineuuid']] = true; } @@ -64,12 +64,12 @@ class Dnbd3Util { // Now query them all $NOW = time(); foreach ($servers as $server) { - $data = Dnbd3Rpc::query($server['addr'], true, false, false, true); - if ($data === Dnbd3Rpc::QUERY_UNREACHABLE) { + $data = Dnbd3Rpc::query($server['addr'], [Dnbd3Rpc::QUERY_STATS, Dnbd3Rpc::QUERY_SPACE]); + if ($data === Dnbd3Rpc::ERROR_UNREACHABLE) { $error = 'No (HTTP) reply from ' . $server['addr']; - } elseif ($data === Dnbd3Rpc::QUERY_NOT_200) { + } elseif ($data === Dnbd3Rpc::ERROR_NOT_200) { $error = 'No HTTP 200 OK from ' . $server['addr']; - } elseif ($data === Dnbd3Rpc::QUERY_NOT_JSON) { + } elseif ($data === Dnbd3Rpc::ERROR_NOT_JSON) { $error = 'Reply to status query is not JSON'; } elseif (!is_array($data) || !isset($data['runId'])) { if (is_array($data) && isset($data['errorMsg'])) { @@ -108,11 +108,9 @@ class Dnbd3Util { /** * A client is booting that has runmode dnbd3 proxy - set config vars accordingly. * - * @param string $machineUuid * @param string $mode always 'proxy' - * @param string $modeData */ - public static function runmodeConfigHook($machineUuid, $mode, $modeData) + public static function runmodeConfigHook(string $machineUuid, string $mode, ?string $modeData) { $self = Property::getServerIp(); // Get all directly assigned locations @@ -121,11 +119,11 @@ class Dnbd3Util { WHERE machineuuid = :uuid', array('uuid' => $machineUuid)); $assignedLocs = array(); - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + foreach ($res as $row) { $assignedLocs[] = $row['locationid']; } - $modeData = (array)json_decode($modeData, true) + self::defaultRunmodeConfig(); - if (!empty($assignedLocs) && isset($modeData['firewall']) && $modeData['firewall']) { + $modeData = (array)json_decode($modeData ?? '{}', true) + self::defaultRunmodeConfig(); + if (!empty($assignedLocs) && ($modeData['firewall'] ?? false)) { // Get all sub-locations too $recursiveLocs = $assignedLocs; $locations = Location::getLocationsAssoc(); @@ -138,13 +136,10 @@ class Dnbd3Util { array('locs' => array_values($recursiveLocs))); // Coalesce overlapping ranges $floatIp = ip2long($self); // Float for 32bit php :/ - if (PHP_INT_SIZE === 4) { - $floatIp = (float)sprintf('%u', $floatIp); // Float for 32bit php :/ - } $ranges = [['startaddr' => $floatIp, 'endaddr' => $floatIp]]; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - settype($row['startaddr'], PHP_INT_SIZE === 4 ? 'float' : 'int'); - settype($row['endaddr'], PHP_INT_SIZE === 4 ? 'float' : 'int'); + foreach ($res as $row) { + settype($row['startaddr'], 'int'); + settype($row['endaddr'], 'int'); self::mergeRanges($ranges, $row); } // Got subnets, build whitelist @@ -164,8 +159,8 @@ class Dnbd3Util { $public = array(); $private = array(); $public[$self] = $self; - while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $ip = $row['fixedip'] ? $row['fixedip'] : $row['clientip']; + foreach ($res as $row) { + $ip = $row['fixedip'] ?: $row['clientip']; if ($ip === '<self>') { continue; } @@ -215,13 +210,9 @@ class Dnbd3Util { * @param int $end end address * @return string CIDR notation */ - private static function range2Cidr($start, $end) + private static function range2Cidr(int $start, int $end): string { - if (PHP_INT_SIZE > 4) { - $bin = decbin((int)$start ^ (int)$end); - } else { - $bin = decbin((int)(float)$start ^ (int)(float)$end); - } + $bin = decbin($start ^ $end); if ($bin === '0') return long2ip($start); $mask = 32 - strlen($bin); @@ -252,18 +243,21 @@ class Dnbd3Util { // $row['startaddr'] must lie before range start, otherwise we'd have hit the case above $row['endaddr'] = $ranges[$key]['endaddr']; unset($ranges[$key]); - continue; + //continue; } } $ranges[] = $row; } - public static function defaultRunmodeConfig() + /** + * @return array{bgr: bool, firewall: bool} + */ + public static function defaultRunmodeConfig(): array { - return array( + return [ 'bgr' => true, 'firewall' => false - ); + ]; } public static function matchAddress($server) |