no longer letting folks register as PI/Tech, as its basically NEVER used correctly
[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 if (0)
56 $form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
57
58 //////////////////// additional messages
59 $form['email']['comment'] = <<< EOF
60 Your <b>E-mail</b> address must be able to receive e-mail and will be
61 used as your $PLC_NAME username
62 EOF;
63
64 $form['site_ids']['comment'] = <<< EOF
65 Select the site where you belong 
66 EOF;
67
68 if (0)
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,'-SORT'=>'name'), 
195                array('site_id', 'name','enabled','peer_id'));
196 // All defined roles
197 $adm->GetRoles();
198
199 list($sites, $roles) = $adm->commit();
200
201 // Prepend invalid site to beginning of list to force user to select a site
202 $sites = array_merge(array(array('site_id' => -1, 'name' => 'Select a site', 'enabled' => TRUE)), $sites);
203
204 // Drop down selection box for each site
205 function site_option($site) {
206   global $person;
207
208   $site_id = $site['site_id'];
209
210   if (!empty($person['site_ids']) && in_array($site_id, $person['site_ids'])) {
211     $selected = 'selected="selected"';
212   } else {
213     $selected = "";
214   }
215
216   $option = "<option value=\"$site_id\" $selected";
217   if ( ! $site['enabled'] )
218     $option .= " disabled='disabled'";
219   $option .= ">";
220   $option .= htmlspecialchars($site['name']);
221   # Safari/IE do not implement disabled correctly
222   if ( ! $site['enabled'] )
223     $option .= " (pending registration)";
224   $option .= "</option>";
225   return $option;
226 }
227 $site_options = implode("\n", array_map('site_option', $sites));
228
229 // Do not tempt users to register for the admin role. Administrators
230 // should register normally, then be granted the admin role by another
231 // admin. Also, all accounts should have the user role (see above).
232 foreach ($roles as $i => $role) {
233   if ($role['name'] == 'admin' || $role['name'] == 'user') {
234     unset($roles[$i]);
235   }
236 }
237
238 // Standard roles
239 global $standard_roles;
240 $standard_roles = array('user' => 'User',
241                         'pi' => 'Principal Investigator',
242                         'tech' => 'Technical Contact',
243                         'admin' => 'Administrator');
244
245 // Drop down selection box for each role
246 function role_option($role) {
247   global $standard_roles, $selected_roles;
248
249   $name = $role['name'];
250
251   if (!empty($person['roles']) && in_array($name, $person['roles'])) {
252     $selected = 'selected="selected"';
253   } else {
254     $selected = "";
255   }
256
257   $display = array_key_exists($name, $standard_roles) ? $standard_roles[$name] : $name;
258
259   $option = "<option value=\"$name\" $selected>";
260   $option .= htmlspecialchars($display);
261   $option .= "</option>";
262   return $option;
263 }
264 $role_options = implode("\n", array_map('role_option', $roles));
265
266 $self = $_SERVER['PHP_SELF'];
267 if (!empty($_SERVER['QUERY_STRING'])) {
268   $self .= "?" . $_SERVER['QUERY_STRING'];
269 }
270
271 print <<<EOF
272 <div class="content">
273
274 <form action="$self" method="post">
275
276 <table border="0" cellpadding="5" cellspacing="0">
277 EOF;
278
279 foreach ($form as $name => $item) {
280
281   if ( ! empty($item['comment'])) {
282     $comment=$item['comment'];
283     print "<tr><td colspan='2'> &nbsp; </td></tr>";
284     print "<tr><td colspan='2'> $comment: </td></tr>";
285   }
286
287   $title = $item['title'];
288   $required = $item['required'] ? '<span class="form-required" title="This field is required.">*</span>' : "";
289   $class = $item['required'] ? "required" : "";
290   if ($item['required'] && !empty($person) && empty($person[$name])) {
291     $class .= " error";
292   }
293
294   // Label part
295   print "<tr>";
296   print <<<EOF
297     <td><label class="$class" for="edit-$name">$title: $required</label></td>\n
298 EOF;
299
300   // input part
301   switch ($name) {
302
303   case 'site_ids':
304     print <<<EOF
305         <td><select name="site_ids[]" id="edit-site_ids" class="form-select $class">
306           $site_options
307         </select></td>\n
308 EOF;
309     break;
310
311   case 'roles':
312     if (0) { /* Not letting users select PI or Tech any more.  Its only lead to confusion and abuse. */
313     print <<<EOF
314         <td><select name="roles[]" multiple="multiple" id="edit-roles" class="form-select $class">
315           $role_options
316         </select></td>\n
317 EOF;
318     }
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 ?>