]> git.nbdom.net Git - nb.git/commitdiff
cidr_range
authorNicolas Boisselier <nicolas.boisselier@semantico.com>
Fri, 24 Jun 2016 16:18:15 +0000 (17:18 +0100)
committerNicolas Boisselier <nicolas.boisselier@semantico.com>
Fri, 24 Jun 2016 16:18:15 +0000 (17:18 +0100)
lib/php/nb.php

index bc0171e056ac7e81d6e34d0b542b64947bb6e8ca..8e9d0cd40598ee5d82471d04a4f8045f83ae072c 100644 (file)
@@ -509,6 +509,26 @@ class NB {
     return self::object2array($rows);
     return $class->out2(self::object2array($rows),(is_scalar($rows) ? $name : array()));
   }
+
+  public static function cidr_range( $cidr, $chkip=null ) {
+    // Assign IP / mask
+    if (empty($cidr)) return false;
+    list($ip,$mask) = explode("/",$cidr);
+
+    // Sanitize IP
+    $ip1 = preg_replace( '_(\d+\.\d+\.\d+\.\d+).*$_', '$1', "$ip.0.0.0" );
+
+    // Calculate range
+    $ip2 = long2ip( ip2long( $ip1 ) - 1 + ( 1 << ( 32 - $mask) ) );
+
+    // are we cidr range cheking?
+    if ( $chkip != null && ! filter_var( $chkip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) === false ) {
+      return ip2long( $ip1 ) <= ip2long( $chkip ) && ip2long( $ip2 ) >= ip2long( $chkip ) ? true : false;
+    } else {
+      return "$ip1 - $ip2";
+    }
+  }
+
 } # < Class
 #die(self::ext2mime('csv'));
 ?>