joinproject.php3 27.3 KB
Newer Older
1
<?php
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003, 2005, 2006 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
# All rights reserved.
#
7
8
9
10
include("defs.php3");

#
# No PAGEHEADER since we spit out a Location header later. See below.
11
#
12
13
14
15
16
17
18
19

#
# Get current user.
# 
$uid = GETLOGIN();

#
# If a uid came in, then we check to see if the login is valid.
20
# We require that the user be logged in to start a second project.
21
22
#
if ($uid) {
23
24
    # Allow unapproved users to join multiple groups ...
    # Must be verified though.
25
26
    LOGGEDINORDIE($uid, CHECKLOGIN_UNAPPROVED|
		  CHECKLOGIN_WEBONLY|CHECKLOGIN_WIKIONLY);
27
28
29
30
31
32
33
34
35
36
    $joining_uid = $uid;
    $returning = 1;
}
else {
    #
    # No uid, so must be new.
    #
    $returning = 0;
}

37
38
39
if (!isset($forwikionly)) {
    $forwikionly = 0;
}
40
unset($addpubkeyargs);
41

Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
43
44
45
$ACCOUNTWARNING =
    "Before continuing, please make sure your username " .
    "reflects your normal login name. ".
    "Emulab accounts are not to be shared amongst users!";
46

Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
48
49
$EMAILWARNING =
    "Before continuing, please make sure the email address you have ".
    "provided is current and non-pseudonymic. Redirections and anonymous ".
50
51
    "email addresses are not allowed.";

52
53
54
55
56
57
#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $returning, $errors)
{
    global $TBDB_UIDLEN, $TBDB_PIDLEN, $TBDB_GIDLEN;
58
    global $ACCOUNTWARNING, $EMAILWARNING;
59
    global $WIKISUPPORT, $forwikionly, $WIKIHOME, $USERSELECTUIDS;
60
61
62
63
64

    if ($forwikionly)
	PAGEHEADER("Wiki Registration");
    else
	PAGEHEADER("Apply for Project Membership");
65

66
    if (! $returning) {
67
68
69
70
71
72
73
	echo "<center>\n";

	if ($forwikionly) {
	    echo "<font size=+2>Register for an Emulab Wiki account</font>
                  <br><br>\n";
	}
        echo "<font size=+1>
74
75
76
               If you already have an Emulab account,
               <a href=login.php3?refer=1>
               <font color=red>please log on first!</font></a>
77
78
79
80
81
82
83
84
              </font>\n";
	if ($forwikionly) {
	    echo "<br>(You will already have a wiki account)\n";
	}
	echo "</center><br>\n";	
    }
    elseif ($forwikionly) {
	USERERROR("You already have a Wiki account!", 1);
85
86
    }

87
    if ($errors) {
Chad Barb's avatar
   
Chad Barb committed
88
89
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
90
              <tr>
Chad Barb's avatar
   
Chad Barb committed
91
                 <th align=center colspan=2>
92
                   <font size=+1 color=red>
Chad Barb's avatar
   
Chad Barb committed
93
                      &nbsp;Oops, please fix the following errors!&nbsp;
94
95
96
97
98
99
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
Chad Barb's avatar
   
Chad Barb committed
100
101
102
103
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
104
105
106
107
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    echo "<SCRIPT LANGUAGE=JavaScript>
              function SetWikiName(theform) 
              {
	          var validchars = 'abcdefghijklmnopqrstuvwxyz0123456789';
                  var usrname    = theform['formfields[usr_name]'].value;
                  var wikiname   = '';
                  var docap      = 1;

		  for (var i = 0; i < usrname.length; i++) {
                      var letter = usrname.charAt(i).toLowerCase();

                      if (validchars.indexOf(letter) == -1) {
                          if (letter == ' ') {
                              docap = 1;
                          }
                          continue;
                      }
                      else {
                          if (docap == 1) {
                              letter = usrname.charAt(i).toUpperCase()
                              docap  = 0;
                          }
                          wikiname = wikiname + letter;
                      }
                  }
                  theform['formfields[wikiname]'].value = wikiname;
              }
          </SCRIPT>\n";
136
137
138

    echo "<table align=center border=1> 
          <tr>
139
            <td align=center colspan=3>
Chad Barb's avatar
   
Chad Barb committed
140
                Fields marked with * are required.
141
142
143
            </td>
          </tr>\n

144
145
146
147
          <form name=myform enctype=multipart/form-data
                action=" . ($forwikionly ?
			    "wikiregister.php3" : "joinproject.php3") . " " .
	        "method=post>\n";
148
149

    if (! $returning) {
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
	if ($USERSELECTUIDS) {
            #
            # UID.
            #
	    echo "<tr>
                      <td colspan=2>*<a
                             href='docwrapper.php3?docname=security.html'
                             target=_blank>Username</a>
                                (alphanumeric, lowercase):</td>
                      <td class=left>
                          <input type=text
                                 name=\"formfields[joining_uid]\"
                                 value=\"" . $formfields[joining_uid] . "\"
	                         size=$TBDB_UIDLEN
                                 onchange=\"alert('$ACCOUNTWARNING')\"
	                         maxlength=$TBDB_UIDLEN>
                      </td>
                  </tr>\n";
	}
169
170
171
172
173

	#
	# Full Name
	#
        echo "<tr>
174
                  <td colspan=2>*Full Name (first and last):</td>
175
176
177
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_name]\"
178
                             onchange=\"SetWikiName(myform);\"
179
180
181
182
183
184
                             value=\"" . $formfields[usr_name] . "\"
	                     size=30>
                  </td>
              </tr>\n";

	#
185
	# WikiName
186
	#
187
188
	if ($WIKISUPPORT) {
	    echo "<tr>
189
190
                      <td colspan=2>*<a
                            href=${WIKIHOME}/bin/view/TWiki/WikiName
191
192
193
194
195
196
197
198
199
200
201
202
203
204
                            target=_blank>WikiName</a>:<td class=left>
                          <input type=text
                                 name=\"formfields[wikiname]\"
                                 value=\"" . $formfields[wikiname] . "\"
	                         size=30>
                      </td>
                  </tr>\n";
	}

	if (! $forwikionly) {
            #
            # Title/Position:
	    #
	    echo "<tr>
205
                      <td colspan=2>*Job Title/Position:</td>
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
                      <td class=left>
                          <input type=text
                                 name=\"formfields[usr_title]\"
                                 value=\"" . $formfields[usr_title] . "\"
  	                         size=30>
                      </td>
                  </tr>\n";

            #
            # Affiliation:
            # 
	    echo "<tr>
                      <td colspan=2>*Institutional<br>Affiliation:</td>
                      <td class=left>
                          <input type=text
                                 name=\"formfields[usr_affil]\"
                                 value=\"" . $formfields[usr_affil] . "\"
	                         size=40>
                      </td>
                  </tr>\n";

	    #
	    # User URL
	    #
	        echo "<tr>
                      <td colspan=2>Home Page URL:</td>
                      <td class=left>
                          <input type=text
                                 name=\"formfields[usr_URL]\"
                                 value=\"" . $formfields[usr_URL] . "\"
	                         size=45>
                      </td>
                  </tr>\n";
	}
240
241
242
243
244

	#
	# Email:
	#
	echo "<tr>
245
                  <td colspan=2>*Email Address[<b>1</b>]:</td>
246
247
248
249
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_email]\"
                             value=\"" . $formfields[usr_email] . "\"
250
                             onchange=\"alert('$EMAILWARNING')\"
251
252
253
254
	                     size=30>
                  </td>
              </tr>\n";

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	if (! $forwikionly) {
	    #
	    # Postal Address
	    #
	    echo "<tr><td colspan=3>*Postal Address:<br /><center>
		    <table>
		      <tr><td>Line 1</td><td colspan=3>
                        <input type=text
                               name=\"formfields[usr_addr]\"
                               value=\"" . $formfields[usr_addr] . "\"
	                       size=45></td></tr>
		      <tr><td>Line 2</td><td colspan=3>
                        <input type=text
                               name=\"formfields[usr_addr2]\"
                               value=\"" . $formfields[usr_addr2] . "\"
	                       size=45></td></tr>
		      <tr><td>City</td><td>
                        <input type=text
                               name=\"formfields[usr_city]\"
                               value=\"" . $formfields[usr_city] . "\"
	                       size=25></td>
		          <td>State/Province</td><td>
                        <input type=text
                               name=\"formfields[usr_state]\"
                               value=\"" . $formfields[usr_state] . "\"
	                       size=2></td></tr>
		      <tr><td>ZIP/Postal Code</td><td>
                        <input type=text
                               name=\"formfields[usr_zip]\"
                               value=\"" . $formfields[usr_zip] . "\"
	                       size=10></td>
		          <td>Country</td><td>
                        <input type=text
                               name=\"formfields[usr_country]\"
                               value=\"" . $formfields[usr_country] . "\"
	                       size=15></td></tr>
                   </table></center></td></tr>";

	    #
	    # Phone
	    #
	    echo "<tr>
                      <td colspan=2>*Phone #:</td>
                      <td class=left>
                          <input type=text
                                 name=\"formfields[usr_phone]\"
                                 value=\"" . $formfields[usr_phone] . "\"
	                         size=15>
                      </td>
                  </tr>\n";
305

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
	    #
	    # SSH public key
	    #
	    echo "<tr>
                     <td rowspan><center>
                                   Your SSH Pub Key: &nbsp<br>
                                        [<b>2</b>]
                                  </center></td>
    
                      <td rowspan><center>Upload (1K max)[<b>3</b>]<br>
                                      <b>Or</b><br>
                                   Insert Key
                                 </center></td>
    
                      <td rowspan>
                          <input type=hidden name=MAX_FILE_SIZE value=1024>
                          <input type=file
                                 name=usr_keyfile
                                 value=\"" . $_FILES['usr_keyfile']['name'] .
		                       "\"
	                         size=50>
                          <br>
                          <br>
	                  <input type=text
                                 name=\"formfields[usr_key]\"
                                 value=\"$formfields[usr_key]\"
	                         size=50
	                         maxlength=1024>
                      </td>
                  </tr>\n";
	}
337

338
339
340
341
342
	#
	# Password. Note that we do not resend the password. User
	# must retype on error.
	#
	echo "<tr>
343
                  <td colspan=2>*Password[<b>1</b>]:</td>
344
345
346
                  <td class=left>
                      <input type=password
                             name=\"formfields[password1]\"
347
                             value=\"$formfields[password1]\"
348
349
350
351
                             size=8></td>
              </tr>\n";

        echo "<tr>
352
                  <td colspan=2>*Retype Password:</td>
353
354
355
                  <td class=left>
                      <input type=password
                             name=\"formfields[password2]\"
356
                             value=\"$formfields[password2]\"
357
358
359
360
                             size=8></td>
             </tr>\n";
    }

361
362
363
364
365
366
367
368
369
370
371
372
373
    if (! $forwikionly) {
        #
        # Project Name:
        #
	echo "<tr>
                  <td colspan=2>*Project Name:</td>
                  <td class=left>
                      <input type=text
                             name=\"formfields[pid]\"
                             value=\"" . $formfields[pid] . "\"
	                     size=$TBDB_PIDLEN maxlength=$TBDB_PIDLEN>
                  </td>
              </tr>\n";
374

375
376
377
378
379
380
381
382
383
384
385
386
387
388
        #
        # Group Name:
        #
	echo "<tr>
                  <td colspan=2>Group Name:<br>
                  (Leave blank unless you <em>know</em> the group name)</td>
                  <td class=left>
                      <input type=text
                             name=\"formfields[gid]\"
                             value=\"" . $formfields[gid] . "\"
	                     size=$TBDB_GIDLEN maxlength=$TBDB_GIDLEN>
                  </td>
              </tr>\n";
    }
389
390

    echo "<tr>
391
              <td colspan=3 align=center>
392
393
394
395
396
397
398
399
400
401
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";

    echo "<h4><blockquote><blockquote>
          <ol>
            <li> Please consult our
402
                 <a href = 'docwrapper.php3?docname=security.html' target='_blank'>
403
                 security policies</a> for information
404
                 regarding passwords and email addresses.\n";
405
    if (!$returning && !$forwikionly) {
406
407
	echo "<li> If you want us to use your existing ssh public key,
                   then either paste it in or specify the path to your
408
                   your identity.pub file.  <font color=red>NOTE:</font>
409
                   We use the <a href=http://www.openssh.org target='_blank'>OpenSSH</a>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
410
                   key format,
411
412
                   which has a slightly different protocol 2 public key format
                   than some of the commercial vendors such as
413
                   <a href=http://www.ssh.com target='_blank'>SSH Communications</a>. If you
414
415
416
417
                   use one of these commercial vendors, then please
                   upload the public  key file and we will convert it
                   for you. <i>Please do not paste it in.</i>\n

418
              <li> Note to <a href=http://www.opera.com target='_blank'><b>Opera 5</b></a>
419
420
421
422
423
                   users: The file upload mechanism is broken in Opera, so
                   you cannot specify a local file for upload. Instead,
                   please paste your public key in.\n";
    }
    echo "</ol>
424
425
426
427
428
429
430
          </blockquote></blockquote>
          </h4>\n";
}

#
# The conclusion of a join request. See below.
# 
431
if (isset($_GET['finished'])) {
432
433
434
435
    if ($forwikionly) 
	PAGEHEADER("Wiki Registration");
    else
	PAGEHEADER("Apply for Project Membership");
436
437
438
439

    #
    # Generate some warm fuzzies.
    #
440
441
442
443
444
445
446
    if ($forwikionly) {
	echo "An email message has been sent to your account so we may verify
              your email address. Please follow the instructions contained in
              that message, which will verify your account, and grant you
              access to the Wiki.\n";
    }
    elseif (! $returning) {
447
448
	echo "<p>
              As a pending user of the Testbed you will receive a key via email.
449
              When you receive the message, please follow the instructions
450
451
452
453
454
              contained in the message, which will verify your identity.
	      <br>
	      <p>
	      When you have done that, the project leader will be
	      notified of your application. ";
455
456
    }
    else {
457
458
          echo "<p>
	  	The project leader has been notified of your application. ";
459
460
    }

461
462
463
464
    echo "He/She will make a decision and either approve or deny your
          application, and you will be notified via email as soon as
	  that happens.\n";

465
466
467
468
469
470
471
    PAGEFOOTER();
    return;
}

#
# On first load, display a virgin form and exit.
#
472
if (! isset($_POST['submit'])) {
473
474
    $defaults = array();
    $defaults[usr_URL] = "$HTTPTAG";
Chad Barb's avatar
   
Chad Barb committed
475
    $defaults[usr_country] = "USA";
476
477
478
479
480
481
482
483
484
485

    #
    # These two allow presetting the pid/gid.
    # 
    if (isset($target_pid) && strcmp($target_pid, "")) {
	$defaults[pid] = $target_pid;
    }
    if (isset($target_gid) && strcmp($target_gid, "")) {
	$defaults[gid] = $target_gid;
    }
486
487
488
489
490
    
    SPITFORM($defaults, $returning, 0);
    PAGEFOOTER();
    return;
}
491
492
493
else {
    # Form submitted. Make sure we have a formfields array and a target_uid.
    if (!isset($_POST['formfields']) ||
494
	!is_array($_POST['formfields'])) {
495
496
497
498
	PAGEARGERROR("Invalid form arguments.");
    }
    $formfields = $_POST['formfields'];
}
499
500
501
502
503
504
505
506
507
508

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

#
# These fields are required!
#
if (! $returning) {
509
510
511
512
513
514
515
516
517
518
519
520
    if ($USERSELECTUIDS) {
	if (!isset($formfields[joining_uid]) ||
	    strcmp($formfields[joining_uid], "") == 0) {
	    $errors["Username"] = "Missing Field";
	}
	elseif (!TBvalid_uid($formfields[joining_uid])) {
	    $errors["UserName"] = TBFieldErrorString();
	}
	elseif (TBCurrentUser($formfields[joining_uid]) ||
		posix_getpwnam($formfields[joining_uid])) {
	    $errors["UserName"] = "Already in use. Pick another";
	}
521
    }
522
523
524
525
    if (!isset($formfields[usr_name]) ||
	strcmp($formfields[usr_name], "") == 0) {
	$errors["Full Name"] = "Missing Field";
    }
526
    elseif (! TBvalid_usrname($formfields[usr_name])) {
527
	$errors["Full Name"] = TBFieldErrorString();
528
    }
529
530
531
532
533
534
    # Make sure user name has at least two tokens!
    $tokens = preg_split("/[\s]+/", $formfields[usr_name],
			 -1, PREG_SPLIT_NO_EMPTY);
    if (count($tokens) < 2) {
	$errors["Full Name"] = "Please provide a first and last name";
    }
535
536
537
538
539
540
541
542
543
544
545
    if ($WIKISUPPORT) {
	if (!isset($formfields[wikiname]) ||
	    strcmp($formfields[wikiname], "") == 0) {
	    $errors["WikiName"] = "Missing Field";
	}
	elseif (! TBvalid_wikiname($formfields[wikiname])) {
	    $errors["WikiName"] = TBFieldErrorString();
	}
	elseif (TBCurrentWikiName($formfields[wikiname])) {
	    $errors["WikiName"] = "Already in use. Pick another";
	}
546
    }
547
548
549
    if (!$forwikionly) {
	if (!isset($formfields[usr_title]) ||
	    strcmp($formfields[usr_title], "") == 0) {
550
	    $errors["Job Title/Position"] = "Missing Field";
551
552
	}
	elseif (! TBvalid_title($formfields[usr_title])) {
553
	    $errors["Job Title/Position"] = TBFieldErrorString();
554
555
556
557
558
559
560
561
562
	}
	if (!isset($formfields[usr_affil]) ||
	    strcmp($formfields[usr_affil], "") == 0) {
	    $errors["Affiliation"] = "Missing Field";
	}
	elseif (! TBvalid_affiliation($formfields[usr_affil])) {
	    $errors["Affiliation"] = TBFieldErrorString();
	}
    }	
563
564
565
566
    if (!isset($formfields[usr_email]) ||
	strcmp($formfields[usr_email], "") == 0) {
	$errors["Email Address"] = "Missing Field";
    }
567
    elseif (! TBvalid_email($formfields[usr_email])) {
568
	$errors["Email Address"] = TBFieldErrorString();
569
    }
570
    elseif (TBCurrentEmail($formfields[usr_email])) {
571
572
	$errors["Email Address"] =
	    "Already in use. <b>Did you forget to login?</b>";
573
    }
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
    if (! $forwikionly) {
	if (isset($formfields[usr_URL]) &&
	    strcmp($formfields[usr_URL], "") &&
	    strcmp($formfields[usr_URL], $HTTPTAG) &&
	    ! CHECKURL($formfields[usr_URL], $urlerror)) {
	    $errors["Home Page URL"] = $urlerror;
	}
	if (!isset($formfields[usr_addr]) ||
	    strcmp($formfields[usr_addr], "") == 0) {
	    $errors["Address 1"] = "Missing Field";
	}
	elseif (! TBvalid_addr($formfields[usr_addr])) {
	    $errors["Address 1"] = TBFieldErrorString();
	}
        # Optional
	if (isset($formfields[usr_addr2]) &&
	    !TBvalid_addr($formfields[usr_addr2])) {
	    $errors["Address 2"] = TBFieldErrorString();
	}
	if (!isset($formfields[usr_city]) ||
	    strcmp($formfields[usr_city], "") == 0) {
	    $errors["City"] = "Missing Field";
	}
	elseif (! TBvalid_city($formfields[usr_city])) {
	    $errors["City"] = TBFieldErrorString();
	}
	if (!isset($formfields[usr_state]) ||
	    strcmp($formfields[usr_state], "") == 0) {
	    $errors["State"] = "Missing Field";
	}
	elseif (! TBvalid_state($formfields[usr_state])) {
	    $errors["State"] = TBFieldErrorString();
	}
	if (!isset($formfields[usr_zip]) ||
	    strcmp($formfields[usr_zip], "") == 0) {
	    $errors["ZIP/Postal Code"] = "Missing Field";
	}
	elseif (! TBvalid_zip($formfields[usr_zip])) {
	    $errors["Zip/Postal Code"] = TBFieldErrorString();
	}
	if (!isset($formfields[usr_country]) ||
	    strcmp($formfields[usr_country], "") == 0) {
	    $errors["Country"] = "Missing Field";
	}
	elseif (! TBvalid_country($formfields[usr_country])) {
	    $errors["Country"] = TBFieldErrorString();
	}
	if (!isset($formfields[usr_phone]) ||
	    strcmp($formfields[usr_phone], "") == 0) {
	    $errors["Phone #"] = "Missing Field";
	}
	elseif (!TBvalid_phone($formfields[usr_phone])) {
	    $errors["Phone #"] = TBFieldErrorString();
	}
628
629
630
631
632
633
634
635
636
637
638
639
    }
    if (!isset($formfields[password1]) ||
	strcmp($formfields[password1], "") == 0) {
	$errors["Password"] = "Missing Field";
    }
    if (!isset($formfields[password2]) ||
	strcmp($formfields[password2], "") == 0) {
	$errors["Confirm Password"] = "Missing Field";
    }
    elseif (strcmp($formfields[password1], $formfields[password2])) {
	$errors["Confirm Password"] = "Does not match Password";
    }
640
641
    elseif (! CHECKPASSWORD(($USERSELECTUIDS ?
			     $formfields[joining_uid] : "ignored"),
642
643
644
645
646
647
			    $formfields[password1],
			    $formfields[usr_name],
			    $formfields[usr_email], $checkerror)) {
	$errors["Password"] = "$checkerror";
    }
}
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
if (!$forwikionly) {
    if (!isset($formfields[pid]) || $formfields[pid] == "") {
	$errors["Project Name"] = "Missing Field";
    }
    else {
        # Confirm pid/gid early to avoid spamming the page.
	$pid = $formfields[pid];

	if (isset($formfields[gid]) && $formfields[gid] != "") {
	    $gid = $formfields[gid];
	}
	else {
	    $gid = $pid;
	}

	if (!TBvalid_pid($pid) || !TBValidProject($pid)) {
	    $errors["Project Name"] = "Invalid Project Name";
	}
	elseif (!TBvalid_gid($gid) || !TBValidGroup($pid, $gid)) {
	    $errors["Group Name"] = "Invalid Group Name";
	}
    }
670
671
}

672
# Present these errors before we call out to do pubkey stuff; saves work.
673
674
675
676
677
678
if (count($errors)) {
    SPITFORM($formfields, $returning, $errors);
    PAGEFOOTER();
    return;
}

679
680
# Okay, do pubkey checks.
if (!$returning && !$forwikionly) {
681
    #
682
    # Pub key provided in form (paste in).
683
    #
684
685
    if (isset($formfields[usr_key]) &&
	strcmp($formfields[usr_key], "")) {
686
        #
687
688
        # This is passed off to the shell, so taint check it.
        # 
689
690
691
692
	if (! preg_match("/^[-\w\s\.\@\+\/\=]*$/", $formfields[usr_key])) {
	    $errors["PubKey"] = "Invalid characters";
	}
	else {
693
694
695
696
697
698
            #
            # Replace any embedded newlines first.
            #
	    $formfields[usr_key] =
		ereg_replace("[\n]", "", $formfields[usr_key]);
	    $usr_key = $formfields[usr_key];
699
700
701
702
703
704
705
706
707
708
709

            #
            # Verify key format.
            #
	    if (ADDPUBKEY(null, "webaddpubkey -n -k '$usr_key' ")) {
		$errors["Pubkey Format"] =
		    "Could not be parsed. Is it a public key?";
	    }
	    else {
		$addpubkeyargs = "-k '$usr_key' ";
	    }
710
	}
711
    }
712

713
714
715
    #
    # If usr provided a file for the key, it overrides the paste in text.
    #
716
717
718
719
720
    if (isset($_FILES['usr_keyfile']) &&
	$_FILES['usr_keyfile']['name'] != "" &&
	$_FILES['usr_keyfile']['name'] != "none") {

	$localfile = $_FILES['usr_keyfile']['tmp_name'];
721

722
	if (! stat($localfile)) {
723
	    $errors["PubKey File"] = "No such file";
724
	}
725
726
727
728
        # Taint check shell arguments always! 
	elseif (! preg_match("/^[-\w\.\/]*$/", $localfile)) {
	    $errors["PubKey File"] = "Invalid characters";
	}
729
	else {
730
731
732
733
734
735
736
737
738
739
740
741
	    chmod($localfile, 0644);

            #
            # Verify key format.
            #
	    if (ADDPUBKEY(null, "webaddpubkey -n $localfile ")) {
		$errors["Pubkey Format"] =
		    "Could not be parsed. Is it a public key?";
	    }
	    else {
		$addpubkeyargs = "$localfile";
	    }
742
743
	}
    }
744
745
}

746
747
748
#
# Need the user, project and group objects for the rest of this.
#
749
if (!$forwikionly) {
750
751
    if (! ($project = Project::LookupByPid($pid))) {
	TBERROR("Could not lookup object for $pid!", 1);
752
    }
753
754
    if (! ($group = Group::LookupByPidGid($pid, $gid))) {
	TBERROR("Could not lookup object for $pid/$gid!", 1);
755
    }
756
757
758
759
760
761
762
    if ($returning) {
	if (! ($user = User::LookupByUid($joining_uid))) {
	    TBERROR("Could not lookup user '$joining_uid'!", 1);
	}
	if ($group->IsMember($user)) {
	    $errors["Membership"] = "You are already a member";
	}
763
    }
764
765
}

766
# Done with sanity checks!
767
768
769
770
771
772
773
if (count($errors)) {
    SPITFORM($formfields, $returning, $errors);
    PAGEFOOTER();
    return;
}

#
774
# Create a new user.
775
776
#
if (! $returning) {
777
    #
778
779
    # Certain of these values must be escaped or otherwise sanitized.
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
780
    $joining_uid       = ($USERSELECTUIDS ? $formfields[joining_uid] : null);
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
    $usr_name          = addslashes($formfields[usr_name]);
    $usr_email         = $formfields[usr_email];
    $password1         = $formfields[password1];
    $password2         = $formfields[password2];
    $wikiname          = ($WIKISUPPORT ? $formfields[wikiname] : "");

    if (!$forwikionly) {
	$usr_affil         = addslashes($formfields[usr_affil]);
	$usr_title         = addslashes($formfields[usr_title]);
	$usr_addr          = addslashes($formfields[usr_addr]);
	$usr_city          = addslashes($formfields[usr_city]);
	$usr_state         = addslashes($formfields[usr_state]);
	$usr_zip           = addslashes($formfields[usr_zip]);
	$usr_country       = addslashes($formfields[usr_country]);
	$usr_phone         = $formfields[usr_phone];
    }
    else {
	$usr_affil         = "";
	$usr_title         = "";
	$usr_addr          = "";
	$usr_city          = "";
	$usr_state         = "";
	$usr_zip           = "";
	$usr_country       = "";
	$usr_phone         = "";
    }

    if (! isset($formfields[usr_URL]) ||
	strcmp($formfields[usr_URL], "") == 0 ||
	strcmp($formfields[usr_URL], $HTTPTAG) == 0) {
	$usr_URL = "";
    }
    else {
	$usr_URL = addslashes($formfields[usr_URL]);
    }

    if (! isset($formfields[usr_addr2])) {
	$usr_addr2 = "";
    }
    else {
	$usr_addr2 = addslashes($formfields[usr_addr2]);
822
823
    }

824
    $args = array();
825
    $args["usr_expires"]   = date("Y:m:d", time() + (86400 * 120));
826
827
828
829
830
831
832
833
834
835
836
837
838
    $args["usr_name"]	   = $usr_name;
    $args["usr_email"]     = $usr_email;
    $args["usr_addr"]      = $usr_addr;
    $args["usr_addr2"]     = $usr_addr2;
    $args["usr_city"]      = $usr_city;
    $args["usr_state"]     = $usr_state;
    $args["usr_zip"]       = $usr_zip;
    $args["usr_country"]   = $usr_country;
    $args["usr_URL"]       = $usr_URL;
    $args["usr_phone"]     = $usr_phone;
    $args["usr_shell"]     = 'tcsh';
    $args["usr_title"]     = $usr_title;
    $args["usr_affil"]     = $usr_affil;
839
    $args["usr_pswd"]      = crypt("$password1");
840
841
842
    $args["wikiname"]      = $wikiname;

    if (! ($user = User::NewUser($joining_uid, 0, $forwikionly, $args))) {
843
	TBERROR("Could not create new user '$usr_email'!", 1);
844
    }
845
846
847
848
    $joining_uid = $user->uid();

    if (!$forwikionly && isset($addpubkeyargs)) {
	ADDPUBKEY($joining_uid, "webaddpubkey -u $joining_uid $addpubkeyargs");
849
    }
850
851
}

852
853
854
855
856
857
858
859
#
# For wikionly registration, we are done.
# 
if ($forwikionly) {
    header("Location: wikiregister.php3?finished=1");
    exit();
}

860
#
861
# If joining a subgroup, also add to project group.
862
#
863
864
865
866
if ($pid != $gid && ! $project->IsMember($user)) {
    if ($project->AddNewMember($user) < 0) {
	TBERROR("Could not add user $joining_uid to project group $pid", 1);
    }
867
868
869
}

#
870
871
# Add to the group, but with trust=none. The project/group leader will have
# to upgrade the trust level, making the new user real.
872
#
873
874
if ($group->AddNewMember($user) < 0) {
    TBERROR("Could not add user $joining_uid to group $pid/$gid", 1);
875
876
}

877
878
879
#
# Generate an email message to the proj/group leaders.
#
880
if ($returning) {
881
    $group->NewMemberNotify($user);
882
}
883
884
885
886
887
888
889

#
# Spit out a redirect so that the history does not include a post
# in it. The back button skips over the post and to the form.
# See above for conclusion.
# 
header("Location: joinproject.php3?finished=1");