Advertisement
raul3k

CIDR 2

Mar 5th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2.  
  3. class Cidr {
  4.  
  5.  
  6.  
  7.     /** ***********************************/
  8.     /** Usable part ***********************/
  9.     /*** **********************************/
  10.  
  11.     public function isIpValid($ip){
  12.         return filter_var($ip, FILTER_VALIDATE_IP);
  13.     }
  14.  
  15.     public function isInOprRange($opr_range, $ip){
  16.         if ( strpos($opr_range,';') !== false ) {
  17.             $ips = explode(';', $opr_range);
  18.             foreach ( $ips as $ip_range ) {
  19.                 if ( strpos($ip_range,'-') !== false ) {
  20.                     list($inicial, $final) = explode('-', $ip_range);
  21.                     if ( $this->simpleInRange($ip, $inicial, $final) ) {
  22.                         return true;
  23.                     }
  24.                 } else {
  25.                     if ( $ip == $ip_range ) {
  26.                         return true;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         return $opr_range == $ip;
  32.     }
  33.  
  34.     public function simpleInRange($ip, $initial, $final){
  35.         $initial = ip2long($initial);
  36.         $final = ip2long($final);
  37.         $ip = ip2long($ip);
  38.  
  39.         return ($ip >= $initial && $ip <= $final);
  40.     }
  41.  
  42.     /** ***********************************/
  43.     /** End of usable part ****************/
  44.     /*** **********************************/
  45.  
  46.     public function getCidrByFirstLastIP($first, $last){
  47.         $first = ip2long($first);
  48.         $last = ip2long($last);
  49.         $qtd = (($last - $first)+2);
  50.         $q = 0;
  51.         while($qtd/2 > 1) {
  52.             $q++;
  53.             $qtd = $qtd/2;
  54.         }
  55.         return 32-($q+1);
  56.  
  57.     }
  58.    
  59.     public function getTotalIPsFromCidr($cidr) {
  60.         return 1 << (32 - $cidr);
  61.     }
  62.  
  63.     public function getFirstUsableIP($ip, $cidr){
  64.         return long2ip(ip2long($this->getNetworkAddress($ip, $cidr)) + 1);
  65.     }
  66.  
  67.     public function getLastUsableIP($ip, $cidr) {
  68.         return long2ip(ip2long($this->getFirstUsableIP($ip, $cidr)) + (1 << (32 - $cidr)) -2);
  69.     }
  70.  
  71.     public function isIPInRange($range, $cidr, $ip){
  72.         // Calculando o range
  73.         $initial = ip2long($this->getFirstUsableIP($range, $cidr));
  74.         $final = ip2long($this->getLastUsableIP($range, $cidr));
  75.         $ip = ip2long($ip);
  76.  
  77.         return ($ip >= $initial && $ip <= $final);
  78.     }
  79.  
  80.     public function getNetmaskByCidr($cidr){
  81.         $bin = '';
  82.         for ( $i=1; $i<=32; $i++ ) {
  83.             $bin .= ($cidr >= $i) ? '1' : '0';
  84.         }
  85.         $netmask = long2ip(bindec($bin));
  86.         if ($netmask == '0.0.0.0') return false;
  87.         return $netmask;
  88.     }
  89.  
  90.     public function getCidrByNetmask($netmask){
  91.         $bits = 0;
  92.         $netmask = explode('.', $netmask);
  93.         foreach ($netmask as $oct) {
  94.             $bits += strlen(str_replace('0', '', decbin($oct)));
  95.         }
  96.         return $bits;
  97.     }
  98.  
  99.     public function getNetworkAddress($ip, $cidr){
  100.         return long2ip((ip2long($ip)) & ((-1 << (32 - (int)$cidr))));
  101.     }
  102. }
  103. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement