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

9 10 11 12 13 14 15 16 17 18
#
# This is a hack to support wikiregister.php3 - normally, this variable would
# be cleared by OptionalPageArguments()
#
if (isset($forwikionly) && $forwikionly == True) {
    $old_forwikionly = True;
} else {
    $old_forwikionly = False;
}

19 20
#
# No PAGEHEADER since we spit out a Location header later. See below.
21
#
22 23 24

#
# Get current user.
25 26
#
$this_user = CheckLogin($check_status);
27

28 29 30 31 32 33 34 35 36 37
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("submit",       PAGEARG_STRING,
				 "forwikionly",  PAGEARG_BOOLEAN,
				 "finished",     PAGEARG_BOOLEAN,
				 "target_pid",   PAGEARG_STRING,
				 "target_gid",   PAGEARG_STRING,
				 "formfields",   PAGEARG_ARRAY);

38 39
#
# If a uid came in, then we check to see if the login is valid.
40
# We require that the user be logged in to start a second project.
41
#
42
if ($this_user) {
43 44
    # Allow unapproved users to join multiple groups ...
    # Must be verified though.
45 46 47
    CheckLoginOrDie(CHECKLOGIN_UNAPPROVED|
		    CHECKLOGIN_WEBONLY|CHECKLOGIN_WIKIONLY);
    $joining_uid = $this_user->uid();
48 49 50 51 52 53 54 55 56
    $returning = 1;
}
else {
    #
    # No uid, so must be new.
    #
    $returning = 0;
}

57 58 59
if ($old_forwikionly == True) {
    $forwikionly = True;
}
60
if (!isset($forwikionly)) {
61
    $forwikionly = False;
62
}
63
unset($addpubkeyargs);
64

Leigh B. Stoller's avatar
Leigh B. Stoller committed
65 66 67 68
$ACCOUNTWARNING =
    "Before continuing, please make sure your username " .
    "reflects your normal login name. ".
    "Emulab accounts are not to be shared amongst users!";
69

Leigh B. Stoller's avatar
Leigh B. Stoller committed
70 71 72
$EMAILWARNING =
    "Before continuing, please make sure the email address you have ".
    "provided is current and non-pseudonymic. Redirections and anonymous ".
73 74
    "email addresses are not allowed.";

75 76 77 78 79 80
#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $returning, $errors)
{
    global $TBDB_UIDLEN, $TBDB_PIDLEN, $TBDB_GIDLEN;
81
    global $ACCOUNTWARNING, $EMAILWARNING;
82
    global $WIKISUPPORT, $forwikionly, $WIKIHOME, $USERSELECTUIDS;
83
    global $WIKIDOCURL;
84 85 86 87 88

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

90
    if (! $returning) {
91 92 93 94 95 96 97
	echo "<center>\n";

	if ($forwikionly) {
	    echo "<font size=+2>Register for an Emulab Wiki account</font>
                  <br><br>\n";
	}
        echo "<font size=+1>
98 99 100
               If you already have an Emulab account,
               <a href=login.php3?refer=1>
               <font color=red>please log on first!</font></a>
101 102 103 104 105 106 107 108
              </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);
109 110
    }

111
    if ($errors) {
112 113
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
114
              <tr>
115
                 <th align=center colspan=2>
116
                   <font size=+1 color=red>
117
                      &nbsp;Oops, please fix the following errors!&nbsp;
118 119 120 121 122 123
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
124 125 126 127
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
128 129 130 131
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    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";
160 161 162

    echo "<table align=center border=1> 
          <tr>
163
            <td align=center colspan=3>
164
                Fields marked with * are required.
165 166 167
            </td>
          </tr>\n

168 169 170 171
          <form name=myform enctype=multipart/form-data
                action=" . ($forwikionly ?
			    "wikiregister.php3" : "joinproject.php3") . " " .
	        "method=post>\n";
172 173

    if (! $returning) {
174 175 176 177 178 179
	if ($USERSELECTUIDS) {
            #
            # UID.
            #
	    echo "<tr>
                      <td colspan=2>*<a
180
                             href='$WIKIDOCURL/SecReqs'
181 182 183 184 185
                             target=_blank>Username</a>
                                (alphanumeric, lowercase):</td>
                      <td class=left>
                          <input type=text
                                 name=\"formfields[joining_uid]\"
186
                                 value=\"" . $formfields["joining_uid"] . "\"
187 188 189 190 191 192
	                         size=$TBDB_UIDLEN
                                 onchange=\"alert('$ACCOUNTWARNING')\"
	                         maxlength=$TBDB_UIDLEN>
                      </td>
                  </tr>\n";
	}
193 194 195 196 197

	#
	# Full Name
	#
        echo "<tr>
198
                  <td colspan=2>*Full Name (first and last):</td>
199 200 201
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_name]\"
202
                             onchange=\"SetWikiName(myform);\"
203
                             value=\"" . $formfields["usr_name"] . "\"
204 205 206 207 208
	                     size=30>
                  </td>
              </tr>\n";

	#
209
	# WikiName
210
	#
211 212
	if ($WIKISUPPORT) {
	    echo "<tr>
213 214
                      <td colspan=2>*<a
                            href=${WIKIHOME}/bin/view/TWiki/WikiName
215 216 217
                            target=_blank>WikiName</a>:<td class=left>
                          <input type=text
                                 name=\"formfields[wikiname]\"
218
                                 value=\"" . $formfields["wikiname"] . "\"
219 220 221 222 223 224 225 226 227 228
	                         size=30>
                      </td>
                  </tr>\n";
	}

	if (! $forwikionly) {
            #
            # Title/Position:
	    #
	    echo "<tr>
229
                      <td colspan=2>*Job Title/Position:</td>
230 231 232
                      <td class=left>
                          <input type=text
                                 name=\"formfields[usr_title]\"
233
                                 value=\"" . $formfields["usr_title"] . "\"
234 235 236 237 238 239 240 241
  	                         size=30>
                      </td>
                  </tr>\n";

            #
            # Affiliation:
            # 
	    echo "<tr>
242
                      <td colspan=2>*Institutional Affiliation:</td>
243
                      <td class=left>
244 245 246 247
			<table>
                          <tr>
                          <td>Name</td>
                          <td><input type=text
248
                                 name=\"formfields[usr_affil]\"
249
                                 value=\"" . $formfields["usr_affil"] . "\"
250 251 252 253 254 255 256 257 258
	                         size=40></td></tr>
			  <tr>
                          <td>Abbreviation:</td>
                          <td><input type=text
                                 name=\"formfields[usr_affil_abbrev]\"
                                 value=\"" . $formfields["usr_affil_abbrev"] . "\"
	                         size=16 maxlength=16> (e.g. MIT)</td>
			  </tr>
        		</table>
259 260 261 262 263 264
                      </td>
                  </tr>\n";

	    #
	    # User URL
	    #
265
	    echo "<tr>
266 267 268 269
                      <td colspan=2>Home Page URL:</td>
                      <td class=left>
                          <input type=text
                                 name=\"formfields[usr_URL]\"
270
                                 value=\"" . $formfields["usr_URL"] . "\"
271 272 273 274
	                         size=45>
                      </td>
                  </tr>\n";
	}
275 276 277 278 279

	#
	# Email:
	#
	echo "<tr>
280
                  <td colspan=2>*Email Address[<b>1</b>]:</td>
281 282 283
                  <td class=left>
                      <input type=text
                             name=\"formfields[usr_email]\"
284
                             value=\"" . $formfields["usr_email"] . "\"
285
                             onchange=\"alert('$EMAILWARNING')\"
286 287 288 289
	                     size=30>
                  </td>
              </tr>\n";

290 291 292 293 294 295 296 297 298
	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]\"
299
                               value=\"" . $formfields["usr_addr"] . "\"
300 301 302 303
	                       size=45></td></tr>
		      <tr><td>Line 2</td><td colspan=3>
                        <input type=text
                               name=\"formfields[usr_addr2]\"
304
                               value=\"" . $formfields["usr_addr2"] . "\"
305 306 307 308
	                       size=45></td></tr>
		      <tr><td>City</td><td>
                        <input type=text
                               name=\"formfields[usr_city]\"
309
                               value=\"" . $formfields["usr_city"] . "\"
310 311 312 313
	                       size=25></td>
		          <td>State/Province</td><td>
                        <input type=text
                               name=\"formfields[usr_state]\"
314
                               value=\"" . $formfields["usr_state"] . "\"
315 316 317 318
	                       size=2></td></tr>
		      <tr><td>ZIP/Postal Code</td><td>
                        <input type=text
                               name=\"formfields[usr_zip]\"
319
                               value=\"" . $formfields["usr_zip"] . "\"
320 321 322 323
	                       size=10></td>
		          <td>Country</td><td>
                        <input type=text
                               name=\"formfields[usr_country]\"
324
                               value=\"" . $formfields["usr_country"] . "\"
325 326 327 328 329 330 331 332 333 334 335
	                       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]\"
336
                                 value=\"" . $formfields["usr_phone"] . "\"
337 338 339
	                         size=15>
                      </td>
                  </tr>\n";
340

341 342 343 344
	    #
	    # SSH public key
	    #
	    echo "<tr>
345 346 347 348
                     <td colspan=2>Upload your SSH Pub Key[<b>2</b>]:<br>
                                       (1K max)</td>
   
                     <td>
349 350
                          <input type=hidden name=MAX_FILE_SIZE value=1024>
                          <input type=file
351 352 353 354 355 356
                                 size=50
                                 name=usr_keyfile ";
	    if (isset($_FILES['usr_keyfile'])) {
		echo "        value=\"" .
		    $_FILES['usr_keyfile']['name'] . "\"";
	    }
Russ Fish's avatar
Russ Fish committed
357
	    echo         "> </td>
358 359
                  </tr>\n";
	}
360

361 362 363 364 365
	#
	# Password. Note that we do not resend the password. User
	# must retype on error.
	#
	echo "<tr>
366
                  <td colspan=2>*Password[<b>1</b>]:</td>
367 368 369
                  <td class=left>
                      <input type=password
                             name=\"formfields[password1]\"
370
                             value=\"" . $formfields["password1"] . "\"
371 372 373 374
                             size=8></td>
              </tr>\n";

        echo "<tr>
375
                  <td colspan=2>*Retype Password:</td>
376 377 378
                  <td class=left>
                      <input type=password
                             name=\"formfields[password2]\"
379
                             value=\"" . $formfields["password2"] . "\"
380 381 382 383
                             size=8></td>
             </tr>\n";
    }

384 385 386 387 388 389 390 391 392
    if (! $forwikionly) {
        #
        # Project Name:
        #
	echo "<tr>
                  <td colspan=2>*Project Name:</td>
                  <td class=left>
                      <input type=text
                             name=\"formfields[pid]\"
393
                             value=\"" . $formfields["pid"] . "\"
394 395 396
	                     size=$TBDB_PIDLEN maxlength=$TBDB_PIDLEN>
                  </td>
              </tr>\n";
397

398 399 400 401 402 403 404 405 406
        #
        # 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]\"
407
                             value=\"" . $formfields["gid"] . "\"
408 409 410 411
	                     size=$TBDB_GIDLEN maxlength=$TBDB_GIDLEN>
                  </td>
              </tr>\n";
    }
412 413

    echo "<tr>
414
              <td colspan=3 align=center>
415 416 417 418 419 420 421 422 423 424
                 <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
425
                 <a href = '$WIKIDOCURL/SecReqs' target='_blank'>
426
                 security policies</a> for information
427
                 regarding passwords and email addresses.\n";
428
    if (!$returning && !$forwikionly) {
429
	echo "<li> If you want us to use your existing ssh public key,
430
                   then please specify the path to your
431
                   your identity.pub file.  <font color=red>NOTE:</font>
432
                   We use the <a href=http://www.openssh.org target='_blank'>OpenSSH</a>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
433
                   key format,
434 435
                   which has a slightly different protocol 2 public key format
                   than some of the commercial vendors such as
436
                   <a href=http://www.ssh.com target='_blank'>SSH Communications</a>. If you
437
                   use one of these commercial vendors, then please
438 439
                   upload the public key file and we will convert it
                   for you.";
440 441
    }
    echo "</ol>
442 443 444 445 446 447 448
          </blockquote></blockquote>
          </h4>\n";
}

#
# The conclusion of a join request. See below.
# 
449
if (isset($finished)) {
450 451 452 453
    if ($forwikionly) 
	PAGEHEADER("Wiki Registration");
    else
	PAGEHEADER("Apply for Project Membership");
454 455 456 457

    #
    # Generate some warm fuzzies.
    #
458 459 460 461 462 463 464
    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) {
465 466
	echo "<p>
              As a pending user of the Testbed you will receive a key via email.
467
              When you receive the message, please follow the instructions
468 469 470 471 472
              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. ";
473 474
    }
    else {
475 476
          echo "<p>
	  	The project leader has been notified of your application. ";
477 478
    }

479 480 481 482
    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";

483 484 485 486 487 488 489
    PAGEFOOTER();
    return;
}

#
# On first load, display a virgin form and exit.
#
490
if (! isset($submit)) {
491
    $defaults = array();
492 493 494 495 496 497 498 499 500 501 502 503 504 505
    $defaults["pid"]         = "";
    $defaults["gid"]         = "";
    $defaults["joining_uid"] = "";
    $defaults["usr_name"]    = "";
    $defaults["usr_email"]   = "";
    $defaults["usr_addr"]    = "";
    $defaults["usr_addr2"]   = "";
    $defaults["usr_city"]    = "";
    $defaults["usr_state"]   = "";
    $defaults["usr_zip"]     = "";
    $defaults["usr_country"] = "";
    $defaults["usr_phone"]   = "";
    $defaults["usr_title"]   = "";
    $defaults["usr_affil"]   = "";
506
    $defaults["usr_affil_abbrev"] = "";
507 508 509 510 511
    $defaults["password1"]   = "";
    $defaults["password2"]   = "";
    $defaults["wikiname"]    = "";
    $defaults["usr_URL"]     = "$HTTPTAG";
    $defaults["usr_country"] = "USA";
512 513 514 515 516

    #
    # These two allow presetting the pid/gid.
    # 
    if (isset($target_pid) && strcmp($target_pid, "")) {
517
	$defaults["pid"] = $target_pid;
518 519
    }
    if (isset($target_gid) && strcmp($target_gid, "")) {
520
	$defaults["gid"] = $target_gid;
521
    }
522 523 524 525 526
    
    SPITFORM($defaults, $returning, 0);
    PAGEFOOTER();
    return;
}
527 528 529
# Form submitted. Make sure we have a formfields array.
if (!isset($formfields)) {
    PAGEARGERROR("Invalid form arguments.");
530
}
531 532 533 534 535 536 537 538 539 540

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

#
# These fields are required!
#
if (! $returning) {
541
    if ($USERSELECTUIDS) {
542 543
	if (!isset($formfields["joining_uid"]) ||
	    strcmp($formfields["joining_uid"], "") == 0) {
544 545
	    $errors["Username"] = "Missing Field";
	}
546
	elseif (!TBvalid_uid($formfields["joining_uid"])) {
547 548
	    $errors["UserName"] = TBFieldErrorString();
	}
549 550
	elseif (User::Lookup($formfields["joining_uid"]) ||
		posix_getpwnam($formfields["joining_uid"])) {
551 552
	    $errors["UserName"] = "Already in use. Pick another";
	}
553
    }
554 555
    if (!isset($formfields["usr_name"]) ||
	strcmp($formfields["usr_name"], "") == 0) {
556 557
	$errors["Full Name"] = "Missing Field";
    }
558
    elseif (! TBvalid_usrname($formfields["usr_name"])) {
559
	$errors["Full Name"] = TBFieldErrorString();
560
    }
561
    # Make sure user name has at least two tokens!
562
    $tokens = preg_split("/[\s]+/", $formfields["usr_name"],
563 564 565 566
			 -1, PREG_SPLIT_NO_EMPTY);
    if (count($tokens) < 2) {
	$errors["Full Name"] = "Please provide a first and last name";
    }
567
    if ($WIKISUPPORT) {
568 569
	if (!isset($formfields["wikiname"]) ||
	    strcmp($formfields["wikiname"], "") == 0) {
570 571
	    $errors["WikiName"] = "Missing Field";
	}
572
	elseif (! TBvalid_wikiname($formfields["wikiname"])) {
573 574
	    $errors["WikiName"] = TBFieldErrorString();
	}
575
	elseif (User::LookupByWikiName($formfields["wikiname"])) {
576 577
	    $errors["WikiName"] = "Already in use. Pick another";
	}
578
    }
579
    if (!$forwikionly) {
580 581
	if (!isset($formfields["usr_title"]) ||
	    strcmp($formfields["usr_title"], "") == 0) {
582
	    $errors["Job Title/Position"] = "Missing Field";
583
	}
584
	elseif (! TBvalid_title($formfields["usr_title"])) {
585
	    $errors["Job Title/Position"] = TBFieldErrorString();
586
	}
587 588
	if (!isset($formfields["usr_affil"]) ||
	    strcmp($formfields["usr_affil"], "") == 0) {
589
	    $errors["Affiliation Name"] = "Missing Field";
590
	}
591
	elseif (! TBvalid_affiliation($formfields["usr_affil"])) {
592 593 594 595 596 597
	    $errors["Affiliation Name"] = TBFieldErrorString();
	}
	if (!isset($formfields["usr_affil_abbrev"]) ||
	    strcmp($formfields["usr_affil_abbrev"], "") == 0) {
	    $errors["Affiliation Abbreviation"] = "Missing Field";
	}
Kevin Atkinson's avatar
Kevin Atkinson committed
598
	elseif (! TBvalid_affiliation_abbreviation($formfields["usr_affil_abbrev"])) {
599
	    $errors["Affiliation Name"] = TBFieldErrorString();
600 601
	}
    }	
602 603
    if (!isset($formfields["usr_email"]) ||
	strcmp($formfields["usr_email"], "") == 0) {
604 605
	$errors["Email Address"] = "Missing Field";
    }
606
    elseif (! TBvalid_email($formfields["usr_email"])) {
607
	$errors["Email Address"] = TBFieldErrorString();
608
    }
609
    elseif (User::LookupByEmail($formfields["usr_email"])) {
610 611
	$errors["Email Address"] =
	    "Already in use. <b>Did you forget to login?</b>";
612
    }
613
    if (! $forwikionly) {
614 615 616 617
	if (isset($formfields["usr_URL"]) &&
	    strcmp($formfields["usr_URL"], "") &&
	    strcmp($formfields["usr_URL"], $HTTPTAG) &&
	    ! CHECKURL($formfields["usr_URL"], $urlerror)) {
618 619
	    $errors["Home Page URL"] = $urlerror;
	}
620 621
	if (!isset($formfields["usr_addr"]) ||
	    strcmp($formfields["usr_addr"], "") == 0) {
622 623
	    $errors["Address 1"] = "Missing Field";
	}
624
	elseif (! TBvalid_addr($formfields["usr_addr"])) {
625 626 627
	    $errors["Address 1"] = TBFieldErrorString();
	}
        # Optional
628 629
	if (isset($formfields["usr_addr2"]) &&
	    !TBvalid_addr($formfields["usr_addr2"])) {
630 631
	    $errors["Address 2"] = TBFieldErrorString();
	}
632 633
	if (!isset($formfields["usr_city"]) ||
	    strcmp($formfields["usr_city"], "") == 0) {
634 635
	    $errors["City"] = "Missing Field";
	}
636
	elseif (! TBvalid_city($formfields["usr_city"])) {
637 638
	    $errors["City"] = TBFieldErrorString();
	}
639 640
	if (!isset($formfields["usr_state"]) ||
	    strcmp($formfields["usr_state"], "") == 0) {
641 642
	    $errors["State"] = "Missing Field";
	}
643
	elseif (! TBvalid_state($formfields["usr_state"])) {
644 645
	    $errors["State"] = TBFieldErrorString();
	}
646 647
	if (!isset($formfields["usr_zip"]) ||
	    strcmp($formfields["usr_zip"], "") == 0) {
648 649
	    $errors["ZIP/Postal Code"] = "Missing Field";
	}
650
	elseif (! TBvalid_zip($formfields["usr_zip"])) {
651 652
	    $errors["Zip/Postal Code"] = TBFieldErrorString();
	}
653 654
	if (!isset($formfields["usr_country"]) ||
	    strcmp($formfields["usr_country"], "") == 0) {
655 656
	    $errors["Country"] = "Missing Field";
	}
657
	elseif (! TBvalid_country($formfields["usr_country"])) {
658 659
	    $errors["Country"] = TBFieldErrorString();
	}
660 661
	if (!isset($formfields["usr_phone"]) ||
	    strcmp($formfields["usr_phone"], "") == 0) {
662 663
	    $errors["Phone #"] = "Missing Field";
	}
664
	elseif (!TBvalid_phone($formfields["usr_phone"])) {
665 666
	    $errors["Phone #"] = TBFieldErrorString();
	}
667
    }
668 669
    if (!isset($formfields["password1"]) ||
	strcmp($formfields["password1"], "") == 0) {
670 671
	$errors["Password"] = "Missing Field";
    }
672 673
    if (!isset($formfields["password2"]) ||
	strcmp($formfields["password2"], "") == 0) {
674 675
	$errors["Confirm Password"] = "Missing Field";
    }
676
    elseif (strcmp($formfields["password1"], $formfields["password2"])) {
677 678
	$errors["Confirm Password"] = "Does not match Password";
    }
679
    elseif (! CHECKPASSWORD(($USERSELECTUIDS ?
680 681 682 683
			     $formfields["joining_uid"] : "ignored"),
			    $formfields["password1"],
			    $formfields["usr_name"],
			    $formfields["usr_email"], $checkerror)) {
684 685 686
	$errors["Password"] = "$checkerror";
    }
}
687
if (!$forwikionly) {
688
    if (!isset($formfields["pid"]) || $formfields["pid"] == "") {
689 690 691 692
	$errors["Project Name"] = "Missing Field";
    }
    else {
        # Confirm pid/gid early to avoid spamming the page.
693
	$pid = $formfields["pid"];
694

695 696
	if (isset($formfields["gid"]) && $formfields["gid"] != "") {
	    $gid = $formfields["gid"];
697 698 699 700 701
	}
	else {
	    $gid = $pid;
	}

702
	if (!TBvalid_pid($pid) || !Project::Lookup($pid)) {
703 704
	    $errors["Project Name"] = "Invalid Project Name";
	}
705
	elseif (!TBvalid_gid($gid) || !Group::LookupByPidGid($pid, $gid)) {
706 707 708
	    $errors["Group Name"] = "Invalid Group Name";
	}
    }
709 710
}

711
# Present these errors before we call out to do pubkey stuff; saves work.
712 713 714 715 716 717
if (count($errors)) {
    SPITFORM($formfields, $returning, $errors);
    PAGEFOOTER();
    return;
}

718 719 720
#
# Need the user, project and group objects for the rest of this.
#
721
if (!$forwikionly) {
722
    if (! ($project = Project::Lookup($pid))) {
723
	TBERROR("Could not lookup object for $pid!", 1);
724
    }
725 726
    if (! ($group = Group::LookupByPidGid($pid, $gid))) {
	TBERROR("Could not lookup object for $pid/$gid!", 1);
727
    }
728
    if ($returning) {
729 730
	$user = $this_user;
	if ($group->IsMember($user, $ignore)) {
731 732
	    $errors["Membership"] = "You are already a member";
	}
733
    }
734 735
}

736 737 738 739 740
#
# If this is a new user, only allow the user creation to proceed if 
# doing so would not add a non-admin (default for new users) to a 
# project with admins.
#
741
if ($ISOLATEADMINS && !$returning && count($project->GetAdmins())) {
742 743 744 745 746 747 748 749 750 751
    $errors["Joining Project"] =
	"You cannot join project '$pid' due to security restrictions!"
	. "  If you were told to join this project specifically, email"
	. " either the project leader OR $TBMAILADDR_OPS.";
    TBERROR("New user '".$formfields["joining_uid"]."' attempted to join project ".
	    "'$pid'\n".
	    "which would create a mix of admin and non-admin ".
	    "users\n\n--- so the user creation was NOT allowed to occur!\n", 0);
}

752
# Done with sanity checks!
753 754 755 756 757 758 759
if (count($errors)) {
    SPITFORM($formfields, $returning, $errors);
    PAGEFOOTER();
    return;
}

#
760 761
# Create a new user. We do this by creating a little XML file to pass to
# the newuser script.
762 763
#
if (! $returning) {
764
    $args = array();
765 766 767 768 769 770 771 772 773
    $args["name"]	   = $formfields["usr_name"];
    $args["email"]         = $formfields["usr_email"];
    $args["address"]       = $formfields["usr_addr"];
    $args["address2"]      = $formfields["usr_addr2"];
    $args["city"]          = $formfields["usr_city"];
    $args["state"]         = $formfields["usr_state"];
    $args["zip"]           = $formfields["usr_zip"];
    $args["country"]       = $formfields["usr_country"];
    $args["phone"]         = $formfields["usr_phone"];
774
    $args["shell"]         = 'tcsh';
775 776
    $args["title"]         = $formfields["usr_title"];
    $args["affiliation"]   = $formfields["usr_affil"];
777
    $args["affiliation_abbreviation"] = $formfields["usr_affil_abbrev"];
778
    $args["password"]      = $formfields["password1"];
779 780 781
    if ($WIKISUPPORT) {
        $args["wikiname"] = $formfields["wikiname"];
    }
782

783 784
    if (isset($formfields["usr_URL"]) &&
	$formfields["usr_URL"] != $HTTPTAG && $formfields["usr_URL"] != "") {
Russ Fish's avatar
Russ Fish committed
785
	$args["URL"] = $formfields["usr_URL"];
786
    }
787
    if ($USERSELECTUIDS) {
788
	$args["login"] = $formfields["joining_uid"];
789 790
    }

791 792 793 794 795
    # Backend verifies pubkey and returns error.
    if (!$forwikionly) {
	if (isset($_FILES['usr_keyfile']) &&
	    $_FILES['usr_keyfile']['name'] != "" &&
	    $_FILES['usr_keyfile']['name'] != "none") {
796

797 798 799
	    $localfile = $_FILES['usr_keyfile']['tmp_name'];
	    $args["pubkey"] = file_get_contents($localfile);
	}
800
    }
801 802
    if (! ($user = User::NewNewUser(($forwikionly ?
				     TBDB_NEWACCOUNT_WIKIONLY : 0),
803 804 805 806 807 808
				    $args,
				    $error)) != 0) {
	$errors["Error Creating User"] = $error;
	SPITFORM($formfields, $returning, $errors);
	PAGEFOOTER();
	return;
809
    }
810
    $joining_uid = $user->uid();
811 812
}

813 814 815 816 817 818 819 820
#
# For wikionly registration, we are done.
# 
if ($forwikionly) {
    header("Location: wikiregister.php3?finished=1");
    exit();
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
#
# If this sitevar is set, check to see if this addition will create a
# mix of admin and non-admin people in the group. 
#
if ($ISOLATEADMINS &&
    !$project->IsMember($user, $ignore)) {
    $members = $project->MemberList();

    foreach ($members as $other_user) {
	if ($user->admin() != $other_user->admin()) {
	    if ($returning) {
		$errors["Joining Project"] =
		    "Improper mix of admin and non-admin users";
		SPITFORM($formfields, $returning, $errors);
		PAGEFOOTER();
		return;
	    }
	    else {
		#
		# The user creation still succeeds, which is good. Do not
		# want the effort to be wasted. But need to indicate that
		# something went wrong. Lets send email to tbops since this
		# should be an uncommon problem.
		#
		TBERROR("New user '$joining_uid' attempted to join project ".
			"'$pid'\n".
			"which would create a mix of admin and non-admin ".
			"users\n", 0);
		
		header("Location: joinproject.php3?finished=1");
		return;
	    }
	}
    }
}

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

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

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

#
# 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");
Russ Fish's avatar
Russ Fish committed
887
?>