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