The buttonbar at the top of each page is now a fixed 'div' element
[gnucomo.git] / src / web / objects.php
1 <?php
2
3 /**************************************************************************
4 **  (c) Copyright 2003, Andromeda Technology & Automation
5 ** This is free software; you can redistribute it and/or modify it under the
6 ** terms of the GNU General Public License, see the file COPYING.
7 ***************************************************************************
8 ** MODULE INFORMATION *
9 ***********************
10 **      FILE NAME      : objects.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.11 $
13 **
14 **  DESCRIPTION      : Objects Administration page.
15 **                     Input parameters: action (POST) : empty, 'Create'
16 **                              objname (POST) : name of the object to create or remove
17 **
18 **  EXPORTED OBJECTS : 
19 **  LOCAL    OBJECTS : 
20 **  MODULES  USED    :
21 ***************************************************************************
22 **  ADMINISTRATIVE INFORMATION *
23 ********************************
24 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
25 **      CREATION DATE   : Dec 04, 2002
26 **      LAST UPDATE     : Aug 04, 2003
27 **      MODIFICATIONS   : 
28 **************************************************************************/
29
30 /*****************************
31    $Log: objects.php,v $
32    Revision 1.11  2007-11-21 14:38:06  arjen
33    The buttonbar at the top of each page is now a fixed 'div' element
34    instead of a framed page.
35    Contributed by Edwin Nadorp.
36
37    Revision 1.10  2005/06/04 07:24:38  arjen
38    New page: Abuse list
39
40    Revision 1.9  2003/09/01 06:55:00  arjen
41    Provides an interface to edit the list of
42    services and users for an object.
43
44    Revision 1.8  2003/08/14 10:35:07  arjen
45    Added editing of detailed object information.
46
47    Revision 1.7  2003/07/15 11:02:25  arjen
48    Use the new object_statistics table.
49
50    Revision 1.6  2003/02/21 08:46:58  arjen
51    Improved the table layout.
52
53    Revision 1.5  2003/02/13 09:01:29  arjen
54    All web interface pages use the page class.
55
56    Revision 1.4  2003/02/13 08:48:23  arjen
57    Added log, notification and parameter counters to the 'object' table.
58    Counting these things at the time a user interface needs them is
59    too slow. Other programs, like gcm_daemon en gcm_input should prepare
60    these counters for quick retrieval.
61
62    Revision 1.3  2003/02/10 15:42:24  arjen
63    Show the total number of Log entries, parameters and notifications
64
65    Revision 1.2  2003/02/05 09:48:14  arjen
66    Added display and handling of notifications
67
68 ******************************/
69
70 // RCSID = "$Id: objects.php,v 1.11 2007-11-21 14:38:06 arjen Exp $";
71
72 ini_set('include_path', '.:./classes:../phpclasses');
73
74 require_once('page.class.php');
75
76 function clientscripts()
77 {
78 ?>
79
80 <script type='text/ecmascript'>
81
82 function CheckCreate(f)
83 {
84    if (f.objectname.value == "")
85    {
86       alert("You must supply a name");
87       return false;
88    }
89    return true;
90 }
91
92 function CheckRemove(f)
93 {
94    var message = "Are you sure you want to remove object ";
95    message += f.objectname.value;
96    message += " ?";
97
98    return confirm(message);
99 }
100
101 </script>
102
103 <?php
104 }
105
106 class object_page extends page
107 {
108
109    var $nr_parameters, $removed_parameters;
110    var $nr_notifications, $closed_notifications;
111    var $nr_logs;
112
113    function GatherStatistics($objectid)
114    {
115       //  Gather statistics on parameters
116
117       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
118                            . $objectid . "' AS BIGINT) AND statname='parameters'");
119       $stat = pg_fetch_object($r, 0);
120       $this->nr_parameters = $stat->statvalue;
121
122       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
123                            . $objectid . "' AS BIGINT) AND statname='removed_parameters'");
124       $stat = pg_fetch_object($r, 0);
125       $this->removed_parameters = $stat->statvalue;
126
127       //  Gather statistics on notifications
128
129       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
130                            . $objectid . "' AS BIGINT) AND statname='notifications'");
131       $stat = pg_fetch_object($r, 0);
132       $this->nr_notifications = $stat->statvalue;
133
134       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
135                            . $objectid . "' AS BIGINT) AND statname='closed_notifications'");
136       $stat = pg_fetch_object($r, 0);
137       $this->closed_notifications = $stat->statvalue;
138
139       //  Gather statistics on log entries
140
141       $r = pg_exec ($this->database, "SELECT statvalue FROM object_statistics WHERE objectid=CAST('"
142                            . $objectid . "' AS BIGINT) AND statname='logs'");
143       $stat = pg_fetch_object($r, 0);
144       $this->nr_logs = $stat->statvalue;
145
146    }
147
148    function Body()
149    {
150       clientscripts();
151
152    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['objectname']))
153    {
154       pg_exec($this->database, "INSERT INTO object (objectname, log_count, parameter_count, notification_count)
155                                VALUES ('" . $_POST['objectname'] . "', '0', '0', '0')");
156    }
157
158    if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['objectname']))
159    {
160       pg_exec($this->database, "DELETE FROM object WHERE objectname='" . $_POST['objectname'] . "'");
161    }
162
163    if (isset($_GET['oid']))
164    {
165       echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = \"<h1>Detailed information for object " . $_GET['oid'] . "<\/h1>\"</script><br>\n";
166
167       if (isset($_POST['action']) && $_POST['action'] == 'Save Changes')
168       {
169          $qry = "UPDATE object SET objectname='" . $_POST['oname'] . "'";
170          $qry .= ", objectcode='" . $_POST['ocode'] . "'";
171          $qry .= ", object_description='" . $_POST['odescription'] . "'";
172          $qry .= ", object_owner='" . $_POST['oowner'] . "'";
173          $qry .= ", physical_location='" . $_POST['olocation'] . "'";
174          $qry .= ", remark='" . $_POST['oremark'] . "'";
175          $qry .= ", timezone='" . $_POST['otimezone'] . "'";
176          $qry .= " WHERE objectid=" . $_GET['oid'];
177
178          pg_exec($this->database, $qry);
179       }
180       else if (isset($_POST['action']) && $_POST['action'] == 'Modify Service')
181       {
182          $obj_srv_res = pg_exec($this->database, "SELECT * from object_service WHERE objectid='" . $_GET['oid']
183                               . "' AND servicecode='" . $_POST['servcode'] . "'");
184
185          if (pg_numrows($obj_srv_res) == 0)
186          {
187             //  Add a new service for this object
188             if (isset($_POST['servused']))
189             {
190                $qry = "INSERT INTO object_service (objectid, servicecode, expected_interval) ";
191                $qry .= "VALUES ('" . $_GET['oid'] . "', '" . $_POST['servcode'] . "', '";
192                $qry .= $_POST['expinterval'] . "')";
193
194                pg_exec($this->database, $qry);
195             }
196          }
197          else
198          {
199             //  Modify or remove the service for this object
200             if (isset($_POST['servused']))
201             {
202                //  Service is still used; update some values.
203                $qry = "UPDATE object_service SET expected_interval='" . $_POST['expinterval'];
204                $qry .= "' WHERE objectid='" . $_GET['oid'];
205                $qry .= "' AND servicecode='" . $_POST['servcode'] . "'";
206                pg_exec($this->database, $qry);
207             }
208             else
209             {
210                //  Checkbox was turned off, remove the service
211
212                $qry = "DELETE FROM object_service WHERE objectid='" . $_GET['oid'];
213                $qry .= "' AND servicecode='" . $_POST['servcode'] . "'";
214                pg_exec($this->database, $qry);
215             }
216          }
217       }
218       else if (isset($_POST['action']) && $_POST['action'] == 'Modify User')
219       {
220          $obj_srv_res = pg_exec($this->database, "SELECT * from object_user WHERE objectid='" . $_GET['oid']
221                               . "' AND username='" . $_POST['username'] . "'");
222
223          if (pg_numrows($obj_srv_res) == 0)
224          {
225             //  Add a new user for this object
226             if (isset($_POST['userused']))
227             {
228                $qry = "INSERT INTO object_user (objectid, username, security_level) ";
229                $qry .= "VALUES ('" . $_GET['oid'] . "', '" . $_POST['username'] . "', '";
230                $qry .= $_POST['seclevel'] . "')";
231
232                pg_exec($this->database, $qry);
233             }
234          }
235          else
236          {
237             //  Modify or remove the user for this object
238             if (isset($_POST['userused']))
239             {
240                //  Service is still used; update some values.
241                $qry = "UPDATE object_user SET security_level='" . $_POST['seclevel'];
242                $qry .= "' WHERE objectid='" . $_GET['oid'];
243                $qry .= "' AND username='" . $_POST['username'] . "'";
244                pg_exec($this->database, $qry);
245             }
246             else
247             {
248                //  Checkbox was turned off, remove the user
249
250                $qry = "DELETE FROM object_user WHERE objectid='" . $_GET['oid'];
251                $qry .= "' AND username='" . $_POST['username'] . "'";
252                pg_exec($this->database, $qry);
253             }
254          }
255       }
256
257       $res = pg_exec($this->database, "SELECT * FROM object
258                                        WHERE objectid='" . $_GET['oid'] . "'");
259       $obj = pg_fetch_object($res, 0);
260
261       echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
262       echo "<table>";
263
264       echo "<tr><td>Name</td><td><input name='oname' type='text' value='";
265       echo $obj->objectname . "'></td></tr>";
266       echo "<tr><td>Identification code</td><td><input name='ocode' type='text' value='";
267       echo $obj->objectcode . "'></td></tr>";
268       echo "<tr><td>Description</td><td><textarea name='odescription' rows='3' cols='30'>";
269       echo $obj->object_description . "</textarea></td></tr>";
270       echo "<tr><td>Owner</td><td><textarea name='oowner' rows='3' cols='30'>";
271       echo $obj->object_owner . "</textarea></td></tr>";
272       echo "<tr><td>Physical location</td><td><textarea name='olocation' rows='3' cols='30'>";
273       echo $obj->physical_location . "</textarea></td></tr>";
274       echo "<tr><td>Remarks</td><td><textarea name='oremark' rows='3' cols='30'>";
275       echo $obj->remark . "</textarea></td></tr>";
276       echo "<tr><td>Timezone</td><td><input name='otimezone' type='text' value='";
277       echo $obj->timezone . "'></td></tr>";
278
279       echo "</table>";
280       echo "<input type='submit' name='action' value='Save Changes'>";
281       echo "</form>";
282
283       echo "<h1>Services on " . $obj->objectname . "</h1><hr>\n";
284
285       echo "<table>";
286       echo "<tr><th>Service name</th><th>Last entry</th><th>Expected interval</th></tr>";
287       $srv_res = pg_exec($this->database, "SELECT servicecode, servicename FROM service
288                                            ORDER BY servicecode");
289       for ($srv_row = 0; $srv_row < pg_numrows($srv_res); $srv_row++)
290       {
291          $srv = pg_fetch_object($srv_res, $srv_row);
292          $srv_obj_res = pg_exec($this->database, "SELECT * FROM object_service WHERE objectid='"
293                                    . $obj->objectid . "' AND servicecode='" . $srv->servicecode . "'");
294
295          echo "<tr>";
296          echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
297          echo "<input type='hidden' name='servcode' value='" . $srv->servicecode . "'>";
298
299          if (pg_numrows($srv_obj_res) == 0)
300          {
301             echo "<td><input name='servused' type='checkbox'> " . $srv->servicename . "</td>";
302             echo "<td>&nbsp;</td>";
303             echo "<td><input name='expinterval' type='text' value='0'></td>";
304             echo "<td><input type='submit' name='action' value='Modify Service'></td>";
305          }
306          else
307          {
308             $obj_srv = pg_fetch_object($srv_obj_res, 0);
309             echo "<td><input name='servused' type='checkbox' checked='true'> " . $srv->servicename . "</td>";
310             echo "<td>" . $obj_srv->last_entry . "</td>";
311             echo "<td><input name='expinterval' type='text' value='" . $obj_srv->expected_interval . "'></td>";
312             echo "<td><input type='submit' name='action' value='Modify Service'></td>";
313          }
314          echo "</form></tr>";
315       }
316       echo "</table>";
317
318       echo "<h1>Users of " . $obj->objectname . "</h1><hr>\n";
319
320       echo "<table>";
321       echo "<tr><th>User name</th><th>Security level</th><th>&nbsp;</th></tr>";
322
323       $usr_res = pg_exec($this->database, "SELECT username, security_level FROM usr ORDER BY username");
324       for ($usr_row = 0; $usr_row < pg_numrows($usr_res); $usr_row++)
325       {
326          $usr = pg_fetch_object($usr_res, $usr_row);
327          $usr_obj_res = pg_exec($this->database, "SELECT * FROM object_user WHERE objectid='"
328                                    . $obj->objectid . "' AND username='" . $usr->username . "'");
329
330          echo "<tr>";
331          echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
332          echo "<input type='hidden' name='username' value='" . $usr->username . "'>";
333
334          if (pg_numrows($usr_obj_res) == 0)
335          {
336             echo "<td><input name='userused' type='checkbox'> " . $usr->username . "</td>";
337             echo "<td><input name='seclevel' type='text' value='" . $usr->security_level . "'></td>";
338             echo "<td><input type='submit' name='action' value='Modify User'></td>";
339          }
340          else
341          {
342             $usr_obj = pg_fetch_object($usr_obj_res, 0);
343             echo "<td><input name='userused' type='checkbox' checked='on'> " . $usr->username . "</td>";
344             echo "<td><input name='seclevel' type='text' value='" . $usr_obj->security_level . "'></td>";
345             echo "<td><input type='submit' name='action' value='Modify User'></td>";
346          }
347          echo "</form></tr>";
348       }
349       echo "</table>";
350    }
351    else
352    {
353       echo "<script type='text/ecmascript'>
354             document.getElementById('menu_title').innerHTML = \"<h1>Objects Administration<\/h1>\"
355             </script>
356             <br>";
357
358
359       $res = pg_exec($this->database, "SELECT * FROM object ORDER BY objectname");
360 ?>
361
362       <table>
363       <tr><th>Object</th><th>Description</th><th>Log entries</th>
364           <th>Parameters</th><th>Notifications</th><th>Abuses</th></tr>
365
366 <?php
367       $obj = 0;
368
369       //The counters are set to zero
370       $count_logs = 0;
371       $count_notifications = 0;
372       $closed_notifications = 0;
373       $count_parameters = 0;
374       $removed_parameters = 0;
375
376       while ($obj < pg_numrows($res))
377       {
378          $u = pg_fetch_object($res, $obj);
379
380          $this->GatherStatistics($u->objectid);
381
382          $count_parameters += $this->nr_parameters;
383          $removed_parameters += $this->removed_parameters;
384          $count_logs = $count_logs + $this->nr_logs;
385
386          $count_notifications += $this->nr_notifications;
387          $closed_notifications += $this->closed_notifications;
388          ?>
389          <tr><td><center><a href='objects.php?oid=<?php echo $u->objectid?>'><img src='server.png' alt='server'></a><br>
390                 <b><?php echo $u->objectname ?></b></center>
391          </td><td>
392                <?php echo nl2br($u->object_description) ?>
393          </td><td class='number'>
394              <?php if ( $this->nr_logs > 0 )
395                    { 
396                        echo "<a href='log.php?oid=$u->objectid'> $this->nr_logs </a>";
397                    }
398                    else
399                    { 
400                       echo "0";
401                    }
402               ?>
403          </td><td class='number'>
404              <?php echo "<a href='parameter.php?oid=$u->objectid'>" . $this->nr_parameters
405                       . " (" . $this->removed_parameters . " removed)</a>" ?>
406          </td><td class='number'>
407              <?php if( $this->nr_notifications == $this->closed_notifications )
408                    {
409                       echo $this->nr_notifications . " (" . $this->closed_notifications . " closed)"; 
410                    }
411                    else
412                    {
413                       echo "<a href='notification.php?oid=$u->objectid'>" . $this->nr_notifications
414                       . " (" . $this->closed_notifications . " closed)</a>"; 
415                    }
416               ?>
417          </td><td class='number'>
418              <?php echo "<a href='abuse.php?oid=$u->objectid'>" . "Abuse list"
419                       . " </a>" ?>
420          </td><td>
421              <form action='objects.php' method='post' onSubmit='return CheckRemove(this)'>
422                  <input type='hidden' name='objectname' value='<?php echo $u->objectname ?>'>
423                  <input type='submit' name='action' value='Remove'>
424              </form>
425          </td></tr>
426          <?php
427          $obj++;
428       }
429
430       //Show the totals
431       echo "<tr><td><strong><B><br><br>TOTALS</B></strong></td>";
432       echo "<td>&nbsp;</td>";
433       echo "<td class='number'>$count_logs</td>";
434       echo "<td class='number'>$count_parameters ($removed_parameters removed)</td>";
435       echo "<td class='number'>$count_notifications ($closed_notifications closed)</td>";
436       echo "<td>&nbsp;</td>";
437       echo "</tr>\n";
438       echo "</table>\n";
439
440 ?>
441
442 <h2>Create new object:</h2>
443
444 <form action='objects.php' method='post' onSubmit='return CheckCreate(this)'>
445 Object's name (FQDN): <input name='objectname' type='text'>
446 <br>
447 <input type='submit' name='action' value='Create'>
448 </form>
449
450 <?php
451
452    }
453    }
454 }
455
456 $obj_page = new object_page("Gnucomo Objects Administration");
457
458 $obj_page->Showpage();
459
460 ?>
461