getting towards base functionality
[PHPIPManager.git] / lib / www.php
index c45de8a..f9648b9 100644 (file)
@@ -1,6 +1,14 @@
 <?php
 // The www class file.
 $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() {
@@ -43,6 +51,7 @@ class www {
        {
                // we build a frame of framey's
                $this->header();
+               $this->printError();
                $this->mainPage();              
                $this->footer();
        }
@@ -51,7 +60,6 @@ class www {
        {
                global $db;
                
-               echo "<h1>Welcome to PHPIPManager</h1>";
                ?>
 <form method="post" action="?action=addsuper">
 Create Supernet: name <input type="text" name="name"></input>
@@ -60,16 +68,59 @@ Mask <input type="text" name="mask"></input>
 Description <input type="text" name="desc"></input>
 <input type="submit" name="go" Value="Create"></input>
 </form>
-               <?php 
+               <?php
+
+               // now print the super nets
+               $res = $db->dbobject->query("select * from supernet");
+               foreach($res as $row) {
+                       //echo "<pre>";
+               //      print_r($row);
+               //      echo "</pre><hr>";
+                       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>";
+               }
+               
+               echo "<a href=\"?action=dumpdb\">dump database</a> <a href=\"?action=restoredb\">restore database</a>";  
+       }
+       
+       function printError()
+       {
+               if(isset($_REQUEST["error"]))
+               {
+                       echo "<font color=\"red\">".$_REQUEST["error"]."</font>";
+               }
+               if(isset($_REQUEST["notice"]))
+               {
+                       echo "<font color=\"green\">".$_REQUEST["notice"]."</font>";
+               }
+               
        }
        
-       function header()
+       function header($title = "Welcome to PHP IP Manager")
        {
                ?>
 <html>
-<head>
+<head><title><?php echo $title ?></title>
 </head>
-<body>
+<body><h1><?php echo $title ?></h1>
                <?php
        }
        
@@ -96,10 +147,176 @@ function www_ip_addSuperRange()
        $mask = $_REQUEST["mask"];
        $desc = $_REQUEST["desc"];
        
-       $sql = "insert into supernet values (NULL, '$name', '$sn', '$mask', '$desc')";
+       $myip = new ip();
+       
+       if($myip->addSupernet($name, $sn, $mask, $desc)) {
+               header("Location: index.php?notice=range added");
+       } else {
+               header("Location: index.php?error=invalid ipaddress");
+       }
+}
+
+function www_ip_allocateRange()
+{
+       global $db, $wwwConnector;
+
+       $id = $_REQUEST["id"];
+       
+       $res = $db->dbobject->query("select * from supernet where sn_id=='$id'");
+       
+       foreach($res as $row) {
+               $sn = $row["sn_ip"];
+       }
+       $wwwConnector->header();
+       $wwwConnector->printError();
+       ?>
+<form method="post" action="?action=allocatesub&id=<?php echo $id ?>">
+<input type="hidden" name="superid" value="<?php echo $id ?>">
+<table>
+<tr><td>Subnet Name</td><td><input type="text" name="subname"></td></tr>
+<tr><td>Subnet IP</td><td><input type="text" name="subip" value="<?php echo $sn?>"></td></tr>
+<tr><td>Subnet Mask</td><td><input type="text" name="submask"></td></tr>
+<tr><td>Description</td><td><input type="text" name="subdesc"></td></tr>
+<tr><td><input type="submit" name="add" value="Add"></td></tr>
+</table>
+</form>
+<?php
+
+       $wwwConnector->footer();
+}
+
+function www_ip_allocateSubRange()
+{
+       global $db, $wwwConnector;
+       
+       $superid = $_REQUEST["superid"];
+       $name = $_REQUEST["subname"];
+       $subip = $_REQUEST["subip"];
+       $mask = $_REQUEST["submask"];
+       $desc = $_REQUEST["subdesc"];
+       
+       $myip = new ip();
+       if($myip->addSubnet($name, $subip, $mask, $desc, $superid)) {
+               header("Location: index.php?notice=range added");
+       } else {
+               header("Location: index.php?error=invalid ipaddress");
+       }
+}
+
+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");
+}
 
-       header("Location: index.php");
+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()
+{
 }
 
 ?>
\ No newline at end of file