getting towards base functionality
[PHPIPManager.git] / lib / ip.php
index 6775fba..a0287f3 100644 (file)
@@ -1,15 +1,70 @@
 <?php
 
+function ipversion($ip)
+{
+       $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($ipv4 == 1) return 4;
+       if($ipv6 == 1) return 6;
+       return 0;
+}
+
+// this function tries to take a short representation of a ip6 address and converts it to a long one.
+// this is not entirely going to work.. but we'll get back to this
+function ip6ToLongStr($ip)
+{
+       if(ipversion($ip)!=6) return false;
+       
+       $pss = explode(":", $ip);
+       $ns = count($pss);
+       
+       $nstr = "";
+       foreach($pss as $ele) {
+               if($nstr != "") {
+                       $nstr .= ":";
+               }
+               if(strlen($ele < 1)) {
+                       // this is where we pad 8-count of 0000:
+                       $pds = 8-$ns+1;
+                       for($i=0; $i < $pds; $i++) {
+                               $nstr .= "0000";
+                       }
+               } else if(strlen($ele) < 4) {
+                       $nstr .= str_pad($ele, 4, "0", STR_PAD_LEFT);
+               } else {
+                       $nstr .= $ele;
+               }
+       }
+       return $nstr;
+}
+
+// this function converts an ip address to a comparable integer
+// so we can bounds-check.
+// it returns an array of [firstnum][lastnum]
+function ip6ToComp($ip, $mask)
+{
+       if($ipversion($ip)!=6) return false;
+       
+       $rip = ip6ToLongStr($ip);
+       
+}
+
+function ip4ToComp($ip, $mask)
+{
+       
+}
+
 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;
+               $ver = ipversion($ip);
+               
+               if($ver == 0) return 0;
 
-               if($ipv6!=false) {
+               if($ver == 6) {
                        if($mask != 0) {
                                if($mask%16 != 0) {
                                        error_log("no");
@@ -41,7 +96,7 @@ class ip {
                        }
                }
                
-               if($ipv4!=false) {
+               if($ver == 4) {
                        if($mask != 0) {
                                
                        }
@@ -50,12 +105,11 @@ class ip {
        
        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);
+               $ver = ipversion($ip);
                
-               if(!$ipv6&&!$ipv4) return false;
-
-               if($ipv6!=false) {
+               if($ver == 0) return false;
+               
+               if($ver == 6) {
                        if($mask != 0) {
                                if($mask%16 != 0) {
                                        error_log("no");
@@ -91,7 +145,7 @@ class ip {
                        }
                }
                
-               if($ipv4!=false) {
+               if($ver == 4) {
                        if($mask != 0) {
                                
                        }
@@ -113,12 +167,28 @@ class ip {
        
        function addSubnet($name, $subnet, $mask, $desc, $super)
        {
-               
+               global $db;
+               if(ip::isValid($subnet, $mask)) {
+                       $sn = ip::truncateNetwork($sn, $mask);
+                       // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
+                       //if(!isSubnet($subnet, $mask, $super)) return "Is not a valid subnet"; 
+                       
+                       $sql = "insert into subnet values (NULL, '$super', '$subnet', '$mask', '$name', '$desc')";
+                       error_log("sql: $sql");
+                       $db->dbobject->query($sql);
+                       return true;
+               }
        }
        
-       function isConflicting($subnet)
+       function isSubnet($subnet, $mask, $superid)
        {
-               
+               return true;
+       }
+       
+       // this function returns true if it is NOT conflicting
+       function isConflicting($subnet, $mask, $superid)
+       {
+               return true;
        }
        
        public $supers = "";