#/usr/bin/perl
#c09ex3.cgi - saves form data to a file, and creates
#three different dynamic Web pages
print "Content-type: text/html\n\n";
use CGI qw(:standard);
use SDBM_File;
use Fcntl;

#prevent Perl from creating undeclared variables
use strict;

#declare variables
my ($name, $email, $comments, $data_ok, $msg);

if ($ENV{'REQUEST_METHOD'} eq "POST") {
        ($name, $email, $comments) = get_input();
        ($name, $email, $comments) = format_input();
        ($data_ok, $msg) = validate_input();
        if ($data_ok eq "Y") {
                save_to_file();
                create_acknowledgment_page();
        }
        else {
                create_error_page();
        }
}
else {
        create_comments_page();
}
exit;

#*****user-defined functions*****
sub get_input {
        return param('Name'), param('Email'), param('Comments');
} #end get_input

sub format_input {
    #declare and assign values to temporary variables
    my ($n, $e, $c);
    ($n, $e, $c) = ($name, $email, $comments);
    #remove leading and trailing spaces from name
    $n =~ s/^ +//;
    $n =~ s/ +$//;
    #remove leading and trailing spaces from email address
    $e =~ s/^ +//;
    $e =~ s/ +$//;
    #remove leading and trailing whitespace characters from comments
    $c =~ s/^\s+//;
    $c =~ s/\s+$//;
    #replace return and newline combination within comments with a space
    $c =~ tr/\r\n/ /;
    #remove extra spaces from within comments
    $c =~ tr/ //s;
    return $n, $e, $c;
    } #end format_input

sub validate_input {
        my $valid = "Y";
        my $errormsg;
        if ($name eq "" or $email eq "" or $comments eq "") {
                $valid = "N";
                $errormsg = "complete all items";
        }
        elsif ($email !~ m/[\w\-]+\@[\w\-]+\.[\w\-]+/) {
                $valid = "N";
                $errormsg = "enter a valid e-mail address";
        }
        return $valid, $errormsg;
} #end validate_input

sub save_to_file {
        #declare variable
        my %mail;

        #open database, add record, close database
        tie(%mail, "SDBM_File", "c09ex3", O_CREAT|O_RDWR, 0666)
             or die "Error opening c09ex3. $!, stopped";
        $mail{$email} = "$name|$comments";
        untie(%mail);
} #end save_to_file

sub create_acknowledgment_page {
        print "<HTML>\n";
        print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
        print "<BODY>\n";
        print "<H2>$name, thank you for the following \n";
        print "comments:<BR><BR>$comments\n";
        print "</H2></BODY></HTML>\n";
} #end create_acknowledgment_page

sub create_error_page {
        print "<HTML>\n";
        print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
        print "<BODY>\n";
        print "<H2>Please return to the form and \n";
        print "$msg.</H2>\n";
        print "</BODY></HTML>\n";
} #end create_error_page

sub create_comments_page {
        my (%mail, $email, $name, $comments);

        tie(%mail, "SDBM_File", "c09ex3", O_RDONLY, 0)
             or die "Error opening c09ex3. $!, stopped";
        $mail{$email} = "$name|$comments";
        untie(%mail);

        print "<HTML>\n";
        print "<HEAD><TITLE>International Coffees</TITLE></HEAD>\n";
        print "<BODY>\n";
        print "<H2>What other coffee lovers say \n";
        print "about our coffees:</H2>\n";
        foreach ($mail{$email}) {
        print "$name said $comments\n";
        }
        print "</BODY></HTML>\n";
} #end create_comments_page