getting towards base functionality
authorpaulr <me@pjr.cc>
Sun, 12 Sep 2010 15:57:52 +0000 (01:57 +1000)
committerpaulr <me@pjr.cc>
Sun, 12 Sep 2010 15:57:52 +0000 (01:57 +1000)
lib/db.php
lib/ip.php
lib/www.php
unittest/ip6tolong.php [new file with mode: 0644]

index e7afc6e..05216dc 100644 (file)
@@ -33,6 +33,31 @@ class db {
        }
 
        public $dbobject = "";
+       
+       function dump() {
+               $sql = "select * from supernet";
+               $res = $this->dbobject->query($sql);
+               
+               foreach($res as $row) {
+                       echo "sn:".$row["sn_id"].":".$row["sn_name"].":".$row["sn_ip"].":".$row["sn_mask"].":".$row["sn_desc"]."\n";
+               }
+               $sql = "select * from subnet";
+               $res = $this->dbobject->query($sql);
+               
+               foreach($res as $row) {
+                       echo "sbn:".$row["sn_id"].":".$row["snid_id"].":".$row["sn_name"].":".$row["sn_ip"].":".$row["sn_mask"].":".$row["sn_desc"]."\n";
+               }
+               $sql = "select * from hosts";
+               $res = $this->dbobject->query($sql);
+               
+               foreach($res as $row) {
+                       echo "hst:".$row["ho_id"].":".$row["ho_sn_id"].":".$row["ho_ip"].":".$row["ho_name"].":".$row["ho_desc"]."\n";
+               }
+       }
+       
+       function restore($restorefile) {
+               
+       }
 }
 
 
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 = "";
index 70274f5..f9648b9 100644 (file)
@@ -3,8 +3,12 @@
 $actionRegister["addsuper"] = "www_ip_addSuperRange";
 $actionRegister["allocate"] = "www_ip_allocateRange";
 $actionRegister["allocatesub"] = "www_ip_allocateSubRange";
+$actionRegister["modsubnet"] = "www_ip_modifySubnet";
 $actionRegister["dumpdb"] = "www_db_dumpdb";
 $actionRegister["restoredb"] = "www_db_restoredb";
+$actionRegister["changesubdetails"] = "www_ip_changesubdetails";
+$actionRegister["addhosts"] = "www_ip_addhost";
+$actionRegister["deletehost"] = "www_ip_deletehost";
 
 class www {
        function Go() {
@@ -72,10 +76,25 @@ Description <input type="text" name="desc"></input>
                        //echo "<pre>";
                //      print_r($row);
                //      echo "</pre><hr>";
-                       echo "<table border=\"1\"><tr><th>".$row["sn_name"]."</th><td>".$row["sn_ip"]."/".$row["sn_mask"]."</td><td>".$row["sn_desc"]."</td>";
+                       echo "<table border=\"1\"><tr><th align=\"left\">".$row["sn_name"]."</th><td>".$row["sn_ip"]."::0/".$row["sn_mask"]."</td><td>".$row["sn_desc"]."</td>";
                        echo "<td><a href=\"?action=allocate&id=".$row["sn_id"]."\">Allocate Subnet</a></td>";
                        echo "<td><a href=\"?action=delete&id=".$row["sn_id"]."\">Delete Supernet</a></td>";
+                       echo "</tr>";
                        // now we search for sub's
+                       $res2 = $db->dbobject->query("select * from subnet where snid_id='".$row["sn_id"]."'");
+                       foreach($res2 as $row2) {
+                               // ("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
+                               $name = $row2["sn_name"];
+                               $subid = $row2["sn_id"];
+                               $subnet = $row2["sn_ip"];
+                               $mask = $row2["sn_mask"];
+                               $desc = $row2["sn_desc"];
+                               echo "<tr>";
+                               echo "<td><li> <i><a href=\"?action=modsubnet&id=".$row2["sn_id"]."\">$name</a></i></td><td>$subnet::0/$mask</td><td>$desc</td>";
+                               echo "<td><a href=\"?action=deletesub&id=".$row2["sn_id"]."\">Delete Subnet</a></td>";
+                               //echo "<td><a href=\"?action=addhost&id=".$row["sn_id"]."\">Add Host</a></td>";
+                               echo "</tr>";
+                       }
                        echo "</table><br>";
                }
                
@@ -184,8 +203,116 @@ function www_ip_allocateSubRange()
        }
 }
 
+function www_ip_modifySubnet()
+{
+       global $db, $wwwConnector;
+
+       $id = $_REQUEST["id"];
+       //("sn_id" INTEGER PRIMARY KEY AUTOINCREMENT,"snid_id" INTEGER,"sn_ip" TEXT,"sn_mask" TEXT,"sn_name" TEXT, "sn_desc" TEXT);';
+       $sql = "select sn_name,sn_desc,sn_ip from subnet where sn_id=='$id'";
+       $res = $db->dbobject->query($sql);
+       
+       foreach($res as $row) {
+               $sn_name = $row["sn_name"];
+               $sn_desc = $row["sn_desc"];
+               $sn_ip = $row["sn_ip"];
+       }
+       
+       $wwwConnector->header();
+       $wwwConnector->printError();
+       
+       // ("ho_id" INTEGER PRIMARY KEY AUTOINCREMENT,"ho_sn_id" INTEGER,"ho_ip" TEXT,"ho_name" TEXT,"ho_desc" TEXT)
+       ?>
+<form method="post" action="?action=changesubdetails&id=<?php echo $id ?>">
+Subnet Name: <input type="text" name="subname" value="<?php echo $sn_name?>">
+Subnet Description: <input type="text" name="subdesc" value="<?php echo $sn_desc?>">
+<input type="submit" name="change" value="Update">
+</form>
+
+<form method="post" action="?action=addhosts&id=<?php echo $id ?>">
+<h3>Add Host</h3>
+Hostname <input type="text" name="hostname">
+IP Address <input type="text" name="hostip" value="<?php echo $sn_ip.":"?>">
+Description <input type="text" name="hostdesc" >
+<input type="submit" name="change" value="Add">
+</form>
+
+
+
+<table border=1>
+<tr><th>Host IP</th><th>Hostname</th><th>Description</th></tr>
+<?php
+       $sql = "select * from hosts where ho_sn_id='$id'";
+       $res = $db->dbobject->query($sql);
+       
+       foreach($res as $row) {
+               $name = $row["ho_name"];
+               $desc = $row["ho_desc"];
+               $ip = $row["ho_ip"];
+               $hid = $row["ho_id"];
+               echo "<tr><td>$ip</td><td>$name</td><td>$desc</td><td><a href=\"?action=deletehost&hostid=$hid&snid=$id\">Delete</a></td></tr>";
+       } 
+?>
+</table>
+
+
+<a href="index.php">Back</a>
+<?php
+
+       $wwwConnector->footer();
+}
+
+function www_ip_deletehost()
+{
+       global $db, $wwwConnector;
+       
+       $id = $_REQUEST["hostid"];
+       $sid = $_REQUEST["snid"];
+       
+       $sql = "delete from hosts where ho_id=='$id'";
+       $db->dbobject->query($sql);
+       
+       header("Location: ?action=modsubnet&id=$sid");
+}
+
+function www_ip_addhost()
+{
+       global $db, $wwwConnector;
+       
+       $id = $_REQUEST["id"];
+       $hn = $_REQUEST["hostname"];
+       $hip = $_REQUEST["hostip"];
+       $desc = $_REQUEST["hostdesc"];
+       
+       $sql = "insert into hosts values (NULL, '$id', '$hip', '$hn', '$desc')";
+       $db->dbobject->query($sql);
+       
+       header("Location: ?action=modsubnet&id=$id");
+}
+
+function www_ip_changesubdetails()
+{
+       global $db, $wwwConnector;
+       
+       $id = $_REQUEST["id"];
+       $name = $_REQUEST["subname"];
+       $desc = $_REQUEST["subdesc"];
+
+       $sql = "update subnet set sn_name='$name', sn_desc='$desc' where sn_id=='$id'";
+       $db->dbobject->query($sql);
+       
+       header("Location: ?action=modsubnet&id=$id");
+}
+
 function www_db_dumpdb()
 {
+       global $db;
+       
+       $datestamp = strftime("%d-%m-%Y-%H.%M.%S");
+       $name = "ipmandbdump-$datestamp.db";
+       header("Content-type: application/octet-stream;\n");
+       header("Content-Disposition: attachment; filename=\"$name\";\n\n");
+       $db->dump();
 }
 
 function www_db_restoredb()
diff --git a/unittest/ip6tolong.php b/unittest/ip6tolong.php
new file mode 100644 (file)
index 0000000..d5ee6c8
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+
+require_once "../lib/lib.php";
+
+$ori_str = "2003:1:2:3::1";
+$str = ip6ToLongStr($ori_str);
+
+echo "str: $ori_str, $str\n";
+
+$ori_str = "2003:1:2:3:23:34:23:1";
+$str = ip6ToLongStr($ori_str);
+
+echo "str: $ori_str, $str\n";
+
+if(function_exists("gmp_add")) echo "gmp_add exists\n";
+if(function_exists("bc_sub")) echo "bc_sub exists\n";
+
+
+?>
\ No newline at end of file