im not actually shure...
[configmanager.git] / lib / www.php
index 3b7c506..21a6d03 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";
@@ -188,11 +188,78 @@ function www_updateHost()
        $hostname = $data[0]["hostname"];
        $htype = $data[0]["hosttype"];
        
-       $func = $HOST_TYPE["$htype"]["updateconfig"];
+       $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"] == "") {
+               if(!isset($_REQUEST["username"])) {
+                       // we need a user
+                       $need_user = true;
+               } else {
+                       $act_user = $_REQUEST["username"];
+               }
+       } else {
+               $act_user = $logdets["username"];
+       }
+       if($logdets["password"] == "") {
+               if(!isset($_REQUEST["password"])) {
+                       // we need a user
+                       $need_pass = true;
+               } else {
+                       $act_pass = $_REQUEST["password"];
+               }
+       } else {
+               $act_pass = $logdets["password"];
+       }
+       if($HOST_TYPE["$htype"]["needsenable"]) {
+               if($logdets["enable"] == "") {
+                       if(!isset($_REQUEST["enable"])) {
+                               $need_enable = true;
+                       } else {
+                               $act_enable = $_REQUEST["enable"];
+                       }
+               } else {
+                       $act_enable = $logdets["enable"];
+               }
+       }
        
-       error_log("calling $func with $hip, and $hconfig");
-       $func("$hip", $hconfig);
+       if($need_user || $need_pass || $need_enable) {
+               echo "No Username/Password information stored, please enter them now (they will not be saved)<br>";
+               echo "<form method=\"post\" action=\"?action=update&hip=10.172.192.1\">";
+               if($need_user) {
+                       echo "User <input type=\"text\" name=\"username\"><br>";
+               }
+               if($need_pass) echo "Password <input type=\"password\" name=\"password\"><br>";
+               if($need_enable) echo "Enable Password <input type=\"text\" name=\"enable\"><br>";
+               echo "<input type=\"submit\" name=\"Update\" value=\"Update\">";
+               echo "<form>";
+       } else {
        
+               $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>";
+               }
+       }
 }
 
 function www_hostTypesDropDown()
@@ -220,9 +287,6 @@ function www_deleteHost()
        } 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>";
-               echo "<pre>";
-               print_r($data);
-               echo "</pre>";
        }
 }
 
@@ -249,12 +313,17 @@ function www_addhost()
        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>
 <tr><td>Update Schedule</td><td><select name="updatesched"><?php www_updateSchedule() ?></select></td></tr></table>
-<input type="submit" value="Next" name="Next">
+<input type="submit" value="Add" name="Add">
 </form>
        <?php
 }
@@ -272,29 +341,35 @@ function www_addHostStageTwo()
        }
        if(isset($_REQUEST["hosttype"])) {
                $htype = $_REQUEST["hosttype"];
-               $func = $HOST_TYPE["$htype"]["configform"];
                $hip = $_REQUEST["hname"];
+               $user = $_REQUEST["username"];
+               $pass = $_REQUEST["password"];
+               $enable = $_REQUEST["enable"];
                $hname = $_REQUEST["hname"];
                $upsched = $_REQUEST["updatesched"];
-               if(function_exists($func)) {
-                       
-                       echo "<form method=\"post\" action=\"?action=addhostthree&hosttype=$htype&hostname=$hname&hostip=$hip&upsched=$upsched\">";
-                       $func();
-                       echo "<input type=\"submit\" value=\"Add\" name=\"Add\">";
-               } else echo "would call $func for $htype but it doesnt exist\n";
-       }
-}
+               
+               $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["hosttype"];
-               $func = $HOST_TYPE["$htype"]["postfunction"];
-               if(function_exists($func)) {
-                       $func();
-               } else echo "would call $func for $htype but it doesnt exist\n";
+               if($user != "" && $pass != "") {
+                       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>";
+               } else {
+                       echo "Host added. but cant run config test until you manually update<br>";
+               }
+               
+               //db_createTable("configs", "hostip", "configtime", "configdata");
+               db_insertData("configs", "$hip", time(), base64_encode($conf));
        }
-       
 }
+
 ?>
\ No newline at end of file