cleanup for plc_sorts.php removal.
[plewww.git] / planetlab / persons / register.php
1 <?php
2 // $Id$
3 //
4 // Account registration and verification form. This form can be called
5 // in one of two ways:
6 //
7 // 1. ?first_name=FIRST_NAME&last_name=LAST_NAME&email=EMAIL...
8 //
9 // Called by the form at the bottom of the page to register a new
10 // account. If any required fields are missing, AddPerson() will fault
11 // and the specified fields will be highlighted. Otherwise, the
12 // account is registered (but not enabled), and VerifyPerson() sends
13 // the user a link back to this page.
14 //
15 // 2. ?id=PERSON_ID&key=VERIFICATION_KEY...
16 //
17 // Sent to the specified user by VerifyPerson(). If the user receives
18 // the message, then the registered e-mail address is considered
19 // valid, and registration can continue. VerifyPerson() is called
20 // again, and sends the current PI(s) (and support if the user is
21 // requesting a PI role) a link to the user's Account Details page to
22 // enable the account.
23 //
24 // Mark Huang <mlhuang@cs.princeton.edu>
25 // Copyright (C) 2007 The Trustees of Princeton University
26 //
27 // $Id$ $
28 //
29
30 // Get session and API handles
31 require_once 'plc_session.php';
32 global $plc, $api, $adm;
33
34 // Print header
35 require_once 'plc_drupal.php';
36 drupal_set_title('Account Registration');
37 include 'plc_header.php';
38
39 // Drupalish, but does not use Drupal itself to generate the form
40 $form = array();
41 $form['first_name'] = array('title' => 'First name', 'required' => TRUE,
42                             'maxlength' => 60, 'size' => 15);
43 $form['last_name'] = array('title' => 'Last name', 'required' => TRUE,
44                             'maxlength' => 60, 'size' => 15);
45 $form['title'] = array('title' => 'Title', 'required' => FALSE,
46                        'maxlength' => 60, 'size' => 5);
47 $form['phone'] = array('title' => 'Telephone', 'required' => FALSE,
48                        'maxlength' => 60, 'size' => 20);
49 $form['email'] = array('title' => 'E-mail', 'required' => TRUE,
50                        'maxlength' => 60, 'size' => 30);
51 $form['password'] = array('title' => 'Password', 'required' => TRUE,
52                        'maxlength' => 60, 'size' => 20);
53 $form['site_ids'] = array('title' => 'Site', 'required' => TRUE);
54 $form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
55
56 //////////////////// additional messages
57 $form['email']['comment'] = <<< EOF
58 Your <b>E-mail</b> address must be able to receive e-mail and will be
59 used as your $PLC_NAME username
60 EOF;
61
62 $form['site_ids']['comment'] = <<< EOF
63 Select the site where you belong 
64 EOF;
65
66 $form['roles']['comment'] = <<< EOF
67 Do not select the <b>Principal Investigator</b> or <b>Technical
68 Contact</b> roles unless you have spoken with the current PI of your
69 site, and you intend to assume either or both of these roles.
70 <br> Use Command-Clic to unselect or for multiple selection
71 EOF;
72
73 ////////////////////
74 global $person;
75 $person = array();
76 foreach ($form as $name => $item) {
77   if (!empty($_REQUEST[$name])) {
78     $person[$name] = $_REQUEST[$name];
79   }
80 }
81
82 // Filter out "Select a site"
83 if (!empty($person['site_ids'])) {
84   $person['site_ids'] = array_filter($person['site_ids'],
85                                      create_function('$site_id', 'return intval($site_id) > 0;'));
86 }
87
88 if (!empty($person)) {
89   // Look for missing/blank entries
90   $missing = array();
91   foreach ($form as $name => $item) {
92     if ($item['required'] && empty($person[$name])) {
93       $missing[] = $item['title'];
94     }
95   }
96   if (!empty($missing)) {
97     $error = "<ul>";
98     foreach ($missing as $field) {
99       $error .= "<li>$field field is required.</li>";
100     }
101     $error .= "</ul>";
102   }
103
104   if (empty($error)) {
105     // N.B.: site_ids and roles are ignored by AddPerson()
106     $person_id = $adm->AddPerson($person);
107     $error = $adm->error();
108   }
109
110   if (empty($error)) {
111     $adm->begin();
112
113     // Add person to requested sites
114     foreach ($person['site_ids'] as $site_id) {
115       $adm->AddPersonToSite($person_id, intval($site_id));
116       $adm->SetPersonPrimarySite($person_id, intval($site_id));
117     }
118
119     // Add requested roles. Always add the user role. 
120     $adm->AddRoleToPerson('user', $person_id);
121     if (!empty($person['roles'])) {
122       foreach ($person['roles'] as $role) {
123         $adm->AddRoleToPerson($role, $person_id);
124       }
125     }
126
127     // Send an e-mail containing a link back to this page, which will
128     // verify the given e-mail address as valid. PIs can still create
129     // and enable accounts on behalf of their users, they just have to
130     // find and enable the accounts manually after registering them.
131     $adm->VerifyPerson($person_id);
132
133     // Disable submit button
134     $person['person_id'] = $person_id;
135
136     $adm->commit();
137     $error = $adm->error();
138   }
139
140   if (!empty($error)) {
141     print '<div class="messages error">' . $error . '</div>';
142   } else {
143     print '<div class="messages status">Your registration request has been received. An e-mail has been sent to ';
144     print $person['email'];
145     print ' with further instructions.</div>';
146   }
147 }
148
149 $PLC_NAME = htmlspecialchars(PLC_NAME);
150
151 // E-mail address verified, go ahead and notify the PI (and possibly
152 // support if a PI role was requested) that a registration request was
153 // received.
154 if (!empty($_REQUEST['id']) && !empty($_REQUEST['key'])) {
155   $person_id = intval($_REQUEST['id']);
156   if ($adm->VerifyPerson($person_id, $_REQUEST['key']) != 1) {
157     print '<div class="messages error">' . $adm->error() . '.</div>';
158   } else {
159     $persons = $adm->GetPersons(array($person_id));
160     $person = $persons[0];
161
162     // Remove the password field from the form so that it is not
163     // highlighted as missing.
164     unset($form['password']);
165
166     print '<div class="messages status">';
167     print 'Your e-mail address has been verified. ';
168     print 'The PI(s) at your site have been notified of your account registration ';
169
170     if (in_array('pi', $person['roles'])) {
171       $support = PLC_MAIL_SUPPORT_ADDRESS;
172       print " and should contact <a href=\"mailto:$support\">$PLC_NAME Support <$support></a>. ";
173       print " $PLC_NAME Support will enable your account if authorized by your PI(s).";
174     } else {
175       print ' and are responsible for enabling your account.';
176     }
177
178     print '</div>';
179   }
180 }
181
182 $self = $_SERVER['PHP_SELF'];
183 if (!empty($_SERVER['QUERY_STRING'])) {
184   $self .= "?" . $_SERVER['QUERY_STRING'];
185 }
186
187 $adm->begin();
188
189 // All defined sites
190 // cannot register with foreign site
191 $adm->GetSites(array('is_public' => TRUE, 'peer_id' => NULL), array('site_id', 'name','enabled','peer_id'));
192 // All defined roles
193 $adm->GetRoles();
194
195 list($sites, $roles) = $adm->commit();
196
197 // Alphabetize sites by name
198 sort_sites($sites);
199
200 // Prepend invalid site to beginning of list to force user to select a site
201 $sites = array_merge(array(array('site_id' => -1, 'name' => 'Select a site', 'enabled' => TRUE)), $sites);
202
203 // Drop down selection box for each site
204 function site_option($site) {
205   global $person;
206
207   $site_id = $site['site_id'];
208
209   if (!empty($person['site_ids']) && in_array($site_id, $person['site_ids'])) {
210     $selected = 'selected="selected"';
211   } else {
212     $selected = "";
213   }
214
215   $option = "<option value=\"$site_id\" $selected";
216   if ( ! $site['enabled'] )
217     $option .= " disabled='disabled'";
218   $option .= ">";
219   $option .= htmlspecialchars($site['name']);
220   # Safari/IE do not implement disabled correctly
221   if ( ! $site['enabled'] )
222     $option .= " (pending registration)";
223   $option .= "</option>";
224   return $option;
225 }
226 $site_options = implode("\n", array_map('site_option', $sites));
227
228 // Do not tempt users to register for the admin role. Administrators
229 // should register normally, then be granted the admin role by another
230 // admin. Also, all accounts should have the user role (see above).
231 foreach ($roles as $i => $role) {
232   if ($role['name'] == 'admin' || $role['name'] == 'user') {
233     unset($roles[$i]);
234   }
235 }
236
237 // Standard roles
238 global $standard_roles;
239 $standard_roles = array('user' => 'User',
240                         'pi' => 'Principal Investigator',
241                         'tech' => 'Technical Contact',
242                         'admin' => 'Administrator');
243
244 // Drop down selection box for each role
245 function role_option($role) {
246   global $standard_roles, $selected_roles;
247
248   $name = $role['name'];
249
250   if (!empty($person['roles']) && in_array($name, $person['roles'])) {
251     $selected = 'selected="selected"';
252   } else {
253     $selected = "";
254   }
255
256   $display = array_key_exists($name, $standard_roles) ? $standard_roles[$name] : $name;
257
258   $option = "<option value=\"$name\" $selected>";
259   $option .= htmlspecialchars($display);
260   $option .= "</option>";
261   return $option;
262 }
263 $role_options = implode("\n", array_map('role_option', $roles));
264
265 $self = $_SERVER['PHP_SELF'];
266 if (!empty($_SERVER['QUERY_STRING'])) {
267   $self .= "?" . $_SERVER['QUERY_STRING'];
268 }
269
270 print <<<EOF
271 <div class="content">
272
273 <form action="$self" method="post">
274
275 <table border="0" cellpadding="5" cellspacing="0">
276 EOF;
277
278 foreach ($form as $name => $item) {
279
280   if ( ! empty($item['comment'])) {
281     $comment=$item['comment'];
282     print "<tr><td colspan='2'> &nbsp; </td></tr>";
283     print "<tr><td colspan='2'> $comment: </td></tr>";
284   }
285
286   $title = $item['title'];
287   $required = $item['required'] ? '<span class="form-required" title="This field is required.">*</span>' : "";
288   $class = $item['required'] ? "required" : "";
289   if ($item['required'] && !empty($person) && empty($person[$name])) {
290     $class .= " error";
291   }
292
293   // Label part
294   print "<tr>";
295   print <<<EOF
296     <td><label class="$class" for="edit-$name">$title: $required</label></td>\n
297 EOF;
298
299   // input part
300   switch ($name) {
301
302   case 'site_ids':
303     print <<<EOF
304         <td><select name="site_ids[]" id="edit-site_ids" class="form-select $class">
305           $site_options
306         </select></td>\n
307 EOF;
308     break;
309
310   case 'roles':
311     print <<<EOF
312         <td><select name="roles[]" multiple="multiple" id="edit-roles" class="form-select $class">
313           $role_options
314         </select></td>\n
315 EOF;
316     break;
317
318   default:
319     $maxlength = $item['maxlength'];
320     $size = $item['size'];
321     $value = !empty($person[$name]) ? $person[$name] : "";
322     $type = $name == 'password' ? "password" : "text";
323     print <<<EOF
324         <td><input type="$type" maxlength="$maxlength" name="$name" id="edit-$name" size="$size" value="$value" class="form-text $class"></td>\n
325 EOF;
326
327   }
328
329   print "</tr>\n";
330 }
331
332 // Do not allow resubmits
333 if (empty($person['person_id'])) {
334   print '<tr><td colspan='2'><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
335 }
336
337 print <<<EOF
338 </table>
339
340 </form>
341 </div>
342 EOF;
343
344 include 'plc_footer.php';
345
346 ?>