setup up host update bit
[configmanager.git] / lib / www.php
index 794279b..dc3a105 100644 (file)
@@ -4,8 +4,8 @@
 // some global menu stuff
 $MENUS["home"]["url"] = "?action=home";
 $MENUS["home"]["name"] = "Home";
-$MENUS["hosts"]["url"] = "?action=hosts";
-$MENUS["hosts"]["name"] = "Hosts";
+//$MENUS["hosts"]["url"] = "?action=hosts";
+//$MENUS["hosts"]["name"] = "Hosts";
 $MENUS["add"]["url"] = "?action=addhost";
 $MENUS["add"]["name"] = "Add Host";
 $MENUS["config"]["url"] = "?action=config";
@@ -15,7 +15,11 @@ $FUNCTIONS["config"] = "www_configure";
 $FUNCTIONS["addhost"] = "www_addhost";
 $FUNCTIONS["addhostnext"] = "www_addHostStageTwo";
 $FUNCTIONS["addhostthree"] = "www_addHostStageThree";
-
+$FUNCTIONS["delete"] = "www_deleteHost";
+$FUNCTIONS["update"] = "www_updateHost";
+$FUNCTIONS["detail"] = "www_hostDetail";
+$FUNCTIONS["getconfig"] = "www_getConfig";
+$FUNCTIONS["downloadconfig"] = "www_downloadConfig";
 
 function www_header()
 {
@@ -74,9 +78,155 @@ function www_body()
        
        $db = db_getDB();
        
-       // here we do the normal body, whatever that
+       // here we do the normal body
        // we'll list the hosts, the size of the current config and
        // the last time it was updated, plus the number of versions
+       $data = db_selectData("hosts");
+       if(!$data) {
+               echo "No hosts currently exist, add them from the right<br>"; 
+       } else {
+               echo "<table border=\"1\">";
+               echo "<tr><th>Name</th><th>Address</th><th>Last Update</th><th>Schedule</th><th>Versions</th><th>Controls</th></tr>";
+               foreach($data as $dstruct) {
+                       //print_r($dstruct);
+                       $name = $dstruct["hostname"];
+                       $addr = $dstruct["hostip"];
+                       if($dstruct["lastupdate"] != 0) {
+                               $dold = round((time()-$dstruct["lastupdate"])/86400);
+                               
+                               $lupdate = strftime("%T %e %b %G", $dstruct["lastupdate"])." ($dold days old)";
+                       } else {
+                               $lupdate = "Never";
+                       }
+                       $sched = $dstruct["updateschedule"];
+                       $vd = count(db_selectData("configs", "hostip", "$addr"));
+                       echo "<tr><td><a href=\"?action=detail&hip=$addr\">$name</a></td><td>$addr</td><td>$lupdate</td><td>$sched</td><td>$vd</td><td>";
+                       echo "<a href=\"?action=delete&hip=$addr\">Delete</a> <a href=\"?action=edit&hip=$addr\">Edit</a> <a href=\"?action=update&hip=$addr\">Update</a>";
+                       echo "</td></tr>";
+               
+               }
+               echo "</table>";
+       }
+       
+}
+
+function www_getConfig()
+{
+       global $HOST_TYPE;
+       
+       $cid = $_REQUEST["cid"];        
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["getconfig"];
+       
+       error_log("calling $func with $hip, and $cid");
+       $rdata = $func("$hip", "$cid");
+       
+       echo "Config:<br>";
+       echo "<pre>";
+       echo $rdata;
+       echo "</pre>";
+}
+
+function www_downloadConfig()
+{
+       global $HOST_TYPE;
+       
+       $cid = $_REQUEST["cid"];        
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["getconfig"];
+       
+       error_log("calling $func with $hip, and $cid");
+       $rdata = $func("$hip", "$cid");
+       
+       echo "Config:<br>";
+       echo "<pre>";
+       echo $rdata;
+       echo "</pre>";
+}
+
+function www_hostDetail()
+{
+       global $HOST_TYPE;
+       
+       //db_createTable("configs", "hostip", "configtime", "configdata");
+       //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+       //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hconfig = $data[0]["hostconfig"];
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["detail"];
+       
+       error_log("calling $func with $hip, and $hconfig");
+       $func("$hip");
+}
+
+function www_updateHost()
+{
+       global $HOST_TYPE;
+       
+       //db_createTable("configs", "hostip", "configtime", "configdata");
+       //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+       //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hconfig = $data[0]["hostconfig"];
+       $hostname = $data[0]["hostname"];
+       $htype = $data[0]["hosttype"];
+       
+       $func = $HOST_TYPE["$htype"]["getconfig"];
+       $logdets = unserialize(base64_decode($hconfig));
+
+       // now check enable, username and password bits for completeness
+       $need_user = false;
+       $need_pass = false;
+       $need_enable = false;
+
+       // this aint the right way.
+       if($logdets["username"] == "" && $_REQUEST["username"] == "") {
+               // we need a user
+               $need_user = true;
+       }
+       if($logdets["password"] == "" && $_REQUEST["password"] == "") {
+               // we need a user
+               $need_pass = true;
+       }
+       if($logdets["enable"] == "" && $_REQUEST["enable"] == "" && $HOST_TYPE["$htype"]["needsenable"]) {
+               $need_enable = true;
+       }
+       
+       $conf = $func($hip, $act_user, $act_pass, $act_enable);
+
+       // function db_getMaxValue($tablename, $column, $columnsel="", $wheresel="")
+       $oldconf_rid = db_getMaxValue("configs", "configs_id", "hostip", "$hip");
+       $oldconf_id = $oldconf_rid[0][0];
+       error_log("oldconf_id is $oldconf_id");
+       
+       // function db_selectData($tablename, $column="", $value="", $orderby = "")
+       $oldconf_r = db_selectData("configs", "configs_id", "$oldconf_id");
+       $oldconf = base64_decode($oldconf_r[0]["configdata"]);
+       
+       if(md5($oldconf) != md5($conf)) {
+               echo "New config found, updating db<br>";
+               db_insertData("configs", "$hip", time(), base64_encode($conf));
+               echo "Config added<br>";
+       } else {
+               echo "New config and old config are the same, not updating<br>";
+       }
        
 }
 
@@ -91,16 +241,57 @@ function www_hostTypesDropDown()
        }
 }
 
+function www_deleteHost()
+{
+       $hip = $_REQUEST["hip"];
+       $data = db_selectData("hosts", "hostip", "$hip");
+       
+       $hostname = $data[0]["hostname"];
+
+       if(isset($_REQUEST["confirmed"])) {
+               db_deleteData("hosts", "hostip", "$hip");
+               db_deleteData("configs", "hostip", "$hip");
+               echo "Done!";
+       } else {
+               echo "Are you use you wish to delete the host, $hostname with IP address $hip? (and all associated config info)<br>";
+               echo "<a href=\"?action=delete&hip=$hip&confirmed=yes\">Yes</a>";
+       }
+}
+
+function www_updateSchedule()
+{
+?>
+<option value="0">Manual Only</option>
+<option value="900">Every 15 Minutes</option>
+<option value="3600">Every Hour</option>
+<option value="14400">Every 4 Hours</option>
+<option value="86400">Once a Day</option>
+<option value="604800">Once a Week</option>
+<option value="2592000">Once a Month (30 Days)</option>
+<option value="7776000">Every 3 Months (90 days)</option>
+<option value="10368000">Every 6 Months (180 Days)</option>
+<?php
+}
+
 function www_addhost()
 {
+       $db = db_getDB();
+       
+       db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
+       db_createTable("configs", "hostip", "configtime", "configdata");
        ?>
 <form method="post" action="?action=addhostnext">
+<i>Note: if you leave the username/password fields blank, you'll be prompted when
+manually updating on the home page</i>
 <table>
 <tr><td>Name</td><td><input type="text" name="cname"></td></tr>
 <tr><td>Hostname/IP Address</td><td><input type="text" name="hname"></td></tr>
+<tr><td>Username</td><td><input type="text" name="username"></td></tr>
+<tr><td>Password</td><td><input type="password" name="password"></td></tr>
+<tr><td>Enable Password</td><td><input type="password" name="enable"></td></tr>
 <tr><td>Host Type</td><td><select name="hosttype"><?php www_hostTypesDropDown() ?></select></td></tr>
-</table>
-<input type="submit" value="Next" name="Next">
+<tr><td>Update Schedule</td><td><select name="updatesched"><?php www_updateSchedule() ?></select></td></tr></table>
+<input type="submit" value="Add" name="Add">
 </form>
        <?php
 }
@@ -108,27 +299,41 @@ function www_addhost()
 function www_addHostStageTwo()
 {
        global $HOST_TYPE;
+       
+       $data = db_selectData("hosts", "hostip", $_REQUEST["hname"]);
+       if($data) {
+               echo "Host already exists in database\n";
+               return false;
+       } else {
+               db_insertData("hosts", $_REQUEST["cname"], $_REQUEST["hname"], $_REQUEST["hosttype"], "", "", $_REQUEST["updatesched"]);
+       }
        if(isset($_REQUEST["hosttype"])) {
                $htype = $_REQUEST["hosttype"];
-               $func = $HOST_TYPE["$htype"]["configform"];
-               if(function_exists($func)) {
-                       echo "<form method=\"post\" action=\"?action=addhostthree&hosttype=$htype\">";
-                       $func();
-                       echo "<input type=\"submit\" value=\"Add\" name=\"Add\">";
-               } else echo "would call $func for $htype but it doesnt exist\n";
-       }
-}
+               $hip = $_REQUEST["hname"];
+               $user = $_REQUEST["username"];
+               $pass = $_REQUEST["password"];
+               $enable = $_REQUEST["enable"];
+               $hname = $_REQUEST["hname"];
+               $upsched = $_REQUEST["updatesched"];
+               
+               $confdetails["username"] = $user;
+               $confdetails["password"] = $pass;
+               $confdetails["enable"] = $enable;
+               
+               $logdata = base64_encode(serialize($confdetails));
+               
+               db_updateData("hosts", "hostconfig", "$logdata", "hostip", "$hip");
+               //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
+               
 
-function www_addHostStageThree()
-{
-       global $HOST_TYPE;
-       if(isset($_REQUEST["hosttype"])) {
-               $htype = $_REQUEST["htype"];
-               $func = $HOST_TYPE["$htype"]["postfunction"];
-               if(function_exists($func)) {
-                       $func();
-               } else echo "would call $func for $htype but it doesnt exist\n";
+               echo "Host added successfully, running configuration test<br>";
+               $func = $HOST_TYPE["$htype"]["getconfig"];
+               $conf = $func($hip, $user, $pass, $enable);
+               echo "The config I got is below, hope its correct as it'll be inserted into the DB as config version 1<br><pre>$conf</pre>";
+               
+               //db_createTable("configs", "hostip", "configtime", "configdata");
+               db_insertData("configs", "$hip", time(), base64_encode($conf));
        }
-       
 }
+
 ?>
\ No newline at end of file