host group operations work very well now.
[glcas.git] / plugins / hosts.php
index f2e8e44..eb93989 100644 (file)
@@ -49,34 +49,83 @@ class GLCASHosts {
                
        }
        
+       
+       // TODO: this whole function wont work correctly
+       // once we move to hosts being in multiple groups..
        function doOperation($url)
        {
                $operation = $_REQUEST["operation"];
                $groupop = $_REQUEST["groupop"];
                $hosts = $this->config->getData("hosts");
                $groups = $this->config->getData("hostgroups");
+               $nogroup = false;
+               $hosts_to_do = null;
+               $nhosts = 0;
+               $groups_to_do = null;
+               $ngroups = 0;
                
                // first check if nogroup is selected
                if(isset($_REQUEST["nonegroup-selected"])) {
-                       echo "nonegroup operation<br>";
+                       //echo "nonegroup operation<br>";
+                       $nogroup = true;
+               }
+               
+               foreach($groups as $key=>$val) {
+                       $grpname = $val["category"];
+                       if(isset($_REQUEST["$grpname-selectgroup"])) {
+                               $group_to_do[$ngroups++] = $grpname;
+                               
+                       }
                }
                
                foreach($hosts as $key=>$val) {
+                       $dohost = false;
                        $hname = $val["category"];
+                       // this bits annoying TODO: find out why this happens
+                       $thname = preg_replace("/\./", "_", $hname);
                        $hip = $val["name"];
                        $hg = $val["val"];
-                       if(isset($_REQUEST["$hname-selecthost"])) {
-                               echo "Select host, $hname true<br>";
+                       
+                       echo "<br>Checking $hname, $hip, $hg<br>";
+                       
+                       // check if no group is on
+                       if($nogroup && $hg == "") {
+                               //echo "Set doing true on nogroup<br>";
+                               $dohost = true;
                        }
-               }
-               
-               foreach($groups as $key=>$val) {
-                       $grpname = $val["category"];
-                       if(isset($_REQUEST["$grpname-selectgroup"])) {
-                               echo "Select group, $grpname true<br>";
+                       
+                       // check if host was in a group selected
+                       if($group_to_do !== null) foreach($group_to_do as $dothese) {
+                               if($hg == $dothese) $dohost = true;
+                       }
+                       
+                       // directly selected hosts
+                       if(isset($_REQUEST["$thname-selecthost"])) {
+                               //echo "add host $hname<br>";
+                               $dohost = true;
+                       }
+                       
+                       // now do the op
+                       if($dohost) {
+                               if($operation == "move") {
+                                       $this->config->delData("hosts", "$hname", "$hip", "$hg");
+                                       $this->config->addData("hosts", "$hname", "$hip", "$groupop");
+                               }
+                               if($operation == "delete") {
+                                       $this->config->delData("hosts", "$hname", "$hip", "$hg");
+                               }
                        }
                }
                
+               /*echo "total to do:<br><pre>";
+               print_r($hosts_to_do);
+               print_r($_REQUEST);
+               echo "</pre>";*/
+               
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/hosts");
+               
+               
        }
        
        function deleteGroup($url)
@@ -109,37 +158,19 @@ class GLCASHosts {
                }
                
                // we just assume class c atm
-               $ips_v = explode(".", $iprange);
-               
-               $ips = $ips_v[0].".".$ips_v[1].".".$ips_v[2];
-               
-               for($i = 1; $i < 32; $i++) {
-                       $hostname = "";
-                       $ips_me = "$ips.$i";
-                       echo "Scanning $ips_me<br>";
-                       error_log("Scanning $ips_me<br>");
-                       flush();
-                       $hostname = gethostbyaddr($ips_me);
-                       if($hostname != $ips_me) {
-                               echo "Found host on $ips_me as $hostname<br>";
-                               error_log("Found host on $ips_me as $hostname");
-                               flush();
-                               $hosts = $this->config->getData("hosts");
-                               $exists = false;
-                               foreach($hosts as $key => $val) {
-                                       if($val["category"] == $hostname && $val["name"] == $ips_me) {
-                                               echo "Host in db already<br>";
-                                               $exists = true;
-                                               flush();
-                                       }
-                                       
-                               }
-                               if(!$exists) $this->config->addData("hosts", "$hostname", "$ips_me", "$hostgroup");
-                               
-                               
-                       }
+               error_log("background scanner, start");
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
+                       $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
+                       system($scall);
+               } else {
+                       error_log("cant find download helper... dieing");
                }
-               echo "Finished";
+               
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/hosts");
+               
+               
        }
        
        function deleteHost($url)
@@ -215,9 +246,10 @@ class GLCASHosts {
                echo "<form method=\"post\"action=\"?action=dooperation\">";
                echo "Selected hosts operation: <select name=\"operation\">";
                echo "<option value=\"move\">Move To</option>";
-               echo "<option value=\"move\">Delete</option>";
+               echo "<option value=\"delete\">Delete</option>";
                echo "</select>";
                echo "Group <select name=\"groupop\">";
+               echo "<option value=\"\">None</option>";
                foreach($groups as $key => $val) {
                        $grpname = $val["category"];
                        echo "<option value=\"$grpname\">$grpname</option>";
@@ -251,7 +283,7 @@ class GLCASHosts {
                                $grpname = $val["category"];
                                echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
                                echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"$grpname-selectgroup\"></th><th>Host</th><th>IP</th><th>Host Group</th><th>Last Seen</th><th>Control</th></tr>";
-                               foreach($hosts as $key => $val) {
+                               if($hosts != false) foreach($hosts as $key => $val) {
                                        $hname = $val["category"];
                                        $hip = $val["name"];
                                        $hg = $val["val"];