brute-force changed access to $_GET['key'] to use get_array instead
[plewww.git] / planetlab / events / events.php
1 <?php
2 // $Id$
3
4 // Require login
5 require_once 'plc_login.php';
6
7 // Get session and API handles
8 require_once 'plc_session.php';
9 global $plc, $api;
10
11 //// Print header
12 require_once 'plc_drupal.php';
13 include 'plc_header.php';
14
15 // Common functions
16 require_once 'plc_functions.php';
17 require_once 'table.php';
18 require_once 'linetabs.php';
19 require_once 'datepicker.php';
20
21 // needs much memory
22 ini_set("memory_limit","256M");
23
24 //set default title
25 drupal_set_title('Events');
26
27 // as per index.php, we get here if _GET['type'] is set
28 $type = get_array($_GET, 'type');
29 $from_date = get_array($_GET, 'from_date');
30 $until_date = get_array($_GET, 'until_date');
31
32 $messages=array();
33
34 //////////////////////////////////////////////////////////// dates
35 // our format is yyyy/MMM/dd
36
37 // given the user-typed string, return (*) a visible string, (*) an int time
38 // the flag should be false for the 'from' field, and true for the until field
39
40 $EPOCH="1 January 2008";
41 $DAY=24*60*60-1;
42
43 function parse_date ($user_date,$until_if_true=false,$DAY,$EPOCH) {
44
45   // empty string
46   if (empty ($user_date)) {
47     if ( ! $until_if_true) {
48     // an empty string means the epoch in from-mode
49       $date=$EPOCH; $time=strtotime($date);
50     } else {
51       $date="now"; $time=strtotime($date);
52     }
53
54   } else {
55     // user-provided string
56     list ($year,$month,$day) = preg_split ('/[\/\.\-]/',$user_date);
57     $date=sprintf("%s %s %s",$day,$month,$year);
58     $time=strtotime($date);
59     //if the flag is set, we add 23h59'59'', so the 'until' date is inclusive
60     if ($until_if_true) {
61       $time += $DAY;
62     }
63     $date=strftime("%Y/%b/%d @ %H:%M",$time);
64   }
65   return array($date,$time);
66 }
67
68 //////////////////////////////////////////////////////////// layout
69 // outline node ids and person ids with a link
70 function e_node ($node_id) {
71   if (! $node_id) return "";
72   return l_node_t($node_id,$node_id);
73 }
74 function e_person ($person_id) {
75   if (! $person_id) return "";
76   return l_person_t($person_id,$person_id);
77 }
78 // xxx broken
79 function e_event ($event_id) {
80   if (! $event_id) return "";
81   return href(l_event("Event","event",$event_id),$event_id);
82 }
83
84 function e_subject ($type,$id) {
85   $mess=$type . " " . $id;
86   switch ($type) {
87   case 'Node': return l_node_t ($id,$mess);
88   case 'Site': return l_site_t ($id,$mess);
89   case 'Person': return l_person_t ($id,$mess);
90   case 'Slice': return l_slice_t ($id,$mess);
91   case 'Interface': return l_interface_t ($id, $mess);
92   case 'Role': case 'Key': case 'PCU': case 'NodeGroup': case "Address":
93     return "$mess";
94   default: return "Unknown $type" . "-" . $id;
95   }
96 }
97
98 // synthesize links to the subject objects from types and ids
99 function e_subjects ($param) {
100   $types=$param['object_types'];
101   $ids=$param['object_ids'];
102   if ( ! $types) return "";
103   return plc_vertical_table(array_map ("e_subject",$types,$ids));
104 }
105
106 function e_issuer ($param) {
107   if ($param['node_id'])        return e_subject('Node',$param['node_id']);
108   if ($param['person_id'])      return e_subject('Person',$param['person_id']);
109   return '???';
110 }
111
112 function e_auth ($event) {
113   if (array_key_exists('auth_type',$event))
114     return $event['auth_type'];
115     else
116       return "";
117 }
118
119 function e_fault ($event) {
120   $f=$event['fault_code'];
121   if ($f==0) return "OK";
122   else return $f;
123 }
124
125 ////////////////////////////////////////////////////////////
126 // for convenience, add 1 day to the 'until' date as otherwise this corresponds to 0:00
127
128 $tabs=array();
129 $tabs['Clear events']=l_events();
130 plekit_linetabs($tabs);
131
132 list($from_string,$from_time) = parse_date ($from_date,false,$DAY,$EPOCH);
133 list($until_string,$until_time) = parse_date ($until_date,true,$DAY,$EPOCH);
134
135 if ($from_time > $until_time) {
136   drupal_set_error("Warning - <from> is after <until>");
137   return;
138  }
139
140 $filter=array();
141 // sort events by time is not good enough, let's use event_id
142 $filter['-SORT']='-event_id';
143 $filter[']time']=$from_time;
144 $filter['[time']=$until_time;
145
146 //////////////////////////////////////// Events
147 if ($type == 'Event') {
148
149   // and the filter applied for fetching events using GetEvent
150   $user_desc=get_array($_GET, 'event');
151   if ( ! empty($user_desc)) {
152     // should parse stuff like 45-90,230-3000 - some other day
153     $filter['event_id']=intval($user_desc);
154   }
155
156   $events = $api->GetEvents($filter);
157   $title="Events [ $from_string - $until_string] matching " . ($user_desc ? $user_desc : "everything");
158
159   // see actual display of $title and $events below
160
161  } else {
162
163   switch ($type) {
164   case 'Person':
165     $primary_key='person_id';
166     $string_key='email';
167     $user_input=$_GET['person'];
168     $method="GetPersons";
169     $object_type='Person';
170     break;
171
172   case 'Node':
173     $primary_key='node_id';
174     $string_key='hostname';
175     $user_input=$_GET['node'];
176     $method="GetNodes";
177     $object_type='Node';
178     break;
179
180   case 'Site':
181     $primary_key='site_id';
182     $string_key='login_base';
183     $user_input=$_GET['site'];
184     $method="GetSites";
185     $object_type='Site';
186     break;
187
188   case 'Slice':
189     $primary_key='slice_id';
190     $string_key='name';
191     $user_input=$_GET['slice'];
192     $method="GetSlices";
193     $object_type='Slice';
194     break;
195   }
196
197   $object_ids=array();
198   $title="Events [ $from_string - $until_string]";
199   $title .= " type=$object_type";
200   $title .= " id(s)=";
201   foreach ( explode(",",$user_input) as $user_desc) {
202 # numeric
203     if (my_is_int($user_desc)) {
204       $obj_check = call_user_func(array($api,$method),array(intval($user_desc)),array($primary_key));
205       if (empty ($obj_check)) {
206         $messages[] = "No such " . $primary_key . ": " . $user_desc;
207       } else {
208         $object_ids[] = $obj_check[0][$primary_key];
209         $title .= " $user_desc, " ;
210       }
211     } else {
212 # string
213       $new_object_ids=call_user_func (array($api,$method), array($string_key=>$user_desc),array($primary_key,$string_key));
214       if (empty($new_object_ids)) {
215         $messages[] = "No " . $string_key . " matching " . $user_desc;
216       } else {
217         foreach ($new_object_ids as $new_obj_id) {
218           $object_ids[] = $new_obj_id[$primary_key];
219           $title .= $new_obj_id[$primary_key] . ", ";
220         }
221       }
222     }
223   }
224
225   $event_objs = $api->GetEventObjects(array('object_id'=>$object_ids,'object_type'=>$object_type),array('event_id'));
226   // get set of event_ids
227   $event_ids = array_map (function ($eo) {return $eo["event_id"];} , $event_objs);
228
229   $events = $api->GetEvents (array('event_id'=>$event_ids));
230
231   // see actual display of $title and $events below
232
233  }
234
235   drupal_set_title ($title);
236 // Show messages
237 if (!empty($messages))
238   foreach ($messages as $line)
239     drupal_set_message($line);
240
241 $headers=array("Id"=>"int",
242                "Time"=>"EnglishDateTime",
243                "Method"=>"string",
244                "Message"=>"string",
245                "Subjects"=>"string",
246                "Issuer"=>"string",
247                "Auth"=>"string",
248                "R"=>"string",
249                "D"=>"none",
250                );
251
252 $table = new PlekitTable ("events",$headers,"0r");
253 $table->set_options (array ('max_pages'=>20));
254 $table->start ();
255 foreach ($events as $event) {
256
257   // the call button
258   $message = htmlentities($event['message'], ENT_QUOTES);
259   $call = htmlentities($event['call'], ENT_QUOTES);
260   $text = sprintf("message=<<%s>>\\n\\ncall=<<%s>>\\n\\nruntime=<<%f>>\\n",$message,$call,$event['runtime']);
261   $method = "<input type=button name='call' value='" . $event['call_name'] ."' onclick='alert(\"" . $text . "\")'";
262   //    $method = sprintf('<span title="%s">%s</span>',$call,$method);
263
264   // the message button
265   $trunc_mess=htmlentities(truncate($event['message'],40),ENT_QUOTES);
266   $message="<input type=button name='message' value='" . $trunc_mess ."' onclick='alert(\"" . $text . "\")'";
267   $details="<input type=button name='message' value='+' onclick='alert(\"" . $text . "\")'";
268   //    $message=sprintf('<span title="%s">%s</span>',$message,$message);
269
270   $message=truncate($event['message'],40);
271   $table->row_start();
272   $table->cell(e_event($event['event_id']));
273   $table->cell(date('M/d/Y H:i', $event['time']));
274   $table->cell($event['call_name']);
275   $table->cell($message);
276   $table->cell(e_subjects($event));
277   $table->cell(e_issuer($event));
278   $table->cell(e_auth($event));
279   $table->cell(e_fault($event));
280   $table->cell($details);
281   $table->row_end();
282 }
283 $table->set_options(array('notes'=>array("The R column shows the call result value, a.k.a. fault_code",
284                                          "Click the button in the D(etails) column to get more details")));
285 $table->end();
286
287 //plekit_linetabs ($tabs,"bottom");
288
289 // Print footer
290 include 'plc_footer.php';
291
292 ?>
293