3 error_log("hosts loaded");
6 $URL_HANDLERS["hosts.*"] = "GLCASHosts";
7 global $BASE_URL, $MENU_ITEMS;
8 $MENU_ITEMS["30hosts"]["name"] = "Known Machines";
9 $MENU_ITEMS["30hosts"]["link"] = "$BASE_URL/hosts";
12 function __construct($config)
14 $this->config = $config;
15 error_log("constructor for GLCASHosts");
21 if(isset($_REQUEST["action"])) {
22 switch($_REQUEST["action"]) {
24 error_log("in updaterepo");
25 GLCASpageBuilder($this,"addHost");
28 error_log("in add group");
29 GLCASpageBuilder($this, "addGroup");
32 error_log("in add group");
33 GLCASpageBuilder($this, "deleteHost");
36 GLCASpageBuilder($this, "scanIPRange");
39 GLCASpageBuilder($this, "deleteGroup");
42 GLCASpageBuilder($this, "doOperation");
47 GLCASpageBuilder($this,"mainBody");
53 // TODO: this whole function wont work correctly
54 // once we move to hosts being in multiple groups..
55 function doOperation($url)
57 $operation = $_REQUEST["operation"];
58 $groupop = $_REQUEST["groupop"];
59 $hosts = $this->config->getData("hosts");
60 $groups = $this->config->getData("hostgroups");
67 // first check if nogroup is selected
68 if(isset($_REQUEST["nonegroup-selected"])) {
69 //echo "nonegroup operation<br>";
73 foreach($groups as $key=>$val) {
74 $grpname = $val["category"];
75 if(isset($_REQUEST["$grpname-selectgroup"])) {
76 $group_to_do[$ngroups++] = $grpname;
81 foreach($hosts as $key=>$val) {
83 $hname = $val["category"];
84 // this bits annoying TODO: find out why this happens
85 $thname = preg_replace("/\./", "_", $hname);
89 echo "<br>Checking $hname, $hip, $hg<br>";
91 // check if no group is on
92 if($nogroup && $hg == "") {
93 //echo "Set doing true on nogroup<br>";
97 // check if host was in a group selected
98 if($group_to_do !== null) foreach($group_to_do as $dothese) {
99 if($hg == $dothese) $dohost = true;
102 // directly selected hosts
103 if(isset($_REQUEST["$thname-selecthost"])) {
104 //echo "add host $hname<br>";
110 if($operation == "move") {
111 $this->config->delData("hosts", "$hname", "$hip", "$hg");
112 $this->config->addData("hosts", "$hname", "$hip", "$groupop");
114 if($operation == "delete") {
115 $this->config->delData("hosts", "$hname", "$hip", "$hg");
120 /*echo "total to do:<br><pre>";
121 print_r($hosts_to_do);
125 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
126 header("Location: $BASE_URL/hosts");
131 function deleteGroup($url)
133 $grpname = $_REQUEST["grpname"];
134 $this->config->delData("hostgroups", "$grpname");
135 $hosts = $this->config->getData("hosts");
136 foreach($hosts as $key=>$val) {
137 $hname = $val["category"];
141 if($hg == $grpname) {
142 $this->config->delData("hosts", "$hname", "$hip", "$hg");
143 $this->config->addData("hosts", "$hname", "$hip", "");
147 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
148 header("Location: $BASE_URL/hosts");
152 function scanIPRange($url)
154 $iprange = $_REQUEST["scanip"];
156 if(isset($_REQUEST["hostgroup"])) {
157 $hostgroup = $_REQUEST["hostgroup"];
160 // we just assume class c atm
161 error_log("background scanner, start");
162 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
163 if(file_exists("$WEB_ROOT_FS/../bin/scanhelper.php")) {
164 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/scanhelper.php '$iprange' '$hostgroup' > /tmp/scanlog 2>&1 &";
167 error_log("cant find download helper... dieing");
170 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
171 header("Location: $BASE_URL/hosts");
176 function deleteHost($url)
178 $hostname = $_REQUEST["hostname"];
179 $this->config->delData("hosts", "$hostname");
180 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
181 header("Location: $BASE_URL/hosts");
184 function addHost($url)
186 $hg = $_REQUEST["hostgroup"];
187 $hostname = $_REQUEST["hostname"];
188 $ip = $_REQUEST["ipaddr"];
190 if($hostname == "" && $ip == "") {
191 echo "Error: must have either ip or hostname\n";
195 if($hostname == "") {
196 // try to lookup hostname from ip
197 $hostname = gethostbyaddr($ip);
201 // try to lookup ip from hostname
202 $ip = gethostbyname($hostname);
205 $hosts = $this->config->getData("hosts");
206 foreach($hosts as $key => $val) {
207 if($val["category"] == $hostname && $val["name"] == $ip) {
208 echo "Error: host already exists<br>";
213 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
216 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
217 header("Location: $BASE_URL/hosts");
221 function addGroup($url)
223 $grpname = $_REQUEST["groupname"];
224 $groups = $this->config->getData("hostgroups");
226 foreach($groups as $key => $val) {
227 if($val["category"] == $grpname) {
228 echo "Error: gorup already exists";
233 $this->config->adddata("hostgroups", "$grpname", "", "");
234 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
235 header("Location: $BASE_URL/hosts");
239 function mainBody($url)
241 $hosts = $this->config->getData("hosts");
242 $groups = $this->config->getData("hostgroups");
245 echo "<h2>Hosts and Groups</h2>";
246 echo "<form method=\"post\"action=\"?action=dooperation\">";
247 echo "Selected hosts operation: <select name=\"operation\">";
248 echo "<option value=\"move\">Move To</option>";
249 echo "<option value=\"delete\">Delete</option>";
251 echo "Group <select name=\"groupop\">";
252 echo "<option value=\"\">None</option>";
253 foreach($groups as $key => $val) {
254 $grpname = $val["category"];
255 echo "<option value=\"$grpname\">$grpname</option>";
258 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
261 echo "<table border=\"1\">";
264 // fist print ungrouped
265 echo "<tr valign=\"top\">";
267 echo "<h3>Ungrouped Hosts</h3><br>";
268 echo "<table border=\"1\"><tr><th><input type=\"checkbox\" name=\"nonegroup-selected\"></th><th>Host</th><th>IP</th><th>Host Group</th><th>Last Seen</th><th>Control</th></tr>";
269 foreach($hosts as $key => $val) {
270 $hname = $val["category"];
273 if($hg == "") $hg = "-";
274 if($hg=="-") echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
280 foreach($groups as $key=>$val) {
283 $grpname = $val["category"];
284 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
285 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>";
286 if($hosts != false) foreach($hosts as $key => $val) {
287 $hname = $val["category"];
290 if($hg == "") $hg = "-";
291 if($hg==$grpname) echo "<tr><td><input type=\"checkbox\" name=\"$hname-selecthost\"></td><td>$hname</td><td>$hip</td><td>$hg</td><td>...</td><td><a href=\"?action=deletehost&hostname=$hname\">Delete</a></tr>";
295 if(($gs%4)==0) echo "</tr><tr>";
300 echo "</tr></table>";
307 echo "<table><tr valign=\"top\"><td>";
309 // the add hosts dialog
310 echo "<h2>Add Host</h2>";
311 echo "<form method=\"post\" action=\"?action=addhost\">";
312 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
313 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
315 echo "Host Group: <select name=\"hostgroup\">";
316 echo "<option value=\"\">None</option>";
317 foreach($groups as $key => $val) {
318 $hgname = $val["category"];
319 echo "<option value=\"$hgname\">$hgname</option>";
321 echo "</select><br>";
323 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
328 // the add groups dialog
329 echo "<h2>Add Group</h2>";
330 echo "<form method=\"post\" action=\"?action=addgroup\">";
331 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
332 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
336 //scan ip range via dns
337 echo "<h2>Scan Range</h2>";
338 echo "<form method=\"post\" action=\"?action=scanrange\">";
339 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
341 echo "Host Group: <select name=\"hostgroup\">";
342 echo "<option value=\"\">None</option>";
343 foreach($groups as $key => $val) {
344 $hgname = $val["category"];
345 echo "<option value=\"$hgname\">$hgname</option>";
347 echo "</select><br>";
349 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";