bugfix: the slice page was broken when nobody is in slice
[plewww.git] / planetlab / includes / plc_functions.php
index 76c81e7..97b0bbb 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
-// $Id$
+// install the default timezone as defined in php.ini
+date_default_timezone_set(ini_get('date.timezone'));
 
 # note: this needs to be consistent with the value in Monitor/monitor/wrapper/plc.py
 global $PENDING_CONSORTIUM_ID;
@@ -59,7 +60,7 @@ function plc_my_person_id () {
   return $plc->person['person_id'];
 }
 
-//////////////////////////////////////////////////////////// links    
+//////////////////////////////////////////////////////////// links
 function href ($url,$text) { return "<a href='" . $url . "'>" . $text . "</a>"; }
 
 // naming scheme is
@@ -74,10 +75,13 @@ function l_actions_download ()              { return "/db/nodes/node_downloads.php"; }
 function l_register_node ()            { return "/registerwizard/index.php"; }
 function l_pcu_add ()                  { return "/registerwizard/index.php/register/stage1_addpcu"; }
 function l_pcu ($pcu_id)               { return "/db/sites/pcu.php?id=$pcu_id"; }
+function l_pcu_href ($pcu_id, $text)   { return href(l_pcu($pcu_id), $text); }
 
 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_interfaces ($node_id)  { return "/db/nodes/node.php?id=$node_id&show_interfaces=1"; }
+function l_node_tags ($node_id)                { return "/db/nodes/node.php?id=$node_id&show_tags=1"; }
 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"; }
@@ -88,8 +92,8 @@ function l_nodes_person ($person_id)  { return "/db/nodes/index.php?person_id=$pe
 function l_nodes_slice ($slice_id)     { return "/db/nodes/index.php?slice_id=$slice_id"; }
 
 function l_interface ($interface_id)   { return "/db/nodes/interface.php?id=$interface_id"; }
-function l_interface_tags($interface_id){ return "/db/nodes/interface.php?id=$interface_id&show_details=0&show_tags=1"; }
-function l_interface_t ($interface_id,$text) { 
+function l_interface_tags($interface_id){ return "/db/nodes/interface.php?id=$interface_id&show_tags=1"; }
+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"; }
 
@@ -98,11 +102,12 @@ function l_sites_peer ($peer_id)   { return "/db/sites/index.php?peerscope=$peer_i
 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_site_obj($site)             { return href (l_site($site['site_id']),$site['name']); }
+function l_site_tags ($site_id)                { return "/db/sites/site.php?id=$site_id&show_tags=1"; }
 
 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_nodes ($slice_id)     { return "/db/slices/index.php?id=$slice_id&show_details=0&show_nodes=1&show_nodes_current=1&show_nodes_add=1"; }
+function l_slice_nodes ($slice_id)     { return "/db/slices/index.php?id=$slice_id&show_nodes=1&show_nodes_current=1&show_nodes_add=1"; }
 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"; }
@@ -113,27 +118,29 @@ function l_slices_local()         { return "/db/slices/index.php?peerscope=local"; }
 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) { 
+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&active_line_tab=All Accounts"; }
 function l_persons_peer ($peer_id)     { return "/db/persons/index.php?peerscope=$peer_id&active_line_tab=Local Accounts"; }
 function l_person ($person_id)         { return "/db/persons/index.php?id=$person_id"; }
-function l_person_roles ($person_id)   { return "/db/persons/index.php?id=$person_id&show_details=0&show_roles=1"; }
+function l_person_roles ($person_id)   { return "/db/persons/index.php?id=$person_id&show_roles=1"; }
 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_persons_slice ($slice_id)   { return "/db/persons/index.php?slice_id=$slice_id"; }
 function l_person_obj ($person)                { return l_person_t($person['person_id'],$person['email']); }
+function l_person_tags ($person_id)    { return "/db/persons/person.php?id=$person_id&show_tags=1"; }
 
 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_tag_roles ($tag_type_id)    { return "/db/tags/index.php?id=$tag_type_id&show_roles=1"; }
 
 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) { 
+function l_nodegroup_t ($nodegroup_id,$text) {
                                          return href(l_nodegroup($nodegroup_id),$text); }
-function l_nodegroup_obj ($nodegroup) { 
+function l_nodegroup_obj ($nodegroup) {
                                          return href(l_nodegroup($nodegroup['nodegroup_id']),$nodegroup['groupname']); }
 
 function l_events ()                   { return "/db/events/index.php"; }
@@ -161,15 +168,15 @@ function l_site_review_pending($site_id){ return "/db/sites/join_request.php?rev
 
 
 //////////////////////////////////////////////////////////// nav tabs
-function tab_nodes ()          { return array ('label'=>'All nodes','url'=>l_nodes(), 
+function tab_nodes ()          { return array ('label'=>'All nodes','url'=>l_nodes(),
                                                'bubble'=>'Display nodes from all peers'); }
-function tab_nodes_local ()    { return array ('label'=>'Local nodes', 'url'=>l_nodes_peer('local'), 
+function tab_nodes_local ()    { return array ('label'=>'Local nodes', 'url'=>l_nodes_peer('local'),
                                                'bubble'=>'Display all nodes local to this peer'); }
-function tab_nodes_site($site_id){ return array ('label'=>'Site nodes', 'url'=>l_nodes_site($site_id), 
+function tab_nodes_site($site_id){ return array ('label'=>'Site nodes', 'url'=>l_nodes_site($site_id),
                                                 'bubble'=>'Display nodes on that site'); }
-function tab_nodes_slice($slice_id){ return array ('label'=>'Slice nodes', 'url'=>l_nodes_slice($slice_id), 
+function tab_nodes_slice($slice_id){ return array ('label'=>'Slice nodes', 'url'=>l_nodes_slice($slice_id),
                                                 'bubble'=>'Display nodes for that slice'); }
-function tab_nodes_mysite ()   { return array ('label'=>'My site nodes', 'url'=>l_nodes_my_site(), 
+function tab_nodes_mysite ()   { return array ('label'=>'My site nodes', 'url'=>l_nodes_my_site(),
                                                'bubble'=>'Display nodes on my site'); }
 function tab_nodes_all_mysite (){ return array ('label'=>'All My site nodes', 'url'=>l_nodes_all_my_site(),
                                                'bubble'=>'Display nodes on all my sites'); }
@@ -181,16 +188,16 @@ function tab_site($site)  { return array ('label'=>'Site '.$site['login_base'], '
 function tab_mysite()          { return array ('label'=>'My site', 'url'=>l_site(plc_my_site_id()),
                                                'bubble'=>'Details for site ' . plc_my_site_id()); }
 function tab_sites ()          { return array ('label'=>'All sites' , 'url'=>l_sites(), 'bubble'=> 'Display all sites'); }
-function tab_sites_local ()    { return array ('label'=>'Local sites' , 'url'=>l_sites_peer('local'), 
+function tab_sites_local ()    { return array ('label'=>'Local sites' , 'url'=>l_sites_peer('local'),
                                                'bubble'=> 'Display all siteslocal to this peer'); }
 //////////
 function tab_slices()          { return array ('label'=>'All slices', 'url'=>l_slices(),
                                                'bubble' => 'Display all slices'); }
 function tab_slices_person()   { return array ('label'=>'My slices', 'url'=>l_slices_person(plc_my_person_id()),
                                                'bubble' => 'Display my all slices'); }
-function tab_slices_mysite ()  { return array ('label'=>'My site slices', 'url'=>l_slices_my_site(), 
+function tab_slices_mysite ()  { return array ('label'=>'My site slices', 'url'=>l_slices_my_site(),
                                                'bubble'=>'Display all slices on my site'); }
-function tab_slices_local ()   { return array ('label'=>'Local slices', 'url'=>l_slices_local(), 
+function tab_slices_local ()   { return array ('label'=>'Local slices', 'url'=>l_slices_local(),
                                                'bubble'=>'Display all slices local to this peer'); }
 function tab_slice($slice)     { return array ('label'=>'Slice '.$slice['name'], 'url'=>l_slice($slice['slice_id']),
                                                'bubble' => 'Details for ' . $slice['name']); }
@@ -221,24 +228,27 @@ function topdomain ($hostname) {
   return $exploded[0];
 }
 
-function is_valid_email_addr ($email) {
-  if (ereg("^.+@.+\\..+$", $email) ) {
-    return true;
-  } else {
-    return false;
-  }
-}
-
-function is_valid_url ($url) {
-  if (ereg("^(http|https)://.+\..+$", strtolower($url) ) ) {
-    return true;
-  } else {
-    return false;
-  }
-}
+//// with php-5.3 on f12, ereg is marked deprecated, using PCRE instead
+//// looks unused
+// function is_valid_email_addr ($email) {
+//  if (preg_match("/^.+@.+\\..+$/", $email) ) {
+//    return true;
+//  } else {
+//    return false;
+//  }
+//}
+//
+//// looks unused
+//function is_valid_url ($url) {
+//  if (preg_match("/^(http|https):\/\/.+\..+$/", strtolower($url) ) ) {
+//    return true;
+//  } 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 ) ) {
+  if (preg_match("/^([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 );
@@ -288,7 +298,7 @@ function is_reserved_network_addr($network_addr) {
   foreach ($reserved_ips as $r) {
     $min = ip2long($r[0]);
     $max = ip2long($r[1]);
-      
+
     if (($lNetwork >= $min) && ($lNetwork <= $max))
       return true;
   }
@@ -307,15 +317,18 @@ function plc_role_global_hash ($api) {
 }
 
 // because GetRoles does not correctly support filters, it's really painful to do this
-function sort_roles ($r1, $r2) { return $r2['role_id'] - $r1['role_id']; }
 function roles_except ($roles, $exception_ids) {
   $result=array();
-  if ($roles) foreach ($roles as $role) {
+  if ($roles)
+    foreach ($roles as $role) {
       if ( ! in_array ($role['role_id'],$exception_ids) ) {
-       $result[]=$role;
+             $result[]=$role;
       }
     }
-  usort($result,sort_roles);
+
+  usort($result,
+        function ($r1, $r2) { return $r2['role_id'] - $r1['role_id']; }
+      );
   return $result;
 }
 
@@ -334,7 +347,7 @@ function plc_nodegroup_global_hash ($api,$tagnames=NULL) {
     }
   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'] . ")"; }
@@ -366,14 +379,18 @@ function plc_itemize ($messages, $class="") {
   return $formatted;
 }
 
-//////////
+////////// just return a truncated text
 function truncate ($text,$numb,$etc = "...") {
-  if (strlen($text) > $numb) {
-    $text = substr($text, 0, $numb);
-    $text = $text.$etc;
-  }
-  return $text;
+  if (strlen($text) <= $numb)  return $text;
+  return substr($text, 0, $numb).$etc;
+}
+// ditto but in case the text is too lare, returns a <span> with its 'title' set to the full value
+function truncate_and_popup ($text,$numb,$etc = "...") {
+  if (strlen($text) <= $numb)  return $text;
+  $display=substr($text, 0, $numb).$etc;
+  return sprintf("<span title='%s'>%s</span>",$text,$display);
 }
+
 // generates <(atom) class=(class)> (text) </(atom)>
 function html_atom ($atom,$text,$class="") {
   $html="<$atom";
@@ -402,7 +419,7 @@ function errors_display ($errors) {
     print( "<div class='plc-error'>" );
     print( "<p>The following errors occured:</p>" );
     print("<ul>");
-    foreach( $errors as $error ) 
+    foreach( $errors as $error )
       print( "<li>$error</li>\n" );
     print( "</ul></div>\n" );
   }
@@ -415,11 +432,45 @@ function bold_html ($text)                { return html_span($text,'bold'); }
 
 // shows a php variable verbatim with a heading message
 function plc_debug ($message,$object) {
-  print "<br>" . $message . "<pre>";
+  print "<br />" . $message . "<pre>";
   print_r ($object);
   print "</pre>";
 }
 
+function plc_debug_txt ($message,$txt) {
+  print "<br />" . $message . "<pre>";
+  $txt=str_replace(" ","&lt;SPC&gt;",$txt);
+  $txt=str_replace("\t","&lt;TAB&gt;",$txt);
+  $txt=str_replace("\n","&lt;LF&gt;",$txt);
+  $txt=str_replace("\r","&lt;CR&gt;",$txt);
+  print $txt . "&lt;EOF&gt;";
+  print "</pre>";
+}
+
+$plc_prof_start=0.;
+$plc_prof_time=0.;
+$plc_prof_counter=0;
+function plc_debug_prof_start () {
+  global $plc_prof_counter, $plc_prof_start, $plc_prof_time;
+  $plc_prof_counter=0;
+  plc_debug(strftime("[0] %T (start)") ,"heating up");
+  $plc_prof_time=microtime(true);
+  $plc_prof_start=$plc_prof_time;
+}
+function plc_debug_prof ($message,$object) {
+  global $plc_prof_counter, $plc_prof_start, $plc_prof_time;
+  $plc_prof_counter+=1;
+  $now=microtime(true);
+  $timelabel=strftime("%T");
+  $prof_message=sprintf("[%d] %s (%2.3f s -- %2.3f s) ",$plc_prof_counter,$timelabel,
+                       ($now-$plc_prof_time),($now-$plc_prof_start));
+  plc_debug($prof_message.$message,$object);
+  $plc_prof_time=$now;
+}
+function plc_debug_prof_end () {
+  plc_debug_prof ("end","cooling down");
+}
+
 if (! function_exists ("drupal_set_error")) {
   function drupal_set_error ($text) {
     drupal_set_message ("<span class=error>$text</span>");
@@ -464,6 +515,27 @@ function plc_redirect ($url) {
   exit ();
 }
 
+//////////////////// the options for an nodetype - suitable for plekit/form
+global $builtin_node_types;
+$builtin_node_types = array ( "regular" => "Regular/Shared",
+                             "reservable" => "Reservable (requires to get leases)");
+function node_type_display ($api, $node_type) {
+  global $builtin_node_types;
+  $val=$builtin_node_types[$node_type];
+  if ( ! $val) $val="??undefined??";
+  return $val;
+}
+
+function node_type_selectors ($api,$node_type) {
+  global $builtin_node_types;
+  foreach ($builtin_node_types as $value=>$display) {
+    $selector=array('display'=>$display, 'value'=>$value);
+    if ($value == $node_type) $selector['selected']=true;
+    $selectors []= $selector;
+  }
+  return $selectors;
+}
+
 //////////////////// the options for an interface - suitable for plekit/form
 //>>> GetNetworkMethods()
 //[u'static', u'dhcp', u'proxy', u'tap', u'ipmi', u'unknown']
@@ -473,8 +545,8 @@ function interface_method_selectors ($api, $method, $primary) {
                           "dhcp"=>"DHCP");
   } else {
     $builtin_methods=array("static"=>"Static",
-                          "dhcp"=>"DHCP", 
-                          "proxy"=>"Proxy",  
+                          "dhcp"=>"DHCP",
+                          "proxy"=>"Proxy",
                           "tap"=>"TUN/TAP",
                           "ipmi"=>"IPMI");
   }
@@ -495,7 +567,7 @@ function pretty_bandwidth ($bw) {
   else                         return strval($bw/1000000000) . " Gbps";
 }
 
-//////////////////// 
+////////////////////
 function instantiation_label ($slice) {
   $instantiation_labels = array ('not-instantiated'=>'NOT',
                                 'plc-instantiated'=>'PLC',
@@ -506,10 +578,10 @@ function instantiation_label ($slice) {
   if (!$result) $result = '??';
   return $result;
 }
-  
+
 //////////////////// toggle areas
 // get_arg ('show_persons',false) returns $_GET['show_persons'] if set and false otherwise
-function get_arg ($name,$default,$method='get') {
+function get_arg ($name,$default=NULL,$method='get') {
   if ($method == 'get') $var=$_GET; else $var=$_POST;
   if (isset ($var[$name])) return $var[$name];
   else return $default;
@@ -527,4 +599,40 @@ function count_english_warning ($objs, $name) {
   if (count ($objs) == 0) $x=plc_warning_html($x . ' !!');
   return $x;
 }
+
+//////////////////// outlining reservable nodes
+function reservable_mark () { return "-R-";}
+function reservable_legend () { return "reservable nodes are marked with " . reservable_mark (); }
+
+//////////////////// Vicci simplified portal support
+function plc_advanced() {
+    global $plc, $api;
+
+    if ((!$plc) || (!$api)) {
+        return FALSE;
+    }
+
+    $person_id = $plc->person['person_id'];
+    $tags = $api->GetPersonTags(array("person_id" => $person_id, "tagname" => "advanced"));
+    if (!$tags) {
+        return FALSE;
+    }
+    return (bool) $tags[0]['value'];
+}
+
+function plc_set_advanced($value) {
+    global $plc, $api;
+
+    $person_id = $plc->person['person_id'];
+    $tags = $api->GetPersonTags(array("person_id" => $person_id, "tagname" => "advanced"));
+    if ($tags) {
+        $result = $api->UpdatePersonTag($tags[0]["person_tag_id"], $value);
+        //print "update " . $tags[0]["person_tag_id"] . " " . $value . " " . $result . "<br>";
+    } else {
+        $api->AddPersonTag($person_id, "advanced", $value);
+        //print "add";
+    }
+}
+
+
 ?>