#!/usr/bin/perl

# QuotesSubmit, by Synecdochic. Heavily hacked up based on the 
# predecessor I wrote years ago, when I still knew Perl.

# get this out of the way first off, so I don't forget...
print "Content-type:text/html\r\n\r\n";

# Okay, idea here: take input, load it into a flatfile divided
# by the HTML necessary to format it in pretty ways. Easiest to
# do the CSS in the cgi script, rather than mucking around with
# all the crap, and then load the quotes page in via SHTML insert
# afterwards.

# We start by taking the cgi input, which (good Lord willin') is 
# a quote someone wants to add to the form. I will be making 
# regular checks on the output to ensure nobody's vandalized
# the page.

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair); 
    $value =~ tr/+/ /; # split up the words from output
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/\</&lt\;/g;  # Letting HTML through would be 
    $value =~ s/\>/&gt\;/g;  #      a very dumb thing to do.
    $value =~ s/\r/\<br\>/g;  # change hard returns to <BR>
    $value =~ s/\cM//g; # delete ^M's

    $FORM{$name} = $value;
}

# And open up the quote file.

open(OUTF,">>q-boys.txt") or die ("Something's wrong with the quotefile: $!"); # open the file

# make sure that nothing else is writing to this right now:
flock(OUTF,2); 
seek(OUTF,0,2);

# All Praise (tm) to Mark for finding my bonehead error:

print OUTF "<div class='quote'>$FORM{'quote'}<br />-- $FORM{'attribution'}</div><br /><br />\r\n";
close(OUTF);

# Okay, and now let's make a thank-you page:

print "<html><head><title>Thank you!</title><link rel=\"stylesheet\" href=\"http://www.kekkai.org/synecdochic/reunion/reunionstyle.css\"></head><body><div id=\"content\"><div class=\"topstrip\">thank you!</div><div class=\"sidebar\"><div class=\"menu\">+ <a href=\"/synecdochic/reunion/index.shtml\">about</a><br>+ <a href=\"/synecdochic/reunion/rules.shtml\">rules</a><br>+ <a href=\"/synecdochic/reunion/signup.shtml\">signup</a><br>+ <a href=\"/synecdochic/reunion/authors.shtml\">authors</a><br>+ <a href=\"/synecdochic/reunion/stories.shtml\">stories</a><br>+ <a href=\"/synecdochic/reunion/quotes.shtml\">quotes</a><br>&nbsp;&nbsp;&nbsp;* <a href=\"/synecdochic/reunion/q-boys.shtml\">boys</a><br>&nbsp;&nbsp;&nbsp;* <a href=\"/synecdochic/reunion/q-songs.shtml\">songs</a><br>&nbsp;&nbsp;&nbsp;* <a href=\"/synecdochic/reunion/q-misc.shtml\">other</a><br>+ <a href=\"mailto:synecdochic\@kekkai.org\">contact</a><br></div></div><p class=\"plain\">Thank you! Your quote has been added. (Please don't refresh this page, or your quote will double-post.)</p>"; 
print "</div></body></html>";
