Optimized the canPass() functions

This commit is contained in:
rodehoed 2021-05-19 08:47:35 +02:00
parent 502bb5fa15
commit 5812a6bb68

View file

@ -120,24 +120,18 @@ class TrafficLimiter extends AbstractPersistence
// Match $_ipKey to $ipRange and if it matches it will return with a true // Match $_ipKey to $ipRange and if it matches it will return with a true
$address = \IPLib\Factory::addressFromString($_SERVER[self::$_ipKey]); $address = \IPLib\Factory::addressFromString($_SERVER[self::$_ipKey]);
$range = \IPLib\Factory::rangeFromString(trim($ipRange)); $range = \IPLib\Factory::rangeFromString(trim($ipRange));
// If $range is null something went wrong (possible invalid ip given in config)
if ($range == null) { // If $range is null something went wrong (possible invalid ip given in config). It's here becaue matches($range) does not accepts null vallue
return false; if ($range == null) return false;
} else {
// Ip-lib does throws and exception when something goes wrong, if so we want to catch it and set contained to false // Ip-lib does throws and exception when something goes wrong, if so we want to catch it and set contained to false
try { try {
$contained = $address->matches($range); return $address->matches($range);
} catch (\Exception $e) { } catch (\Exception $e) {
// If something is wrong with matching the ip, we set $contained to false // If something is wrong with matching the ip, we do nothing
return false;
}
}
// Matches return true!
if ($contained === true) {
return true;
} else {
return false;
} }
return false;
} }
/** /**