download this file: class.ipv4.php view text/plain: class.ipv4.php file encoding: UTF-8 [goback]
<?php
##
## this file name is 'class.ipv4.php'
##
## check korean ip address(ipv4) or ip network
##
## [author]
##  - Chilbong Kim, <san2(at)linuxchannel.net>
##  - http://linuxchannel.net/
##
## [changes]
##  - 2004.01.29 : bug fixed, bit &
##  - 2003.09.30 : re-checked by whois.apnic.net
##  - 2003.09.29 : add more and ...
##  - 2003.09.28 : new build
##
## [reference]
##  - http://irms.nic.or.kr/servlet/member.view.ISPIpAddress
##    (check date: 20030530)
##
## [usage]
##  - integer ipv4::iskrip(ipv4 ipaddr) // check korean ip address
##  - string ipv4::network(ipv4 ipaddr) // get korean ip-network
##
## [example]
##
##  echo ipv4::iskrip('192.168.10.3'); // 2
##  echo ipv4::iskrip(gethostbyname('www.linuxchannel.net')); // 1
##  echo ipv4::iskrip($_SERVER['REMOTE_ADDR']); // FALSE or 1 or 2
##
##  echo ipv4::network('192.168.10.3'); // 192.168.0.0/255.255.0.0
##  echo ipv4::network('192.100.10.3'); // 192.0.0.0/255.0.0.0
##  echo ipv4::network($_SERVER['REMOTE_ADDR']); // FALSE or some string
##

class ipv4
{
  
## check korean ip address
  ##
  ## [usage]
  ##  - integer ipv4::iskrip(ipv4 ipaddr)
  ##
  ## [return]
  ##  - 0 or FALSE : is not a korean ip address
  ##  - 1 or TRUE  : is a korean ip address
  ##  - 2 or TRUE  : is an internal ip address
  ##
  ## [example]
  ##  echo ipv4::iskrip('192.168.10.3'); // 2
  ##  echo ipv4::iskrip(gethostbyname('www.linuxchannel.net')); // 1
  ##  echo ipv4::iskrip($_SERVER['REMOTE_ADDR']); // FALSE or 1 or 2
  ##
  
function &iskrip($ipaddr)
  {
    if(
ipv4::getnetwork($ipaddr,'internal')) return 2;
    else if(
ipv4::getnetwork($ipaddr,'korean')) return 1;
    else return 
FALSE;
  }

  
## get korean ip network for an ip address
  ##
  ## [usage]
  ##  - string ipv4::network(ipv4 ipaddr)
  ##
  ## [example]
  ##  echo ipv4::network('192.168.10.3'); // 192.168.0.0/255.255.0.0
  ##  echo ipv4::network('192.100.10.3'); // 192.0.0.0/255.0.0.0
  ##  echo ipv4::network($_SERVER['REMOTE_ADDR']); // FALSE or some string
  ##
  
function &network($ipaddr)
  {
    if(!
$network ipv4::getnetwork($ipaddr,'internal'))
    { 
$network ipv4::getnetwork($ipaddr,'korean'); }

    return 
$network;
  }

  
## inside function
  ##
  
function &getnetwork($ipaddr$bkey='korean')
  {
    
## convert to binaray
    ##
    
$ipb sprintf('%032b',ip2long($ipaddr));

    
$krnetwork ipv4::krnetwork($bkey);

    
## get netmasks for internal ip address
    ##
    
$netmasks array_values(array_unique($krnetwork));
    
$size sizeof($netmasks);

    
## get network
    ##
    
for($i=0$i<$size$i++)
    {
        
$mask sprintf('%032b',ip2long($netmasks[$i]));
        
$network long2ip(bindec($ipb $mask)); // is a network address

        
if($netmask $krnetwork[$network])
        {
            return 
$network.'/'.$netmask// string
            
break;
        }
    }

    
//return ipv4::network($ipaddr,'korean'); // call by self
    
return FALSE// is not an korean ip address
  
}

  
## inside function
  ##
  
function &krnetwork($key='korean')
  {
    
$ipv4['internal'] = array
    (
    
'10.0.0.0'  =>'255.0.0.0',   '127.0.0.0'  =>'255.0.0.0',
    
'172.16.0.0'=>'255.240.0.0''192.168.0.0'=>'255.255.0.0',
    );

    
$ipv4['korean'] = array
    (
    
'129.254.0.0' =>'255.255.0.0',  '141.223.0.0' =>'255.255.0.0',  
    
'147.6.0.0'   =>'255.255.0.0',  '147.43.0.0'  =>'255.255.0.0',
    
'147.46.0.0'  =>'255.255.0.0',  '146.47.0.0'  =>'255.255.0.0',
    
'150.150.0.0' =>'255.255.0.0',  '150.183.0.0' =>'255.255.0.0',
    
'150.197.0.0' =>'255.255.0.0',  '152.10.0.0'  =>'255.255.0.0',
    
'158.44.0.0'  =>'255.255.0.0',  '163.152.0.0' =>'255.255.0.0',
    
'163.180.0.0' =>'255.255.0.0',  '163.239.0.0' =>'255.255.0.0',
    
'164.124.0.0' =>'255.255.0.0',  '164.125.0.0' =>'255.255.0.0',
    
'165.132.0.0' =>'255.255.0.0',  '165.133.0.0' =>'255.255.0.0',
    
'165.141.0.0' =>'255.255.0.0',  '165.186.0.0' =>'255.255.0.0',
    
'165.194.0.0' =>'255.255.0.0',  '165.213.0.0' =>'255.255.0.0',
    
'165.229.0.0' =>'255.255.0.0',  '165.243.0.0' =>'255.255.0.0',
    
'165.244.0.0' =>'255.255.0.0',  '165.246.0.0' =>'255.255.0.0',
    
'166.79.0.0'  =>'255.255.0.0',  '166.103.0.0' =>'255.255.0.0',
    
'166.104.0.0' =>'255.255.0.0',  '166.125.0.0' =>'255.255.0.0',
    
'168.78.0.0'  =>'255.255.0.0',  '168.115.0.0' =>'255.255.0.0',
    
'168.126.0.0' =>'255.255.0.0',  '168.131.0.0' =>'255.255.0.0',
    
'168.154.0.0' =>'255.255.0.0',  '168.188.0.0' =>'255.255.0.0',
    
'168.219.0.0' =>'255.255.0.0',  '202.6.95.0'  =>'255.255.255.0',
    
'202.14.103.0'=>'255.255.255.0','202.14.165.0'=>'255.255.255.0',
    
'202.21.0.0'  =>'255.255.248.0','202.20.82.0' =>'255.255.255.0',
    
'202.20.83.0' =>'255.255.255.0','202.20.84.0' =>'255.255.255.0',
    
'202.20.85.0' =>'255.255.255.0','202.20.86.0' =>'255.255.255.0',
    
'202.20.128.0'=>'255.255.128.0','202.20.99.0' =>'255.255.255.0',
    
'202.20.119.0'=>'255.255.255.0','202.30.0.0'  =>'255.255.0.0',
    
'202.31.0.0'  =>'255.255.0.0',  '203.224.0.0' =>'255.224.0.0',
    
'210.90.0.0'  =>'255.255.0.0',  '210.91.0.0'  =>'255.255.0.0',
    
'210.92.0.0'  =>'255.252.0.0',  '210.96.0.0'  =>'255.224.0.0',
    
'210.178.0.0' =>'255.255.0.0',  '210.179.0.0' =>'255.255.0.0',
    
'210.180.0.0' =>'255.252.0.0',  '210.204.0.0' =>'255.252.0.0',
    
'210.216.0.0' =>'255.248.0.0',  '211.32.0.0'  =>'255.224.0.0',
    
'211.104.0.0' =>'255.248.0.0',  '211.112.0.0' =>'255.248.0.0',
    
'211.168.0.0' =>'255.248.0.0',  '211.176.0.0' =>'255.240.0.0',
    
'211.192.0.0' =>'255.192.0.0',  '218.36.0.0'  =>'255.252.0.0',
    
'218.48.0.0'  =>'255.248.0.0',  '218.144.0.0' =>'255.240.0.0',
    
'218.232.0.0' =>'255.248.0.0',  '219.240.0.0' =>'255.255.0.0',
    
'219.241.0.0' =>'255.255.0.0',  '219.248.0.0' =>'255.248.0.0',
    
'220.64.0.0'  =>'255.224.0.0',  '220.116.0.0' =>'255.252.0.0',
    
'220.120.0.0' =>'255.248.0.0',  '221.138.0.0' =>'255.255.0.0',
    
'221.139.0.0' =>'255.255.0.0',  '221.140.0.0' =>'255.252.0.0',
    
'221.144.0.0' =>'255.240.0.0',  '221.160.0.0' =>'255.248.0.0',
    
'221.168.0.0' =>'255.255.0.0',  '61.32.0.0'   =>'255.248.0.0',
    
'61.40.0.0'   =>'255.252.0.0',  '61.72.0.0'   =>'255.248.0.0',
    
'61.80.0.0'   =>'255.252.0.0',  '61.84.0.0'   =>'255.255.0.0',
    
'61.85.0.0'   =>'255.255.0.0',  '61.96.0.0'   =>'255.240.0.0',
    
'61.248.0.0'  =>'255.248.0.0',
    );

    return 
$ipv4[$key];
  }

  function &
ip2base($ipaddr)
  {
    return 
sprintf('%u',ip2long($ipaddr));
  }

  function &
base2ip($base)
  {
    return 
long2ip($base);
  }
// end of class

?>