From fba4605715e5a068e9398ad4660eb87327fe2f15 Mon Sep 17 00:00:00 2001 From: Alex Whitman Date: Sat, 25 Apr 2020 09:25:29 +0100 Subject: [PATCH] Fix ranges including CIDR notation Previous code checked for '/' first, so a range such as 127.0.0.0/24-127.0.3.0/24 would be parsed as a network which would then split on '/' to determine the IP and netmask, eventually causing an invalid IP to be parsed, throwing an error. --- src/Range.php | 16 +++++++++++----- tests/RangeTest.php | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Range.php b/src/Range.php index 08919fa..96664ab 100644 --- a/src/Range.php +++ b/src/Range.php @@ -39,17 +39,23 @@ public function __construct(IP $firstIP, IP $lastIP) */ public static function parse($data) { - if (strpos($data,'/') || strpos($data,' ')) { + if (strpos($data, '-') || strpos($data, ' ')) { + $delimeter = '-'; + if (strpos($data, ' ')) { + $delimeter = ' '; + } + list($first, $last) = explode($delimeter, $data, 2); + $firstNetwork = Network::parse($first); + $lastNetwork = Network::parse($last); + $firstIP = $firstNetwork->getFirstIP(); + $lastIP = $lastNetwork->getLastIP(); + } elseif (strpos($data,'/') || strpos($data,' ')) { $network = Network::parse($data); $firstIP = $network->getFirstIP(); $lastIP = $network->getLastIP(); } elseif (strpos($data, '*') !== false) { $firstIP = IP::parse(str_replace('*', '0', $data)); $lastIP = IP::parse(str_replace('*', '255', $data)); - } elseif (strpos($data, '-')) { - list($first, $last) = explode('-', $data, 2); - $firstIP = IP::parse($first); - $lastIP = IP::parse($last); } else { $firstIP = IP::parse($data); $lastIP = clone $firstIP; diff --git a/tests/RangeTest.php b/tests/RangeTest.php index 4f81f8a..165286c 100644 --- a/tests/RangeTest.php +++ b/tests/RangeTest.php @@ -66,6 +66,8 @@ public function getTestParseData() array('127.0.0.1/24', array('127.0.0.0', '127.0.0.255')), array('127.*.0.0', array('127.0.0.0', '127.255.0.0')), array('127.255.255.0', array('127.255.255.0', '127.255.255.0')), + array('127.0.0.0/24-127.0.3.0/24', array('127.0.0.0', '127.0.3.255')), + array('127.0.0.0/24 127.0.3.0/24', array('127.0.0.0', '127.0.3.255')), ); }