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