From bad151149db279ae431857b9f31047dd780b9b33 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Fri, 16 Jan 2009 15:20:13 +0000 Subject: [PATCH] checkpoint --- planetlab/includes/plc_functions.php | 431 ++++++--------------- planetlab/includes/plc_functions_trash.php | 270 +++++++++++++ planetlab/includes/plc_minitabs.php | 2 +- planetlab/includes/plc_sorts.php | 77 +--- planetlab/includes/plc_tables.php | 5 + 5 files changed, 386 insertions(+), 399 deletions(-) create mode 100644 planetlab/includes/plc_functions_trash.php diff --git a/planetlab/includes/plc_functions.php b/planetlab/includes/plc_functions.php index 671dca2..b973971 100644 --- a/planetlab/includes/plc_functions.php +++ b/planetlab/includes/plc_functions.php @@ -2,277 +2,50 @@ // $Id$ -function plc_person_link ($person_id) { - if ( empty ($person_id)) { - return ""; - } else { - return '' . $person_id . ''; - } - } - -function plc_node_link ($node_id) { - if ( empty ($node_id)) { - return ""; - } else { - return '' . $node_id . ''; - } - } - -// pagination function -function paginate( $fn_array, $table_id, $caption, $limit, $main_field, $other_func= NULL, $fid= NULL ) { - // get vars from call adjust them - $dir= strtolower( $caption ); - $echo= ""; - - $link_page= 'index.php'; - - // check for page number - if( empty( $_GET['page'] ) ) - $page= 1; - else - $page= $_GET['page']; - - // reorder array_chunk - foreach( $fn_array as $arr1 ) { - unset( $arr2 ); - - foreach( $arr1 as $key => $val ) { -// if( substr( $key, -3 ) == "_id" ) - if ( $key == $table_id ) { - $id[$key]= $val; - } else { - $data[$key]= $val; - } - } - - foreach( $id as $key => $val ) - $arr2[$key]= $val; - - foreach( $data as $key => $val ) - $arr2[$key]= $val; - - $as_array[]= $arr2; - } - - $totalrows= count( $as_array ); - - // if array has no rows display msg - if( $totalrows == 0 ) - return "Nothing to display"; - - // set key and break up data struct - $newkey= $page - 1; - $newarray= array_chunk( $as_array, $limit ); - - // start table output - $echo.= "\n"; - - // if there is a caption add it to table - if( $caption ) - $echo.= "\n"; - - $echo.= ""; - - // go through keys of one array row for table headers - foreach( $newarray[$newkey][0] as $key => $val ) { -// if( substr( $key, -3 ) != "_id" ) - if ( $key != $table_id && $key != 'peer_id' ) - $echo.= ""; - } - - if( $other_func == 'slivers' ) - $echo.= ""; - - $echo.= "\n"; - - // go through array row by row to output table rows - foreach( $newarray[$newkey] as $assoc ) { - - $extraclass=""; - if ($assoc['peer_id']) { - $extraclass="plc-foreign"; - } - - - $echo.= ""; - - foreach( $assoc as $key => $val ) { - // do not rely on keys order - $id = $assoc[$table_id]; -// if( substr( $key, -3 ) == "_id" ) - if ($key == $table_id) { -// $id= $val; - continue; - } elseif( $key == $main_field ) { - $echo.= ""; - } elseif ($key != 'peer_id') { - $echo.= ""; - } - - } - - if( $other_func == 'slivers' ) - $echo.= ""; - - $echo.= "\n"; - } - - // close table - $echo.= "
$caption
". ucfirst( $key ) ."Slivers
$val"; - if( is_array( $val ) ) { - $count= 1; - $tot= count( $val ); - foreach( $val as $k => $v ) { - $echo.= $v; - if( $count != $tot ) - $echo.= ", "; - $count++; - } - } - else - $echo.= $val; - $echo.= "view
\n"; - $echo.= "
\n"; - - // find total number of pages - $numofpages = $totalrows / $limit; - - // start navigation links - if( $numofpages > 1 ) { - // if page is not 1 display first and prev links - if( $page != 1 && $page ) { - $pageprev= $page - 1; - $echo.= "FIRST   "; - $echo.= " PREV ".$limit."   "; - } - else - $echo.= "PREV ". $limit ." "; - - // if less than 30 pages display all - // otherwise show 30 pages but put ... inbetween - if( $numofpages < 30 ) { - $npages= $numofpages; - $start= 1; - } - else { - $npages= $page + 9; - if( $npages > $numofpages ) - $npages= $numofpages; - $start= $page - 10; - if( $start < 1 ) - $start= 1; - if( $page != 1 ) - $echo.= " ... "; - } - - // display pages, no link if current page - for( $i= $start; $i <= $npages; $i++ ) { - if( $i == $page ) - $echo.= $i ." "; - else - $echo.= "$i "; - - } - - if( ( $totalrows % $limit ) != 0 ) { - $last= $numofpages + 1; - if( $i == $page ) - $echo.= $i ." "; - else - $echo.= "$i "; - } - else - $last= $numofpages; - - if( $numofpages >= 30 ) { - if( $page != $numofpages ) - $echo.= " ... "; - } - - if( ( $totalrows - ($limit * $page) ) > 0 ) { - $pagenext= $page + 1; - $echo.= "   NEXT ".$limit."   "; - } - else - $echo.= "NEXT ". $limit; +// will trash this eventually +require_once 'plc_functions_trash.php'; - $echo.= " LAST\n"; - - } - - return $echo; - -} - -// function for getting the diff of multi dimention array -function arr_diff( $a1, $a2 ) { - $diff= array(); - foreach( $a1 as $k=>$v ) { - unset( $dv ); - for( $x= 0; $x < count( $a2 ); $x++ ) { - if( is_int( $k ) ) { - if( array_search( $v, $a2 ) === false ) - $dv=$v; - else if( is_array( $v ) ) - $dv= arr_diff( $v, $a2[$x] ); - if( $dv && !in_array( $dv, $diff ) ) - $diff[]=$dv; - } - else { - - if( !$a2[$k] ) - $dv=$v; - else if(is_array($v)) - $dv=arr_diff($v,$a2[$x]); - if($dv) - $diff[$x]=$dv; - } - } - } - return $diff; +//////////////////////////////////////////////////////////// validation functions +function topdomain ($hostname) { + $exploded=array_reverse(explode(".",$hostname)); + return $exploded[0]; } - -function is_valid_email_addr($email) -{ - if( ereg("^.+@.+\\..+$", $email) ) +function is_valid_email_addr ($email) { + if (ereg("^.+@.+\\..+$", $email) ) { return true; - else + } else { return false; + } } -function is_valid_url($url) -{ - if( ereg("^(http|https)://.+\..+$", strtolower($url) ) ) +function is_valid_url ($url) { + if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) { return true; - else + } else { return false; + } } -function is_valid_ip($ip) -{ - if( ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) - { +function is_valid_ip ($ip) { + if (ereg("^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$", $ip ) ) { // it's at least in the right format, now check to see if // each part is equal to less than 255 $parts= explode( '.', $ip ); $count= count($parts); - for( $i= 0; $i < $count; $i++ ) - { - if( intval($parts[$i]) > 255 ) - return false; - } + for( $i= 0; $i < $count; $i++ ) { + if( intval($parts[$i]) > 255 ) + return false; + } return true; - } - else + } else { return false; + } } - -function is_valid_network_addr($network_addr,$mask) -{ +function is_valid_network_addr($network_addr,$mask) { $lNetwork= ip2long($network_addr); $lMask= ip2long($mask); @@ -290,8 +63,7 @@ function is_valid_network_addr($network_addr,$mask) // returns whether or not a network address is in the reserved space // in the case of a invalid network address, false will be returned. -function is_reserved_network_addr($network_addr) -{ +function is_reserved_network_addr($network_addr) { $lNetwork= ip2long($network_addr); if( $lNetwork == -1 ) @@ -303,18 +75,82 @@ function is_reserved_network_addr($network_addr) array('172.16.0.0','172.31.0.0'), array('192.168.0.0','192.168.255.0') ); - foreach ($reserved_ips as $r) - { - $min = ip2long($r[0]); - $max = ip2long($r[1]); + foreach ($reserved_ips as $r) { + $min = ip2long($r[0]); + $max = ip2long($r[1]); - if (($lNetwork >= $min) && ($lNetwork <= $max)) - return true; - } + if (($lNetwork >= $min) && ($lNetwork <= $max)) + return true; + } return false; } +//////////////////////////////////////////////////////////// peerscopes +function plc_peer_info ($api,$peerscope) { + switch ($_GET['peerscope']) { + case '': + $peer_filter=array(); + $peer_label="all peers"; + break; + case 'local': + $peer_filter=array("peer_id"=>NULL); + $peer_label=PLC_SHORTNAME; + break; + case 'foreign': + $peer_filter=array("~peer_id"=>NULL); + $peer_label="foreign peers"; + break; + default: + $peer_id=intval($_GET['peerscope']); + $peer_filter=array("peer_id"=>$peer_id); + $peer=$api->GetPeers(array("peer_id"=>$peer_id)); + $peer_label='peer "' . $peer[0]['peername'] . '"'; + break; + } + return array ($peer_filter,$peer_label); +} + +//////////////////////////////////////////////////////////// links +function href ($url,$text) { return "" . $text . ""; } + +function l_nodes () { return "/db/nodes/newindex.php"; } +function l_node_u ($node_id) { return "/db/nodes/node.php?id=" . $node_id; } +function l_node ($node_id) { return href (l_node_u($node_id),$node_id); } +function l_node2 ($node_id,$text) { return href (l_node_u($node_id),$text); } + +function l_sites () { return "/db/sites/index.php"; } +function l_site_u ($site_id) { return "/db/persons/index.php?id=" . $site_id; } +function l_site ($site_id) { return href (l_site_u($site_id),$site_id); } +function l_site2 ($site_id,$text) { return href (l_site_u($site_id),$text); } + +function l_slices () { return "/db/slices/index.php"; } +function l_slice_u ($slice_id) { return "/db/persons/index.php?id=" . $slice_id; } +function l_slice ($slice_id) { return href (l_slice_u($slice_id),$slice_id); } +function l_slice2 ($slice_id,$text) { return href (l_slice_u($slice_id),$text); } + +function l_persons () { return "/db/persons/index.php"; } +function l_person_u ($person_id) { return "/db/persons/index.php?id=" . $person_id; } +function l_person ($person_id) { return href (l_person_u($person_id),$person_id); } +function l_person2 ($person_id,$text) { return href (l_person_u($person_id),$text); } + +function l_interfaces () { return "/db/interfaces/index.php"; } +function l_interface_u ($interface_id) { return "/db/interfaces/index.php?id=" . $interface_id; } +function l_interface ($interface_id) { return href (l_interface_u($interface_id),$interface_id); } +function l_interface2 ($interface_id,$text) { return href (l_interface_u($interface_id),$text); } + +function l_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; } + +//////////////////////////////////////////////////////////// titles +function t_site($site) { return " on site " . $site['name'] . " (" . $site['login_base'] .")"; } +function t_slice ($slice) { return " running slice " . $slice['name'] . " (" . $slice['slice_id'] . ")"; } + +//////////////////////////////////////////////////////////// nav tabs +function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node_u($node_id)); } +function tabs_site($site) { return array('Site ' . $site['name']=>l_site_u($site_id)); } +function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice_u($slice_id)); } + +//////////////////////////////////////////////////////////// presentation // builds a table from an array of strings, with the given class function plc_make_table ($class, $messages) { // pretty print the cell @@ -329,13 +165,6 @@ function plc_make_table ($class, $messages) { return $formatted; } -// shows a php variable verbatim with a heading message -function plc_debug($message,$object) { - print "
" . $message . "
";
-  print_r ($object);
-  print "
"; -} - // attempt to normalize the delete buttons and confirmations function plc_delete_button($width=15) { return 'Delete this entry'; @@ -354,7 +183,7 @@ function plc_delete_link_button($url,$delete_message,$width=15) { } function plc_event_button($type,$param,$id) { - return ' '; + return ' '; } function plc_comon_button ($field, $value,$target="") { @@ -367,65 +196,21 @@ function plc_comon_button ($field, $value,$target="") { return $result; } -function plc_peers_option_list ($api) { - - // get list of peers - $peers=$api->GetPeers(NULL,array('peer_id','peername')); - if (count($peers)==0) { - $predef=array(array("peer_id"=>"","peername"=>"All (no known peers)")); - } else { - $predef=array(array("peer_id"=>"","peername"=>"All peers"), - array("peer_id"=>"local","peername"=>"Local only")); - // show a 'foreign' button only if that makes sense - if (count($peers) >= 2) { - $predef [] = array("peer_id"=>"foreign","peername"=>"Foreign peers"); - } - } - - $result=""; - foreach ($predef as $a) { - $peer_line = "\n"; - $result .= $peer_line; - } - - if (!empty($peers)) { - foreach ($peers as $a) { - $peer_line = "\n"; - $result .= $peer_line; - } - } - - return $result; +//////////////////////////////////////////////////////////// +function plc_error ($text) { + // should use the same channel as the php errors.. + print "
Error " . $text . "
"; } -function plc_peer_info ($api,$peerscope) { - switch ($_GET['peerscope']) { - case '': - $peer_filter=array(); - $peer_label="all peers"; - break; - case 'local': - $peer_filter=array("peer_id"=>NULL); - $peer_label="local peer"; - break; - case 'foreign': - $peer_filter=array("~peer_id"=>NULL); - $peer_label="foreign peers"; - break; - default: - $peer_id=intval($_GET['peerscope']); - $peer_filter=array("peer_id"=>$peer_id); - $peer=$api->GetPeers(array("peer_id"=>$peer_id)); - $peer_label='peer "' . $peer[0]['peername'] . '"'; - break; - } - return array ($peer_filter,$peer_label); -} - -function topdomain ($hostname) { - $exploded=array_reverse(explode(".",$hostname)); - return $exploded[0]; +function plc_warning ($text) { + print "
Warning " . $text . "
"; } +// shows a php variable verbatim with a heading message +function plc_debug ($message,$object) { + print "
" . $message . "
";
+  print_r ($object);
+  print "
"; +} ?> diff --git a/planetlab/includes/plc_functions_trash.php b/planetlab/includes/plc_functions_trash.php new file mode 100644 index 0000000..3fe1200 --- /dev/null +++ b/planetlab/includes/plc_functions_trash.php @@ -0,0 +1,270 @@ +' . $person_id . ''; + } + } + +function plc_node_link ($node_id) { + if ( empty ($node_id)) { + return ""; + } else { + return '' . $node_id . ''; + } + } + +// pagination function +function paginate( $fn_array, $table_id, $caption, $limit, $main_field, $other_func= NULL, $fid= NULL ) { + // get vars from call adjust them + $dir= strtolower( $caption ); + $echo= ""; + + $link_page= 'index.php'; + + // check for page number + if( empty( $_GET['page'] ) ) + $page= 1; + else + $page= $_GET['page']; + + // reorder array_chunk + foreach( $fn_array as $arr1 ) { + unset( $arr2 ); + + foreach( $arr1 as $key => $val ) { +// if( substr( $key, -3 ) == "_id" ) + if ( $key == $table_id ) { + $id[$key]= $val; + } else { + $data[$key]= $val; + } + } + + foreach( $id as $key => $val ) + $arr2[$key]= $val; + + foreach( $data as $key => $val ) + $arr2[$key]= $val; + + $as_array[]= $arr2; + } + + $totalrows= count( $as_array ); + + // if array has no rows display msg + if( $totalrows == 0 ) + return "Nothing to display"; + + // set key and break up data struct + $newkey= $page - 1; + $newarray= array_chunk( $as_array, $limit ); + + // start table output + $echo.= "\n"; + + // if there is a caption add it to table + if( $caption ) + $echo.= "\n"; + + $echo.= ""; + + // go through keys of one array row for table headers + foreach( $newarray[$newkey][0] as $key => $val ) { +// if( substr( $key, -3 ) != "_id" ) + if ( $key != $table_id && $key != 'peer_id' ) + $echo.= ""; + } + + if( $other_func == 'slivers' ) + $echo.= ""; + + $echo.= "\n"; + + // go through array row by row to output table rows + foreach( $newarray[$newkey] as $assoc ) { + + $extraclass=""; + if ($assoc['peer_id']) { + $extraclass="plc-foreign"; + } + + + $echo.= ""; + + foreach( $assoc as $key => $val ) { + // do not rely on keys order + $id = $assoc[$table_id]; +// if( substr( $key, -3 ) == "_id" ) + if ($key == $table_id) { +// $id= $val; + continue; + } elseif( $key == $main_field ) { + $echo.= ""; + } elseif ($key != 'peer_id') { + $echo.= ""; + } + + } + + if( $other_func == 'slivers' ) + $echo.= ""; + + $echo.= "\n"; + } + + // close table + $echo.= "
$caption
". ucfirst( $key ) ."Slivers
$val"; + if( is_array( $val ) ) { + $count= 1; + $tot= count( $val ); + foreach( $val as $k => $v ) { + $echo.= $v; + if( $count != $tot ) + $echo.= ", "; + $count++; + } + } + else + $echo.= $val; + $echo.= "view
\n"; + $echo.= "
\n"; + + // find total number of pages + $numofpages = $totalrows / $limit; + + // start navigation links + if( $numofpages > 1 ) { + // if page is not 1 display first and prev links + if( $page != 1 && $page ) { + $pageprev= $page - 1; + $echo.= "FIRST   "; + $echo.= " PREV ".$limit."   "; + } + else + $echo.= "PREV ". $limit ." "; + + // if less than 30 pages display all + // otherwise show 30 pages but put ... inbetween + if( $numofpages < 30 ) { + $npages= $numofpages; + $start= 1; + } + else { + $npages= $page + 9; + if( $npages > $numofpages ) + $npages= $numofpages; + $start= $page - 10; + if( $start < 1 ) + $start= 1; + if( $page != 1 ) + $echo.= " ... "; + } + + // display pages, no link if current page + for( $i= $start; $i <= $npages; $i++ ) { + if( $i == $page ) + $echo.= $i ." "; + else + $echo.= "$i "; + + } + + if( ( $totalrows % $limit ) != 0 ) { + $last= $numofpages + 1; + if( $i == $page ) + $echo.= $i ." "; + else + $echo.= "$i "; + } + else + $last= $numofpages; + + if( $numofpages >= 30 ) { + if( $page != $numofpages ) + $echo.= " ... "; + } + + if( ( $totalrows - ($limit * $page) ) > 0 ) { + $pagenext= $page + 1; + $echo.= "   NEXT ".$limit."   "; + } + else + $echo.= "NEXT ". $limit; + + $echo.= " LAST\n"; + + } + + return $echo; + +} + + + +function plc_peers_option_list ($api) { + + // get list of peers + $peers=$api->GetPeers(NULL,array('peer_id','peername')); + if (count($peers)==0) { + $predef=array(array("peer_id"=>"","peername"=>"All (no known peers)")); + } else { + $predef=array(array("peer_id"=>"","peername"=>"All peers"), + array("peer_id"=>"local","peername"=>"Local only")); + // show a 'foreign' button only if that makes sense + if (count($peers) >= 2) { + $predef [] = array("peer_id"=>"foreign","peername"=>"Foreign peers"); + } + } + + $result=""; + foreach ($predef as $a) { + $peer_line = "\n"; + $result .= $peer_line; + } + + if (!empty($peers)) { + foreach ($peers as $a) { + $peer_line = "\n"; + $result .= $peer_line; + } + } + + return $result; +} + + + +// function for getting the diff of multi dimention array +function arr_diff( $a1, $a2 ) { + $diff= array(); + foreach( $a1 as $k=>$v ) { + unset( $dv ); + for( $x= 0; $x < count( $a2 ); $x++ ) { + if( is_int( $k ) ) { + if( array_search( $v, $a2 ) === false ) + $dv=$v; + else if( is_array( $v ) ) + $dv= arr_diff( $v, $a2[$x] ); + if( $dv && !in_array( $dv, $diff ) ) + $diff[]=$dv; + } + else { + + if( !$a2[$k] ) + $dv=$v; + else if(is_array($v)) + $dv=arr_diff($v,$a2[$x]); + if($dv) + $diff[$x]=$dv; + } + } + } + return $diff; +} + +?> diff --git a/planetlab/includes/plc_minitabs.php b/planetlab/includes/plc_minitabs.php index c1dc3cf..2df465f 100644 --- a/planetlab/includes/plc_minitabs.php +++ b/planetlab/includes/plc_minitabs.php @@ -7,7 +7,7 @@ drupal_set_html_head(' '); -function plc_show_options($array) { +function plc_tabs($array) { print '
'; print '