private function IpAddress::parse in IP address fields 2.0.x
Same name and namespace in other branches
- 8 src/IpAddress.php \Drupal\field_ipaddress\IpAddress::parse()
Find if the value given is an IP, IP range, or other.
1 call to IpAddress::parse()
- IpAddress::__construct in src/
IpAddress.php - On construction, parse the given value.
File
- src/
IpAddress.php, line 144
Class
- IpAddress
- IpTools class.
Namespace
Drupal\field_ipaddressCode
private function parse($value) {
$value = trim(str_replace(' ', '', $value));
// Check that we have zero or one range delimiter.
$this
->checkSingleDelimiter($value);
// Check if this is a simple range.
if (strpos($value, '-') !== FALSE) {
// Break its parts apart.
list($start, $end) = explode('-', $value, 2);
if (!$this
->isIpAddress($start)) {
// Return false on failure.
return FALSE;
}
if ($this
->isIpV4($start)) {
$this->family = self::IP_FAMILY_4;
// End IP may be partial
if (strpos($end, '.') === FALSE) {
// Break $start into octets, and replace the last one.
$end_parts = explode('.', $start);
if (count($end_parts) != 4) {
throw new \Exception('Invalid IP range format.');
}
$end_parts[3] = $end;
$end = implode('.', $end_parts);
if (!$this
->isIpAddress($end)) {
throw new \Exception('Invalid IP range format.');
}
}
else {
// Should be a simple IP for the end as well.
if (!$this
->isIpAddress($end)) {
throw new \Exception('Invalid end of range IP.');
}
}
}
else {
$this->family = self::IP_FAMILY_6;
if (!$this
->isIpAddress($end)) {
throw new \Exception('Invalid end of range IP.');
}
}
if ($this
->getFamily($start) !== $this
->getFamily($end)) {
throw new \Exception('Mismatching IP families in range.');
}
// Simple is ... simple, assign the bounds.
$this->start = $start;
$this->end = $end;
$this->type = self::IP_RANGE_SIMPLE;
}
elseif (strpos($value, '*') !== FALSE) {
list($prefix, $suffix) = explode('*', $value, 2);
// Replace wildcard with lower end, which is 0 in both IPv4 and IPv6.
$start = $prefix . '0' . $suffix;
// And check this is a valid IP.
if (!$this
->isIpAddress($start)) {
throw new \Exception('Invalid IP address.');
}
if ($this
->isIpV4($start)) {
$this->family = self::IP_FAMILY_4;
$end = $prefix . '255' . $suffix;
}
if ($this
->isIpV6($start)) {
$this->family = self::IP_FAMILY_6;
$end = $prefix . 'ff' . $suffix;
}
if (!$this
->isIpAddress($end)) {
throw new \Exception('Invalid IP address.');
}
$this->start = $start;
$this->end = $end;
$this->type = self::IP_RANGE_SIMPLE;
}
elseif (strpos($value, '/') !== FALSE) {
// Break its parts apart.
list($ip, $prefix) = explode('/', $value, 2);
if (!$this
->isIpAddress($ip) || !is_numeric($prefix) || $prefix <= 0) {
return FALSE;
}
$this->family = $this
->getFamily($ip);
// Check that the prefix is not larger than address.
if ($this->family == self::IP_FAMILY_4 && $prefix > 32 || $this->family == self::IP_FAMILY_6 && $prefix > 128) {
return FALSE;
}
$this->type = self::IP_RANGE_CIDR;
// Calculate CIDR address bounds.
if ($this->family == self::IP_FAMILY_4) {
$this
->calcCidr4($ip, $prefix);
}
else {
$this
->calcCidr6($ip, $prefix);
}
}
elseif ($this
->isIpAddress($value)) {
$this->type = self::IP_RANGE_NONE;
$this->family = $this
->getFamily($value);
$this->start = $value;
$this->end = $value;
}
else {
return FALSE;
}
}