When removing an object, clean up logs, parameters and notifications.
[gnucomo.git] / src / web / objects.php
index 7222e7b..b17e638 100644 (file)
@@ -9,11 +9,11 @@
 ***********************
 **      FILE NAME      : objects.php
 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
-**      VERSION NUMBER : $Revision: 1.7 $
+**      VERSION NUMBER : $Revision: 1.12 $
 **
 **  DESCRIPTION      : Objects Administration page.
 **                     Input parameters: action (POST) : empty, 'Create'
-**                                      objname (POST) : name of the object to create or remove
+**                              objname (POST) : name of the object to create or remove
 **
 **  EXPORTED OBJECTS : 
 **  LOCAL    OBJECTS : 
 ********************************
 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
 **      CREATION DATE   : Dec 04, 2002
-**      LAST UPDATE     : Feb 14, 2003
+**      LAST UPDATE     : Jul 13, 2010
 **      MODIFICATIONS   : 
 **************************************************************************/
 
 /*****************************
    $Log: objects.php,v $
-   Revision 1.7  2003-07-15 11:02:25  arjen
+   Revision 1.12  2011-03-24 09:59:21  arjen
+   When removing an object, clean up logs, parameters and notifications.
+
+   Revision 1.11  2007/11/21 14:38:06  arjen
+   The buttonbar at the top of each page is now a fixed 'div' element
+   instead of a framed page.
+   Contributed by Edwin Nadorp.
+
+   Revision 1.10  2005/06/04 07:24:38  arjen
+   New page: Abuse list
+
+   Revision 1.9  2003/09/01 06:55:00  arjen
+   Provides an interface to edit the list of
+   services and users for an object.
+
+   Revision 1.8  2003/08/14 10:35:07  arjen
+   Added editing of detailed object information.
+
+   Revision 1.7  2003/07/15 11:02:25  arjen
    Use the new object_statistics table.
 
    Revision 1.6  2003/02/21 08:46:58  arjen
@@ -52,7 +70,7 @@
 
 ******************************/
 
-// RCSID = "$Id: objects.php,v 1.7 2003-07-15 11:02:25 arjen Exp $";
+// RCSID = "$Id: objects.php,v 1.12 2011-03-24 09:59:21 arjen Exp $";
 
 ini_set('include_path', '.:./classes:../phpclasses');
 
@@ -62,7 +80,7 @@ function clientscripts()
 {
 ?>
 
-<script language='JavaScript'>
+<script type='text/ecmascript'>
 
 function CheckCreate(f)
 {
@@ -134,92 +152,346 @@ class object_page extends page
    {
       clientscripts();
 
-   echo "<h1>Objects Administration</h1><hr>";
-
    if (isset($_POST['action']) && $_POST['action'] == 'Create' && !empty($_POST['objectname']))
    {
       pg_exec($this->database, "INSERT INTO object (objectname, log_count, parameter_count, notification_count)
                                VALUES ('" . $_POST['objectname'] . "', '0', '0', '0')");
    }
 
-   if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['objectname']))
+   if (isset($_POST['action']) && $_POST['action'] == 'Remove' && !empty($_POST['objectid']))
    {
-      pg_exec($this->database, "DELETE FROM object WHERE objectname='" . $_POST['objectname'] . "'");
+      $oid = $_POST['objectid'];
+
+      //  Cleanup notifications for this object
+
+      $r = pg_exec($this->database, "SELECT notificationid FROM notification WHERE objectid='$oid'");
+      for ($i = 0; $i < pg_numrows($r); $i++)
+      {
+         $notif = pg_fetch_object($r, $i);
+         $notid = $notif->notificationid;
+         pg_exec($this->database, "DELETE FROM log_notification WHERE notificationid='$notid'");
+         pg_exec($this->database, "DELETE FROM action_user WHERE notificationid='$notid'");
+         pg_exec($this->database, "DELETE FROM parameter_notification WHERE notificationid='$notid'");
+      }
+      pg_exec($this->database, "DELETE FROM notification WHERE objectid='$oid'");
+
+      //  Cleanup parameters for this object
+
+      $r = pg_exec($this->database, "SELECT paramid FROM parameter WHERE objectid='$oid'");
+      for ($i = 0; $i < pg_numrows($r); $i++)
+      {
+         $param = pg_fetch_object($r, $i);
+         $parid = $param->paramid;
+         pg_exec($this->database, "DELETE FROM history WHERE paramid='$parid'");
+         pg_exec($this->database, "DELETE FROM property WHERE paramid='$parid'");
+      }
+      pg_exec($this->database, "DELETE FROM parameter WHERE objectid='$oid'");
+
+      //  Cleanup log and abuses for this object
+
+      pg_exec($this->database, "DELETE FROM log_abuse WHERE objectid='$oid'");
+      pg_exec($this->database, "DELETE FROM object_abuse WHERE objectid='$oid'");
+      pg_exec($this->database, "DELETE FROM log WHERE objectid='$oid'");
+
+      //  Cleanup services, statistics, users and finally, the object itself.
+
+      pg_exec($this->database, "DELETE FROM object_service WHERE objectid='$oid'");
+      pg_exec($this->database, "DELETE FROM object_statistics WHERE objectid='$oid'");
+      pg_exec($this->database, "DELETE FROM object_user WHERE objectid='$oid'");
+      pg_exec($this->database, "DELETE FROM object WHERE objectid='$oid'");
    }
 
-   $res = pg_exec($this->database, "SELECT objectid,objectname, log_count, notification_count
-                                    FROM object ORDER BY objectname");
-?>
-
-   <table>
-   <tr><th>Object</th><th>Log entries</th>
-       <th>Parameters</th><th>Notifications</th>
-   </tr>
+   if (isset($_GET['oid']))
+   {
+      echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = \"<h1>Detailed information for object " . $_GET['oid'] . "<\/h1>\"</script><br>\n";
+
+      if (isset($_POST['action']) && $_POST['action'] == 'Save Changes')
+      {
+         $qry = "UPDATE object SET objectname='" . $_POST['oname'] . "'";
+         $qry .= ", objectcode='" . $_POST['ocode'] . "'";
+         $qry .= ", object_description='" . $_POST['odescription'] . "'";
+         $qry .= ", object_owner='" . $_POST['oowner'] . "'";
+         $qry .= ", physical_location='" . $_POST['olocation'] . "'";
+         $qry .= ", remark='" . $_POST['oremark'] . "'";
+         $qry .= ", timezone='" . $_POST['otimezone'] . "'";
+         $qry .= " WHERE objectid=" . $_GET['oid'];
+
+         pg_exec($this->database, $qry);
+      }
+      else if (isset($_POST['action']) && $_POST['action'] == 'Modify Service')
+      {
+         $obj_srv_res = pg_exec($this->database, "SELECT * from object_service WHERE objectid='" . $_GET['oid']
+                              . "' AND servicecode='" . $_POST['servcode'] . "'");
+
+         if (pg_numrows($obj_srv_res) == 0)
+         {
+            //  Add a new service for this object
+            if (isset($_POST['servused']))
+            {
+               $qry = "INSERT INTO object_service (objectid, servicecode, expected_interval) ";
+               $qry .= "VALUES ('" . $_GET['oid'] . "', '" . $_POST['servcode'] . "', '";
+               $qry .= $_POST['expinterval'] . "')";
+
+               pg_exec($this->database, $qry);
+            }
+         }
+         else
+         {
+            //  Modify or remove the service for this object
+            if (isset($_POST['servused']))
+            {
+               //  Service is still used; update some values.
+               $qry = "UPDATE object_service SET expected_interval='" . $_POST['expinterval'];
+               $qry .= "' WHERE objectid='" . $_GET['oid'];
+               $qry .= "' AND servicecode='" . $_POST['servcode'] . "'";
+               pg_exec($this->database, $qry);
+            }
+            else
+            {
+               //  Checkbox was turned off, remove the service
+
+               $qry = "DELETE FROM object_service WHERE objectid='" . $_GET['oid'];
+               $qry .= "' AND servicecode='" . $_POST['servcode'] . "'";
+               pg_exec($this->database, $qry);
+            }
+         }
+      }
+      else if (isset($_POST['action']) && $_POST['action'] == 'Modify User')
+      {
+         $obj_srv_res = pg_exec($this->database, "SELECT * from object_user WHERE objectid='" . $_GET['oid']
+                              . "' AND username='" . $_POST['username'] . "'");
+
+         if (pg_numrows($obj_srv_res) == 0)
+         {
+            //  Add a new user for this object
+            if (isset($_POST['userused']))
+            {
+               $qry = "INSERT INTO object_user (objectid, username, security_level) ";
+               $qry .= "VALUES ('" . $_GET['oid'] . "', '" . $_POST['username'] . "', '";
+               $qry .= $_POST['seclevel'] . "')";
+
+               pg_exec($this->database, $qry);
+            }
+         }
+         else
+         {
+            //  Modify or remove the user for this object
+            if (isset($_POST['userused']))
+            {
+               //  Service is still used; update some values.
+               $qry = "UPDATE object_user SET security_level='" . $_POST['seclevel'];
+               $qry .= "' WHERE objectid='" . $_GET['oid'];
+               $qry .= "' AND username='" . $_POST['username'] . "'";
+               pg_exec($this->database, $qry);
+            }
+            else
+            {
+               //  Checkbox was turned off, remove the user
+
+               $qry = "DELETE FROM object_user WHERE objectid='" . $_GET['oid'];
+               $qry .= "' AND username='" . $_POST['username'] . "'";
+               pg_exec($this->database, $qry);
+            }
+         }
+      }
+
+      $res = pg_exec($this->database, "SELECT * FROM object
+                                       WHERE objectid='" . $_GET['oid'] . "'");
+      $obj = pg_fetch_object($res, 0);
+
+      echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
+      echo "<table>";
+
+      echo "<tr><td>Name</td><td><input name='oname' type='text' value='";
+      echo $obj->objectname . "'></td></tr>";
+      echo "<tr><td>Identification code</td><td><input name='ocode' type='text' value='";
+      echo $obj->objectcode . "'></td></tr>";
+      echo "<tr><td>Description</td><td><textarea name='odescription' rows='3' cols='30'>";
+      echo $obj->object_description . "</textarea></td></tr>";
+      echo "<tr><td>Owner</td><td><textarea name='oowner' rows='3' cols='30'>";
+      echo $obj->object_owner . "</textarea></td></tr>";
+      echo "<tr><td>Physical location</td><td><textarea name='olocation' rows='3' cols='30'>";
+      echo $obj->physical_location . "</textarea></td></tr>";
+      echo "<tr><td>Remarks</td><td><textarea name='oremark' rows='3' cols='30'>";
+      echo $obj->remark . "</textarea></td></tr>";
+      echo "<tr><td>Timezone</td><td><input name='otimezone' type='text' value='";
+      echo $obj->timezone . "'></td></tr>";
+
+      echo "</table>";
+      echo "<input type='submit' name='action' value='Save Changes'>";
+      echo "</form>";
+
+      echo "<h1>Services on " . $obj->objectname . "</h1><hr>\n";
+
+      echo "<table>";
+      echo "<tr><th>Service name</th><th>Last entry</th><th>Expected interval</th></tr>";
+      $srv_res = pg_exec($this->database, "SELECT servicecode, servicename FROM service
+                                           ORDER BY servicecode");
+      for ($srv_row = 0; $srv_row < pg_numrows($srv_res); $srv_row++)
+      {
+         $srv = pg_fetch_object($srv_res, $srv_row);
+         $srv_obj_res = pg_exec($this->database, "SELECT * FROM object_service WHERE objectid='"
+                                   . $obj->objectid . "' AND servicecode='" . $srv->servicecode . "'");
+
+         echo "<tr>";
+         echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
+         echo "<input type='hidden' name='servcode' value='" . $srv->servicecode . "'>";
+
+         if (pg_numrows($srv_obj_res) == 0)
+         {
+            echo "<td><input name='servused' type='checkbox'> " . $srv->servicename . "</td>";
+            echo "<td>&nbsp;</td>";
+            echo "<td><input name='expinterval' type='text' value='0'></td>";
+            echo "<td><input type='submit' name='action' value='Modify Service'></td>";
+         }
+         else
+         {
+            $obj_srv = pg_fetch_object($srv_obj_res, 0);
+            echo "<td><input name='servused' type='checkbox' checked='true'> " . $srv->servicename . "</td>";
+            echo "<td>" . $obj_srv->last_entry . "</td>";
+            echo "<td><input name='expinterval' type='text' value='" . $obj_srv->expected_interval . "'></td>";
+            echo "<td><input type='submit' name='action' value='Modify Service'></td>";
+         }
+         echo "</form></tr>";
+      }
+      echo "</table>";
+
+      echo "<h1>Users of " . $obj->objectname . "</h1><hr>\n";
+
+      echo "<table>";
+      echo "<tr><th>User name</th><th>Security level</th><th>&nbsp;</th></tr>";
+
+      $usr_res = pg_exec($this->database, "SELECT username, security_level FROM usr ORDER BY username");
+      for ($usr_row = 0; $usr_row < pg_numrows($usr_res); $usr_row++)
+      {
+         $usr = pg_fetch_object($usr_res, $usr_row);
+         $usr_obj_res = pg_exec($this->database, "SELECT * FROM object_user WHERE objectid='"
+                                   . $obj->objectid . "' AND username='" . $usr->username . "'");
+
+         echo "<tr>";
+         echo "<form action='objects.php?oid=" . $obj->objectid . "' method='POST'>";
+         echo "<input type='hidden' name='username' value='" . $usr->username . "'>";
+
+         if (pg_numrows($usr_obj_res) == 0)
+         {
+            echo "<td><input name='userused' type='checkbox'> " . $usr->username . "</td>";
+            echo "<td><input name='seclevel' type='text' value='" . $usr->security_level . "'></td>";
+            echo "<td><input type='submit' name='action' value='Modify User'></td>";
+         }
+         else
+         {
+            $usr_obj = pg_fetch_object($usr_obj_res, 0);
+            echo "<td><input name='userused' type='checkbox' checked='on'> " . $usr->username . "</td>";
+            echo "<td><input name='seclevel' type='text' value='" . $usr_obj->security_level . "'></td>";
+            echo "<td><input type='submit' name='action' value='Modify User'></td>";
+         }
+         echo "</form></tr>";
+      }
+      echo "</table>";
+   }
+   else
+   {
+      echo "<script type='text/ecmascript'>
+            document.getElementById('menu_title').innerHTML = \"<h1>Objects Administration<\/h1>\"
+            </script>
+            <br>";
 
-<?php
-   $obj = 0;
 
-   //The counters are set to zero
-   $count_logs = 0;
-   $count_notifications = 0;
-   $closed_notifications = 0;
-   $count_parameters = 0;
-   $removed_parameters = 0;
+      $res = pg_exec($this->database, "SELECT * FROM object ORDER BY objectname");
+?>
 
-   while ($obj < pg_numrows($res))
-   {
-      $u = pg_fetch_object($res, $obj);
-
-      $this->GatherStatistics($u->objectid);
-
-      $count_parameters += $this->nr_parameters;
-      $removed_parameters += $this->removed_parameters;
-      $count_logs = $count_logs + $this->nr_logs;
-
-      //$nr_notifications = $u->notification_count;
-      $count_notifications += $this->nr_notifications;
-      $closed_notifications += $this->closed_notifications;
-      ?>
-      <tr><td><center><img src='server.png'><br>
-             <b><?php echo $u->objectname ?></b></center>
-      </td><td class='number'>
-          <?php echo "<a href='log.php?oid=$u->objectid'> $this->nr_logs </a>" ?>
-      </td><td class='number'>
-          <?php echo "<a href='parameter.php?oid=$u->objectid'>" . $this->nr_parameters
-                   . " (" . $this->removed_parameters . " removed)</a>" ?>
-      </td><td class='number'>
-          <?php echo "<a href='notification.php?oid=$u->objectid'>" . $this->nr_notifications . " (" . $this->closed_notifications . " closed)</a>" ?>
-      </td><td>
-          <form action='objects.php' method='post' onSubmit='return CheckRemove(this)'>
-              <input type='hidden' name='objectname' value='<?php echo $u->objectname ?>'>
-              <input type='submit' name='action' value='Remove'>
-          </form>
-      </td></tr>
-      <?php
-      $obj++;
-   }
+      <table>
+      <tr><th>Object</th><th>Description</th><th>Log entries</th>
+          <th>Parameters</th><th>Notifications</th><th>Abuses</th></tr>
 
-   //Show the totals
-   echo "<tr><td><strong><B><br><br>TOTALS</B></strong></td>";
-   echo "<td class='number'>$count_logs</td>";
-   echo "<td class='number'>$count_parameters ($removed_parameters removed)</td>";
-   echo "<td class='number'>$count_notifications ($closed_notifications closed)</td></tr>";
-   echo "</table>";
+<?php
+      $obj = 0;
+
+      //The counters are set to zero
+      $count_logs = 0;
+      $count_notifications = 0;
+      $closed_notifications = 0;
+      $count_parameters = 0;
+      $removed_parameters = 0;
+
+      while ($obj < pg_numrows($res))
+      {
+         $u = pg_fetch_object($res, $obj);
+
+         $this->GatherStatistics($u->objectid);
+
+         $count_parameters += $this->nr_parameters;
+         $removed_parameters += $this->removed_parameters;
+         $count_logs = $count_logs + $this->nr_logs;
+
+         $count_notifications += $this->nr_notifications;
+         $closed_notifications += $this->closed_notifications;
+         ?>
+         <tr><td><center><a href='objects.php?oid=<?php echo $u->objectid?>'><img src='server.png' alt='server'></a><br>
+                <b><?php echo $u->objectname ?></b></center>
+         </td><td>
+               <?php echo nl2br($u->object_description) ?>
+         </td><td class='number'>
+             <?php if ( $this->nr_logs > 0 )
+                   { 
+                       echo "<a href='log.php?oid=$u->objectid'> $this->nr_logs </a>";
+                   }
+                   else
+                   { 
+                      echo "0";
+                   }
+              ?>
+         </td><td class='number'>
+             <?php echo "<a href='parameter.php?oid=$u->objectid'>" . $this->nr_parameters
+                      . " (" . $this->removed_parameters . " removed)</a>" ?>
+         </td><td class='number'>
+             <?php if( $this->nr_notifications == $this->closed_notifications )
+                   {
+                      echo $this->nr_notifications . " (" . $this->closed_notifications . " closed)"; 
+                  }
+                   else
+                   {
+                      echo "<a href='notification.php?oid=$u->objectid'>" . $this->nr_notifications
+                      . " (" . $this->closed_notifications . " closed)</a>"; 
+                   }
+              ?>
+         </td><td class='number'>
+             <?php echo "<a href='abuse.php?oid=$u->objectid'>" . "Abuse list"
+                      . " </a>" ?>
+         </td><td>
+             <form action='objects.php' method='post' onSubmit='return CheckRemove(this)'>
+                 <input type='hidden' name='objectid' value='<?php echo $u->objectid ?>'>
+                 <input type='submit' name='action' value='Remove'>
+             </form>
+         </td></tr>
+         <?php
+         $obj++;
+      }
+
+      //Show the totals
+      echo "<tr><td><strong><B><br><br>TOTALS</B></strong></td>";
+      echo "<td>&nbsp;</td>";
+      echo "<td class='number'>$count_logs</td>";
+      echo "<td class='number'>$count_parameters ($removed_parameters removed)</td>";
+      echo "<td class='number'>$count_notifications ($closed_notifications closed)</td>";
+      echo "<td>&nbsp;</td>";
+      echo "</tr>\n";
+      echo "</table>\n";
 
 ?>
 
 <h2>Create new object:</h2>
-<p>
 
 <form action='objects.php' method='post' onSubmit='return CheckCreate(this)'>
 Object's name (FQDN): <input name='objectname' type='text'>
 <br>
 <input type='submit' name='action' value='Create'>
 </form>
-</p>
+
 <?php
 
    }
+   }
 }
 
 $obj_page = new object_page("Gnucomo Objects Administration");