signup.php 16.8 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
chdir("..");
include("defs.php3");
chdir("apt");
include("quickvm_sup.php");
Leigh Stoller's avatar
Leigh Stoller committed
28
$page_title = "Signup";
29
# Do not create anything, just do the checks.
30
$debug = 0;
31 32
# Update mode.
$promoting = 0;
33 34 35 36

#
# Get current user.
#
37
RedirectSecure();
38
$this_user = CheckLogin($check_status);
39
if (isset($this_user)) {
40
    # Allow unapproved users to join multiple groups ...
41 42 43 44 45 46 47 48 49 50
    CheckLoginOrDie(CHECKLOGIN_UNAPPROVED|CHECKLOGIN_NONLOCAL);
}

# Force nonlocal user to provide more complete personal info.
if ($this_user && $this_user->IsNonLocal()) {
    # Might want to use a different test, or add a flag to the users table
    # indicating that a user has been promoted to allow project create/join.
    if (!$this_user->country() || $this_user->country() == "") {
        $promoting = 1;
    }
51
}
52 53 54 55 56 57 58 59 60

#
# Verify page arguments.
#
$optargs = OptionalPageArguments("create",       PAGEARG_STRING,
				 "uid",		 PAGEARG_STRING,
				 "email",        PAGEARG_STRING,
				 "pid",          PAGEARG_STRING,
				 "verify",       PAGEARG_STRING,
61
				 "finished",     PAGEARG_BOOLEAN,
62
				 "joinproject",  PAGEARG_BOOLEAN,
63
                                 "toomany",      PAGEARG_BOOLEAN,
64 65 66 67 68 69 70 71
				 "formfields",   PAGEARG_ARRAY);

#
# Spit the form
#
function SPITFORM($formfields, $showverify, $errors)
{
    global $TBDB_UIDLEN, $TBDB_PIDLEN, $TBDOCBASE, $WWWHOST;
72
    global $ACCOUNTWARNING, $EMAILWARNING, $this_user, $joinproject, $toomany;
73
    global $promoting;
74 75
    $button_label = "Create Account";

Leigh Stoller's avatar
Leigh Stoller committed
76 77
    SPITHEADER(1);

78
    echo "<link rel='stylesheet'
79
                href='css/bootstrap-formhelpers.min.css'>\n";
80

81
    echo "<div id='signup-body'></div>\n";
82
    echo "<div id='toomany_div'></div>\n";
83 84 85 86 87 88 89
    echo "<script type='text/plain' id='form-json'>\n";
    echo htmlentities(json_encode($formfields)) . "\n";
    echo "</script>\n";
    echo "<script type='text/plain' id='error-json'>\n";
    echo htmlentities(json_encode($errors));
    echo "</script>\n";
    echo "<script type='text/javascript'>\n";
90

91 92 93
    if (isset($joinproject)) {
	$joinproject = ($joinproject ? "true" : "false");
	echo "window.APT_OPTIONS.joinproject = $joinproject;\n";
94 95
    } else {
	echo "window.APT_OPTIONS.joinproject = false;\n";
96 97
    }
    if ($showverify) {
98
        echo "window.APT_OPTIONS.ShowVerifyModal = true;\n";
99
    }
100
    if ($this_user) {
101 102 103 104 105 106 107
        echo "window.APT_OPTIONS.this_user = true;\n";
        if ($promoting) {
            echo "window.APT_OPTIONS.promoting = true;\n";
        }
        else {
            echo "window.APT_OPTIONS.promoting = false;\n";
        }
108 109 110
    }
    else {
	echo "window.APT_OPTIONS.this_user = false;\n";
111
    }
112 113 114 115 116 117
    if ($toomany) {
	echo "window.APT_OPTIONS.toomany = true;\n";
    }
    else {
	echo "window.APT_OPTIONS.toomany = false;\n";
    }
118

119
    echo "</script>\n";
120

121
    echo "<script src='js/lib/jquery-2.0.3.min.js'></script>\n";
122 123 124 125 126 127 128 129

    REQUIRE_UNDERSCORE();
    REQUIRE_SUP();
    REQUIRE_APTFORMS();
    REQUIRE_FORMHELPERS();
    SPITREQUIRE("js/signup.js");

    AddTemplateList(array("about-account", "verify-modal", "signup-personal", "signup-project", "signup", "toomany-modal"));
130 131
    SPITFOOTER();
}
132 133 134

if (isset($finished) && $finished) {
    SPITHEADER(1);
135 136 137 138
    echo "Thank you! Stay tuned for email notifying you that your account has ".
	"been activated. Please be sure ".
	"to set your spam filter to allow all email from '@${OURDOMAIN}' and ".
	"'@flux.utah.edu'.".
139 140 141 142 143
    SPITNULLREQUIRE();
    SPITFOOTER();
    exit(0);
}

144 145 146 147 148 149 150
#
# If not clicked, then put up a form.
#
if (! isset($create)) {
    $defaults = array();
    $errors   = array();

Leigh Stoller's avatar
Leigh Stoller committed
151
    # Default to start
152 153 154 155 156 157 158 159 160 161
    if (!isset($joinproject)) {
        $joinproject = 0;
        $defaults["startorjoin"] = "start";
    }
    elseif ($joinproject) {
        $defaults["startorjoin"] = "join";
    }
    else {
        $defaults["startorjoin"] = "start";
    }
Leigh Stoller's avatar
Leigh Stoller committed
162

163 164
    if ($this_user && $promoting) {
        $defaults["uid"]         = $this_user->uid();
165
        $defaults["fullname"]    = $this_user->name();
166 167 168 169 170 171 172 173 174 175 176 177 178
        $defaults["email"]       = $this_user->email();
        $defaults["city"]        = $this_user->city();
        $defaults["state"]       = $this_user->state();
        $defaults["country"]     = $this_user->country();
        $defaults["affiliation"] = $this_user->affil();
    }
    else {
        if (isset($uid)) {
            $defaults["uid"] = CleanString($uid);
        }
        if (isset($email)) {
            $defaults["email"] = CleanString($email);
        }
179 180
    }
    if (isset($pid)) {
181
        $defaults["pid"] = CleanString($pid);
Leigh Stoller's avatar
Leigh Stoller committed
182 183
        $defaults["startorjoin"] = "join";
        $joinproject = 1;
184 185 186 187 188 189 190 191 192 193 194
    }
    
    SPITFORM($defaults, 0, $errors);
    return;
}

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

195 196 197 198 199 200 201 202 203 204 205 206
#
# Check for start or join right away so we know what we be doing.
#
if (!isset($formfields["startorjoin"]) || $formfields["startorjoin"] == "") {
    $errors["error"] = "Neither start or join selected";
    SPITFORM($defaults, 0, $errors);
    return;
}
if ($formfields["startorjoin"] == "join") {
    $joinproject = 1;
}

207 208 209
#
# These fields are required
#
210
if (!$this_user || $promoting) {
211 212 213 214 215 216 217
    if (!isset($formfields["uid"]) ||
	strcmp($formfields["uid"], "") == 0) {
	$errors["uid"] = "Missing Field";
    }
    elseif (!TBvalid_uid($formfields["uid"])) {
	$errors["uid"] = TBFieldErrorString();
    }
218 219 220
    elseif (!$promoting &&
            (User::Lookup($formfields["uid"]) ||
             posix_getpwnam($formfields["uid"]))) {
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	$errors["uid"] = "Already in use. Pick another";
    }
    if (!isset($formfields["fullname"]) ||
	strcmp($formfields["fullname"], "") == 0) {
	$errors["fullname"] = "Missing Field";
    }
    elseif (! TBvalid_usrname($formfields["fullname"])) {
	$errors["fullname"] = TBFieldErrorString();
    }
    # Make sure user name has at least two tokens!
    $tokens = preg_split("/[\s]+/", $formfields["fullname"],
			 -1, PREG_SPLIT_NO_EMPTY);
    if (count($tokens) < 2) {
	$errors["fullname"] = "Please provide a first and last name";
    }
    if (!isset($formfields["email"]) ||
	strcmp($formfields["email"], "") == 0) {
	$errors["email"] = "Missing Field";
    }
    elseif (! TBvalid_email($formfields["email"])) {
	$errors["email"] = TBFieldErrorString();
    }
243 244
    elseif (!$promoting &&
            User::LookupByEmail($formfields["email"])) {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        #
        # Treat this error separate. Not allowed.
        #
	$errors["email"] =
	    "Already in use. Did you forget to login?";
    }
    if (!isset($formfields["affiliation"]) ||
	strcmp($formfields["affiliation"], "") == 0) {
	$errors["affiliation"] = "Missing Field";
    }
    elseif (! TBvalid_affiliation($formfields["affiliation"])) {
	$errors["affiliation"] = TBFieldErrorString();
    }
    if (!isset($formfields["country"]) ||
	strcmp($formfields["country"], "") == 0) {
	$errors["country"] = "Missing Field";
    }
    elseif (! TBvalid_country($formfields["country"])) {
	$errors["country"] = TBFieldErrorString();
    }
    if (!isset($formfields["state"]) ||
	strcmp($formfields["state"], "") == 0) {
	$errors["state"] = "Missing Field";
    }
    elseif (! TBvalid_state($formfields["state"])) {
	$errors["state"] = TBFieldErrorString();
    }
    if (!isset($formfields["city"]) ||
	strcmp($formfields["city"], "") == 0) {
	$errors["city"] = "Missing Field";
    }
    elseif (! TBvalid_city($formfields["city"])) {
	$errors["city"] = TBFieldErrorString();
    }
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    if (!$promoting) {
        if (!isset($formfields["password1"]) ||
            strcmp($formfields["password1"], "") == 0) {
            $errors["password1"] = "Missing Field";
        }
        if (!isset($formfields["password2"]) ||
            strcmp($formfields["password2"], "") == 0) {
            $errors["password2"] = "Missing Field";
        }
        elseif (strcmp($formfields["password1"], $formfields["password2"])) {
            $errors["password2"] = "Does not match password";
        }
        elseif (! CHECKPASSWORD($formfields["uid"],
                                $formfields["password1"],
                                $formfields["fullname"],
                                $formfields["email"], $checkerror)) {
            $errors["password1"] = "$checkerror";
        }
297 298 299 300 301 302 303 304 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 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    }
}

if (!isset($formfields["pid"]) ||
    strcmp($formfields["pid"], "") == 0) {
    $errors["pid"] = "Missing Field";
}
else {
    # Lets not allow pids that are too long, via this interface.
    if (strlen($formfields["pid"]) > $TBDB_PIDLEN) {
	$errors["pid"] =
	    "too long - $TBDB_PIDLEN chars maximum";
    }
    elseif (!TBvalid_newpid($formfields["pid"])) {
	$errors["pid"] = TBFieldErrorString();
    }
    $project = Project::LookupByPid($formfields["pid"]);
    if ($joinproject) {
	if (!$project) {
	    $errors["pid"] = "No such project. Did you spell it properly?";
	}
    }
    elseif ($project) {
	$errors["pid"] = "Already in use. Select another";
    }
}
if (!$joinproject) {
    if (!isset($formfields["proj_title"]) ||
	strcmp($formfields["proj_title"], "") == 0) {
	$errors["proj_title"] = "Missing Field";
    }
    elseif (! TBvalid_description($formfields["proj_title"])) {
	$errors["proj_title"] = TBFieldErrorString();
    }
    if (!isset($formfields["proj_url"]) ||
	strcmp($formfields["proj_url"], "") == 0 ||
	strcmp($formfields["proj_url"], $HTTPTAG) == 0) {    
	$errors["proj_url"] = "Missing Field";
    }
    elseif (! CHECKURL($formfields["proj_url"], $urlerror)) {
	$errors["proj_url"] = $urlerror;
    }
    if (!isset($formfields["proj_why"]) ||
	strcmp($formfields["proj_why"], "") == 0) {
	$errors["proj_why"] = "Missing Field";
    }
    elseif (! TBvalid_why($formfields["proj_why"])) {
	$errors["proj_why"] = TBFieldErrorString();
    }
}

# Present these errors before we call out to do anything else.
if (count($errors)) {
    SPITFORM($formfields, 0, $errors);
    return;
}

#
# Lets get the user to do the email verification now before
# we go any further. We use a session variable to store the
# key we send to the user in email.
#
359
if (!$this_user || $promoting) {
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    session_start();
    if (!isset($_SESSION["verify_key"])) {
	$_SESSION["verify_key"] = substr(GENHASH(), 0, 16);
    }
    #
    # Once the user verifies okay, we remember that in the session
    # in case there is a later error below.
    #
    if (!isset($_SESSION["verified"])) {
	if (!isset($verify) || $verify == "" ||
	    $verify != $_SESSION["verify_key"]) {
	    mail($formfields["email"],
		 "Confirm your email to create your account",
		 "Here is your user verification code. Please copy and\n".
		 "paste this code into the box on the account page.\n\n".
		 "\t" . $_SESSION["verify_key"] . "\n",
Leigh Stoller's avatar
Leigh Stoller committed
376
		 "From: $APTMAIL");
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	
	    #
            # Respit complete form but show the verify email modal.
	    #
	    SPITFORM($formfields, 1, $errors);
	    return;
	}
	#
        # Success. Lets remember that in case we get an error below and
        # the form is redisplayed. 
	#
	$_SESSION["verified"] = 1;
    }
}

392 393 394
if ($debug) {
    TBERROR("New APT User ($joinproject)" .
	    print_r($formfields, TRUE), 0);
395 396 397 398
    SPITFORM($formfields, 0, $errors);
    return;
}

399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
#
# If a nonlocal user upgrading account, do a ModUserInfo and then
# continue as a normal user (even though they are still nonlocal).
#
if ($this_user && $promoting) {
    $args["name"]	   = $formfields["fullname"];
    $args["city"]          = $formfields["city"];
    $args["state"]         = $formfields["state"];
    $args["country"]       = $formfields["country"];
    $args["shell"]         = 'tcsh';
    $args["affiliation"]   = $formfields["affiliation"];

    if (! User::ModUserInfo($this_user, $this_user->uid(), $args, $errors)) {
        # Always respit the form so that the form fields are not lost.
        SPITFORM($formfields, 0, $errors);
        PAGEFOOTER();
        return;
    }
    $this_user->Refresh();
}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
#
# Create the User first, then the Project/Group.
# Certain of these values must be escaped or otherwise sanitized.
#
if (!$this_user) {
    $args = array();
    $args["uid"]	   = $formfields["uid"];
    $args["name"]	   = $formfields["fullname"];
    $args["email"]         = $formfields["email"];
    $args["city"]          = $formfields["city"];
    $args["state"]         = $formfields["state"];
    $args["country"]       = $formfields["country"];
    $args["shell"]         = 'tcsh';
    $args["affiliation"]   = $formfields["affiliation"];
    $args["password"]      = $formfields["password1"];
435 436
    # Force initial SSL cert generation.
    $args["passphrase"]    = $formfields["password1"];
437
    # Flag to the backend.
438
    $args["portal"]	   = $PORTAL_GENESIS;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

    #
    # Backend verifies pubkey and returns error. We first look for a 
    # file and then fall back to an inline field. See SPITFORM().
    #
    if (isset($_FILES['keyfile']) &&
	$_FILES['keyfile']['name'] != "" &&
	$_FILES['keyfile']['name'] != "none") {

	$localfile = $_FILES['keyfile']['tmp_name'];
	$args["pubkey"] = file_get_contents($localfile);
	$formfields["pubkey"] = $args["pubkey"];
    }
    elseif (isset($formfields["pubkey"]) && $formfields["pubkey"] != "") {
	$args["pubkey"] = $formfields["pubkey"];
    }

    #
    # Joining a project is a different path.
    #
    if ($joinproject) {
	if (! ($user = User::NewNewUser(0, $args, $error)) != 0) {
	    $errors["error"] = $error;
	    SPITFORM($formfields, 0, $errors);
	    return;
	}
	$group = $project->LoadDefaultGroup();
	if ($project->AddNewMember($user) < 0) {
	    TBERROR("Could not add new user to project group $pid", 1);
	}
	$group->NewMemberNotify($user);
470
	header("Location: instantiate.php");
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	return;
    }

    # Just collect the user XML args here and pass the file to NewNewProject.
    # Underneath, newproj calls newuser with the XML file.
    #
    # Calling newuser down in Perl land makes creation of the leader account
    # and the project "atomic" from the user's point of view.  This avoids a
    # problem when the DB is locked for daily backup: in newproject, the call
    # on NewNewUser would block and then unblock and get done; meanwhile the
    # PHP thread went away so we never returned here to call NewNewProject.
    #
    if (! ($newuser_xml = User::NewNewUserXML($args, $error)) != 0) {
	$errors["error"] = $error;
	TBERROR("Error Creating new APT user XML:\n${error}\n\n" .
		print_r($args, TRUE), 0);
	SPITFORM($formfields, 0, $errors);
	return;
    }
}
491
elseif ($joinproject) {
492 493 494 495 496 497 498 499 500 501
    $isapproved = 0;
    if ($project->IsMember($this_user, $isapproved)) {
	$errors["pid"] = "You are already a member of the project! ";
	if (!$isapproved) {
	    $errors["pid"] .=
		"Please wait for your membership to be activated.";
	}
	SPITFORM($formfields, 0, $errors);
	return;
    }
502 503 504
    if ($project->AddNewMember($this_user) < 0) {
	TBERROR("Could not add new user to project group $pid", 1);
    }
505
    $group = $project->LoadDefaultGroup();
506
    $group->NewMemberNotify($this_user);
507
    header("Location: signup.php?finished=1");
508 509
    return;
}
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

#
# Now for the new Project
#
$args = array();
if (isset($newuser_xml)) {
    $args["newuser_xml"]   = $newuser_xml;
}
if ($this_user) {
    # An existing, logged-in user is starting the project.
    $args["leader"]	   = $this_user->uid();
}
$args["name"]		   = $formfields["pid"];
$args["short description"] = $formfields["proj_title"];
$args["URL"]               = $formfields["proj_url"];
$args["long description"]  = $formfields["proj_why"];
# We do not care about these anymore. Just default to something.
$args["members"]           = 1;
$args["num_pcs"]           = 1;
$args["public"]            = 1;
$args["linkedtous"]        = 1;
$args["plab"]              = 0;
$args["ron"]               = 0;
$args["funders"]           = "None";
534
$args["whynotpublic"]      = $PORTAL_GENESIS;
535
# Flag to the backend.
536
$args["portal"] 	   = $PORTAL_GENESIS;
537 538 539 540

if (! ($project = Project::NewNewProject($args, $error))) {
    $errors["error"] = $error;
    if ($suexec_retval < 0) {
541
	TBERROR("Error Creating APT/CloudLab Project\n${error}\n\n" .
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		print_r($args, TRUE), 0);
    }
    SPITFORM($formfields, 0, $errors);
    return;
}
#
# Destroy the session if we had a new user. 
#
if (!$this_user) {
    session_destroy();
}

#
# 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.
# 
559
header("Location: signup.php?finished=1");
560 561

?>