The IP addresses in IPv4 and IPv6 are complex to manage. In cause, the differents allowed formats.
The module allow to uncompress an IPv6 address (::1 is converted to 0000:0000:0000:0000:0000:0000:0000:0001), compress the IPv6 address (1:0:0::1 is converted to 1::1), reverse an IP address (to use in DNS server in Reverse zones), calculate a CIDR associated to a IPv4 netmask.
It is also capable to determine if an IP is in or outside a network, based on the network address and the CIDR, which is the base of the routing softwares.
It allow to calculate the first and last address of a network, based on an IP and a CIDR. In IPv4, the first address is the network address and the last address is the broadcast address.
Namespace Domframework
/** Manage the IP addresses conversions
No property available
/** This function return the netmask associated to a CIDR. Work only on IPv4 addresses (CIDR between 0 and 32)@param string $cidr
The CIDR to convert in netmask@param boolean|null $maskdirect
If true return a direct mask, if false return a wildcard mask@return
the mask@return
false if the CIDR is not between 0 and 32
/** Return the provided CIDR in binary. Length must be in bytes. Return FALSE if the parameters are invalid@param integer $cidr
The CIDR to convert@param integer $length
The length to use
/** Return the IP adddress with filling the fields with the missing zeros. Valid only on IPv6 (but don't change anything if the provided address is IPv4)@param string $ip
The IP to complete@return
string The address in nibbles Example : $ip = "::", return "0000:0000:0000:0000:0000:0000:0000:0000" Example : $ip = "::ffff:127.0.0.1", return "0000:0000:0000:0000:0000:0000:ffff:7f00:0001"
/**
Return the IPv6 to compressed (or compact) form.
Remove the 0 when they are placed on the begin of the nibble.
Remove all the blocks only zero to convert them to :: (but only one time)
Example: 2001:0660:530d:0201:0000:0000:0000:0124 => 2001:660:530d:201::124
If an IPv4 is provided, return it without modification
@param string $ip
The IP to compress
/**
Get an IPv6 address with the format
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x
and return it with format
xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx
Return false if the IP provided is not complete
@param string $ipv6
The IPv6 to group
/** This function return true if the provided address is in the provided network with the associated cidr@param string $ip
The IPv4 or IPv6 to test@param string $network
The IPv4 or IPv6 network base@param string $cidr
The CIDR to apply@return
boolean True if $ip is in $network/$cidr
/** This function return the CIDR associated to the provided netmask Ex. Return 24 for a mask 255.255.255.0 in direct Ex. Return 24 for a mask 0.0.0.255 in wildcard Work only in IPv4 Return FALSE if the provided mask is invalid (155.0.0.0 by example) Throw an exception if the provided IP is invalid@param string $netmask
The mask to convert in CIDR@param boolean|null $maskdirect
If true check a direct mask, if false check a wildcard mask
/** Get the first IP of a network. IPv4 and IPv6 compatible@param string $ip
The IPv4 or IPv6 in the network@param string $cidr
The CIDR to apply@return
string the network base Example : $ip="192.168.1.2", $cidr=24 => return "192.168.1.0"
/** Get the last IP of a network. IPv4 and IPv6 compatible@param string $ip
The IPv4 or IPv6 in the network@param string $cidr
The CIDR to apply@return
string the network base Example : $ip="192.168.1.2", $cidr=24 => return "192.168.1.255"
/**
Reverse the provided IP address
The IPv6 are returned in format :
x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x
@param string $ipReverse
The IPv6 to reverse
/** Base conversion with 128 bits support for IPv6 Based on http://fr2.php.net/manual/en/function.base-convert.php#109660@param string $str
The string to convert@param integer|null $frombase
The base of the provided string (10 by default)@param integer|null $tobase
The base of the returned string (36 by default)
/**
Return the IPv6 uncompressed (all the fields exists). They are not filled
by zeros
If the provided IP is IPv4, there is no change applied
Return False if the parameter is invalid
Based on http://www.weberdev.com/get_example.php3?ExampleID=3921
@param string $ip
The IP address to uncompress
Example : $ip="::" => return "0:0:0:0:0:0:0:0"
Example : $ip = "::ffff:127.0.0.1",
return "0:0:0:0:0:0:ffff:7f00:1"
/** Return true if the provided CIDR is valid. The CIDR can be valid in IPv4 or IPv6@param integer $cidr
The CIDR to test@return
boolean The CIDR is valid
/**
Return true if the provided IP address is valid (IPv4 or IPv6)
@param string $ip
The IP Address to validate
/**
Return true if the provided IP address is valid (IPv4 or IPv6) and the
provided CIDR is valid too
@param string $ip
The IP Address to validate with a CIDR
/**
Return true if the provided IP address is valid and is IPv4
@param string $ip
The IP Address to validate
/**
Return true if the provided IP address is valid and is IPv4 and the
provided CIDR is valid too
@param string $ip
The IP Address to validate with a CIDR
/** Return true if the provided CIDR is valid. The CIDR can be valid in IPv4.@param integer $cidr
The CIDR to test@return
boolean The CIDR is valid
/**
Return true if the provided IP address is valid and is IPv6
@param string $ip
The IP Address to validate
/**
Return true if the provided IP address is valid and is IPv6 and the
provided CIDR is valid too
@param string $ip
The IP Address to validate with a CIDR
/** Return true if the provided CIDR is valid. The CIDR can be valid in IPv6.@param integer $cidr
The CIDR to test@return
boolean The CIDR is valid