#!/usr/bin/perl -Tw

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);


my $DATAFILE = '/home/fallen/ham.andcheese.org/potluck-data';
my $query = new CGI;

print $query->header();

print <<EOT;
<html>
    <head>
    <title>
    Ceremony of Union RSVP/Potluck menu
    </title>
    </head>
<body background="0awhtbg.gif">
<center><img src="5um7.gif" width="372" height="71"></center>
<br><br>
Welcome to Wendy and Aaron&lsquo;s Ceremony of Union RSVP/Potluck menu page.
Here you can RSVP and let us know what you are planning on
bringing to the event.<br><br>

<form METHOD="post" ACTION="potluck.pl">
Name(s):<br>
<input TYPE="text" NAME="who" SIZE="80"><br><br>

What you want to bring to the potluck:<br>

<input TYPE="text" NAME="what" SIZE ="80"><br><br>

<input TYPE="submit" VALUE="Submit Dish" LABEL="Submit Dish"><br><br>
</form> 

The List:<br>

    <table border='1'>
    <tr>
    <th>Name</th>
    <th>Dish</th>
    </tr>

EOT


my $fh;
open ($fh, "<$DATAFILE");	# no failure check - intentional
while (<$fh>)
{
    /(.*?)\|(.*)$/;
    my ($who, $what) = ($1, $2);
    my $cells = $query->td($who);
    $cells .= $query->td($what);
    $cells .= "\n";
    print $query->Tr($cells);
}

close $fh;

# Done outputting stuff. If we've been submitted to, do something

if ($query->param('who'))	# we got submitted
{
    $fh = undef; 
    open($fh, ">>$DATAFILE");
    if (!$fh)
    {
	print $query->p("Your submission could not be accepted at this time. "
			. "This script was unable to open its data file. "
			. "The error is: $!");
    }

    # some sanitization
    my $who = $query->param('who');
    my $what = $query->param('what');
    strip_all_html(\$who);
    strip_all_html(\$what);
    $who =~ s/\|//g;
    $what =~ s/\|//g;

    if (($who =~ /\S/) && ($what =~ /\S/))
    {
	print $fh "$who|$what\n";
	my $cells = $query->td($who);
	$cells .= $query->td($what);
	$cells .= "\n";
	print $query->Tr($cells);
    }
    close $fh; 
}

print "</table></body></html>";



    
    

#########################################################################

# Removes all HTML tags.
# Arguments:
# $string: string to strip tags from
# Returns: stripped string
sub strip_all_html
{
    my ($strref) = @_;
    $$strref =~ s/<.*?>//g;
    return 1;
}

#########################################################################
