4 // Account registration and verification form. This form can be called
7 // 1. ?first_name=FIRST_NAME&last_name=LAST_NAME&email=EMAIL...
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.
15 // 2. ?id=PERSON_ID&key=VERIFICATION_KEY...
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.
24 // Mark Huang <mlhuang@cs.princeton.edu>
25 // Copyright (C) 2007 The Trustees of Princeton University
30 // Get session and API handles
31 require_once 'plc_session.php';
32 global $plc, $api, $adm;
35 require_once 'plc_drupal.php';
36 include 'plc_header.php';
38 require_once 'plc_functions.php';
39 require_once 'nifty.php';
40 require_once 'details.php';
42 drupal_set_title('Account Registration');
44 // Drupalish, but does not use Drupal itself to generate the form
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);
62 $person_form['roles'] = array('title' => 'Additional Roles', 'required' => FALSE);
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
70 $person_form['site_ids']['comment'] = <<< EOF
71 Select the site where you belong
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
82 //////////////////// parse form values and store in $person
85 foreach ($person_form as $name => $item) {
86 if (!empty($_REQUEST[$name])) {
87 $person[$name] = $_REQUEST[$name];
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;'));
97 //////////////////// minimal checking
98 function check_form ($person) {
102 // Look for missing/blank entries
104 foreach ($person_form as $name => $item) {
105 if ($item['required'] && empty($person[$name])) {
106 $missing[] = $item['title'];
111 if (!empty($missing)) {
113 foreach ($missing as $field) $warnings []= "$field field is required.";
114 print html_div(plc_itemize($warnings),"messages error");
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");
129 //////////////////// perform api calls
130 function register_person ($person) {
134 $errors = errors_init ();
136 // N.B.: site_ids and roles are ignored by AddPerson()
137 $person_id = $adm->AddPerson($person);
138 $errors = errors_record ($adm,$errors);
140 if (empty($errors)) {
143 // Add person to requested sites
144 foreach ($person['site_ids'] as $site_id) {
145 $adm->AddPersonToSite($person_id, intval($site_id));
146 $adm->SetPersonPrimarySite($person_id, intval($site_id));
149 // Add requested roles. Always add the user role.
150 $adm->AddRoleToPerson('user', $person_id);
151 if (!empty($person['roles'])) {
152 foreach ($person['roles'] as $role) {
153 $adm->AddRoleToPerson($role, $person_id);
157 // Send an e-mail containing a link back to this page, which will
158 // verify the given e-mail address as valid. PIs can still create
159 // and enable accounts on behalf of their users, they just have to
160 // find and enable the accounts manually after registering them.
161 $adm->VerifyPerson($person_id);
163 // Disable submit button
164 $person['person_id'] = $person_id;
167 $errors = errors_record ($adm,$errors);
170 if (!empty($errors)) {
171 errors_display($errors);
173 $email=$person['email'];
174 $text="Your registration request has been received. An e-mail has been sent to $email with further instructions.";
175 print html_div($text,"messages status");
180 if (!empty($person) && check_form ($person))
181 register_person($person);
183 $PLC_NAME = htmlspecialchars(PLC_NAME);
185 // E-mail address verified, go ahead and notify the PI (and possibly
186 // support if a PI role was requested) that a registration request was
188 if (!empty($_REQUEST['id']) && !empty($_REQUEST['key'])) {
189 $person_id = intval($_REQUEST['id']);
190 if ($adm->VerifyPerson($person_id, $_REQUEST['key']) != 1) {
191 plc_error($adm->error());
193 $persons = $adm->GetPersons(array($person_id));
194 $person = $persons[0];
196 // Remove the password field from the form so that it is not
197 // highlighted as missing.
198 unset($person_form['password']);
201 $messages []= 'Your e-mail address has been verified. ';
202 $messages []= 'The PI(s) at your site have been notified of your account registration ';
204 if (! in_array('pi', $person['roles'])) {
205 $messages []= 'They are responsible for enabling your account.';
207 $support = PLC_MAIL_SUPPORT_ADDRESS;
208 $messages []= "They should contact <a href=\"mailto:$support\">$PLC_NAME Support <$support></a>. ";
209 $messages []= "$PLC_NAME Support will enable your account once authorized by your PI(s).";
212 plc_itemize($messages,"messages status");
219 // cannot register with foreign site
220 $adm->GetSites(array('is_public' => TRUE, 'peer_id' => NULL,'-SORT'=>'name'),
221 array('site_id', 'name','enabled','peer_id'));
225 list($sites, $roles) = $adm->commit();
227 // Prepend invalid site to beginning of list to force user to select a site
228 $sites = array_merge(array(array('site_id' => -1, 'name' => 'Select a site', 'enabled' => TRUE)), $sites);
230 // Drop down selection box for each site
231 function site_option($site) {
234 $site_id = $site['site_id'];
236 if (!empty($person['site_ids']) && in_array($site_id, $person['site_ids'])) {
237 $selected = 'selected="selected"';
242 $option = "<option value='$site_id' $selected";
243 if ( ! $site['enabled'] )
244 $option .= " disabled='disabled'";
246 $option .= htmlspecialchars($site['name']);
247 # Safari/IE do not implement disabled correctly
248 if ( ! $site['enabled'] )
249 $option .= " (pending registration)";
250 $option .= "</option>";
253 $site_options = implode("\n", array_map('site_option', $sites));
255 // Do not tempt users to register for the admin role. Administrators
256 // should register normally, then be granted the admin role by another
257 // admin. Also, all accounts should have the user role (see above).
258 foreach ($roles as $i => $role) {
259 if ($role['name'] == 'admin' || $role['name'] == 'user') {
265 global $standard_roles;
266 $standard_roles = array('user' => 'User',
267 'pi' => 'Principal Investigator',
268 'tech' => 'Technical Contact',
269 'admin' => 'Administrator');
271 // Drop down selection box for each role
272 function role_option($role) {
273 global $standard_roles, $selected_roles;
275 $name = $role['name'];
277 if (!empty($person['roles']) && in_array($name, $person['roles'])) {
278 $selected = 'selected="selected"';
283 $display = array_key_exists($name, $standard_roles) ? $standard_roles[$name] : $name;
285 $option = "<option value=\"$name\" $selected>";
286 $option .= htmlspecialchars($display);
287 $option .= "</option>";
290 $role_options = implode("\n", array_map('role_option', $roles));
293 $nifty=new PlekitNifty ('register','person-register','medium');
295 $details = new PlekitDetails(TRUE);
297 $details->form_start(l_person_register(),array());
299 // Do not allow resubmits
300 $register_button="<input type='submit' name='op' value='Register' class='form-submit' />";
301 if (empty($person['person_id'])) {
302 $details->tr($register_button,'center');
306 foreach ($person_form as $name => $item) {
308 if ( ! empty($item['comment'])) {
310 $details->tr($item['comment'] . ":");
313 $title = $item['title'];
314 $required = $item['required'] ? '<span class="form-required" title="This field is required.">*</span>' : "";
315 $class = $item['required'] ? "required" : "";
316 if ($item['required'] && !empty($person) && empty($person[$name])) {
321 $left_part = "<label class='$class' for='edit-$name'>$title: $required</label>";
327 $right_part= "<select name='site_ids[]' id='edit-site_ids' class='form-select $class'> $site_options </select>";
332 if (0) { /* Not letting users select PI or Tech any more. Its only lead to confusion and abuse. */
333 $right_part = "<select name='roles[]' multiple='multiple' id='edit-roles' class='form-select $class'> $role_options </select>";
338 $maxlength = $item['maxlength'];
339 $size = $item['size'];
340 $value = !empty($person[$name]) ? $person[$name] : "";
341 $type = $name == 'password' ? "password" : "text";
342 $right_part = "<input type='$type' maxlength='$maxlength' name='$name' id='edit-$name' size='$size' value='$value' class='form-text $class'>";
346 $details->th_td($left_part,$right_part);
349 // Do not allow resubmits
350 if (empty($person['person_id'])) {
352 $details->tr($register_button,'center');
355 $details->form_end();
359 include 'plc_footer.php';