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