make the site registration page more picky, and refuse numeric-only inputs
[plewww.git] / planetlab / sites / register.php
index 5e0f25a..f9ed091 100644 (file)
@@ -1,25 +1,23 @@
 <?php
-// $Id$
 //
-// Site registration and verification form. 
+// Site registration and verification form.
 //
-// Thierry Parmentelat -- INRIA 
-// based on persons/register.php 
+// Thierry Parmentelat -- INRIA
+// based on persons/register.php
 //
 
 // Get session and API handles
 require_once 'plc_session.php';
 global $plc, $api, $adm;
 
-// Get sorting functions
-require_once 'plc_sorts.php';
-
 // Print header
 require_once 'plc_drupal.php';
 drupal_set_title('New Site Registration');
 include 'plc_header.php';
 
-include 'site_form.php';
+require_once 'site_form.php';
+require_once 'details.php';
+require_once 'nifty.php';
 
 $verbose = FALSE;
 
@@ -35,9 +33,15 @@ $pi=$input['pi'];
 $tech=$input['tech'];
 $address=$input['address'];
 
+# allow address to be left out
+function non_empty_address ($address) {
+  return !empty($address['line1']);
+}
+
 if (! $empty_form ) {
   // Look for missing/blank entries
   $error = form_check_required ($site_form, $input);
+  $error .= form_check_non_numeric($site_form, $input);
   $messages= array();
   $verboses= array();
 
@@ -56,29 +60,31 @@ if (! $empty_form ) {
     // begin
     // AddSite(..)
     // AddSiteAddress('login_base',...)
-    // because if the first call fails because login_base is already taken, 
+    // because if the first call fails because login_base is already taken,
     // then you're going to add the new address to the wrong site
-    // 
+    //
     // the bottom line is, there's no advantage in using begin/commit at all
-    
+
     // creating the site
     $site['enabled']=FALSE;
+    global $PENDING_CONSORTIUM_ID;
+    $site['ext_consortium_id']=$PENDING_CONSORTIUM_ID;
     $site_id=$adm->AddSite($site);
     $api_error .= $adm->error();
     if (empty($api_error)) {
-      $verboses [] = "Site created as disabled";
+      $verboses [] = "Site created as disabled, with ext_consortium_id=".$PENDING_CONSORTIUM_ID;
     } else {
       $error .= $api_error;
     }
   }
-  
+
   if (empty($error)) {
 
     // Thierry on august 22 2007
     // use NotifySupport to send this message, and get rid of the fake account
 
     $subject = "Site registration form : new site " . $site['name'];
-    // should use GetMessages and stuff, but I do not trust the way 
+    // should use GetMessages and stuff, but I do not trust the way
     // templates are created in db-config, for upgrades notably
     $template = <<< EOF
 We received a site registration form for site name %s
@@ -87,26 +93,29 @@ To review, please visit https://%s:%d/db/sites/join_request.php?review=t&site_id
 EOF;
     $body=sprintf($template,$site['name'],PLC_WWW_HOST,PLC_WWW_SSL_PORT,$site_id);
     $adm->NotifySupport($subject,$body);
-      
+
     $messages [] = "Your registration request has been received.";
     $messages [] = "A mail was sent to the operations team, your application should be processed shortly.";
     $messages [] = "Upon approval, the PI will receive an information e-mail";
     $messages [] = "Please send a message to " . PLC_MAIL_SUPPORT_ADDRESS . " if this request is not instructed within a few days.";
 
   // creating address
-    $adm->AddSiteAddress($site_id,$address);
-    $api_error = $adm->error();
-    if (empty($api_error)) {
-      $verboses [] = "Address created";
-    } else {
-      $error .= $api_error;
+    if (non_empty_address($address)) {
+      $adm->AddSiteAddress($site_id,$address);
+      $api_error = $adm->error();
+      if (empty($api_error)) {
+             $verboses [] = "Address created";
+      } else {
+             $error .= $api_error;
+      }
     }
 
     // creating PI
     // Thierry 23 august 2007
     // avoid using a pre-existing federated account
     $known_pi = $adm->GetPersons(array("email"=>$pi['email'],
-                                      "peer_id"=>NULL),array("person_id"));
+                                                              "peer_id"=>NULL),
+                                 array("person_id"));
     if ($known_pi) {
       $messages [] = " Note: PI was already known";
       $pi_id=$known_pi[0]['person_id'];
@@ -115,9 +124,9 @@ EOF;
       $pi_id=$adm->AddPerson($pi);
       $api_error = $adm->error();
       if (empty($api_error)) {
-       $verboses [] = "PI created as disabled</p>";
+             $verboses [] = "PI created as disabled</p>";
       } else {
-       $error .= $api_error;
+             $error .= $api_error;
       }
     }
     if ($adm->AddPersonToSite($pi_id,$site_id)) {
@@ -126,41 +135,47 @@ EOF;
     if ($adm->AddRoleToPerson('pi',$pi_id)) {
       $verboses [] = $pi['email'] . " granted PI role</p>";
     }
-    
+
     if ($pi['email'] == $tech['email']) {
       // need to assign tech role so the registration request gets filled properly
       if ($adm->AddRoleToPerson('tech',$pi_id)) {
-       $verboses [] = $pi['email'] . " granted Tech role</p>";
+             $verboses [] = $pi['email'] . " granted Tech role</p>";
       }
     } else {
       // creating TECH
       $known_tech = $adm->GetPersons(array("email"=>$tech['email'],
-                                          "peer_id"=>NULL),array("person_id"));
+                                                                  "peer_id"=>NULL),
+                                     array("person_id"));
+      // jan-2013 with improvements in plcapi-5.1-6 about managing persons and tags,
+      // AddPerson has gone more picky and we need to remove some fields
+      // that no longer are silently ignored by AddPerson
+      $user_role_required=$tech['user-role'];
+      unset($tech['user-role']);
       if ($known_tech) {
-       $messages [] = " Note: Tech was already known";
-       $tech_id=$known_tech[0]['person_id'];
+             $messages [] = " Note: Tech was already known";
+             $tech_id=$known_tech[0]['person_id'];
       } else {
-       $tech['enabled']=FALSE;
-       $tech_id=$adm->AddPerson($tech);
-       $api_error = $adm->error();
-       if (empty($api_error)) {
-         $verboses [] = "Tech created as disabled</p>";
-       } else {
-         $error .= $api_error;
-       }
+             $tech['enabled']=FALSE;
+        $tech_id=$adm->AddPerson($tech);
+        $api_error = $adm->error();
+        if (empty($api_error)) {
+          $verboses [] = "Tech created as disabled</p>";
+        } else {
+          $error .= $api_error;
+        }
       }
       if ($adm->AddPersonToSite($tech_id,$site_id)) {
-       $verboses [] = "Tech attached to new site";
+        $verboses [] = "Tech attached to new site";
       }
       if ($adm->AddRoleToPerson('tech',$tech_id)) {
-       $verboses [] = $tech['email'] . " granted Tech role";
+        $verboses [] = $tech['email'] . " granted Tech role";
       }
-      if ( ($tech['user-role']) && $adm->AddRoleToPerson('user',$tech_id) ) {
-       $verboses [] = $tech['email'] . " granted User role";
+      if ( $user_role_required && $adm->AddRoleToPerson('user',$tech_id) ) {
+        $verboses [] = $tech['email'] . " granted User role";
       }
     }
   }
-    
+
 
   // Show messages
   if (!empty($messages)) {
@@ -170,7 +185,7 @@ EOF;
     }
     print "</ul></div>";
   }
-       
+
   if ($verbose && !empty($verboses)) {
     print '<div class="messages status"><ul>';
     print "<p> Verbose messages</p>";
@@ -179,7 +194,7 @@ EOF;
     }
     print "</ul></div>";
   }
-       
+
   if (!empty($error)) {
     print '<div class="messages error">' . $error . '</div>';
   } else {
@@ -198,23 +213,32 @@ print <<<EOF
 
 <form action="$self" method="post">
 
-<table border="0" width="100%" cellspacing="0" cellpadding="3">
 EOF;
 
+$nifty=new PlekitNifty ('register','site-register','medium');
+$nifty->start();
+$details = new PlekitDetails(TRUE);
+$details -> start();
+
+$register_button='<input type="submit" name="op" value="Register"  class="form-submit" />';
+
 // Do not allow resubmits
 if (empty($site['site_id'])) {
-  print '<tr><td colspan=2 align=center><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
+  $details->tr($register_button,'center');
 }
 
-form_render_table2 ($site_form, $input, ! $empty_form);
+form_render_details ($details, $site_form, $input, ! $empty_form);
 
 // Do not allow resubmits
 if (empty($site['site_id'])) {
-  print '<tr><td colspan=2> &nbsp; </td></tr>';
-  print '<tr><td colspan=2 align=center><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
+  // some space
+  $details->space();
+  $details->tr($register_button,'center');
 }
 
-print "</table></form></div>";
+$details -> end();
+$nifty->end();
+print "</form></div>";
 
 include 'plc_footer.php';