signup.php 14.4 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 32 33

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

#
# Verify page arguments.
#
$optargs = OptionalPageArguments("create",       PAGEARG_STRING,
				 "uid",		 PAGEARG_STRING,
				 "email",        PAGEARG_STRING,
				 "pid",          PAGEARG_STRING,
				 "verify",       PAGEARG_STRING,
49
				 "finished",     PAGEARG_BOOLEAN,
50
				 "joinproject",  PAGEARG_BOOLEAN,
51
                                 "toomany",      PAGEARG_BOOLEAN,
52 53 54 55 56 57 58 59
				 "formfields",   PAGEARG_ARRAY);

#
# Spit the form
#
function SPITFORM($formfields, $showverify, $errors)
{
    global $TBDB_UIDLEN, $TBDB_PIDLEN, $TBDOCBASE, $WWWHOST;
60
    global $ACCOUNTWARNING, $EMAILWARNING, $this_user, $joinproject, $toomany;
61 62 63
    $button_label = "Create Account";

    echo "<link rel='stylesheet'
64
                href='css/bootstrap-formhelpers.min.css'>\n";
65 66 67

    SPITHEADER(1);

68
    echo "<div id='signup-body'></div>\n";
69
    echo "<div id='toomany_div'></div>\n";
70 71 72 73 74 75 76
    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";
77

78 79 80
    if (isset($joinproject)) {
	$joinproject = ($joinproject ? "true" : "false");
	echo "window.APT_OPTIONS.joinproject = $joinproject;\n";
81 82
    } else {
	echo "window.APT_OPTIONS.joinproject = false;\n";
83 84
    }
    if ($showverify) {
85
        echo "window.APT_OPTIONS.ShowVerifyModal = true;\n";
86
    }
87
    if ($this_user) {
88 89 90 91
	echo "window.APT_OPTIONS.this_user = true;\n";
    }
    else {
	echo "window.APT_OPTIONS.this_user = false;\n";
92
    }
93 94 95 96 97 98
    if ($toomany) {
	echo "window.APT_OPTIONS.toomany = true;\n";
    }
    else {
	echo "window.APT_OPTIONS.toomany = false;\n";
    }
99

100
    echo "</script>\n";
101

102
    echo "<script src='js/lib/jquery-2.0.3.min.js'></script>\n";
103
    echo "<script src='js/lib/bootstrap.js'></script>\n";
104
    echo "<script src='js/lib/require.js' data-main='js/signup'></script>";
105 106
    SPITFOOTER();
}
107 108 109

if (isset($finished) && $finished) {
    SPITHEADER(1);
110 111 112 113
    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'.".
114 115 116 117 118
    SPITNULLREQUIRE();
    SPITFOOTER();
    exit(0);
}

119 120 121 122 123 124 125
#
# If not clicked, then put up a form.
#
if (! isset($create)) {
    $defaults = array();
    $errors   = array();

Leigh Stoller's avatar
Leigh Stoller committed
126 127 128 129
    # Default to start
    $defaults["startorjoin"] = "start";
    $joinproject = 0;

130 131 132 133 134
    if (isset($uid)) {
	$defaults["uid"] = CleanString($uid);
    }
    if (isset($pid)) {
	$defaults["pid"] = CleanString($pid);
Leigh Stoller's avatar
Leigh Stoller committed
135 136
        $defaults["startorjoin"] = "join";
        $joinproject = 1;
137 138 139 140 141 142 143 144 145 146 147 148 149 150
    }
    if (isset($email)) {
	$defaults["email"] = CleanString($email);
    }
    
    SPITFORM($defaults, 0, $errors);
    return;
}

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

151 152 153 154 155 156 157 158 159 160 161 162
#
# 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;
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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 240 241 242 243 244 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 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
#
# These fields are required
#
if (! $this_user) {
    if (!isset($formfields["uid"]) ||
	strcmp($formfields["uid"], "") == 0) {
	$errors["uid"] = "Missing Field";
    }
    elseif (!TBvalid_uid($formfields["uid"])) {
	$errors["uid"] = TBFieldErrorString();
    }
    elseif (User::Lookup($formfields["uid"]) ||
	    posix_getpwnam($formfields["uid"])) {
	$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();
    }
    elseif (User::LookupByEmail($formfields["email"])) {
        #
        # 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();
    }
    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";
    }
}

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.
#
if (!$this_user) {
    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
328
		 "From: $APTMAIL");
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	
	    #
            # 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;
    }
}

344 345 346
if ($debug) {
    TBERROR("New APT User ($joinproject)" .
	    print_r($formfields, TRUE), 0);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    SPITFORM($formfields, 0, $errors);
    return;
}

#
# 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"];
366 367
    # Force initial SSL cert generation.
    $args["passphrase"]    = $formfields["password1"];
368
    # Flag to the backend.
369
    $args["genesis"]	   = ($ISAPT ? "aptlab" : "cloudlab");
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400

    #
    # 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);
401
	header("Location: instantiate.php");
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
	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;
    }
}
422
elseif ($joinproject) {
423 424 425 426 427 428 429 430 431 432
    $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;
    }
433 434 435
    if ($project->AddNewMember($this_user) < 0) {
	TBERROR("Could not add new user to project group $pid", 1);
    }
436
    $group = $project->LoadDefaultGroup();
437
    $group->NewMemberNotify($this_user);
438
    header("Location: signup.php?finished=1");
439 440
    return;
}
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

#
# 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";
465
$args["whynotpublic"]      = ($ISAPT ? "aptlab" : "cloudlab");
466
# Flag to the backend.
467
$args["genesis"]	   = ($ISAPT ? "aptlab" : "cloudlab");
468 469 470 471

if (! ($project = Project::NewNewProject($args, $error))) {
    $errors["error"] = $error;
    if ($suexec_retval < 0) {
472
	TBERROR("Error Creating APT/CloudLab Project\n${error}\n\n" .
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
		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.
# 
490
header("Location: signup.php?finished=1");
491 492

?>