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