Konwertery adresow ip w postaci: AAA.BBB.CCC.DDD, integer i binary do kazdej innej formy + sprawdzanie czy IP nalezy do danej sieci/maska.
<?php
/**
* Basic operations on IP addresses
* - conversions from (IP, decimal, binary) to (IP, decimal, binary)
* - checking whether IP is in specified network
*
* @author dr_bonzo
* @version 0.1
*/
class Ip_Manipulator
{
/**
* Indicates whether $sIp_address is in $sNetwork_address/$sNetmask network
*
* @param string $sIp_address
* @param string $sNetwork_address
* @param string $sNetmask
*
* @return bool
* @throws Exception On wrong addresses
*/
static public function ipIsInNetwork
( $sIp_address, $sNetwork_address, $sNetmask ) {
if ( ! self::ipAddressIsValid( $sIp_address ) )
{
throw new Exception( 'Invalid IP address (' . $sIp_address . ')' );
}
if ( ! self::networkIsValid( $sNetwork_address ) )
{
throw new Exception( 'Invalid network address (' . $sNetwork_address . ')' );
}
if ( ! self::netmaskIsValid( $sNetmask ) )
{
throw new Exception( 'Invalid Netmask (' . $sNetmask . ')' );
}
$iIp_address_dec = self::ipToDec( $sIp_address );
$sIp_address_bin = self::decToBin( $iIp_address_dec );
$iNetwork_address_dec = self::ipToDec( $sNetwork_address );
$sNetwork_address_bin = self::decToBin( $iNetwork_address_dec );
if ( preg_match( \"/^d{1,2}$/\", $sNetmask ) === 1 ) {
$iNetmask_dec = bindec( $sNetmask_bin ); }
else
{
$iNetmask_dec = self::ipToDec( $sNetmask );
$sNetmask_bin = self::decToBin( $iNetmask_dec );
}
$sIp_n_netmask_combined = self::decToBin( $iIp_address_dec & $iNetmask_dec );
return ( $sIp_n_netmask_combined === $sNetwork_address_bin );
}
/**
* Indicates whether IP address is valid
*
* @param string $sIp_address
* @return bool Whether ip is valid
*/
static private function ipAddressIsValid
( $sIp_address ) {
return ( preg_match( \"/^d{1,3}(.d{1,3}){3}$/\", $sIp_address ) === 1 ); }
/**
* Indicates whether network address is valid
*
* @param string $sNetwork_address
* @return bool
*/
static private function networkIsValid
( $sNetwork_address ) {
return ( preg_match( \"/^d{1,3}(.d{1,3}){3}$/\", $sNetwork_address ) === 1 ); }
/**
* Indicates whether mask is valid
*
* @param string $sNetmask
* @return bool
*/
static private function netmaskIsValid
( $sNetmask ) {
return ( preg_match( \"/(^d{1,3}(.d{1,3}){3}$|d{1,2})/\", $sNetmask ) === 1 ); }
/**
* Converts IP address (aaa.bbb.ccc.ddd) to decimal number
* (float type, because address can be > 2E9 -- that means negative integer)
*
* @param string $sIp
* @return float Ip address
*/
static public function ipToDec
( $sIp ) {
$fIp = 0;
for ( $i = 0; $i < 4; $i++ )
{
$fIp = $fIp * 256
+ intval( $aIp[ $i ] ); }
}
/**
* Converts IP address (aaa.bbb.ccc.ddd) to binary number
*
* @param string $sIp
* @return string
*/
static public function ipToBin
( $sIp ) {
$fIp = self::ipToDec( $sIp );
return $sIp_bin;
}
/**
* Converts decimal representation of IP address to IP address (aaa.bbb.ccc.ddd)
*
* @param float $fIp
* @return string
*/
static public function decToIp
( $fIp ) {
$sIp = '';
for ( $i = 0; $i < 4; $i++ )
{
$sIp = '.' . ( ( $fIp % 256 + 256 ) % 256 ) . $sIp;
$fIp = floor( $fIp / 256
); }
}
/**
* Converts decimal representation of IP address to binary repr.
*
* @param float $fIp
* @return string
*/
static public function decToBin
( $fIp ) {
return $sIp_bin;
}
/**
* Converts binary representation of IP address to its decmal repr.
*
* @param string $sIp_bin
* @return float
*/
static public function binToDec
( $sIp_bin ) {
}
/**
* Converts binary representation of IP address to IP address (aaa.bbb.ccc.ddd)
*
* @param string $sIp_bin
* @return string
*/
static public function binToIp
( $sIp_bin ) {
$sIp = self::decToIp( self::binToDec( $sIp_bin ) );
}
}
/*** USAGE ***/
try
{
print( ( Ip_Manipulator
::ipIsInNetwork( '172.17.1.3', '172.16.0.0', '15' ) ?
'Nalezy do sieci' : 'Nie nalezy do sieci' ) . '<br />' ); print( ( Ip_Manipulator
::ipIsInNetwork( '192.168.1.3', '192.168.13.0', '255.255.255.0' ) ?
'Nalezy do sieci' : 'Nie nalezy do sieci' ) . '<br />' ); print( \"<hr />IP = '192.168.1.3'<br />\" );
print( 'IP -> integer: ' . Ip_Manipulator
::ipToDec( '192.168.1.3' ) . '<br />' ); print( 'IP -> binary: ' . Ip_Manipulator
::ipToBin( '192.168.1.3' ) . '<br />' );
print( \"<hr />IP = 3232235779<br />\" );
print( 'integer -> IP: ' . Ip_Manipulator
::decToIp( 3232235779 ) . '<br />' ); print( 'integer -> binary: ' . Ip_Manipulator
::decToBin( 3232235779 ) . '<br />' );
print( \"<hr />IP = '11000000101010000000000100000011'<br />\" );
print( 'binary -> integer: ' . Ip_Manipulator
::binToDec( '11000000101010000000000100000011' ) . '<br />' ); print( 'binary -> IP: ' . Ip_Manipulator
::binToIp( '11000000101010000000000100000011' ) . '<br />' ); }
catch ( Exception $eException )
{
print( 'ERROR: ' . $eException->getMessage() ); }
?>
PS. Przypomne ze MySQL ma szybsza funkcje INET_ATON() i INET_NTOA() do konwersji IP <-> integer.
Nie lubię jednorożców.