brute-force changed access to $_GET['key'] to use get_array instead
[plewww.git] / planetlab / sites / site.php
index d9c5cdb..e6aa096 100644 (file)
@@ -22,9 +22,9 @@ require_once 'details.php';
 require_once 'form.php';
 require_once 'toggle.php';
 
-// -------------------- 
+// --------------------
 // recognized URL arguments
-$site_id=intval($_GET['id']);
+$site_id = intval(get_array($_GET, 'id'));
 if ( ! $site_id ) { plc_error('Malformed URL - id not set'); return; }
 
 ////////////////////
@@ -38,6 +38,9 @@ if (empty($sites)) {
 
 $site=$sites[0];
 // var names to api return
+// Thomas Dreibholz suggested that utf8_decode could be non-needed
+// and maybe even harmful on modern systems like f23
+// $sitename= htmlentities(utf8_decode($site['name']));
 $sitename= htmlentities($site['name']);
 $abbreviated_name= htmlentities($site['abbreviated_name']);
 $site_url= $site['url'];
@@ -48,8 +51,9 @@ $max_slivers= $site['max_slivers'];
 $max_slices= $site['max_slices'];
 
 $enabled = $site['enabled'];
+$ext_consortium_id = $site ['ext_consortium_id'];
 
-// get peer 
+// get peer
 $peer_id= $site['peer_id'];
 $peers = new Peers ($api);
 $local_peer = ! $peer_id;
@@ -58,37 +62,51 @@ $local_peer = ! $peer_id;
 $is_site_pi = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
 $is_site_tech = ( $local_peer && plc_is_pi() && plc_in_site($site_id) );
 $is_site_admin = ($local_peer && plc_is_admin());
-  
+
 $address_ids= $site['address_ids'];
 $pcu_ids= $site['pcu_ids'];
 $node_ids= $site['node_ids'];
 $person_ids= $site['person_ids'];
 $slice_ids= $site['slice_ids'];
 
-$api->begin();
+//$api->begin();
 // gets address info
-$api->GetAddresses( $address_ids );
+$addresses = $api->GetAddresses( $address_ids );
 
 // gets node info
-$api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
-
+if ($node_ids) {
+       $nodes = $api->GetNodes( $node_ids, array( "node_id", "hostname", "boot_state", "pcu_ids", "ports" ) );
+} else {
+       $nodes = array();
+}
 // gets person info
-$api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
-
-$api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
+if ($person_ids) {
+       $persons = $api->GetPersons( $person_ids, array( "role_ids", "person_id", "first_name", "last_name", "email", "enabled" , "slice_ids") );
+} else {
+       $persons = array();
+}
 
+if ($slice_ids) {
+       $slices = $api->GetSlices ( $slice_ids, array ("slice_id", "name", "instantiation", "node_ids", "person_ids" ) );
+} else {
+       $slices = array();
+}
 ////////////////////
 // PCU stuff - not too sure why, but GetPCUs is not exposed to the 'user' role
 $display_pcus = (plc_is_admin() || plc_is_pi() || plc_is_tech());
-if ($display_pcus) 
-  $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
-
+if ($display_pcus) {
+       if ($pcu_ids) {
+               $pcus = $api->GetPCUs ($pcu_ids, array ('hostname', 'pcu_id' ));
+       } else {
+               $pcus = array();
+       }
+}
 // get results
-if ($display_pcus)
-  list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
-else
-  list( $addresses, $nodes, $persons, $slices )= $api->commit();
-  
+//if ($display_pcus)
+//  list( $addresses, $nodes, $persons, $slices, $pcus )= $api->commit();
+//else
+//  list( $addresses, $nodes, $persons, $slices )= $api->commit();
+
 $techs = array();
 $pis = array();
 $disabled_persons = array();
@@ -98,7 +116,7 @@ if ($persons) foreach( $persons as $person ) {
   if ( in_array( '20', $role_ids ))    $pis[] = $person;
   if ( in_array( '40', $role_ids ))    $techs[] = $person;
   if ( ! $person['enabled'] )          $disabled_persons[] = $person;
-  
+
 }
 
 $has_disabled_persons = count ($disabled_persons) !=0;
@@ -109,7 +127,7 @@ if ($slices) foreach ($slices as $slice) $slivers_count += count ($slice['node_i
 
 ////////////////////////////////////////
 drupal_set_title("Details for site " . $sitename);
-  
+
 $tabs=array();
 
 $tabs []= tab_mysite();
@@ -130,7 +148,7 @@ if ( $is_site_admin)
                        'bubble'=>"Delete site $sitename",
                        'confirm'=>"Are you sure you want to delete site $login_base");
 
-if ( $is_site_pi ) 
+if ( $is_site_pi )
   $tabs ['Add slice'] = array ('url'=>l_slice_add(),
                              'method'=>'post',
                              'bubble'=>'Create new slice in site');
@@ -150,16 +168,28 @@ plekit_linetabs($tabs);
 // show gray background on foreign objects : start a <div> with proper class
 $peers->block_start ($peer_id);
 
-if ( $local_peer && ( ! $enabled ) )
-  plc_warning ("This site is not enabled - Please visit " . 
-              href (l_sites_pending(),"this page") . 
-              " to review pending applications.");
+// sanity checks
+if ( $local_peer ) {
+  // pending site
+  global $PENDING_CONSORTIUM_ID;
+  if ( $ext_consortium_id === $PENDING_CONSORTIUM_ID) {
+    if ( ! $enabled )
+      plc_warning ("This site is under pending registration - Please visit " .
+                  href (l_sites_pending(),"this page") .
+                  " to review pending applications.");
+    else
+      plc_warning ("This site is pending but is also enabled - something is wrong. You should fix the issue with plcsh");
+  } else {
+    if ( ! $enabled)
+      plc_warning ("This site is disabled.");
+  }
+}
 
 $can_update=(plc_is_admin ()  && $local_peer) || ( plc_in_site($site_id) && plc_is_pi());
 
 
 $toggle = new PlekitToggle ('site',"Details",
-                           array('visible'=>get_arg('show_details',true),
+                           array('visible'=>get_arg('show_details'),
                                  'bubble'=>'Display and modify details for that site'));
 $toggle->start();
 
@@ -169,7 +199,7 @@ $f = $details->form_start(l_actions(),array('action'=>'update-site','site_id'=>$
 
 $details->start();
 
-if ( ! $site['is_public']) 
+if ( ! $site['is_public'])
   $details->tr(plc_warning_html("This site is not public!"));
 
 $details->th_td("Full name",$sitename,'name',array('width'=>50));
@@ -179,7 +209,7 @@ $details->th_td("Latitude",$site_lat,'latitude');
 $details->th_td("Longitude",$site_long,'longitude');
 
 // modifiable by admins only
-if (plc_is_admin()) 
+if (plc_is_admin())
   $details->th_td("Login base",$login_base,'login_base',array('width'=>12));
 else
   $details->th_td("Login base",$login_base);
@@ -189,7 +219,7 @@ else
   $details->th_td("Max slices",$max_slices);
 if (plc_is_admin())
 {
-  $selectors=array(array('display'=>"False",'value'=>'0'), 
+  $selectors=array(array('display'=>"False",'value'=>'0'),
                                   array('display'=>"True",'value'=>'1'));
   $selectors[intval($enabled)]['selected'] = 'selected';
 
@@ -219,18 +249,18 @@ if ( $local_peer ) {
   $nodes_title = "Nodes : ";
   $nodes_title .= count($nodes) . " total";
   $nodes_title .= " / " . $nb_boot . " boot";
-  if ($nb_boot < 2 ) 
+  if ($nb_boot < 2 )
     $nodes_title = plc_warning_html ($nodes_title);
   $nodes_title .= href(l_nodes_site($site_id)," (See as nodes)");
 
   $toggle=new PlekitToggle ('nodes',$nodes_title,
-                           array('visible'=>get_arg('show_nodes',true)));
+                           array('visible'=>get_arg('show_nodes')));
   $toggle->start();
 
   $headers=array();
   $sort_column = '0';
   if ($display_pcus) { $headers['PCU']='string'; $sort_column = '1' ; }
-  $headers['hostname']='string'; 
+  $headers['hostname']='string';
   $headers['state']='string';
 
   $table = new PlekitTable ('nodes',$headers,$sort_column,array('search_area'=>false,
@@ -251,7 +281,7 @@ if ( $local_peer ) {
     if (empty($ports)) return plc_error_html('???');
     $port=$ports[0];
     $pcu=$pcu_hash[$pcu_id];
-    $display= $pcu['hostname'] . ' : ' . $port;
+    $display= l_pcu_href($pcu_id, $pcu['hostname'] . ' : ' . $port);
     $pcu_hash[$pcu_id]['displayed']=true;
     return $display;
   }
@@ -268,7 +298,7 @@ if ( $local_peer ) {
     $table->row_end();
   }
   // show undisplayed PCU's if any
-  if ($display_pcus) 
+  if ($display_pcus)
     if ($pcu_hash) foreach ($pcu_hash as $id=>$pcu) {
        if (!$pcu['displayed']) {
          $table->row_start();
@@ -276,7 +306,7 @@ if ( $local_peer ) {
          $table->row_end();
        }
       }
-    
+
   $table->tfoot_start();
   $table->row_start();
   $button=new PlekitFormButton (l_node_add(),"node_add","Add node","POST");
@@ -284,20 +314,20 @@ if ( $local_peer ) {
   $table->row_end();
   $table->end();
   $toggle->end();
-    
+
   //////////////////// Users
   $persons_title = "Users : ";
   $persons_title .= count($person_ids) . " total";
   $persons_title .= " / " . count ($pis) . " PIs";
   $persons_title .= " / " . count ($techs) . " Techs";
-  if ($has_disabled_persons) 
+  if ($has_disabled_persons)
     $persons_title .= " / " . count($disabled_persons) . " Disabled";
-  if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 ) 
+  if ( (count ($pis) == 0) || (count ($techs) == 0) || (count($person_ids) >= 30) || count($disabled_persons) != 0 )
     $persons_title = plc_warning_html ($persons_title);
   $persons_title .= href(l_persons_site($site_id)," (See as users)");
 
   $toggle=new PlekitToggle ('persons',$persons_title,
-                           array('visible'=>get_arg('show_persons',false)));
+                           array('visible'=>get_arg('show_persons')));
   $toggle->start();
 
   $headers = array ();
@@ -330,12 +360,12 @@ if ( $local_peer ) {
   $slices_title .= $max_slices . " max";
   $slices_title .= " / " . count($slice_ids) . " running";
   $slices_title .= " / $slivers_count slivers";
-  if (count($slice_ids) >= $max_slices) 
+  if (count($slice_ids) >= $max_slices)
     $slices_title = plc_warning_html($slices_title);
   $slices_title .= href(l_slices_site($site_id)," (See as slices)");
-  
+
   $toggle=new PlekitToggle ('slices',$slices_title,
-                           array('visible'=>get_arg('show_slices',false)));
+                           array('visible'=>get_arg('show_slices')));
   $toggle->start();
 
   $headers = array ();
@@ -365,13 +395,74 @@ if ( $local_peer ) {
     $table->row_start();
     $table->cell($button->html(),array('hfill'=>true,'align'=>'right'));
   }
-    
+
+  $table->end();
+  $toggle->end();
+
+  $form=new PlekitForm (l_actions(), array('site_id'=>$site_id));
+  $form->start();
+  //////////////////////////////////////////////////////////// Tags
+  // tags section
+  // already inside a if ( $local_peer )...
+
+  $tags=$api->GetSiteTags (array('site_id'=>$site_id));
+  function get_tagname ($tag) { return $tag['tagname'];}
+  // xxx looks like tech-only see an error here,
+  // might be that GetSiteTags is not accessible or something
+  $tagnames = array_map ("get_tagname",$tags);
+
+  $toggle = new PlekitToggle ('tags',count_english($tags,'tag'),
+                             array('bubble'=>'Inspect and set tags on that site',
+                                   'visible'=>get_arg('show_tags')));
+  $toggle->start();
+
+  $headers=array("Name"=>"string",
+                "Value"=>"string",
+                );
+  if (plc_is_admin()) $headers[plc_delete_icon()]="none";
+
+  $table_options=array("notes_area"=>false,"pagesize_area"=>false,"search_width"=>10);
+  $table=new PlekitTable("site_tags",$headers,0,$table_options);
+  $table->start();
+  if ($tags) foreach ($tags as $tag) {
+      $table->row_start();
+      $table->cell(l_tag_obj($tag));
+      $table->cell($tag['value']);
+      // the remove checkbox
+      if (plc_is_admin()) $table->cell ($form->checkbox_html('site_tag_ids[]',$tag['site_tag_id']));
+      $table->row_end();
+    }
+
+  if ($is_site_pi || $is_site_admin) {
+    $table->tfoot_start();
+
+    // remove tag
+    $table->row_start();
+    $table->cell($form->submit_html("delete-site-tags","Remove Tags"),
+                // use the whole columns and right adjust
+                array('hfill'=>true,'align'=>'right'));
+    $table->row_end();
+
+    // set tag area
+    $table->row_start();
+    // get list of tag names in the site/* category
+    $all_tags= $api->GetTagTypes( array ("category"=>"site*","-SORT"=>"tagname"), array("tagname","tag_type_id"));
+    // xxx cannot use onchange=submit() - would need to somehow pass action name
+    function tag_selector ($tag) { return array("display"=>$tag['tagname'],"value"=>$tag['tag_type_id']); }
+    $selector=array_map("tag_selector",$all_tags);
+    $table->cell($form->select_html("tag_type_id",$selector,array('label'=>"Choose")));
+    $table->cell($form->text_html("value","",array('width'=>8)));
+    $table->cell($form->submit_html("set-tag-on-site","Set Tag"),array('columns'=>2,'align'=>'left'));
+    $table->row_end();
+  }
+
   $table->end();
   $toggle->end();
+  $form->end();
 
   //////////////////// Addresses
   $toggle=new PlekitToggle ('addresses',"Addresses",
-                           array('visible'=>get_arg('show_addresses',false)));
+                           array('visible'=>get_arg('show_addresses')));
   $toggle->start();
   if ( ! $addresses) {
     print "<p class='addresses'>No known address for this site</p>";