search for 'person-registration.txt' in e.g. /etc/planetlab/php
[plewww.git] / planetlab / persons / register.php
index 53b1dfc..db2b44f 100644 (file)
@@ -1,4 +1,3 @@
-
 <?php
 // $Id$
 //
@@ -34,47 +33,67 @@ global $plc, $api, $adm;
 
 // Print header
 require_once 'plc_drupal.php';
-drupal_set_title('Account Registration');
 include 'plc_header.php';
 
+require_once 'plc_functions.php';
+require_once 'nifty.php';
+require_once 'details.php';
+
+drupal_set_title('Account Registration');
+
 // Drupalish, but does not use Drupal itself to generate the form
-$form = array();
-$form['first_name'] = array('title' => 'First name', 'required' => TRUE,
+global $person_form;
+
+$person_form = array();
+$person_form['first_name'] = array('title' => 'First name', 'required' => TRUE,
                            'maxlength' => 60, 'size' => 15);
-$form['last_name'] = array('title' => 'Last name', 'required' => TRUE,
+$person_form['last_name'] = array('title' => 'Last name', 'required' => TRUE,
                            'maxlength' => 60, 'size' => 15);
-$form['title'] = array('title' => 'Title', 'required' => FALSE,
+$person_form['title'] = array('title' => 'Title', 'required' => FALSE,
                       'maxlength' => 60, 'size' => 5);
-$form['phone'] = array('title' => 'Telephone', 'required' => FALSE,
+$person_form['phone'] = array('title' => 'Telephone', 'required' => FALSE,
                       'maxlength' => 60, 'size' => 20);
-$form['email'] = array('title' => 'E-mail', 'required' => TRUE,
+$person_form['email'] = array('title' => 'E-mail', 'required' => TRUE,
                       'maxlength' => 60, 'size' => 30);
-$form['password'] = array('title' => 'Password', 'required' => TRUE,
+$person_form['password'] = array('title' => 'Password', 'required' => TRUE,
                       'maxlength' => 60, 'size' => 20);
-$form['site_ids'] = array('title' => 'Site', 'required' => TRUE);
-$form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
+$person_form['site_ids'] = array('title' => 'Site', 'required' => TRUE);
+if (0)
+  $person_form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
 
 //////////////////// additional messages
-$form['email']['comment'] = <<< EOF
+$person_form['email']['comment'] = <<< EOF
 Your <b>E-mail</b> address must be able to receive e-mail and will be
 used as your $PLC_NAME username
 EOF;
 
-$form['site_ids']['comment'] = <<< EOF
-Select the site where you belong 
-EOF;
 
-$form['roles']['comment'] = <<< EOF
+// dirty hack feb 2018; if this file can be found,
+// its contents is used instead of the hard-wired message
+// it is searched along php's include path, so it should be
+// allright to save it as /etc/planetlab/php/person-registration.txt
+// of course html tags like <code> and <br /> are OK
+global $message_filename;
+$message_filename = "person-registration.txt";
+
+try {
+    $person_form['site_ids']['comment'] = file_get_contents($message_filename, TRUE);
+} catch (Exception $e) {
+     $person_form['site_ids']['comment'] = "Select the site where you belong";
+}
+
+if (0)
+  $person_form['roles']['comment'] = <<< EOF
 Do not select the <b>Principal Investigator</b> or <b>Technical
 Contact</b> roles unless you have spoken with the current PI of your
 site, and you intend to assume either or both of these roles.
 <br> Use Command-Clic to unselect or for multiple selection
 EOF;
 
-////////////////////
+//////////////////// parse form values and store in $person
 global $person;
 $person = array();
-foreach ($form as $name => $item) {
+foreach ($person_form as $name => $item) {
   if (!empty($_REQUEST[$name])) {
     $person[$name] = $_REQUEST[$name];
   }
@@ -86,41 +105,69 @@ if (!empty($person['site_ids'])) {
                                     create_function('$site_id', 'return intval($site_id) > 0;'));
 }
 
-if (!empty($person)) {
+//////////////////// minimal checking
+function check_form ($person) {
+  global $person_form;
+  global $adm;
+
   // Look for missing/blank entries
   $missing = array();
-  foreach ($form as $name => $item) {
+  foreach ($person_form as $name => $item) {
     if ($item['required'] && empty($person[$name])) {
       $missing[] = $item['title'];
     }
   }
+
+  // missing fields
   if (!empty($missing)) {
-    $error = "<ul>";
-    foreach ($missing as $field) {
-      $error .= "<li>$field field is required.</li>";
-    }
-    $error .= "</ul>";
+    $warnings=array();
+    foreach ($missing as $field) $warnings []= "$field field is required.";
+    print html_div(plc_itemize($warnings),"messages error");
+    return FALSE;
   }
 
-  if (empty($error)) {
-    // N.B.: site_ids and roles are ignored by AddPerson()
-    $person_id = $adm->AddPerson($person);
-    $error = $adm->error();
+  // check that the email address is not already used on this peer
+  $email=$person['email'];
+  $already = $adm->GetPersons ( array('email'=>$person['email'],'peer_id'=>NULL) );
+  if ( ! empty ($already) ) {
+    print html_div(plc_itemize(array("Email $email already used !")),"messages error");
+    return FALSE;
   }
 
-  if (empty($error)) {
+  return TRUE;
+}
+
+//////////////////// perform api calls
+function register_person ($person) {
+
+  global $adm;
+
+  $errors = errors_init ();
+
+  // 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
+  $site_ids=$person['site_ids'];
+  unset ($person['site_ids']);
+  $roles=$person['roles'];
+  unset ($person['roles']);
+
+  $person_id = $adm->AddPerson($person);
+  $errors = errors_record ($adm,$errors);
+
+  if (empty($errors)) {
     $adm->begin();
 
     // Add person to requested sites
-    foreach ($person['site_ids'] as $site_id) {
+    foreach ($site_ids as $site_id) {
       $adm->AddPersonToSite($person_id, intval($site_id));
       $adm->SetPersonPrimarySite($person_id, intval($site_id));
     }
 
-    // Add requested roles. Always add the user role. 
+    // Add requested roles. Always add the user role.
     $adm->AddRoleToPerson('user', $person_id);
-    if (!empty($person['roles'])) {
-      foreach ($person['roles'] as $role) {
+    if (!empty($roles)) {
+      foreach ($roles as $role) {
        $adm->AddRoleToPerson($role, $person_id);
       }
     }
@@ -135,18 +182,22 @@ if (!empty($person)) {
     $person['person_id'] = $person_id;
 
     $adm->commit();
-    $error = $adm->error();
+    $errors = errors_record ($adm,$errors);
   }
 
-  if (!empty($error)) {
-    print '<div class="messages error">' . $error . '</div>';
+  if (!empty($errors)) {
+    errors_display($errors);
   } else {
-    print '<div class="messages status">Your registration request has been received. An e-mail has been sent to ';
-    print $person['email'];
-    print ' with further instructions.</div>';
+    $email=$person['email'];
+    $text="Your registration request has been received. An e-mail has been sent to $email with further instructions.";
+    print html_div($text,"messages status");
   }
 }
 
+////////////////////
+if (!empty($person) && check_form ($person))
+  register_person($person);
+
 $PLC_NAME = htmlspecialchars(PLC_NAME);
 
 // E-mail address verified, go ahead and notify the PI (and possibly
@@ -155,41 +206,43 @@ $PLC_NAME = htmlspecialchars(PLC_NAME);
 if (!empty($_REQUEST['id']) && !empty($_REQUEST['key'])) {
   $person_id = intval($_REQUEST['id']);
   if ($adm->VerifyPerson($person_id, $_REQUEST['key']) != 1) {
-    print '<div class="messages error">' . $adm->error() . '.</div>';
+    plc_error($adm->error());
   } else {
     $persons = $adm->GetPersons(array($person_id));
     $person = $persons[0];
 
     // Remove the password field from the form so that it is not
     // highlighted as missing.
-    unset($form['password']);
+    unset($person_form['password']);
 
-    print '<div class="messages status">';
-    print 'Your e-mail address has been verified. ';
-    print 'The PI(s) at your site have been notified of your account registration ';
+    $messages = array();
+    $messages []= 'Your e-mail address has been verified. ';
+    $messages []= 'The PI(s) at your site have been notified of your account registration ';
 
-    if (in_array('pi', $person['roles'])) {
-      $support = PLC_MAIL_SUPPORT_ADDRESS;
-      print " and should contact <a href=\"mailto:$support\">$PLC_NAME Support <$support></a>. ";
-      print " $PLC_NAME Support will enable your account if authorized by your PI(s).";
+    if (! in_array('pi', $person['roles'])) {
+      $messages []= 'They are responsible for enabling your account.';
     } else {
-      print ' and are responsible for enabling your account.';
+      $support = PLC_MAIL_SUPPORT_ADDRESS;
+      $messages []= "They should contact <a href=\"mailto:$support\">$PLC_NAME Support <$support></a>. ";
+      $messages []= "$PLC_NAME Support will enable your account once authorized by your PI(s).";
     }
 
-    print '</div>';
+    plc_itemize($messages,"messages status");
   }
 }
 
-$self = $_SERVER['PHP_SELF'];
-if (!empty($_SERVER['QUERY_STRING'])) {
-  $self .= "?" . $_SERVER['QUERY_STRING'];
-}
-
 $adm->begin();
 
 // All defined sites
 // cannot register with foreign site
-$adm->GetSites(array('is_public' => TRUE, 'peer_id' => NULL), array('site_id', 'name','enabled','peer_id'));
+// we also hide sites that are created by sfa
+// as well as the ones that have a disabled_registration tag set
+$adm->GetSites(array('is_public' => TRUE, 'peer_id' => NULL,
+                     'sfa_created' => NULL,
+                     'disabled_registration' => NULL,
+                     '-SORT'=>'name'),
+                  array('site_id', 'name', 'enabled', 'peer_id',
+                     'sfa_created', 'disabled_registration'));
 // All defined roles
 $adm->GetRoles();
 
@@ -210,14 +263,14 @@ function site_option($site) {
     $selected = "";
   }
 
-  $option = "<option value=\"$site_id\" $selected";
+  $option = "<option value='$site_id' $selected";
   if ( ! $site['enabled'] )
     $option .= " disabled='disabled'";
   $option .= ">";
   $option .= htmlspecialchars($site['name']);
   # Safari/IE do not implement disabled correctly
   if ( ! $site['enabled'] )
-    $option .= " (pending registration)";
+    $option .= " (disabled, or pending registration)";
   $option .= "</option>";
   return $option;
 }
@@ -260,25 +313,25 @@ function role_option($role) {
 }
 $role_options = implode("\n", array_map('role_option', $roles));
 
-$self = $_SERVER['PHP_SELF'];
-if (!empty($_SERVER['QUERY_STRING'])) {
-  $self .= "?" . $_SERVER['QUERY_STRING'];
-}
-
-print <<<EOF
-<div class="content">
 
-<form action="$self" method="post">
+$nifty=new PlekitNifty ('register','person-register','medium');
+$nifty->start();
+$details = new PlekitDetails(TRUE);
+$details -> start();
+$details->form_start(l_person_register(),array());
 
-<table border="0" cellpadding="5" cellspacing="0">
-EOF;
+// Do not allow resubmits
+$register_button="<input type='submit' name='op' value='Register'  class='form-submit' />";
+if (empty($person['person_id'])) {
+  $details->tr($register_button,'center');
+  $details->space();
+}
 
-foreach ($form as $name => $item) {
+foreach ($person_form as $name => $item) {
 
   if ( ! empty($item['comment'])) {
-    $comment=$item['comment'];
-    print "<tr><td colspan='2'> &nbsp; </td></tr>";
-    print "<tr><td colspan='2'> $comment: </td></tr>";
+    $details->space();
+    $details->tr($item['comment'] . ":");
   }
 
   $title = $item['title'];
@@ -289,28 +342,20 @@ foreach ($form as $name => $item) {
   }
 
   // Label part
-  print "<tr>";
-  print <<<EOF
-    <td><label class="$class" for="edit-$name">$title: $required</label></td>\n
-EOF;
+  $left_part = "<label class='$class' for='edit-$name'>$title: $required</label>";
 
   // input part
   switch ($name) {
 
   case 'site_ids':
-    print <<<EOF
-        <td><select name="site_ids[]" id="edit-site_ids" class="form-select $class">
-         $site_options
-        </select></td>\n
-EOF;
+    $right_part= "<select name='site_ids[]' id='edit-site_ids' class='form-select $class'> $site_options </select>";
     break;
 
   case 'roles':
-    print <<<EOF
-        <td><select name="roles[]" multiple="multiple" id="edit-roles" class="form-select $class">
-          $role_options
-        </select></td>\n
-EOF;
+    $right_part="";
+    if (0) { /* Not letting users select PI or Tech any more.  Its only lead to confusion and abuse. */
+      $right_part = "<select name='roles[]' multiple='multiple' id='edit-roles' class='form-select $class'> $role_options </select>";
+    }
     break;
 
   default:
@@ -318,26 +363,22 @@ EOF;
     $size = $item['size'];
     $value = !empty($person[$name]) ? $person[$name] : "";
     $type = $name == 'password' ? "password" : "text";
-    print <<<EOF
-       <td><input type="$type" maxlength="$maxlength" name="$name" id="edit-$name" size="$size" value="$value" class="form-text $class"></td>\n
-EOF;
-
+    $right_part = "<input type='$type' maxlength='$maxlength' name='$name' id='edit-$name' size='$size' value='$value' class='form-text $class'>";
+    break;
   }
 
-  print "</tr>\n";
+  $details->th_td($left_part,$right_part);
 }
 
 // Do not allow resubmits
 if (empty($person['person_id'])) {
-  print '<tr><td colspan="2"><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
+  $details->space();
+  $details->tr($register_button,'center');
 }
 
-print <<<EOF
-</table>
-
-</form>
-</div>
-EOF;
+$details->form_end();
+$details->end();
+$nifty->end();
 
 include 'plc_footer.php';