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