added alot of host gorup operations
[glcas.git] / plugins / hosts.php
1 <?php 
2
3 error_log("hosts loaded");
4
5 global $URL_HANDLERS;
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";
10
11 class GLCASHosts {
12         function __construct($config)
13         {
14                 $this->config = $config;
15                 error_log("constructor for GLCASHosts");
16                 
17         }
18         
19         function go($url)
20         {
21                 if(isset($_REQUEST["action"])) {
22                         switch($_REQUEST["action"]) {
23                                 case "addhost":
24                                         error_log("in updaterepo");
25                                         GLCASpageBuilder($this,"addHost");
26                                         return;
27                                 case "addgroup":
28                                         error_log("in add group");
29                                         GLCASpageBuilder($this, "addGroup");
30                                         return;
31                                 case "deletehost":
32                                         error_log("in add group");
33                                         GLCASpageBuilder($this, "deleteHost");
34                                         return;
35                                 case "scanrange":
36                                         GLCASpageBuilder($this, "scanIPRange");
37                                         return;
38                                 case "delgroup":
39                                         GLCASpageBuilder($this, "deleteGroup");
40                                         return;
41                                 case "dooperation":
42                                         GLCASpageBuilder($this, "doOperation");
43                                         return;
44                         }
45                 }
46                 
47                 GLCASpageBuilder($this,"mainBody");
48                 return;
49                 
50         }
51         
52         function doOperation($url)
53         {
54                 $operation = $_REQUEST["operation"];
55                 $groupop = $_REQUEST["groupop"];
56                 $hosts = $this->config->getData("hosts");
57                 $groups = $this->config->getData("hostgroups");
58                 
59                 // first check if nogroup is selected
60                 if(isset($_REQUEST["nonegroup-selected"])) {
61                         echo "nonegroup operation<br>";
62                 }
63                 
64                 foreach($hosts as $key=>$val) {
65                         $hname = $val["category"];
66                         $hip = $val["name"];
67                         $hg = $val["val"];
68                         if(isset($_REQUEST["$hname-selecthost"])) {
69                                 echo "Select host, $hname true<br>";
70                         }
71                 }
72                 
73                 foreach($groups as $key=>$val) {
74                         $grpname = $val["category"];
75                         if(isset($_REQUEST["$grpname-selectgroup"])) {
76                                 echo "Select group, $grpname true<br>";
77                         }
78                 }
79                 
80         }
81         
82         function deleteGroup($url)
83         {
84                 $grpname = $_REQUEST["grpname"];
85                 $this->config->delData("hostgroups", "$grpname");
86                 $hosts = $this->config->getData("hosts");
87                 foreach($hosts as $key=>$val) {
88                         $hname = $val["category"];
89                         $hip = $val["name"];
90                         $hg = $val["val"];
91                         
92                         if($hg == $grpname) {
93                                 $this->config->delData("hosts", "$hname", "$hip", "$hg");
94                                 $this->config->addData("hosts", "$hname", "$hip", "");
95                         }
96                 }
97                 
98                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
99                 header("Location: $BASE_URL/hosts");
100                 
101         }
102         
103         function scanIPRange($url)
104         {
105                 $iprange = $_REQUEST["scanip"];
106                 $hostgroup = "";
107                 if(isset($_REQUEST["hostgroup"])) {
108                         $hostgroup = $_REQUEST["hostgroup"];
109                 }
110                 
111                 // we just assume class c atm
112                 $ips_v = explode(".", $iprange);
113                 
114                 $ips = $ips_v[0].".".$ips_v[1].".".$ips_v[2];
115                 
116                 for($i = 1; $i < 32; $i++) {
117                         $hostname = "";
118                         $ips_me = "$ips.$i";
119                         echo "Scanning $ips_me<br>";
120                         error_log("Scanning $ips_me<br>");
121                         flush();
122                         $hostname = gethostbyaddr($ips_me);
123                         if($hostname != $ips_me) {
124                                 echo "Found host on $ips_me as $hostname<br>";
125                                 error_log("Found host on $ips_me as $hostname");
126                                 flush();
127                                 $hosts = $this->config->getData("hosts");
128                                 $exists = false;
129                                 foreach($hosts as $key => $val) {
130                                         if($val["category"] == $hostname && $val["name"] == $ips_me) {
131                                                 echo "Host in db already<br>";
132                                                 $exists = true;
133                                                 flush();
134                                         }
135                                         
136                                 }
137                                 if(!$exists) $this->config->addData("hosts", "$hostname", "$ips_me", "$hostgroup");
138                                 
139                                 
140                         }
141                 }
142                 echo "Finished";
143         }
144         
145         function deleteHost($url)
146         {
147                 $hostname = $_REQUEST["hostname"];
148                 $this->config->delData("hosts", "$hostname");
149                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
150                 header("Location: $BASE_URL/hosts");
151         }
152         
153         function addHost($url)
154         {
155                 $hg = $_REQUEST["hostgroup"];
156                 $hostname = $_REQUEST["hostname"];
157                 $ip = $_REQUEST["ipaddr"];
158                 
159                 if($hostname == "" && $ip == "") {
160                         echo "Error: must have either ip or hostname\n";
161                         return 0;
162                 }
163                 
164                 if($hostname == "") {
165                         // try to lookup hostname from ip
166                         $hostname = gethostbyaddr($ip);
167                 }
168                 
169                 if($ip == "") {
170                         // try to lookup ip from hostname
171                         $ip = gethostbyname($hostname);
172                 }
173                 
174                 $hosts = $this->config->getData("hosts");
175                 foreach($hosts as $key => $val) {
176                         if($val["category"] == $hostname && $val["name"] == $ip) {
177                                 echo "Error: host already exists<br>";
178                                 return 0;
179                         }
180                 }
181                 
182                 $this->config->addData("hosts", "$hostname", "$ip", "$hg");
183                 
184                 
185                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
186                 header("Location: $BASE_URL/hosts");
187                 
188         }
189         
190         function addGroup($url)
191         {
192                 $grpname = $_REQUEST["groupname"];
193                 $groups = $this->config->getData("hostgroups");
194                 
195                 foreach($groups as $key => $val) {
196                         if($val["category"] == $grpname) {
197                                 echo "Error: gorup already exists";
198                                 return 0;
199                         }
200                 }
201                 
202                 $this->config->adddata("hostgroups", "$grpname", "", "");
203                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
204                 header("Location: $BASE_URL/hosts");
205                 
206         }
207         
208         function mainBody($url)
209         {
210                 $hosts = $this->config->getData("hosts");
211                 $groups = $this->config->getData("hostgroups");
212                 
213                 $gs = 0;
214                 echo "<h2>Hosts and Groups</h2>";
215                 echo "<form method=\"post\"action=\"?action=dooperation\">";
216                 echo "Selected hosts operation: <select name=\"operation\">";
217                 echo "<option value=\"move\">Move To</option>";
218                 echo "<option value=\"move\">Delete</option>";
219                 echo "</select>";
220                 echo "Group <select name=\"groupop\">";
221                 foreach($groups as $key => $val) {
222                         $grpname = $val["category"];
223                         echo "<option value=\"$grpname\">$grpname</option>";
224                 }
225                 echo "</select>";
226                 echo "<input type=\"submit\" name=\"Go\" value=\"Go\">";
227                 
228                 
229                 echo "<table border=\"1\">";
230                 
231                 $gs++;
232                 // fist print ungrouped
233                 echo "<tr valign=\"top\">";
234                 echo "<td>";
235                 echo "<h3>Ungrouped Hosts</h3><br>";
236                 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>";
237                 foreach($hosts as $key => $val) {
238                         $hname = $val["category"];
239                         $hip = $val["name"];
240                         $hg = $val["val"];
241                         if($hg == "") $hg = "-";
242                         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>";
243                 }
244                 echo "</table>";
245                 echo "</td>";
246
247                 if($groups) {
248                         foreach($groups as $key=>$val) {
249                                 $gs++;
250                                 echo "<td>";
251                                 $grpname = $val["category"];
252                                 echo "<h3>Host Group: $grpname <a href=\"?action=delgroup&grpname=$grpname\">Delete</a></h3><br>";
253                                 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>";
254                                 foreach($hosts as $key => $val) {
255                                         $hname = $val["category"];
256                                         $hip = $val["name"];
257                                         $hg = $val["val"];
258                                         if($hg == "") $hg = "-";
259                                         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>";
260                                 }
261                                 echo "</table>";
262                                 echo "</td>";
263                                 if(($gs%4)==0) echo "</tr><tr>";
264                         }
265                 }
266                 
267                 
268                 echo "</tr></table>";
269                 echo "</form>";
270                 
271                 echo "<hr>";
272                 
273                 
274                 
275                 echo "<table><tr valign=\"top\"><td>";
276                 
277                 // the add hosts dialog
278                 echo "<h2>Add Host</h2>";
279                 echo "<form method=\"post\" action=\"?action=addhost\">";
280                 echo "Hostname: <input type=\"text\" name=\"hostname\"><br>";
281                 echo "IP Address: <input type=\"text\" name=\"ipaddr\"><br>";
282                 if($groups) {
283                         echo "Host Group: <select name=\"hostgroup\">";
284                         echo "<option value=\"\">None</option>";
285                         foreach($groups as $key => $val) {
286                                 $hgname = $val["category"];
287                                 echo "<option value=\"$hgname\">$hgname</option>";
288                         }
289                         echo "</select><br>";
290                 }
291                 echo "<input type=\"submit\" name=\"add\" value=\"Add\">";
292                 echo "</form>";
293                 
294                 echo "</td><td>";
295                 
296                 // the add groups dialog
297                 echo "<h2>Add Group</h2>";
298                 echo "<form method=\"post\" action=\"?action=addgroup\">";
299                 echo "Groupname: <input type=\"text\" name=\"groupname\"><br>";
300                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
301                 echo "</form>";
302                 
303                 echo "</td><td>";
304                 //scan ip range via dns
305                 echo "<h2>Scan Range</h2>";
306                 echo "<form method=\"post\" action=\"?action=scanrange\">";
307                 echo "Range (i.e. 10.1.2.0): <input type=\"text\" name=\"scanip\"><br>";
308                 if($groups) {
309                         echo "Host Group: <select name=\"hostgroup\">";
310                         echo "<option value=\"\">None</option>";
311                         foreach($groups as $key => $val) {
312                                 $hgname = $val["category"];
313                                 echo "<option value=\"$hgname\">$hgname</option>";
314                         }
315                         echo "</select><br>";
316                 }
317                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
318                 echo "</form>";
319                                 
320                 echo "</table>";
321                 
322         }
323         
324         private $config;
325 }
326
327 ?>