events page has new links to caller page
[plewww.git] / planetlab / includes / plc_functions.php
1 <?php
2
3 function plc_person_link ($person_id) {
4   if ( empty ($person_id)) {
5     return "";
6   } else {
7     return '<a href="/db/persons/index.php?id=' . $person_id . '">' . $person_id . '</a>';
8   }
9   }
10
11 function plc_node_link ($node_id) {
12   if ( empty ($node_id)) {
13     return "";
14   } else {
15     return '<a href="/db/nodes/index.php?id=' . $node_id . '">' . $node_id . '</a>';
16   }
17   }
18
19 // pagination function
20 function paginate( $fn_array, $table_id, $caption, $limit, $main_field, $other_func= NULL, $fid= NULL ) {
21   // get vars from call adjust them
22   $dir= strtolower( $caption );
23   $echo= "";
24
25   $link_page= 'index.php';
26
27   // check for page number
28   if( empty( $_GET['page'] ) )
29     $page= 1;
30   else
31     $page= $_GET['page'];
32
33   // reorder array_chunk
34   foreach( $fn_array as $arr1 ) {
35     unset( $arr2 );
36     
37     foreach( $arr1 as $key => $val ) {
38 //      if( substr( $key, -3 ) == "_id" )
39       if ( $key == $table_id  ) {
40         $id[$key]= $val;
41       } else {
42         $data[$key]= $val;
43       }
44     }
45     
46     foreach( $id as $key => $val )
47       $arr2[$key]= $val;
48
49     foreach( $data as $key => $val )
50       $arr2[$key]= $val;
51
52     $as_array[]= $arr2;
53   }
54
55   $totalrows= count( $as_array );
56
57   // if array has no rows display msg
58   if( $totalrows == 0 )
59     return "Nothing to display";
60
61   // set key and break up data struct
62   $newkey= $page - 1;  
63   $newarray= array_chunk( $as_array, $limit );
64
65   // start table output
66   $echo.= "<table class='list_set' border=0 cellpadding=2>\n";
67
68   // if there is a caption add it to table
69   if( $caption )
70     $echo.= "<caption class='list_set'>$caption</caption>\n";
71
72   $echo.= "<thead><tr class='list_set'>";
73
74   // go through keys of one array row for table headers
75   foreach( $newarray[$newkey][0] as $key => $val ) {
76 //    if( substr( $key, -3 ) != "_id" )
77     if ( $key != $table_id && $key != 'peer_id' )
78       $echo.= "<th class='list_set'>". ucfirst( $key ) ."</th>";
79   }
80
81   if( $other_func == 'slivers' )
82     $echo.= "<th>Slivers</th>";
83
84   $echo.= "</tr></thead><tbody>\n";
85
86   // go through array row by row to output table rows
87   foreach( $newarray[$newkey] as $assoc ) {
88
89     $extraclass="";
90     if ($assoc['peer_id']) {
91       $extraclass="plc-foreign";
92     }
93     
94
95     $echo.= "<tr class='list_set'>";
96
97     foreach( $assoc as $key => $val ) {
98       // do not rely on keys order
99       $id = $assoc[$table_id];
100 //      if( substr( $key, -3 ) == "_id" )
101       if ($key == $table_id) {
102 //      $id= $val;
103         continue;
104       } elseif( $key == $main_field ) {
105         $echo.= "<td class='list_set $extraclass'><a href='/db/$dir/$link_page?id=$id'>$val</a></td>";
106       } elseif ($key != 'peer_id') {
107         $echo.= "<td class='list_set $extraclass'>";
108         if( is_array( $val ) ) {
109           $count= 1;
110           $tot= count( $val );
111           foreach( $val as $k => $v ) {
112             $echo.= $v;
113             if( $count != $tot )
114               $echo.= ", ";
115             $count++;
116           }
117         }
118         else
119           $echo.= $val;
120         $echo.= "</td>";
121       }
122
123     }
124
125     if( $other_func == 'slivers' )
126       $echo.= "<td><a href='slivers.php?node=$fid&slice=$id'>view</a></td>";
127
128     $echo.= "</tr>\n";
129   }
130
131   // close table
132   $echo.= "</tbody></table>\n";
133   $echo.= "<hr />\n";
134
135   // find total number of pages
136   $numofpages = $totalrows / $limit;
137
138   // start navigation links
139   if( $numofpages > 1 ) {
140     // if page is not 1 display first and prev links
141     if( $page != 1 && $page ) {
142         $pageprev= $page - 1;
143         $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=1\">FIRST</a> &nbsp; ";
144         $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pageprev\">PREV ".$limit."</a> &nbsp; ";
145       }
146       else
147         $echo.= "PREV ". $limit ." ";
148
149     // if less than 30 pages display all
150     // otherwise show 30 pages but put ... inbetween
151     if( $numofpages < 30 ) {
152       $npages= $numofpages;
153       $start= 1;
154     }
155     else {
156       $npages= $page + 9;
157       if( $npages > $numofpages )
158         $npages= $numofpages;
159       $start= $page - 10;
160       if( $start < 1 )
161         $start= 1;
162       if( $page != 1 )
163         $echo.= " ... ";
164     }
165
166     // display pages, no link if current page
167     for( $i= $start; $i <= $npages; $i++ ) {
168       if( $i == $page )
169         $echo.= $i ." ";
170       else
171         $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
172
173     }
174
175     if( ( $totalrows % $limit ) != 0 ) {
176       $last= $numofpages + 1;
177       if( $i == $page )
178         $echo.= $i ." ";
179       else
180         $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
181     }
182     else
183       $last= $numofpages;
184
185     if( $numofpages >= 30 ) {
186       if( $page != $numofpages )
187         $echo.= " ... ";
188     }
189
190     if( ( $totalrows - ($limit * $page) ) > 0 ) {
191       $pagenext= $page + 1;
192       $echo.= " &nbsp; <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pagenext\">NEXT ".$limit."</a> &nbsp; ";
193     }
194     else
195       $echo.= "NEXT ". $limit;
196
197     $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=". intval( $last ) ."\">LAST</a>\n";
198
199   }
200
201   return $echo;
202   
203 }
204
205 // function for getting the diff of multi dimention array
206 function arr_diff( $a1, $a2 ) {
207   $diff= array();
208   foreach( $a1 as $k=>$v ) {
209     unset( $dv );
210     for( $x= 0; $x < count( $a2 ); $x++ ) {
211       if( is_int( $k ) ) {
212         if( array_search( $v, $a2 ) === false )
213           $dv=$v;
214         else if( is_array( $v ) )
215           $dv= arr_diff( $v, $a2[$x] );
216         if( $dv && !in_array( $dv, $diff ) )
217           $diff[]=$dv;
218       }
219       else {
220
221         if( !$a2[$k] )
222           $dv=$v;
223         else if(is_array($v))
224           $dv=arr_diff($v,$a2[$x]);
225         if($dv)
226           $diff[$x]=$dv;
227       }
228     }
229   }
230   return $diff;
231 }
232
233
234 function is_valid_email_addr($email)
235 {
236   if( ereg("^.+@.+\\..+$", $email) )
237     return true;
238   else
239     return false;
240 }
241
242 function is_valid_url($url)
243 {
244   if( ereg("^(http|https)://.+\..+$", strtolower($url) ) )
245     return true;
246   else
247     return false;
248 }
249
250 function is_valid_ip($ip)
251 {
252   if( ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) )
253     {
254       // it's at least in the right format, now check to see if
255       // each part is equal to less than 255
256       $parts= explode( '.', $ip );
257       $count= count($parts);
258
259       for( $i= 0; $i < $count; $i++ )
260         {
261           if( intval($parts[$i]) > 255 )
262             return false;
263         }
264
265       return true;
266     }
267   else
268     return false;
269 }
270
271
272 function is_valid_network_addr($network_addr,$mask)
273 {
274   $lNetwork= ip2long($network_addr);
275   $lMask= ip2long($mask);
276
277   // are they the correct format?
278   if( $lNetwork == -1 || $lMask == -1 )
279     return false;
280
281   // is network address valid for the mask?
282   if( ($lNetwork & $lMask) != $lNetwork )
283     return false;
284
285   return true;
286 }
287
288
289 // returns whether or not a network address is in the reserved space
290 // in the case of a invalid network address, false will be returned.
291 function is_reserved_network_addr($network_addr)
292 {
293   $lNetwork= ip2long($network_addr);
294
295   if( $lNetwork == -1 )
296     return false;
297
298   // does the network address fall in a reserved block?
299   $reserved_ips = array (
300                          array('10.0.0.0','10.255.255.255'),
301                          array('172.16.0.0','172.31.0.0'),
302                          array('192.168.0.0','192.168.255.0')
303                          );
304   foreach ($reserved_ips as $r)
305     {
306       $min = ip2long($r[0]);
307       $max = ip2long($r[1]);
308       
309       if (($lNetwork >= $min) && ($lNetwork <= $max))
310           return true;
311     }
312
313   return false;
314 }
315
316 // builds a table from an array of strings, with the given class
317 function plc_make_table ($class, $messages) {
318   // pretty print the cell
319   $formatted = "";
320   if (! empty ($messages)) {
321     $formatted="<table class='" . $class . "'>";
322     foreach ($messages as $message) {
323       $formatted .= "<tr><td>" . $message . "</td></tr>";
324     }
325     $formatted .= "</table>";
326   }
327   return $formatted;
328 }
329
330 // shows a php variable verbatim with a heading message
331 function plc_debug($message,$object) {
332   print "<br>" . $message . "<pre>";
333   print_r ($object);
334   print "</pre>";
335 }
336
337 // attempt to normalize the delete buttons and confirmations
338 function plc_delete_button($width=15) {
339   return '<span title="Delete this entry"><img width=' . $width . ' alt="Delete this entry" src="/planetlab/includes/delete.png"></span>';
340 }
341
342 function plc_js_confirm($message) {
343   return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
344 }
345
346 function plc_delete_link($url,$delete_message,$visible) {
347   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
348 }
349
350 function plc_delete_link_button($url,$delete_message,$width=15) {
351   return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . plc_delete_button($width) . "</a>";
352 }
353
354 function plc_event_button($type,$param,$id) {
355   return '<a href="/db/events/index.php?type=' . $type . '&' . $param . '=' . $id . '"> <span title="Related events"> <img src="/planetlab/includes/event.png" width=18></span></a>';
356 }
357
358 function plc_comon_button ($field, $value,$target="") {
359   $result='<a ';
360   if (!empty($target)) {
361     $result.='target="' . $target . '" ';
362   }
363   $result.='href="/db/nodes/comon.php?' . $field . "=" . $value . '">';
364   $result.='<span title="Link to Comon"> <img src="/planetlab/includes/comon.png" width="18"></span></a>';
365   return $result;
366 }
367
368 function plc_peers_option_list ($api) {
369
370     // get list of peers
371     $peers=$api->GetPeers(NULL,array('peer_id','peername'));
372     if (count($peers)==0) {
373       $predef=array(array("peer_id"=>"","peername"=>"All (no known peers)"));
374     } else {
375       $predef=array(array("peer_id"=>"","peername"=>"All peers"),
376                     array("peer_id"=>"local","peername"=>"Local only"));
377       // show a 'foreign' button only if that makes sense
378       if (count($peers) >= 2) {
379         $predef [] = array("peer_id"=>"foreign","peername"=>"Foreign peers");
380       }
381     }
382     
383     $result="";
384     foreach ($predef as $a) {
385       $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
386       $result .= $peer_line;
387     }
388
389     if (!empty($peers)) {
390       foreach ($peers as $a) {
391         $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
392         $result .= $peer_line;
393       }
394     }
395
396     return $result;
397 }
398
399 ?>