09edc5ce30d22514d9c07ac60521901a334fc596
[gnucomo.git] / src / web / notification.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      : notification.php
11 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
12 **      VERSION NUMBER : $Revision: 1.5 $
13 **
14 **  DESCRIPTION      : Display and handle notifications.
15 **                     There are two major views to this page: either a list of
16 **                     all notifications for an object or the display of a single
17 **                     notification. In the 'single notification' view, actions
18 **                     can be added to the notification and the status can be
19 **                     changed as a result of those actions.
20 **
21 **                     Input: GET['oid']   - If set, list all notifications for object.
22 **                            GET['notid'] - If set, display a single notification.
23 **                            POST['notid']- If set, update the notification.
24 **
25 **  EXPORTED OBJECTS : 
26 **  LOCAL    OBJECTS : 
27 **  MODULES  USED    :
28 ***************************************************************************
29 **  ADMINISTRATIVE INFORMATION *
30 ********************************
31 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
32 **      CREATION DATE   : Dec 12, 2002
33 **      LAST UPDATE     : Feb 16, 2003
34 **      MODIFICATIONS   : 
35 **************************************************************************/
36
37 /*****************************
38    $Log: notification.php,v $
39    Revision 1.5  2007-11-21 14:38:06  arjen
40    The buttonbar at the top of each page is now a fixed 'div' element
41    instead of a framed page.
42    Contributed by Edwin Nadorp.
43
44    Revision 1.4  2007/01/11 13:44:29  arjen
45    Manually edit parameters.
46    View logs from abusing IP addresses.
47
48    Revision 1.3  2003/02/21 08:49:16  arjen
49    Keep a record of each time a notification
50    is displayed. Either on a listing or in detail.
51
52    Revision 1.2  2003/02/13 09:01:29  arjen
53    All web interface pages use the page class.
54
55    Revision 1.1  2003/02/05 09:48:14  arjen
56    Added display and handling of notifications
57
58 ******************************/
59
60 // RCSID = "$Id: notification.php,v 1.5 2007-11-21 14:38:06 arjen Exp $";
61
62 ini_set('include_path', '.:./classes:../phpclasses');
63
64 require_once('page.class.php');
65
66 function clientscripts()
67 {
68 ?>
69
70 <script type='text/ecmascript'>
71
72 function CheckRemark(f)
73 {
74    if (f.remark.value == "")
75    {
76       alert("Your remark is still empty");
77       return false;
78    }
79    return true;
80 }
81
82 </script>
83
84 <?php
85 }
86
87 /*
88  *   Add an action to the notification $note.
89  *   The status of the notification may alter as a result.
90  */
91
92 function add_action($db, $note, $actionid, $remark)
93 {
94    $r = pg_exec($db, "SELECT statuscode FROM action WHERE actionid=CAST('" . $actionid."' AS BIGINT)");
95    $action = pg_fetch_object($r, 0);
96
97    if ($action->statuscode != "" && $note->statuscode != $action->statuscode)
98    {
99       //  Update the status of the notification.
100
101       $note->statuscode = $action->statuscode;
102
103       pg_exec($db, "UPDATE notification SET statuscode='" . $note->statuscode
104                 . "' WHERE notificationid = " . $note->notificationid);
105    }
106
107    //  Add the action to the action_user table.
108
109    pg_exec($db, "INSERT INTO action_user (actionid, username, notificationid,
110                            timestamp, statuscode, remarks) values ('$actionid', '"
111                          . $_SESSION['username'] . "', '" . $note->notificationid
112                          . "', '" . date('Y-m-d H:i:s') . "', '" . $note->statuscode
113                          . "', '" . $remark . "')");
114
115    return $note->statuscode;
116 }
117
118 class notification_page extends page
119 {
120
121    function notification_list($objectid)
122    {
123
124       //  Display a list of all notifications for this object['oid']
125
126       $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $objectid);
127       $obj = pg_fetch_object($res, 0);
128       echo "<h1>Notifications for " . $obj->objectname . "</h1><hr>";
129
130       $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid,
131                                            statuscode, priority
132                                        FROM notification WHERE objectid= CAST ('" 
133                                 . $objectid ."' AS BIGINT) AND statuscode != 'cls'"
134                                        . " ORDER BY notificationid");
135
136       echo "<table>";
137       echo '<tr><th>Time</th><th>Number</th><th>Issue</th><th>Status</th><th>Priority</th></tr>';
138       $row = 0;
139       while ($row < pg_numrows($res))
140       {
141          $note = pg_fetch_object($res, $row);
142          $r = pg_exec($this->database, "SELECT name from type_of_issue WHERE type_of_issueid = CAST('"
143                        . $note->type_of_issueid . "' AS BIGINT)");
144          $issue = pg_fetch_object($r, 0);
145          echo "<tr><td align='center'>\n";
146          echo $note->timestamp;
147          echo "</td><td>";
148          echo $note->notificationid;
149          echo "</td><td>";
150          echo "<a href='notification.php?notid=";
151          echo $note->notificationid;
152          echo "'>";
153          echo $issue->name;
154          echo "</a>";
155          echo "</td><td>";
156          echo $note->statuscode;
157          echo "</td><td>";
158          echo $note->priority;
159          echo "</td></tr>\n";
160          $row++;
161
162          //Save evidence that this notification was presented to the user.
163          //$note->statuscode = add_action($this->database, $note, 21,
164          //            "This notification was displayed with others from the same object");
165       }
166       echo "</table>";
167    }
168
169    function display_notification($note)
170    {
171       $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid= CAST ('" . $note->objectid . "' AS BIGINT)");
172       $obj = pg_fetch_object($res, 0);
173       echo "<h1>Notification " . $note->notificationid . " for " . $obj->objectname . "</h1><hr>";
174       echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = '<h1>Notification " . $note->notificationid . " for " . $obj->objectname . "<\/h1>'</script><br>";
175
176       $r = pg_exec($this->database, "SELECT description FROM type_of_issue WHERE type_of_issueid = CAST ('"
177                        . $note->type_of_issueid . "' AS BIGINT)");
178       $issue = pg_fetch_object($r, 0);
179
180       $r = pg_exec($this->database, "SELECT statusname FROM status
181                                      WHERE statuscode = '" . $note->statuscode . "'");
182
183       $status = pg_fetch_object($r, 0);
184
185       echo "Issue : " . $issue->description . ", Priority " . $note->priority . "<br>";
186       echo "Creation time : " . $note->timestamp . "<br>";
187       echo "Current status : " . $status->statusname . "<br>";
188
189       //  List all actions that occurred on this notification
190
191       echo "<h2>Action history for notification " . $note->notificationid . "</h2>";
192       echo "<table>";
193       echo "<tr><th>Time</th><th>User</th><th>Remarks</th></tr>";
194
195       $r = pg_exec($this->database, "SELECT actionid, username, timestamp, remarks
196                                      FROM action_user WHERE notificationid= CAST ('" 
197                                      . $note->notificationid . "' AS BIGINT )"
198                                      ." ORDER BY timestamp");
199       for ($row = 0; $row < pg_numrows($r); $row++)
200       {
201          $action_user = pg_fetch_object($r, $row);
202
203          echo "<tr><td class='time'>";
204          echo $action_user->timestamp;
205          echo "</td><td>";
206          echo $action_user->username;
207          echo "</td><td>";
208          echo htmlspecialchars(stripslashes($action_user->remarks));
209          echo "</td></tr>";
210       }
211       echo "</table>";
212
213       //  See if we can list any parameters for this notification
214
215       $r = pg_exec($this->database, "SELECT * FROM parameter WHERE paramid IN
216                                      ( SELECT paramid FROM parameter_notification
217                                      WHERE notificationid = CAST ('" . $note->notificationid 
218                                      . "' AS BIGINT) )");
219       if (pg_numrows($r) > 0)
220       {
221          echo "<h2>Parameters involved in this notification</h2>";
222          echo "<table>";
223          echo "<tr><th>Class</th><th>Name</th><th>Description</th><th>History</th></tr>";
224
225          for ($row = 0; $row < pg_numrows($r); $row++)
226          {
227             $p = pg_fetch_object($r, $row);
228             $res = pg_exec($this->database, "SELECT * from history WHERE paramid= CAST('" . $p->paramid
229                                             . "' AS BIGINT) ORDER BY modified");
230
231             echo "<tr><td>";
232             echo $p->class;
233             echo "</td><td>";
234             echo $p->name;
235             echo "</td><td>";
236             echo $p->description;
237             echo "</td><td>";
238             for ($hrow = 0; $hrow < pg_numrows($res); $hrow++)
239             {
240                $hist = pg_fetch_object($res, $hrow);
241                echo $hist->modified . " ";
242                echo $hist->change_nature . " ";
243                echo $hist->changed_property . " ";
244                echo $hist->new_value;
245                echo "<br>";
246             }
247             echo "</td></tr>";
248
249          }
250          echo "</table>";
251       }
252
253       //  See if we can list any log entries for this notification
254
255       $r = pg_exec($this->database, "SELECT * FROM log WHERE logid IN
256                                      ( SELECT logid FROM log_notification
257                                      WHERE notificationid = CAST ('" 
258                                       . $note->notificationid . "' AS BIGINT)) ORDER BY object_timestamp");
259       if (pg_numrows($r) > 0)
260       {
261          echo "<h2>Log entries involved in this notification</h2>";
262          echo "<table>";
263          echo "<tr><th>Time</th><th>Service</th><th>Log entry</th></tr>";
264
265          for ($row = 0; $row < pg_numrows($r); $row++)
266          {
267             $p = pg_fetch_object($r, $row);
268
269             echo "<tr><td>";
270             echo $p->object_timestamp;
271             echo "</td><td>";
272             echo $p->servicecode;
273             echo "</td><td>";
274             echo htmlentities($p->rawdata);
275             echo "</td></tr>";
276
277          }
278          echo "</table>";
279       }
280
281    }
282
283    function notification_form($note)
284    {
285       /*
286        *  Depending on the state of the notification, specific actions
287        *  are possible. One such actions determines a transition to
288        *  the next state. The array $possible_action lists the state transitions
289        *  for each state.
290        */
291
292       $possible_action = array
293       (
294          "opn" => array (3, 6, 7, 13),
295          "pen" => array (3, 6, 7, 8, 11, 18),
296          "inv" => array (3, 9),
297          "vrf" => array (12, 13),
298          "cls" => array (19)
299       );
300
301       echo "<h2>Enter a remark for the next action</h2>";
302       echo "<form method='post' onSubmit='return CheckRemark(this)' action=''>";
303       echo "<textarea name='remark' cols='30' rows='5'></textarea><br>";
304
305       /*  Display a list of possible actions */
306
307       echo "<h2>Select one of the actions to take below</h2>";
308
309       $first = true;
310
311       foreach ($possible_action[$note->statuscode] as $act)
312       {
313          $action = pg_fetch_object(pg_exec("SELECT description FROM action WHERE actionid= CAST('"
314           . $act . "' AS BIGINT)"),0);
315          echo "<input type='radio' name='actionid'";
316          if ($first)
317          {
318             $first = false;
319             echo " CHECKED";
320          }
321          echo " value='$act'>" . $action->description . "<br>";
322       }
323
324       echo "<input type='submit' value='Submit action'>";
325       echo "</form>";
326    }
327
328    function Body()
329    {
330
331       if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['oid']))
332       {
333
334          //  Display a list of all notifications for this object['oid']
335
336          $res = pg_exec($this->database, "SELECT objectname FROM object WHERE objectid=" . $_GET['oid']);
337          $obj = pg_fetch_object($res, 0);
338          echo "<script type='text/ecmascript'>document.getElementById('menu_title').innerHTML = '<h1>Notifications for " . $obj->objectname . "<\/h1>'</script><br>";
339
340          $res = pg_exec($this->database, "SELECT notificationid, timestamp, type_of_issueid,
341                                               statuscode, priority
342                                           FROM notification WHERE objectid= CAST ('" 
343                                           . $_GET['oid'] ."' AS BIGINT) AND statuscode != 'cls'"
344                                           . " ORDER BY notificationid");
345
346          echo "<table>";
347          echo '<tr><th>Time</th><th>Number</th><th>Issue</th><th>Status</th><th>Priority</th></tr>';
348          $row = 0;
349          while ($row < pg_numrows($res))
350          {
351             $note = pg_fetch_object($res, $row);
352             $r = pg_exec($this->database, "SELECT name from type_of_issue WHERE type_of_issueid = CAST('"
353                           . $note->type_of_issueid . "' AS BIGINT)");
354             $issue = pg_fetch_object($r, 0);
355             echo "<tr><td align='center'>\n";
356             echo $note->timestamp;
357             echo "</td><td>";
358             echo $note->notificationid;
359             echo "</td><td>";
360             echo "<a href='notification.php?notid=";
361             echo $note->notificationid;
362             echo "'>";
363             echo $issue->name;
364             echo "</a>";
365             echo "</td><td>";
366             echo $note->statuscode;
367             echo "</td><td>";
368             echo $note->priority;
369             echo "</td></tr>\n";
370             $row++;
371
372             //Save evidence that this notification was presented to the user.
373             //$note->statuscode = add_action($this->database, $note, 21,
374             //            "This notification was displayed with others from the same object");
375          }
376          echo "</table>";
377       }
378       else if ($_SERVER['REQUEST_METHOD'] == 'GET' && !empty($_GET['notid']))
379       {
380          clientscripts();
381
382          //  Display all information about a single notification['notid']
383
384          $local_sql = "SELECT * from notification
385                        WHERE notificationid = CAST('" . $_GET['notid'] . "' AS BIGINT)";
386          $res = pg_exec($this->database, $local_sql);
387          $note = pg_fetch_object($res, 0);
388
389          //Gather proof that the notification has been presented
390          if ($note->statuscode == 'new')
391          {
392             $note->statuscode = add_action($this->database, $note, 2,
393                                   "Notification displayed in detail through web interface");
394          }
395          else
396          {
397             $note->statuscode = add_action($this->database, $note, 20,
398                                   "Notification redisplayed in detail through web interface");
399          }
400          $this->display_notification($note);
401          echo "<hr>";
402          $this->notification_form($note);
403          $this->notification_list($note->objectid);
404       }
405       else if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_GET['notid']))
406       {
407          clientscripts();
408
409          //  Display all information about a single notification['notid']
410
411          $res = pg_exec($this->database, "SELECT * from notification
412                                           WHERE notificationid=" . $_GET['notid']);
413          $note = pg_fetch_object($res, 0);
414
415          $note->statuscode = add_action($this->database, $note, $_POST['actionid'], $_POST['remark']);
416          $this->display_notification($note);
417          echo "<hr>";
418          $this->notification_form($note);
419          $this->notification_list($note->objectid);
420       }
421       else
422       {
423          echo "<h1>Insufficient input to create page</h1>";
424          phpinfo();
425       }
426    }
427 }
428
429 $notif_page = new notification_page("Gnucomo Notifications");
430
431 $notif_page->Showpage();
432
433 ?>