signup.php 16.6 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2015 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");
28
# Do not create anything, just do the checks.
29
$debug = 0;
30 31
# Update mode.
$promoting = 0;
32 33 34 35

#
# Get current user.
#
36
RedirectSecure();
37
$this_user = CheckLogin($check_status);
38
if (isset($this_user)) {
39
    # Allow unapproved users to join multiple groups ...
40 41 42 43 44 45 46 47 48 49
    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;
    }
50
}
51 52 53 54 55 56 57 58 59

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

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

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

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

80
    echo "<div id='signup-body'></div>\n";
81
    echo "<div id='toomany_div'></div>\n";
82 83 84 85 86 87 88
    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";
89

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

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

120
    echo "<script src='js/lib/jquery-2.0.3.min.js'></script>\n";
121
    echo "<script src='js/lib/bootstrap.js'></script>\n";
122
    echo "<script src='js/lib/require.js' data-main='js/signup'></script>";
123 124
    SPITFOOTER();
}
125 126 127

if (isset($finished) && $finished) {
    SPITHEADER(1);
128 129 130 131
    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'.".
132 133 134 135 136
    SPITNULLREQUIRE();
    SPITFOOTER();
    exit(0);
}

137 138 139 140 141 142 143
#
# If not clicked, then put up a form.
#
if (! isset($create)) {
    $defaults = array();
    $errors   = array();

Leigh Stoller's avatar
Leigh Stoller committed
144 145 146 147
    # Default to start
    $defaults["startorjoin"] = "start";
    $joinproject = 0;

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    if ($this_user && $promoting) {
        $defaults["uid"]         = $this_user->uid();
        $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);
        }
163 164
    }
    if (isset($pid)) {
165
        $defaults["pid"] = CleanString($pid);
Leigh Stoller's avatar
Leigh Stoller committed
166 167
        $defaults["startorjoin"] = "join";
        $joinproject = 1;
168 169 170 171 172 173 174 175 176 177 178
    }
    
    SPITFORM($defaults, 0, $errors);
    return;
}

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

179 180 181 182 183 184 185 186 187 188 189 190
#
# 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;
}

191 192 193
#
# These fields are required
#
194
if (!$this_user || $promoting) {
195 196 197 198 199 200 201
    if (!isset($formfields["uid"]) ||
	strcmp($formfields["uid"], "") == 0) {
	$errors["uid"] = "Missing Field";
    }
    elseif (!TBvalid_uid($formfields["uid"])) {
	$errors["uid"] = TBFieldErrorString();
    }
202 203 204
    elseif (!$promoting &&
            (User::Lookup($formfields["uid"]) ||
             posix_getpwnam($formfields["uid"]))) {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	$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();
    }
227 228
    elseif (!$promoting &&
            User::LookupByEmail($formfields["email"])) {
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        #
        # 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();
    }
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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";
        }
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 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
    }
}

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.
#
343
if (!$this_user || $promoting) {
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
    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
360
		 "From: $APTMAIL");
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
	
	    #
            # 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;
    }
}

376 377 378
if ($debug) {
    TBERROR("New APT User ($joinproject)" .
	    print_r($formfields, TRUE), 0);
379 380 381 382
    SPITFORM($formfields, 0, $errors);
    return;
}

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#
# 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();
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
#
# 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"];
419 420
    # Force initial SSL cert generation.
    $args["passphrase"]    = $formfields["password1"];
421
    # Flag to the backend.
422 423 424
    $args["genesis"]	   = ($ISAPT ? "aptlab" : 
			      ($ISPNET ? "phantomnet" : 
			       "cloudlab"));
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

    #
    # 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);
456
	header("Location: instantiate.php");
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	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;
    }
}
477
elseif ($joinproject) {
478 479 480 481 482 483 484 485 486 487
    $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;
    }
488 489 490
    if ($project->AddNewMember($this_user) < 0) {
	TBERROR("Could not add new user to project group $pid", 1);
    }
491
    $group = $project->LoadDefaultGroup();
492
    $group->NewMemberNotify($this_user);
493
    header("Location: signup.php?finished=1");
494 495
    return;
}
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519

#
# 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";
520
$args["whynotpublic"]      = ($ISAPT ? "aptlab" : ($ISPNET ? "phantomnet" : "cloudlab"));
521
# Flag to the backend.
522
$args["genesis"]	   = ($ISAPT ? "aptlab" : ($ISPNET ? "phantomnet" : "cloudlab"));
523 524 525 526

if (! ($project = Project::NewNewProject($args, $error))) {
    $errors["error"] = $error;
    if ($suexec_retval < 0) {
527
	TBERROR("Error Creating APT/CloudLab Project\n${error}\n\n" .
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
		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.
# 
545
header("Location: signup.php?finished=1");
546 547

?>