X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=planetlab%2Fincludes%2Fplc_functions.php;h=9a5eff5599daf33b778b00e244a964935a5b9a9f;hb=ca6e72138a20a68200f98f4efe713d39cc2c56af;hp=68cbcddee86044fc69f7495b736376d912d72a0e;hpb=980597eb6da7f6bae88f0a0ea64b9c575eca60d9;p=plewww.git diff --git a/planetlab/includes/plc_functions.php b/planetlab/includes/plc_functions.php index 68cbcdd..9a5eff5 100644 --- a/planetlab/includes/plc_functions.php +++ b/planetlab/includes/plc_functions.php @@ -3,7 +3,137 @@ // $Id$ // will trash this eventually -require_once 'plc_functions_trash.php'; + //require_once 'plc_functions_trash.php'; + +// utility +function my_is_int ($x) { + return (is_numeric($x) ? intval($x) == $x : false); +} +//////////////////////////////////////////////////////////// roles & other checks on global $plc +function plc_is_admin () { + global $plc; + return in_array( 10, $plc->person['role_ids']); +} +function plc_is_pi () { + global $plc; + return in_array( 20, $plc->person['role_ids']); +} +function plc_is_tech () { + global $plc; + return in_array( 40, $plc->person['role_ids']); +} +function plc_in_site ($site_id) { + global $plc; + return in_array( $site_id, $plc->person['site_ids']); +} + +function plc_my_site_id () { + global $plc; + return $plc->person['site_ids'][0]; +} + +function plc_my_person_id () { + global $plc; + return $plc->person['person_id']; +} + +//////////////////////////////////////////////////////////// links +function href ($url,$text) { return "" . $text . ""; } + +// naming scheme is +// l_objects() -> the url to the page that list objects +// l_object($object_id) -> the url to hte page thas details object with given id +// l_object_t($object_id,text) -> an tag that shows text and links to the above +// l_object_add () -> the url to that object-afding page + +function l_actions () { return "/db/actions.php"; } +// some complex node actions are kept separate, e.g. the ones related to getbootmedium +function l_actions_download () { return "/db/nodes/node_downloads.php"; } + +function l_nodes () { return "/db/nodes/index.php"; } +function l_nodes_peer ($peer_id) { return "/db/nodes/index.php?peerscope=$peer_id"; } +function l_node ($node_id) { return "/db/nodes/node.php?id=$node_id"; } +function l_node_t ($node_id,$text) { return href (l_node($node_id),$text); } +function l_node_obj($node) { return href(l_node($node['node_id']),$node['hostname']); } +function l_node_add () { return "/db/nodes/node_add.php"; } +function l_nodes_site ($site_id) { return "/db/nodes/index.php?site_id=$site_id"; } + +function l_interface ($interface_id) { return "/db/nodes/interface.php?id=$interface_id"; } +function l_interface_t ($interface_id,$text) { + return href (l_interface($interface_id),$text); } +function l_interface_add($node_id) { return "/db/nodes/interface.php?node_id=$node_id"; } + +function l_sites () { return "/db/sites/index.php"; } +function l_sites_peer ($peer_id) { return "/db/sites/index.php?peerscope=$peer_id"; } +function l_site ($site_id) { return "/db/sites/index.php?id=$site_id"; } +function l_site_t ($site_id,$text) { return href (l_site($site_id),$text); } + +function l_slices () { return "/db/slices/index.php"; } +function l_slices_peer ($peer_id) { return "/db/slices/index.php?peerscope=$peer_id"; } +function l_slice ($slice_id) { return "/db/slices/index.php?id=$slice_id"; } +function l_slice_t ($slice_id,$text) { return href (l_slice($slice_id),$text); } +function l_slice_add () { return "/db/slices/slice_add.php"; } +function l_slices_site($site_id) { return "/db/slices/index.php?site_id=$site_id"; } +// from an object +function l_slice_obj ($slice) { return l_slice_t ($slice['slice_id'],$slice['name']); } + +function l_sliver ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=$node_id&slice_id=$slice_id"; } +function l_sliver_t ($node_id,$slice_id,$text) { + return href (l_sliver($node_id,$slice_id),$text) ; } + +function l_persons () { return "/db/persons/index.php"; } +function l_persons_peer ($peer_id) { return "/db/persons/index.php?peerscope=$peer_id"; } +function l_person ($person_id) { return "/db/persons/index.php?id=$person_id"; } +function l_person_t ($person_id,$text) { return href (l_person($person_id),$text); } +function l_persons_site ($site_id) { return "/db/persons/index.php?site_id=$site_id"; } + +function l_tags () { return "/db/tags/index.php"; } +function l_tag ($tag_type_id) { return "/db/tags/index.php?id=$tag_type_id"; } +function l_tag_obj ($tag) { return href(l_tag($tag['tag-type_id']),$tag['tagname']); } + +function l_nodegroups () { return "/db/tags/nodegroups.php"; } +function l_nodegroup ($nodegroup_id) { return "/db/tags/nodegroup.php?id=$nodegroup_id"; } +function l_nodegroup_t ($nodegroup_id,$text) { + return href(l_nodegroup($nodegroup_id),$text); } + +function l_events () { return "/db/events/index.php"; } +function l_event ($type,$param,$id) { return "/db/events/index.php?type=$type&$param=$id"; } + +function l_peers() { return "/db/peers/index.php"; } +function l_peer($peer_id) { return "/db/peers/index.php?id=$peer_id"; } +function l_peer_t($peer_id,$text) { return href(l_peer($peer_id),$text); } + +function l_comon($id_name,$id_value) { return "/db/nodes/comon.php?$id_name=$id_value"; } +function l_sirius() { return "/db/sirius/index.php"; } +function l_about() { return "/db/about.php"; } +function l_doc_plcapi() { return "/db/doc/PLCAPI.php"; } +function l_doc_nmapi() { return "/db/doc/NMAPI.php"; } +function l_admin() { return "/db/adminsearch.php"; } + +function l_login() { return "/db/login.php"; } +function l_logout() { return "/planetlab/logout.php"; } +function l_sulogout() { return "/planetlab/sulogout.php"; } +function l_reset_password() { return "/db/persons/reset_password.php"; } +function l_person_register() { return "/db/persons/register.php"; } +function l_site_register() { return "/db/sites/register.php"; } +function l_sites_pending() { return "/db/sites/join_request.php"; } + +function tabs_events() { return array('image'=>'/planetlab/icons/event.png','height'=>18);} +function tabs_comon() { return array('image'=>'/planetlab/icons/comon.png','height'=>18);} + +// returns array ['url' => path, 'values' => hash (key=>value)* ] +function split_url ($full_url) { + list($url,$args) = explode("?",$full_url); + $values=array(); + if ($args) { + $pairs=explode("&",$args); + foreach ($pairs as $pair) { + list ($name,$value) = explode("=",$pair); + $values[$name]=$value; + } + } + return array("url"=>$url,"values"=>$values); +} //////////////////////////////////////////////////////////// validation functions function topdomain ($hostname) { @@ -86,149 +216,121 @@ function is_reserved_network_addr($network_addr) { return false; } -//////////////////////////////////////////////////////////// roles & other checks on global $plc -function plc_is_admin () { - global $plc; - return in_array( 10, $plc->person['role_ids']); -} -function plc_is_pi () { - global $plc; - return in_array( 20, $plc->person['role_ids']); -} -function plc_is_tech () { - global $plc; - return in_array( 40, $plc->person['role_ids']); -} -function plc_in_site ($site_id) { - global $plc; - return in_array( $site_id, $plc->person['site_ids']); +//////////////////////////////////////////////////////////// roles +function plc_role_global_hash ($api) { + $hash=array(); + $roles=$api->GetRoles(); + foreach ($roles as $role) { + $hash[$role['role_id']]=$role['name']; + } + return $hash; } - -//////////////////////////////////////////////////////////// peer & peerscopes -// when shortnames are needed on peers -function plc_peer_get_hash ($api) { - $peer_columns=array('peer_id','shortname'); - $peer_filter=array(); - $peers = $api->GetPeers($peer_filter,$peer_columns); - $peer_hash=array(); - foreach ($peers as $peer) { - $peer_hash[$peer['peer_id']]=$peer; - } + +//////////////////////////////////////////////////////////// nodegroups +// hash by 'tagname=value' +function plc_nodegroup_global_hash ($api,$tagnames=NULL) { + $filter=NULL; + // xxx somehow this does not work; I've checked that the feature is working from plcsh + // but I suspect the php marshalling or something; no time to fix, get all nodegroups for now + // if ($tagnames) $filter=array("tagname"=>$tagnames); + $nodegroups=$api->GetNodeGroups($filter); + $hash=array(); + if ($nodegroups) foreach ($nodegroups as $nodegroup) { + $key=$nodegroup['tagname']."=".$nodegroup['value']; + $hash[$key]=$nodegroup; + } + return $hash; } + +//////////////////////////////////////////////////////////// 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'] . ")"; } -function plc_peer_shortname ($peer_hash,$peer_id) { - if ( ! $peer_id ) { - return PLC_SHORTNAME; - } else { - return $peer_hash[$node['peer_id']]['shortname']; +//////////////////////////////////////////////////////////// nav tabs +function tabs_node($node) { return array('Node ' . $node['hostname']=>l_node($node_id)); } +function tabs_site($site) { return array('Site ' . $site['name']=>l_site($site_id)); } +function tabs_slice($slice) { return array('Slice ' . $slice['name']=>l_slice($slice_id)); } + +//////////////////////////////////////////////////////////// html fragments +function plc_vertical_table ($messages, $class="") { + // pretty print the cell + if ( empty( $messages) ) return ""; + $formatted = ""; + $formatted .= ""; } + $formatted .= ""; + return $formatted; } -// to set the background to grey on foreign objects -function plc_peer_block_start ($peer_hash,$peer_id) { - if ( ! $peer_id ) { - print "
"; - } else { - // set two classes, one eneraic to all foreign, and one based on the peer's shortname for finer grain tuning - printf ('
";} + print "

$text

\n"; } -function plc_peer_block_end () { - print "
\n"; +function plc_error ($text) { + // should use the same channel as the php errors.. + print "
Error " . $text . "
"; } -//// standard peerscope syntax -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; +function plc_errors ($errors) { + if ($errors) { + print( "
" ); + print( "

The following errors occured:

" ); + print("
    "); + foreach( $errors as $error ) + print( "
  • $error
  • \n" ); + print( "
\n" ); } - 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 plc_warning_html ($text) { return "" . $text . "";} +function plc_warning ($text) { print plc_warning_html("Warning " . $text); } +function plc_foreign_html($text) { return "$text"; } -function l_interface_u ($interface_id) { return "/db/nodes/interfaces.php?id=" . $interface_id; } -function l_interface_add_u($node_id) { return "/db/nodes/interfaces.php?node_id=" . $node_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_nodegroup_u ($nodegroup_id) { return "/db/nodes/node_groups.php?id=" . $nodegroup_id; } -function l_nodegroup2 ($nodegroup_id,$text) { return href(l_nodegroup_u($nodegroup_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/slices/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_sliver_u ($node_id,$slice_id) { return "/db/nodes/slivers.php?node_id=" . $node_id. "&slice_id=" . $slice_id; } -function l_sliver3 ($node_id,$slice_id,$text) { return href (l_sliver_u($node_id,$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_event ($type,$param,$id) { return '/db/events/index.php?type=' . $type . '&' . $param . '=' . $id; } -function l_comon($id_name,$id_value) { return '/db/nodes/comon.php?' . $id_name . "=" . $id_value; } - -function l_logout() { return "/planetlab/logout.php"; } +// shows a php variable verbatim with a heading message +function plc_debug ($message,$object) { + print "
" . $message . "
";
+  print_r ($object);
+  print "
"; +} -//////////////////////////////////////////////////////////// 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'] . ")"; } +function truncate ($text,$numb,$etc = "...") { + if (strlen($text) > $numb) { + $text = substr($text, 0, $numb); + $text = $text.$etc; + } + return $text; +} +function html_div ($text,$class="") { + $html="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)); } +if (! function_exists ("drupal_set_error")) { + function drupal_set_error ($text) { + drupal_set_message ("$text"); + } + } -//////////////////////////////////////////////////////////// presentation +//////////////////////////////////////////////////////////// sort out for obsolete / trash // builds a table from an array of strings, with the given class // attempt to normalize the delete buttons and confirmations -function plc_delete_button($width=15) { - return 'Delete this entry'; +function plc_delete_icon($width=15) { + return ""; } -function plc_js_confirm($message) { - return "onclick=\"javascript:return confirm('Are you sure you want to delete " . $message . " ?')\""; +function plc_bubble($text,$bubble) { + return "$text"; } - -function plc_delete_link($url,$delete_message,$visible) { - return "" . $visible . ""; -} - -function plc_delete_link_button($url,$delete_message,$width=15) { - return "" . plc_delete_button($width) . ""; +function plc_delete_icon_bubble ($bubble,$width=15) { + return plc_bubble(plc_delete_icon($width),$bubble); } function plc_event_button($type,$param,$id) { @@ -245,34 +347,10 @@ function plc_comon_button ($id_name, $id_value,$target="") { return $result; } -function plc_make_table ($class, $messages) { - // pretty print the cell - $formatted = ""; - if (! empty ($messages)) { - $formatted=""; - foreach ($messages as $message) { - $formatted .= ""; - } - $formatted .= "
" . $message . "
"; - } - return $formatted; -} - -//////////////////////////////////////////////////////////// -function plc_error ($text) { - // should use the same channel as the php errors.. - print "
Error " . $text . "
"; -} - -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 "
"; +//////////////////// +function plc_redirect ($url) { + header ("Location: " . $url); + exit (); } ?>