#!/usr/bin/perl
###########################################################################
# Formmail - the new release of newmail.                                  #
# This is an adaptive form of the newmail, Adding in previous features    #
# With others, origanol taken from newmail.                               #
# Modified by Bob                                                         #
# Modified 25 July 2001                                                   #
###########################################################################

    ##################################################
    # Variables                                      #
    #                                                #
    ##################################################

use CGI qw(param);
use CGI 2.47 qw(:standard);
use File::Basename;
use HTML::Entities qw(encode_entities);
use Mail::Internet;
use Mail::Cclient qw(set_callback);
use MIME::Entity;
use IO::File;
use MIME::Parser;
use Net::SMTP;
use Time::CTime;
use Time::ParseDate;
use URI::Escape;
use URI::WithBase;
use CGI::Carp qw(fatalsToBrowser);

$mailprog="/usr/lib/sendmail -t";

$version	= "2.02B";

    ##################################################
    # Inital Stuff                                   #
    #                                                #
    ##################################################

# &readform;
&variable_check;

    ##################################################
    # Backup Form Reader                             #
    #                                                #
    ##################################################

sub readform
{
  if ($ENV{'REQUEST_METHOD'} eq 'GET') {
    @pairs = split(/&/, $ENV{'QUERY_STRING'});
  } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
  } else {
    print "Content-type: text/html\n\n";
    print "<p>Use Post or Get<br>\n";
  }

  foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/<!--(.|\n)*-->//g;
    if ($value =~ "%3C") { $value = ""; }
    if ($form{$name}) {
      $form{$name} .= ", $value";
    } else {
      push @names,$name;
      push @values,$value;

#      $form{$name} = $value;
    }
  }
}

    ##################################################
    # Get Date Info                                  #
    #                                                #
    ##################################################

sub getdate {

@days	= ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months = ('01','02','03','04','05','06','07','08','09','10','11','12');
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year 	= $year+1900;
$date 	= "$days[$wday] $months[$mon]/$mday/$year";
$time	= "$hour:$min";

}

    ##################################################
    # Variable check - check sanity                  #
    #                                                #
    ##################################################

sub variable_check {

$q = new CGI;

&getdate;

$mailhead	= $q -> param('mailhead');
$mailformat	= $q -> param('mailformat');
$mailto         = $q -> param('mailto');
$mailfrom       = $q -> param('mailfrom');
$mailsubject    = $q -> param('mailsubject');
$message        = $q -> param('message');
$linkto         = $q -> param('linkto');
$mailhide       = $q -> param('mailhide');
$mailoptions    = $q -> param('mailoptions');
$mailnoqueue    = $q -> param('mailnoqueue');
$decision       = $q -> param('decision');
$remote_add     = $ENV{'REMOTE_ADDR'};
$remote_browser	= $ENV{'HTTP_USER_AGENT'};
$remote_script	= $ENV{'SCRIPT_NAME'};
$remote_htt	= $ENV{'HTTP_REFERER'};

$delimiter      = "fdesswqa";

    ##################################################
    # Check sanity - Ensure all variables are        #
    # correct, and to work out environment           #
    #                                                #
    ##################################################

   # Check essential stuff
   if ($mailto eq "") {
print "Content-type: text/html\n\n";
print "<br><br><Br><br><br>";
print "Im sorry the server hit a problem and threw a wobbly. <br>\n";
print "Dont worry the administrator has been emailed and will try and sort\n";
print "If you have any problems, please email support\@argonet.co.uk\n";   
exit;
   }

   # Check email
   if ($mailfrom =~ m/.\@./) {
      $good =1;
      } else {
      $mailfrom = "null.user\@argonet.co.uk";
      }

   # Check Mail Header
   if ($mailhead eq "yes") {
   $mhead	= "The following email was sent from  $remote_add\n";
   $mhead	.= "On $date at $time \n";
   $mhead	.= "Using the following browser : $remote_browser \n";
   $mhead	.= "With the following script : $remote_script  \n";
   $mhead	.= "from the following page : $remote_htt \n\n";
   $mhead	.= "The information entered on your website includes the following\n";
   } else {
   $mhead	= " \n ";
   }

   # Check the MailHide Option
   if ($mailhide eq "system") {
   $mail_hide_var = "1";
   } else {
   $mail_hide_var = "0";
   }

   # Check Mail Format
   if ($mailformat eq "table") {
   &send_table;
   } elsif ($mailformat eq "line") {
   &send_liner;
   } else {
   &send_generic;
   }
}

sub send_generic {

open (MAIL2,"|$mailprog") or &error("unable to open main Mail Program.");
print MAIL2 "To: $mailto\n";
print MAIL2 "From: $mailfrom\n";
print MAIL2 "Subject: $mailsubject\n";

print MAIL2 "\n";
print MAIL2 "$mhead \n";

for $i (param()) {

   if ($mail_hide_var eq "1" && ($i eq "mailto" || $i eq "mailhide" || $i eq "mailformat" || $i eq "mailoptions" || $i eq "linkto" || $i eq "mailhead")) {
   $good2 ="1";
   } else {

   $temp_var	= $q -> param($i);
   print MAIL2 "$i\:\n";
   print MAIL2 "$temp_var \n ";
   print MAIL2 " \n ";

   }

}
close (MAIL2);
&thank_you;
}

sub send_liner {
 
open (MAIL2,"|$mailprog") or &error("unable to open main Mail Program.");
print MAIL2 "To: $mailto\n";
print MAIL2 "From: $mailfrom\n";
print MAIL2 "Subject: $mailsubject\n";
 
print MAIL2 "\n";
print MAIL2 "$mhead \n";
 
for $i (param()) {
 
   if ($mail_hide_var eq "1" && ($i eq "mailto" || $i eq "mailhide" || $i eq "mailformat" || $i eq "mailoptions" || $i eq "linkto" || $i eq "mailhead")) {
   $good2 ="1";
   } else {
 
   $temp_var    = $q -> param($i);
   print MAIL2 "$i \: $temp_var \n";
 
   }
 
}
 
close (MAIL2);
 
&thank_you;
}

sub send_table {

open (MAIL2,"|$mailprog") or &error("unable to open main Mail Program.");
print MAIL2 "To: $mailto\n";
print MAIL2 "From: $mailfrom\n";
print MAIL2 "Subject: $mailsubject\n";

print MAIL2 "\n"; 
print MAIL2 "$mhead \n";

print MAIL2 "Variable  \t \| \t Result\n\n";
 
for $i (param()) {
#  push @var, $i, param($i), $delimiter;
#  push @array, param($i), $delimiter;
 
#  push @param_name, $i;
#  push @param_vari, $q -> param($i), $delimiter;
#  push @param_name, $i;
#  push @param_vari, $q -> param($i), $delimiter;
 
   if ($mail_hide_var eq "1" && ($i eq "mailto" || $i eq "mailhide" || $i eq "mailformat" || $i eq "mailoptions" || $i eq "linkto" || $i eq "mailhead")) {
   $good2 ="1";
   } else {
 
   $temp_var       = $q -> param($i);
   print MAIL2 "$i \t \| \t $temp_var \n ";

   } 
 
}
 
close (MAIL2);

&thank_you;


}

    ##################################################
    # Error Script for taking care of problems       #
    #                                                #
    ##################################################

sub error {

$errors= $_[0];

print "Content-type: text/html\n\n";
print "<br><br><Br><br><br>";
print "Im sorry the server hit a problem and threw a wobbly. <br>\n";
print "Dont worry the administrator has been emailed and will try and sort\n";
print "If you have any problems, please email support\@argonet.co.uk\n";
print "$!\n";
open (MAIL,"|$mailprog");
print MAIL "To: $mastermail\n";
print MAIL "From: $mastermail\n";
print MAIL "Subject: Form mail Script has a problem on 213.161.67.202\n";
print MAIL "Bob We have a problem, the formmail Script on 213.161.67.202 is not \n";
print MAIL "feeling well at the moment the mailing error is the following.\n";
print MAIL "==============================\n\n";
print MAIL "An error has occured. \n The error is $errors<br>\n\n";
print MAIL "The IP address that caused this problem is \"$ENV{'REMOTE_ADDR'}\"\n";
print MAIL "The query string is         \"$ENV{'QUERY_STRING'}\"\n";
print MAIL "The server address is       \"$ENV{'SERVER_ADDR'}\"\n";
print MAIL "The script name is          \"$ENV{'SCRIPT_FILENAME'}\"\n";
print MAIL "The Host of the computer is \"$ENV{'SERVER_NAME'}\"\n";
print MAIL "==============================\n\n";
print MAIL "$TailMessage_error\n";
print MAIL "$Signature_error\n\n";
close (MAIL);
exit;

}

sub thank_you {

   if ($linkto eq "") {

   print "Content-type: text/html\n\n"; 
   print "<html><head><title>Thank You</title></head>";
   print "<body><p>Thank you for your comments, your email has been sent";
   print "</body></html>";

   } else {

   print "Location: $linkto\n";
   print "Content-type: text/html\n\n"; 
}

}

