moved some functionality into the ip class
[PHPIPManager.git] / lib / ip.php
index e07eb30..91d7013 100644 (file)
@@ -1,9 +1,114 @@
 <?php
 
 class ip {
-       function addSupernet($supernet)
+       
+       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($i!=0) $slt .= ":";
+                                               if(strlen($pss[$i]) < 1) {
+                                                       error_log("no 3");
+                                                       return false;
+                                               }
+                                               $slt .= "".$pss[$i];
+                                       }
+                                       
+                                       // we are still a valid ipv6 ip address/mask
+                                       error_log("Valid");
+                                       return $slt;
+                               }
+                       }
+               }
+               
+               if($ipv4!=false) {
+                       if($mask != 0) {
+                               
+                       }
+               }
+       }
+       
+       function addSupernet($name, $sn, $mask, $desc)
        {
+               global $db;
                
+               if(ip::isValid($sn, $mask)) {
+                       $sn = ip::truncateNetwork($sn, $mask);
+                       $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
+                       $db->dbobject->query($sql);
+                       return true;
+               }
+               return false;
        }
        
        function addSubnet($subnet)