5 function plc_person_link ($person_id) {
6 if ( empty ($person_id)) {
9 return '<a href="/db/persons/index.php?id=' . $person_id . '">' . $person_id . '</a>';
13 function plc_node_link ($node_id) {
14 if ( empty ($node_id)) {
17 return '<a href="/db/nodes/index.php?id=' . $node_id . '">' . $node_id . '</a>';
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 );
27 $link_page= 'index.php';
29 // check for page number
30 if( empty( $_GET['page'] ) )
35 // reorder array_chunk
36 foreach( $fn_array as $arr1 ) {
39 foreach( $arr1 as $key => $val ) {
40 // if( substr( $key, -3 ) == "_id" )
41 if ( $key == $table_id ) {
48 foreach( $id as $key => $val )
51 foreach( $data as $key => $val )
57 $totalrows= count( $as_array );
59 // if array has no rows display msg
61 return "Nothing to display";
63 // set key and break up data struct
65 $newarray= array_chunk( $as_array, $limit );
68 $echo.= "<table class='list_set' border=0 cellpadding=2>\n";
70 // if there is a caption add it to table
72 $echo.= "<caption class='list_set'>$caption</caption>\n";
74 $echo.= "<thead><tr class='list_set'>";
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>";
83 if( $other_func == 'slivers' )
84 $echo.= "<th>Slivers</th>";
86 $echo.= "</tr></thead><tbody>\n";
88 // go through array row by row to output table rows
89 foreach( $newarray[$newkey] as $assoc ) {
92 if ($assoc['peer_id']) {
93 $extraclass="plc-foreign";
97 $echo.= "<tr class='list_set'>";
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) {
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 ) ) {
113 foreach( $val as $k => $v ) {
127 if( $other_func == 'slivers' )
128 $echo.= "<td><a href='slivers.php?node=$fid&slice=$id'>view</a></td>";
134 $echo.= "</tbody></table>\n";
137 // find total number of pages
138 $numofpages = $totalrows / $limit;
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> ";
146 $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pageprev\">PREV ".$limit."</a> ";
149 $echo.= "PREV ". $limit ." ";
151 // if less than 30 pages display all
152 // otherwise show 30 pages but put ... inbetween
153 if( $numofpages < 30 ) {
154 $npages= $numofpages;
159 if( $npages > $numofpages )
160 $npages= $numofpages;
168 // display pages, no link if current page
169 for( $i= $start; $i <= $npages; $i++ ) {
173 $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
177 if( ( $totalrows % $limit ) != 0 ) {
178 $last= $numofpages + 1;
182 $echo.= "<a href=\"". $_SERVER['REQUEST_URI'] ."&page=$i\">$i</a> ";
187 if( $numofpages >= 30 ) {
188 if( $page != $numofpages )
192 if( ( $totalrows - ($limit * $page) ) > 0 ) {
193 $pagenext= $page + 1;
194 $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=$pagenext\">NEXT ".$limit."</a> ";
197 $echo.= "NEXT ". $limit;
199 $echo.= " <a href=\"". $_SERVER['REQUEST_URI'] ."&page=". intval( $last ) ."\">LAST</a>\n";
207 // function for getting the diff of multi dimention array
208 function arr_diff( $a1, $a2 ) {
210 foreach( $a1 as $k=>$v ) {
212 for( $x= 0; $x < count( $a2 ); $x++ ) {
214 if( array_search( $v, $a2 ) === false )
216 else if( is_array( $v ) )
217 $dv= arr_diff( $v, $a2[$x] );
218 if( $dv && !in_array( $dv, $diff ) )
225 else if(is_array($v))
226 $dv=arr_diff($v,$a2[$x]);
236 function is_valid_email_addr($email)
238 if( ereg("^.+@.+\\..+$", $email) )
244 function is_valid_url($url)
246 if( ereg("^(http|https)://.+\..+$", strtolower($url) ) )
252 function is_valid_ip($ip)
254 if( ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) )
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);
261 for( $i= 0; $i < $count; $i++ )
263 if( intval($parts[$i]) > 255 )
274 function is_valid_network_addr($network_addr,$mask)
276 $lNetwork= ip2long($network_addr);
277 $lMask= ip2long($mask);
279 // are they the correct format?
280 if( $lNetwork == -1 || $lMask == -1 )
283 // is network address valid for the mask?
284 if( ($lNetwork & $lMask) != $lNetwork )
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)
295 $lNetwork= ip2long($network_addr);
297 if( $lNetwork == -1 )
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')
306 foreach ($reserved_ips as $r)
308 $min = ip2long($r[0]);
309 $max = ip2long($r[1]);
311 if (($lNetwork >= $min) && ($lNetwork <= $max))
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
322 if (! empty ($messages)) {
323 $formatted="<table class='" . $class . "'>";
324 foreach ($messages as $message) {
325 $formatted .= "<tr><td>" . $message . "</td></tr>";
327 $formatted .= "</table>";
332 // shows a php variable verbatim with a heading message
333 function plc_debug($message,$object) {
334 print "<br>" . $message . "<pre>";
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>';
344 function plc_js_confirm($message) {
345 return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\"";
348 function plc_delete_link($url,$delete_message,$visible) {
349 return "<a href='" . $url . "' " . plc_js_confirm($delete_message) . ">" . $visible . "</a>";
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>";
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>';
360 function plc_comon_button ($field, $value,$target="") {
362 if (!empty($target)) {
363 $result.='target="' . $target . '" ';
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>';
370 function plc_peers_option_list ($api) {
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)"));
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");
386 foreach ($predef as $a) {
387 $peer_line = "<option value='" . $a['peer_id'] . "'>" . $a['peername'] . "</option>\n";
388 $result .= $peer_line;
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;
401 function plc_peer_info ($api,$peerscope) {
402 switch ($_GET['peerscope']) {
404 $peer_filter=array();
405 $peer_label="all peers";
408 $peer_filter=array("peer_id"=>NULL);
409 $peer_label="local peer";
412 $peer_filter=array("~peer_id"=>NULL);
413 $peer_label="foreign peers";
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'] . '"';
422 return array ($peer_filter,$peer_label);
425 function topdomain ($hostname) {
426 $exploded=array_reverse(explode(".",$hostname));