working a bit
[PHPIPManager.git] / lib / ip.php
index e07eb30..95d71b0 100644 (file)
@@ -1,6 +1,103 @@
 <?php
 
 class ip {
+       
+       static function isValid($ip, $mask = 0)
+       {
+               $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip);
+               $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
+               
+               if(!$ipv6&&!$ipv4) return false;
+
+               if($ipv6!=false) {
+                       if($mask != 0) {
+                               if($mask%16 != 0) {
+                                       error_log("no");
+                                       return false;
+                               } else {
+                                       $sns = $mask / 16;
+                                       
+                                       // now check that we have $sns number of subnets specified
+                                       $pss = explode(":", $ip);
+                                       $ns = count($pss);
+                                       
+                                       // we need to specify an error we can throw back at the user
+                                       if($ns < $sns) {
+                                               error_log("no 2, $ns, $sns, $ip");
+                                               print_r($pss);
+                                               return false;
+                                       }
+                                       for($i = 0; $i < $sns; $i++) {
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                       }
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return true;
+                               }
+                       }
+               }
+               
+               if($ipv4!=false) {
+                       if($mask != 0) {
+                               
+                       }
+               }
+       }
+       
+       static function truncateNetwork($ip, $mask)
+       {
+               $ipv4 = ereg('^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$', $ip);
+               $ipv6 = ereg("^[0-9a-fA-F:]+$", $ip);
+               
+               if(!$ipv6&&!$ipv4) return false;
+
+               if($ipv6!=false) {
+                       if($mask != 0) {
+                               if($mask%16 != 0) {
+                                       error_log("no");
+                                       return false;
+                               } else {
+                                       $sns = $mask / 16;
+                                       
+                                       // now check that we have $sns number of subnets specified
+                                       $pss = explode(":", $ip);
+                                       $ns = count($pss);
+                                       
+                                       // we need to specify an error we can throw back at the user
+                                       if($ns < $sns) {
+                                               error_log("no 2, $ns, $sns, $ip");
+                                               print_r($pss);
+                                               return false;
+                                       }
+                                       
+                                       $slt = "";
+                                       for($i = 0; $i < $sns; $i++) {
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                               $slt .= $pss[$i].":";
+                                       }
+                                       $slt .= ":0";
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return $slt;
+                               }
+                       }
+               }
+               
+               if($ipv4!=false) {
+                       if($mask != 0) {
+                               
+                       }
+               }
+       }
+       
        function addSupernet($supernet)
        {