|
CSCI A348/548
|
Register for a project before the due date. Here's the code for the mock-up that we are using now and installation instructions:
Location: /u/dgerman/httpd/cgi-bin/projects
Files:
registration (the script)
people (datafile, contains students, passwords, project number registered for)
projects (numbers of projects, with descriptions)
semaphore (used to make sure one process works at a time with the file)
people
rnajlis:333998888:0 xuedeng:222887777:0 dgerman:dgerman:1
projects
1::Videconferencing with Java Media Framework. 2::On-line Chef. 3::Employee tracker. 4::Your <code><font size=+3>a</font>ma<font size=+3>z</font>on.com</code> 5::On-line postcards. 6::Project registration site. 7::Library check-in/check-out tracking and reservation system. 8::Your own QuizSite. 9::Shopping mall item locator. 10::Dynamic map server. 11::Other (choose, and let us know what).
semaphore
this is a semaphore
registration
#!/usr/bin/perl
open (MBOX, "/u/dgerman/httpd/cgi-bin/projects/semaphore");
flock MBOX, 2;
&readParse;
&printHeader;
&process;
&printTrailer;
flock MBOX, 8;
close(MBOX);
sub readParse {
if ($ENV{REQUEST_METHOD} eq 'GET') {
$in = $ENV{QUERY_STRING};
} elsif ($ENV{REQUEST_METHOD} eq 'POST') {
read(STDIN, $in, $ENV{CONTENT_LENGTH});
} else {
print "Method not supported."; &printTrailer; exit;
}
@in = split(/&/, $in);
foreach $elem (@in) {
$elem =~ s/\+/ /g;
($name, $value) = split(/=/, $elem);
$name =~ s/%(..)/chr(hex($1))/ge;
$value =~ s/%(..)/chr(hex($1))/ge;
$in{$name} = $value;
}
}
sub process {
if ($ENV{REQUEST_METHOD} eq 'GET') {
&print_form;
} elsif ($ENV{REQUEST_METHOD} eq 'POST') {
$in{uname} =~ tr/[A-Z]/[a-z]/;
$in{pword} =~ tr/[A-Z]/[a-z]/;
if ($in{state} eq "one") {
open (AB, "/u/dgerman/httpd/cgi-bin/projects/people");
$found = "no";
$uname = $in{uname};
$pword = $in{pword};
while ($x = <AB>) {
if ($x =~ /^$uname:$pword:/i) {
$found = "yes";
}
}
close(AB);
if ($found eq "yes") {
&show_projects;
} else {
print "Username or student ID invalid. <p>";
print "Please back up and try again. ";
}
} elsif ($in{state} eq "two") {
$project = $in{project};
$uname = $in{uname};
open (AB, "/u/dgerman/httpd/cgi-bin/projects/people");
@file = <AB>;
foreach $x (@file) {
if ($x =~ /^$uname:/) {
@x = split(/:/, $x);
$x[$#x] = $project;
$x = join(':', @x);
}
}
close(AB);
open (AB, ">/u/dgerman/httpd/cgi-bin/projects/people");
foreach $x (@file) {
$x =~ s/\s//g;
print AB $x, "\n";
}
close(AB);
open (UGH, "/u/dgerman/httpd/cgi-bin/projects/people");
while ($x = <UGH>) {
@x = split(/:/, $x);
$uname = $x[0];
$x[2] =~ s/\s//g;
$relationship{$uname} = $x[2];
}
close(UGH);
open (AB, "/u/dgerman/httpd/cgi-bin/projects/projects");
print "<table>";
while ($x = <AB>) {
$i += 1;
@x = split(/::/, $x); $index = $x[0]; $descr = $x[1];
print qq{ <tr> <td valign=top align=right>
<font color=blue>$i.</font>
</td> <td> $descr </td> </tr> };
$list = "";
foreach $key (keys %relationship) {
if ($relationship{$key} == $index) {
$list .= "<li> $key\n";
}
}
if ($list) {
print "<tr> <td> </td> <td> <ol> $list </ol> </td> </tr> ";
}
}
close(AB);
print "</table>";
$uname = $in{uname}; $pword = $in{pword};
print qq{ <input type="hidden" name="uname" value="$uname">
<input type="hidden" name="pword" value="$pword">
<input type="submit" value="Proceed">
<input type="hidden" name="state" value="two">
</form>
};
} else {
}
} else {
}
}
sub printHeader {
print "Content-type: text/html\n\n<html>";
print qq{<head><title>A348 Project Registration</title>
</head><body bgcolor=white>};
}
sub printTrailer { print "</body></html>"; }
sub print_form {
print qq{
Welcome to A348 Project Registration! <p>
Please enter your username and student ID below. <p>
<form method="POST" action="/cgi-bin/projects/registration">
<table border cellpadding=3 bgcolor=lightgrey>
<tr> <td> Username: </td> <td>
<input type="text" name="uname"> </td> </tr>
<tr> <td> Password: </td> <td>
<input type="password" name="pword"> </td> </tr>
</table> <p>
Then press <input type="submit" value="Proceed">
<input type="hidden" name="state" value="one">
</form> <p>
<img src="http://www.best.indiana.edu/left.gif"> If your
student ID does not work try your username as the password as well.
};
}
sub show_projects {
print qq{
Here are the projects, together with associated participants: <p>
Please register your preference below: <p>
};
print "<form method=POST action=/cgi-bin/projects/registration>\n";
open (UGH, "/u/dgerman/httpd/cgi-bin/projects/people");
while ($x = <UGH>) {
@x = split(/:/, $x);
$uname = $x[0];
$x[2] =~ s/\s//g;
$relationship{$uname} = $x[2];
}
close(UGH);
open (AB, "/u/dgerman/httpd/cgi-bin/projects/projects");
print "<table>";
while ($x = <AB>) {
@x = split(/::/, $x); $index = $x[0]; $descr = $x[1];
print qq{ <tr> <td valign=top> <input type="radio" name="project"
value="$index"> </td> <td> $descr </td> </tr> };
$list = "";
foreach $key (keys %relationship) {
if ($relationship{$key} == $index) {
$list .= "<li> $key\n";
}
}
if ($list) {
print " <tr> <td> </td> <td> <ol> $list </ol> </td> </tr> ";
}
}
close(AB);
print "</table>";
$uname = $in{uname}; $pword = $in{pword};
print qq{ <input type="hidden" name="uname" value="$uname">
<input type="hidden" name="pword" value="$pword">
<input type="submit" value="Proceed">
<input type="hidden" name="state" value="two">
</form>
};
}
A348/A548