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