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