#!/usr/bin/perl
#-*- perl -*-
#
# Author:  Lucas Pereira (lucasp@cs.stanford.edu)
#    based on mailform.cgi, by      
#          Andrew Beers <beers@cs.stanford.edu>
#
# Form processing script that will add/remove a person from a demo
# schedule slot.  See other scripts in $ROOTDIR
#
# Usage: <form action=/cgi-bin/scheduledemo.cgi> 
#

# Assume that it's always going to output html text, even if
# an error...
print "Content-type:  text/html\n\n";

if ($#ARGV != 3) {
    print "Error.  cgi script called with only ".($#ARGV+2)." arguments!\n";
    exit(-1);
}

# Where data, other scripts live....
$ROOTDIR  = $ARGV[0];
$DATANAME = $ARGV[1];
$TIME = $ARGV[2];
$SLOT = $ARGV[3];

# Make sure method and type are Posting a Form...
#$method = $ENV{'REQUEST_METHOD'};
#$type = $ENV{'CONTENT_TYPE'};
#if($method ne "POST") {
#    print "This script only accepts POST input...\n";
#    exit(-1);
#}
#if($type ne "application/x-www-form-urlencoded") {
#    print "This script only accepts form results...\n";
#    exit(-1);
#}

# Check out the argument to make sure this isn't a security risk
$semi = grep(/;/,$ARGV[0]);
if($semi != 0) {
    printf "<html>\n";
    printf "<head>\n<title>Security Violation</title>\n</head>\n\n";
    printf "<body>\n";
    printf "<h1>Improper use of mailform</h1>\n";
    printf "Your attempt to violate the integrity of this server by using ";
    printf "a common security hole has failed.  Your attempt has been logged ";
    printf "and reported to the server's administrator.<p>\n";
    printf "Please do not attempt this again.<p>\n";
    printf "\n</body>\n</html>\n";
    exit 0;
}

$_ = <STDIN>;
split('&');

# Figure out the current value of the time slot...
# /usr/graphics/web/content/docs/courses/cs248-00/demotimes/
open(IN, "proj.times");
while ($line = <IN>) {
    ($keyword, $nslots, $time, @rest) = split(' ', $line);
    if ($keyword eq ".slots" && $time eq $TIME) {
	for ($i = 0; $i <= $SLOT; $i++) {
	    $line = <IN>;
	    $SUCCESS = 1;
	    last if (substr($line,0,1) eq ".");
	    if ($i == $SLOT) {
		($oldgroup, $oldpeople, $oldplatform) = 
		    split('\|', $line);
	    }
	}
	last;
    }
}
close IN;
if (!$SUCCESS) {
    print "Error: Did not find the requested time slot!\n";
    exit(-1);
}

print "<html>\n";
print "<head>\n<title>cs248 Signup Slot</title>\n</head>\n\n";
print "<body>\n";
print "<h1> cs248 Signup Slot -- $TIME </h1> \n";


# print warning
if (defined($oldpeople)) {
    print "<h2> Warning: Editing an existing slot</h2>\n";
    print "Previous group name: $oldgroup<br>\n";
#    print "Previous People: $oldpeople<br>\n";
#    print "Previous Platform: $oldplatform<br>\n";
    print "Do not proceed unless you are listed above!\n";
}

# New form
print "<h2> New Data</h2>\n";

print "

<p>


<FORM method=POST 
      action=scheduledemo3.cgi?$ROOTDIR+$DATANAME+$TIME+$SLOT>
Name: <br>
<INPUT TYPE=TEXT SIZE=30 NAME=GROUP VALUE=\"$oldgroup\"> <BR>

<!-- All People in the Group:<br>
<INPUT TYPE=TEXT SIZE=70 NAME=PEOPLE VALUE=\"$oldpeople\"> <BR>
Anticipated Platform:<br>
<INPUT TYPE=TEXT SIZE=70 NAME=PLATFORM VALUE=\"$oldplatform\"> <BR>
-->

<input type=checkbox name=remove value=remove> 
        Remove project from this time slot <br>
<p>
<INPUT TYPE=SUBMIT VALUE=\"Submit\">
<p>
<INPUT TYPE=RESET VALUE=\"Clear\">

 

</FORM>

<p>
If you are having problems with this form, please contact
cs248-aut0506-tas\@lists.stanford.edu.

"


