Commit 9ac3d870 authored by Leigh B. Stoller's avatar Leigh B. Stoller
Browse files

A morass of form changes. The main goals are to avoid the loss of info

when backing up (cause of an error that needs to be fixed) since not
all browsers handle this the same. Instead, redraw the form with all
of the original info and a list of error messages at the top.
Conceptually simple change, but it turns out to be a pain to implement
since you need to combine the form and processing code in one page
(well, its just a lot easier to do that), and then change all of the
forms to deal with a "default" value. That is, each different kind of
input tag (text, radio, select, checkbox, etc.) requires slightly
different changes to do that. Lots of forms, lots of entries on the
forms, and its a long slow tedious process. Much nicer though, although
the code is a bit harder to grok. At the same time, I added a lot more
sanity checks of the information being passed in.

The other change is to deal with how browsers handle the back button
on a form thats been properly submitted. Not all browsers use
the cache directives the same, and I was often typing back, only to
have some form get reposted. Thats a major pain in the butt. The way
to deal with that is to have the processor send out a Location header,
which modifies the browser history so that the post is no longer in
the history. You back up straight to the unposted form (if its in the
cache). I've done this to only some forms, since its a bit of a pain
to rework things so that you can jump ahead to a page that spits out
the requisite warm fuzzies for the specific operation just completed.

I've done newproject, joinproject, update user info, newimageid, and
newimaged_dz forms.
parent f4fdddcf
<?php
include("defs.php3");
PAGEHEADER("Apply for Project Membership");
#
# Get current user.
#
$uid = GETLOGIN();
#
# If a uid came in, then we check to see if the login is valid.
# If the login is not valid, then quit cause we don't want to display the
# personal information for some random ?uid argument.
#
if ($uid) {
LOGGEDINORDIE($uid);
$query_result = mysql_db_query($TBDBNAME,
"SELECT * FROM users WHERE uid='$uid'");
if (! $query_result) {
$err = mysql_error();
TBERROR("Database Error confirming user $uid: $err\n", 1);
}
if (($row = mysql_fetch_array($query_result)) == 0) {
USERERROR("You do not appear to have an account!", 1);
}
$usr_expires = $row[usr_expires];
$usr_email = $row[usr_email];
$usr_URL = $row[usr_URL];
$usr_addr = $row[usr_addr];
$usr_name = $row[usr_name];
$usr_phone = $row[usr_phone];
$usr_title = $row[usr_title];
$usr_affil = $row[usr_affil];
$returning = 1;
}
else {
$returning = 0;
}
?>
<table align="center" border="1">
<tr>
<td align="center" colspan="2">
Fields marked with * are required.</td>
</tr>
<form action="usradded.php3" method="post">
<?php
if ($returning) {
echo "<tr>
<td>*Username (no blanks, lowercase):</td>
<td class=\"left\">
<input type=\"readonly\" name=\"joining_uid\"
value=\"$uid\"></td>
</tr>\n";
echo "<tr>
<td>*Full Name:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_name\"
value=\"$usr_name\"></td>
</tr>\n";
echo "<tr>
<td>*Title/Position:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_title\"
value=\"$usr_title\"></td>
</tr>\n";
echo "<tr>
<td>*Institutional<br>Affiliation:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_affil\"
value=\"$usr_affil\"></td>
</tr>\n";
echo "<tr>
<td>*Email Address:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_email\"
value=\"$usr_email\"></td>
</tr>\n";
echo "<tr>
<td>Home Page URL:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_url\"
value=\"$usr_URL\"></td>
</tr>\n";
echo "<tr>
<td>*Mailing Address:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_addr\"
value=\"$usr_addr\"></td>
</tr>\n";
echo "<tr>
<td>*Phone #:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_phone\"
value=\"$usr_phone\"></td>
</tr>\n";
echo "<tr>
<td>Expiration date:</td>
<td class=\"left\">
<input type=\"readonly\" name=\"usr_expires\"
value=\"$usr_expires\"</td>
</tr>\n";
}
else {
echo "<tr>
<td>*Username:</td>
<td class=\"left\">
<input type=\"text\" name=\"joining_uid\"
size=$TBDB_UIDLEN maxlength=$TBDB_UIDLEN></td>
</tr>\n";
echo "<tr>
<td>*Full Name:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_name\" size=30></td>
</tr>\n";
echo "<tr>
<td>*Title/Position:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_title\" size=30></td>
</tr>\n";
echo "<tr>
<td>*Institutional<br>Affiliation:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_affil\" size=40></td>
</tr>\n";
echo "<tr>
<td>*Email Address[1]:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_email\" size=30></td>
</tr>\n";
echo "<tr>
<td>Home Page URL:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_url\"
value=\"http://\" size=45></td>
</tr>\n";
echo "<tr>
<td>*Mailing Address:</td>
<td class=\"left\">
<input type\"text\" name=\"usr_addr\" size=40></td>
</tr>\n";
echo "<tr>
<td>*Phone #:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_phone\" size=16></td>
</tr>\n";
$expiretime = date("m/d/Y", time() + (86400 * 90)); #add 90 days
echo "<tr>
<td>Expiration date:</td>
<td class=\"left\">
<input type=\"text\" name=\"usr_expires\" size=10
value=\"$expiretime\"></td>
</tr>\n";
echo "<tr>
<td>*Password[1]:</td>
<td><input type=\"password\" name=\"password1\" size=12></td>
</tr>
<tr>
<td>*Retype Password:</td>
<td><input type=\"password\" name=\"password2\" size=12></td>
</tr>\n";
}
#
# The only common fields!
#
# Note PID max length.
#
echo "<tr>
<td>*Project:</td>
<td class=\"left\">
<input type=\"text\" name=\"pid\"
size=$TBDB_PIDLEN maxlength=$TBDB_PIDLEN></td>
</tr>\n";
#
# Note GID max length.
#
echo "<tr>
<td>Group:<br>
(Leave blank unless you <em>know</em> the group name)</td>
<td class=\"left\">
<input type=\"text\" name=\"gid\"
size=$TBDB_GIDLEN maxlength=$TBDB_GIDLEN></td>
</tr>\n";
?>
<td colspan="2" align="center">
<b><input type="submit" value="Submit"></b></td></tr>
</form>
</table>
<?php
echo "<h4><blockquote><blockquote>
<dl COMPACT>
<dt>[1]
<dd>Please consult our
<a href = 'docwrapper.php3?docname=security.html'>
security policies</a> for information
regarding passwords and email address.
</dl>
</blockquote></blockquote></h4>\n";
#
# Standard Testbed Footer
#
PAGEFOOTER();
?>
<?php
include("defs.php3");
#
# No PAGEHEADER since we spit out a Location header later. See below.
#
#
# Only known and logged in users can begin experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
#
# Default priorities Needs to move someplace else!
#
$priorities = array();
$priorities["high"] = "high";
$priorities["low"] = "low";
#
# Helper function to respit on error.
#
function RESPIT($tag, $value)
{
global $formfields;
$errors[$tag] = $value;
SPITFORM($formfields, $errors);
PAGEFOOTER();
die("");
}
#
# Spit the form out using the array of data.
#
function SPITFORM($formfields, $errors)
{
global $TBDB_PIDLEN, $TBDB_GIDLEN, $TBDB_EIDLEN;
global $nsdata, $projlist, $priorities, $exp_nsfile;
PAGEHEADER("Begin a Testbed Experiment");
if ($errors) {
echo "<table align=center border=0 cellpadding=0 cellspacing=2>
<tr>
<td nowrap align=center colspan=3>
<font size=+1 color=red>
Oops, please fix the following errors!
</font>
</td>
</tr>\n";
while (list ($name, $message) = each ($errors)) {
echo "<tr>
<td align=right><font color=red>$name:</font></td>
<td>&nbsp &nbsp</td>
<td align=left><font color=red>$message</font></td>
</tr>\n";
}
echo "</table><br>\n";
}
echo "<table align=center border=1>
<tr>
<td align=center colspan=3>
<em>(Fields marked with * are required)</em>
</td>
</tr>\n";
echo "<form enctype=multipart/form-data
action=beginexp.php3 method=post>\n";
#
# Select Project
#
echo "<tr>
<td colspan=2>*Select Project:</td>
<td><select name=\"formfields[exp_pid]\">
<option value=''>Please Select &nbsp</option>\n";
while (list($project) = each($projlist)) {
$selected = "";
if (strcmp($formfields[exp_pid], $project) == 0)
$selected = "selected";
echo " <option $selected value=\"$project\">
$project </option>\n";
}
echo " </select>";
echo " </td>
</tr>\n";
#
# Name:
#
echo "<tr>
<td colspan=2>*Name (no blanks):</td>
<td class=left>
<input type=text
name=\"formfields[exp_id]\"
value=\"" . $formfields[exp_id] . "\"
size=$TBDB_EIDLEN
maxlength=$TBDB_EIDLEN>
</td>
</tr>\n";
#
# Description
#
echo "<tr>
<td colspan=2>*Description:<br>
(a short pithy sentence)</td>
<td class=left>
<input type=text
name=\"formfields[exp_description]\"
value=\"" . $formfields[exp_description] . "\"
size=50>
</td>
</tr>\n";
#
# NS file
#
if (isset($nsdata)) {
echo "<tr>
<td colspan=2>*Your Auto Generated NS file: &nbsp</td>
<input type=hidden name=nsdata value=$nsdata>
<td><a target=_blank href=spitnsdata.php3?nsdata=$nsdata>
View NS File</a></td>
</tr>\n";
}
else {
echo "<tr>
<td rowspan>*Your NS file: &nbsp</td>
<td rowspan><center>Upload (20K max)<br>
<br>
Or<br>
<br>
On Server (/proj or /users)
</center></td>
<td rowspan>
<input type=hidden name=MAX_FILE_SIZE value=20000>
<input type=file
name=exp_nsfile
value=\"" . $exp_nsfile . "\"
size=40>
<br>
<br>
<input type=text
name=\"formfields[exp_localnsfile]\"
value=\"" . $formfields[exp_localnsfile] . "\"
size=50>
</td>
</tr>\n";
}
#
# Swappable?
#
echo "<tr>
<td colspan=2>Swappable?[<b>1</b>]:</td>
<td class=left>
<input type=checkbox
name=\"formfields[exp_swappable]\"
value=Yep";
if (isset($formfields[exp_swappable]) &&
strcmp($formfields[exp_swappable], "Yep") == 0)
echo " checked";
echo " > Yes
</td>
</tr>\n";
#
# Priority
#
echo "<tr>
<td colspan=2>Priority[<b>2</b>]:</td>
<td class=left>\n";
reset($priorities);
while (list ($prio, $value) = each($priorities)) {
$checked = "";
if (isset($formfields["exp_priority"]) &&
! strcmp($formfields["exp_priority"], $prio))
$checked = "checked";
echo "<input $checked type=radio value=$prio
name=\"formfields[exp_priority]\">
$prio &nbsp\n";
}
echo " </td>
</tr>\n";
#
# Select a group
#
echo "<tr>
<td colspan=2>Group[<b>3</b>]:</td>
<td><select name=\"formfields[exp_gid]\">
<option value=''>Default Group </option>\n";
reset($projlist);
while (list($project, $grouplist) = each($projlist)) {
for ($i = 0; $i < count($grouplist); $i++) {
$group = $grouplist[$i];
if (strcmp($project, $group)) {
$selected = "";
if (isset($formfields[exp_gid]) &&
isset($formfields[exp_pid]) &&
strcmp($formfields[exp_pid], $project) == 0 &&
strcmp($formfields[exp_gid], $group) == 0)
$selected = "selected";
echo "<option $selected value=\"$group\">
$project/$group</option>\n";
}
}
}
echo " </select>
</td>
</tr>\n";
#
# Batch Experiment?
#
echo "<tr>
<td colspan=2>Batch Experiment?[<b>4</b>]:</td>
<td class=left>
<input type=checkbox
name=\"formfields[exp_batched]\"
value=Yep";
if (isset($formfields[exp_batched]) &&
strcmp($formfields[exp_batched], "Yep") == 0)
echo " checked";
echo " > Yes
</td>
</tr>\n";
echo "<tr>
<td align=center colspan=3>
<b><input type=submit value=Submit name=submit></b></td>
</tr>
</form>
</table>\n";
echo "<blockquote><blockquote><blockquote>
<ol>
<li>Check if your experiment can be swapped out and swapped back
in without harm to your experiment. Useful for scheduling when
resources are tight. More information on swapping
is contained in the
<a href='$TBDOCBASE/faq.php3#UTT-Swapping'>Emulab FAQ</a>.
<li>You get brownie points for marking your experiments as Low
Priority, which indicates that we can swap you out before high
priority experiments.
<li>Leave as the default group, or pick a subgroup that
corresponds to the project you selected.
<li>Check this if you want to create a
<a href='$TBDOCBASE/tutorial/tutorial.php3#BatchMode'>
batch mode</a> experiment.
</ol>
</blockquote></blockquote></blockquote>\n";
echo "<p><blockquote>
<ul>
<li> Please <a href='nscheck_form.php3'>syntax check</a> your NS
file first!
<li> You can view a <a href='showosid_list.php3'>list of OSIDs</a>
that are available for you to use in your NS file.
<li> Create your own <a href='newimageid_explain.php3'>
custom disk images</a>.
</ul>
</blockquote>\n";
}
#
# See if nsdata was provided. Clear it if an empty string, otherwise
# reencode it *only* if not from the form. It appears that php decodes
# it for you when it comes in as GET argument, but leaves it encoded
# when its a POST argument.
#
if (isset($nsdata)) {
if (strcmp($nsdata, "") == 0)
unset($nsdata);
elseif (! isset($submit))
$nsdata = rawurlencode($nsdata);
}
#
# See what projects the uid can create experiments in. Must be at least one.
#
$projlist = TBProjList($uid, $TB_PROJECT_CREATEEXPT);
if (! count($projlist)) {
USERERROR("You do not appear to be a member of any Projects in which ".
"you have permission to create new experiments.", 1);
}
#
# On first load, display a virgin form and exit.
#
if (! isset($submit)) {
$defaults = array();
$defaults[exp_swappable] = "Yep";
$defaults[exp_priority] = "low";
SPITFORM($defaults, 0);
PAGEFOOTER();
return;
}
#
# Otherwise, must validate and redisplay if errors
#
$errors = array();
#
# Project:
#
if (!isset($formfields[exp_pid]) ||
strcmp($formfields[exp_pid], "") == 0) {
$errors["Project"] = "Not Selected";
}
elseif (!TBValidProject($formfields[exp_pid])) {
$errors["Project"] = "No such project";
}
#
# EID.
#
if (!isset($formfields[exp_id]) ||
strcmp($formfields[exp_id], "") == 0) {
$errors["Experiment Name"] = "Missing Field";
}
else {
if (! ereg("^[-_a-zA-Z0-9]+$", $formfields[exp_id])) {
$errors["Experiment Name"] = "Must be alphanumeric characters only!";
}
elseif (strlen($formfields[exp_id]) > $TBDB_EIDLEN) {
$errors["Experiment Name"] =
"Too long! Must be less than or equal to $TBDB_EIDLEN";
}
}
#
# Description
#
if (!isset($formfields[exp_description]) ||
strcmp($formfields[exp_description], "") == 0) {
$errors["Description"] = "Missing Field";
}
#
# Swappable
#
if (isset($formfields[exp_swappable]) &&
strcmp($formfields[exp_swappable], "") &&
strcmp($formfields[exp_swappable], "Yep")) {
$errors["Swappable"] = "Bad Value";
}
#
# Priority
#
if (isset($formfields[exp_priority]) &&
strcmp($formfields[exp_priority], "") &&
! isset($priorities[$formfields[exp_priority]])) {
$errors["Priority"] = "Bad Value";
}
#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
#
if (count($errors)) {
SPITFORM($formfields, $errors);
PAGEFOOTER();
return;
}
$exp_name = addslashes($formfields[exp_description]);
$exp_pid = $formfields[exp_pid];
$exp_id = $formfields[exp_id];
if (isset($formfields[exp_gid])) {
$exp_gid = $formfields[exp_gid];
}