cleanup sort_sites() calls after 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 // Prepend invalid site to beginning of list to force user to select a site
198 $sites = array_merge(array(array('site_id' => -1, 'name' => 'Select a site', 'enabled' => TRUE)), $sites);
199
200 // Drop down selection box for each site
201 function site_option($site) {
202   global $person;
203
204   $site_id = $site['site_id'];
205
206   if (!empty($person['site_ids']) && in_array($site_id, $person['site_ids'])) {
207     $selected = 'selected="selected"';
208   } else {
209     $selected = "";
210   }
211
212   $option = "<option value=\"$site_id\" $selected";
213   if ( ! $site['enabled'] )
214     $option .= " disabled='disabled'";
215   $option .= ">";
216   $option .= htmlspecialchars($site['name']);
217   # Safari/IE do not implement disabled correctly
218   if ( ! $site['enabled'] )
219     $option .= " (pending registration)";
220   $option .= "</option>";
221   return $option;
222 }
223 $site_options = implode("\n", array_map('site_option', $sites));
224
225 // Do not tempt users to register for the admin role. Administrators
226 // should register normally, then be granted the admin role by another
227 // admin. Also, all accounts should have the user role (see above).
228 foreach ($roles as $i => $role) {
229   if ($role['name'] == 'admin' || $role['name'] == 'user') {
230     unset($roles[$i]);
231   }
232 }
233
234 // Standard roles
235 global $standard_roles;
236 $standard_roles = array('user' => 'User',
237                         'pi' => 'Principal Investigator',
238                         'tech' => 'Technical Contact',
239                         'admin' => 'Administrator');
240
241 // Drop down selection box for each role
242 function role_option($role) {
243   global $standard_roles, $selected_roles;
244
245   $name = $role['name'];
246
247   if (!empty($person['roles']) && in_array($name, $person['roles'])) {
248     $selected = 'selected="selected"';
249   } else {
250     $selected = "";
251   }
252
253   $display = array_key_exists($name, $standard_roles) ? $standard_roles[$name] : $name;
254
255   $option = "<option value=\"$name\" $selected>";
256   $option .= htmlspecialchars($display);
257   $option .= "</option>";
258   return $option;
259 }
260 $role_options = implode("\n", array_map('role_option', $roles));
261
262 $self = $_SERVER['PHP_SELF'];
263 if (!empty($_SERVER['QUERY_STRING'])) {
264   $self .= "?" . $_SERVER['QUERY_STRING'];
265 }
266
267 print <<<EOF
268 <div class="content">
269
270 <form action="$self" method="post">
271
272 <table border="0" cellpadding="5" cellspacing="0">
273 EOF;
274
275 foreach ($form as $name => $item) {
276
277   if ( ! empty($item['comment'])) {
278     $comment=$item['comment'];
279     print "<tr><td colspan='2'> &nbsp; </td></tr>";
280     print "<tr><td colspan='2'> $comment: </td></tr>";
281   }
282
283   $title = $item['title'];
284   $required = $item['required'] ? '<span class="form-required" title="This field is required.">*</span>' : "";
285   $class = $item['required'] ? "required" : "";
286   if ($item['required'] && !empty($person) && empty($person[$name])) {
287     $class .= " error";
288   }
289
290   // Label part
291   print "<tr>";
292   print <<<EOF
293     <td><label class="$class" for="edit-$name">$title: $required</label></td>\n
294 EOF;
295
296   // input part
297   switch ($name) {
298
299   case 'site_ids':
300     print <<<EOF
301         <td><select name="site_ids[]" id="edit-site_ids" class="form-select $class">
302           $site_options
303         </select></td>\n
304 EOF;
305     break;
306
307   case 'roles':
308     print <<<EOF
309         <td><select name="roles[]" multiple="multiple" id="edit-roles" class="form-select $class">
310           $role_options
311         </select></td>\n
312 EOF;
313     break;
314
315   default:
316     $maxlength = $item['maxlength'];
317     $size = $item['size'];
318     $value = !empty($person[$name]) ? $person[$name] : "";
319     $type = $name == 'password' ? "password" : "text";
320     print <<<EOF
321         <td><input type="$type" maxlength="$maxlength" name="$name" id="edit-$name" size="$size" value="$value" class="form-text $class"></td>\n
322 EOF;
323
324   }
325
326   print "</tr>\n";
327 }
328
329 // Do not allow resubmits
330 if (empty($person['person_id'])) {
331   print '<tr><td colspan='2'><input type="submit" name="op" value="Register"  class="form-submit" /></td></tr>';
332 }
333
334 print <<<EOF
335 </table>
336
337 </form>
338 </div>
339 EOF;
340
341 include 'plc_footer.php';
342
343 ?>