#!/usr/local/bin/perl

%results=parse_form();

sub parse_form {
if ($ENV{'REQUEST_METHOD'} eq 'GET')
{
        @pairs = split(/&/,
             $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
        @pairs = split(/&/, $buffer);
} else {
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
        $key =~ tr/+/ /;
        $key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
        $key =~ tr/+/ /;
        $key =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
        $value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
        $formdata{$key} .= ", $value";
} else {
        $formdata{$key} = $value;
}
}
}
1;

sub errormessage {
print "Content-type: text/html\n\n";
print "The server can't open the file.\n";
exit;
}
if(!%results){
$number=$formdata{'social'};
open(LOG, ">>../logfile1.txt") || errormessage();
flock(LOG, 2);
print LOG "$number\n";
flock(LOG, 8);
close(LOG);

print "Content-type: text/html\n\n";
print<<BEGIN
<html><head><title>Shana's Database </title></head>
<body bgcolor="black" text="white" link="white" >
<form method="post">
<h1>Social Security Number 
<input type="text" name="social" value=$formdata{$social} >
<br><br>
<INPUT TYPE="submit" VALUE="SUBMIT" NAME="submit">
<INPUT TYPE="reset" VALUE="Start Over" NAME="startover"><br>
<br>
<a href="../3832.html"> <font color="white">GO BACK HOME </a></font>
</body></html>
BEGIN

}else{

print "Content-type: text/html\n\n";
print "Set-Cookie:information= $formdata{'$key'}\n";
print<<BEGIN
<html><head><title>Shana's Database </title></head>
<body bgcolor="black" text="white">
<form method="post">
<h1>Social Security Number
<input type="text" name="social" value=$formdata{'social'} >
<br><br>
<INPUT TYPE="submit" VALUE="Order Bed" NAME="submit">
<INPUT TYPE="reset" VALUE="Start Over" NAME="startover">
</body></html>
BEGIN
}
}
