#!/usr/bin/perl

# Import common form checking subroutine
require "/secure/i/invisible/cgi/form_check.pl";

$thisscript="sendmail.pl";
$mailprog = '/usr/sbin/sendmail';

# Get form data
&decode_form;

# Check form data for abuse
&check_data("Invisible send form (sendmail.pl)", 'commentsfield');

print "Content-type: text/html\r\n\r\n";


#$recipient="stephen\@room101.co.uk";
$recipient="info\@invisibleink.co.uk";

$recipientname="Info";

$subject="INVISIBLE INK WEB ENQUIRY";

# Only send mail if form data is clean and email address parses
if ((!$formdata_blacklisted) && ($FORM{'emailaddress'} =~ /^[0-9A-Za-z\.\-\_]+\@[0-9A-Za-z\.\-]+\.[a-zA-Z]{2,4}$/))
{
$fromemail = "$FORM{emailaddress}";
$fromname = "$FORM{personsname}";
open (MAIL, "| $mailprog $recipient");

#open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
print MAIL "From: \"$fromname\" <$fromemail>\n";
print MAIL "To: \"$recipientname\" <$recipient>\n";
print MAIL "Reply-to: \"$fromname\" <$fromemail>\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$FORM{personsname} has filled out the INVISIBLE INK web form\n\n";
print MAIL "Name:               $FORM{personsname}\n";
print MAIL "Email:              $FORM{emailaddress}\n";
print MAIL "Phone:              $FORM{phonenumber}\n";
print MAIL "I am interested in: $FORM{checkboxName}\n";
print MAIL "Comments:           $FORM{commentsfield}\n";
print MAIL "How did you hear about us?:           $FORM{hear}\n";

close MAIL;
}

$thefile = openfile("enquiry_thanks.html");
print $thefile;

sub openfile {

        #USEAGE: $thefile = openfile("thefile.html");

        local($filepath) = @_;

        if ( !(-e $filepath) ) { return "File doesn't exist"; }

        $/ = undef;
open( THEFILE, $filepath ) || die ( "Can't open $thefile" );

        $bigscalar = <THEFILE>;

        close( THEFILE );

        $/ = "\n";

        return $bigscalar;

}

sub decode_form {
        $decoded=1;
        (*fval) = @_ if @_ ;

        local ($buf);
        if ($ENV{'REQUEST_METHOD'} eq 'POST') {
                read(STDIN,$buf,$ENV{'CONTENT_LENGTH'});
        }
        else {
                $buf=$ENV{'QUERY_STRING'};
        }
        if ($buf eq "") {
                        return 0 ;
                }
        else {
                @fval=split(/&/,$buf);
                foreach $i (0 .. $#fval){
                        ($name,$val)=split (/=/,$fval[$i],2);
                        $val=~tr/+/ /;
                        $val=~ s/%(..)/pack("c",hex($1))/ge;
                        $name=~tr/+/ /;
                        $name=~ s/%(..)/pack("c",hex($1))/ge;
                        if (!$val){ next; }#ie if empty (if we dont do this, a multiple named field will become something like ",,,"
                        if (!defined($FORM{$name})) {
                                $FORM{$name}=$val;
                        }
                        else {
                                $FORM{$name} .= ",$val";

                                #if you want multi-selects to goto into an array change to:
                                #$FORM{$name} .= "\0$val";
                        }



                   }
                }
return 1;
}
