#!/usr/local/bin/perl

if ($ENV{REQUEST_METHOD} eq "GET") {
    $data=$ENV{QUERY_STRING};
} elsif ($ENV{REQUEST_METHOD} eq "POST") {
    for ($i=0;$i<$ENV{CONTENT_LENGTH};$i++) {
	$data .=getc;
    }
}

@data=split(/&/, $data);

if ($#data > 1) {

    $ok = 0;

    foreach $item (0..5) {

	$data[$item] = substr($data[$item], index($data[$item], '=') + 1);
	$data[$item] =~ s/\+/ /g;
	$data[$item] =~ s/%(..)/pack("c",hex($1))/ge;
	
    }

    if (($data[0] ne '') && ($data[1] ne '') && ($data[2] ne '') && ($data[5] ne '')) {

	open(OUT, ">> guestbook");
	print(OUT join('&', @data), "\n");
	close(OUT);

    }

}

print(<<BACK_TO_PERL);
Content-type: text/html

<html>
<head>
<title>Guestbook</title>
</head>
<body>
<h1>Guestbook</h1>
<h2>Add Yourself:</h2>
<form method=post action="/cgi-bin/safeperl/kebl0110/guestbook">
Your name<br><input type="text" name="name" size=16><p>
The town where you live<br><input type="text" name="town" size=16><p>
The country where you live<br><input type="text" name="country" size=12><p>
Your email address, if you have one<br><input type="text" name="email" size=72><p>
Your web page, if you have one<br><input type="text" name="web" size=72><p>
Your favourite game show<br><input type="text" name="show" size=16><p>
<input type="submit" value="Add Yourself to the Guestbook"> <input type="reset" value="Clear your inputs from the form">
</form>
<h2>The Guestbook</h2>
BACK_TO_PERL

open(IN, "/u4/uzke/kebl0110/cgi/out/guestbook");

if ($ENV{HTTP_USER_AGENT} =~ /ozilla/) {

    print("<table border cellpadding=5><tr><th>Name</th><th>Town</th><th>Country</th><th>Email</th><th>WWW</th><th>Favourite Game Show</th></tr>\n");

    while ($line = <IN>) {

	chop($line);
	@line = split('&', $line);
	print("<tr><td>$line[0]</td><td>$line[1]</td><td>$line[2]</td><td>");

	if ($line[3] ne '') {

	    print("<a href=\"mailto:$line[3]\">email</a>");

	}

	print("</td><td>");

	if ($line[4] ne '') {

	    print("<a href=\"$line[4]\">WWW</a>");

	}

	print("</td><td>$line[5]</td></tr>\n");

    }

    print("</table>\n");

} else {

    while ($line = <IN>) {

	chop($line);
	@line = split('&', $line);
	print("Name: $line[0]");

	if ($line[3] ne '') {

	    print("<a href=\"mailto:$line[3]\"> [ email ]</a>");

	}

	if ($line[4] ne '') {

	    print("<a href=\"$line[4]\"> [ WWW ]</a>");

	}

	print("<br>\nLives in $line[1], $line[2]<br>\nFave Game Show: $line[5]<p>\n");

    }

}

close(IN);

print(<<BACK_TO_PERL);
<hr>
With many thanks to <a href="/~kebl0206">Mark Rigby-Jones</a> for knowing more interesting things than me.<br>
Back to <a href="/~kebl0110/GS/gs.html">The UK Game Show Page</a>
</body>
</html>
BACK_TO_PERL
