#!/usr/bin/perl
use Socket;
$|=1;
$SEND_MAIL="/usr/lib/sendmail -t";
# $SMTP_SERVER="localhost";
@okaydomains=("http://darulsafa.com", "http://www.darulsafa.com");
$SCRIPT_NAME="referus.cgi";
$SITE_NAME="darulsafa";
$SITE_URL="http://www.darulsafa.com/";
$ENDLINE="";
$FINLINE="------------------------------------------------------------------------------";
$MAXNUM=3;
$LOGFILE="reflog.txt";
if ($SENDMAIL ne "")
{&test_sendmail;}
&valid_page; #if script is called from offsite, bounce it!
&decode_vars;
if ( $ENV{'REQUEST_METHOD'} ne "POST")
{
&draw_request;
exit;
}
&do_log;
&process_mail;
print "Location: $JUMP_TO\n\n";
##################################################################
sub process_mail
{
for ($i=1;$i<$MAXNUM+1;$i++)
{
$recipname="recipname_$i";
$recipemail="recipemail_$i";
if ($fields{$recipemail} eq "")
{
next;
}
if (&valid_address == 0)
{
next;
}
$subject = "Suggestion from $fields{'send_name'}";
$msgtxt = <<__STOP_OF_MAIL__;
Dear $fields{$recipname},
This mail is sent to you by $fields{'send_name'} to visit the following Interesting Page:
$JUMP_TO
Please click the above URL or copy and Paste in address bar to view this Page.
darulsafa.com
__STOP_OF_MAIL__
if ($fields{'message'} ne "")
{
$msgtxt .= "$FINLINE\n";
$msgtxt .= "Special Message from $fields{'send_name'} to you: \n";
$msgtxt .= "$fields{'message'}\n\n";
}
$msgtxt .= "$FINLINE\n";
$msgtxt .= "$SITE_URL\n\n";
$msgtxt .= "$SITE_NAME\n";
$msgtxt .= "$ENDLINE\n";
$mailresult=&sendmail($fields{send_email}, $fields{send_email}, $fields{$recipemail}, $SMTP_SERVER, $subject, $msgtxt);
if ($mailresult ne "1")
{print "Content-type: text/html\n\n";
print "MAIL NOT SENT. SMTP ERROR: $mailresult\n";
exit
}
}
}
##################################################################
sub draw_request
{
print "Content-type: text/html\n\n";
print <<__REQUEST__;
SUGGEST THIS PAGE TO A FRIEND...
$ENV{'HTTP_REFERER'}
If you have a friend that you would like to recommend this page to,
or if you just want to send yourself a reminder, here is the easy
way to do it!
Simply fill in the name & e-mail address of the person(s) you wish to tell
about darulsafa.com, your name and e-mail address and click
the SEND button.
After sending the e-mail, you will be transported back to the
page you recommended!
|
__REQUEST2__
}
##################################################################
# NOTHING TO MESS WITH BEYOND THIS POINT!!!!
##################################################################
sub decode_vars
{
$i=0;
if ( $ENV{'REQUEST_METHOD'} eq "GET")
{
$temp=$ENV{'QUERY_STRING'};
}
else
{
read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
}
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split(/=/,$item,2);
$content=~tr/+/ /;
$content=~s/%(..)/pack("c",hex($1))/ge;
$content=~s/\012//gs;
$content=~s/\015/ /gs;
$fields{$key}=$content;
}
if ($fields{'call_by'} eq "")
{
$JUMP_TO = $ENV{'HTTP_REFERER'};
}
else
{
$JUMP_TO = $fields{'call_by'};
}
}
##################################################################
sub valid_address
{
$testmail = $fields{$recipemail};
if ($testmail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$testmail !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/)
{
return 0;
}
else
{
return 1;
}
}
sub valid_page
{
if (@okaydomains == 0) {return;}
$DOMAIN_OK=0;
$RF=$ENV{'HTTP_REFERER'};
$RF=~tr/A-Z/a-z/;
foreach $ts (@okaydomains)
{
if ($RF =~ /$ts/)
{ $DOMAIN_OK=1; }
}
if ( $DOMAIN_OK == 0)
{ print "Content-type: text/html\n\n Sorry, cant run it from here....";
exit;
}
}
##################################################################
sub test_sendmail
{
@ts=split(/ /,$MAIL_PROGRAM);
if ( -e $ts[0] )
{
return;
}
print "Content-type: text/html\n\n";
print "$ts[0] NOTFOUND. PLEASE CHECK YOUR SCRIPT'S MAIL_PROGRAM VARIABLE
";
exit;
}
sub do_log
{
open (ZL,">>$LOGFILE");
$date=localtime(time);
for ($i=1;$i<$MAXNUM+1;$i++)
{
$recipname="recipname_$i";
$recipemail="recipemail_$i";
if ($fields{$recipemail} eq "")
{
next;
}
if (&valid_address == 0)
{
next;
}
$logline="$date\|$JUMP_TO\|$fields{'send_email'}\|$fields{$recipemail}\|\n";
print ZL $logline;
}
close(ZL);
}
###################################################################
###################################################################
sub sendmail {
my ($fromaddr, $replyaddr, $to, $smtp, $subject, $message) = @_;
$to =~ s/[ \t]+/, /g; # pack spaces and add comma
$fromaddr =~ s/.*<([^\s]*?)>/$1/; # get from email address
$replyaddr =~ s/.*<([^\s]*?)>/$1/; # get reply email address
$replyaddr =~ s/^([^\s]+).*/$1/; # use first address
$message =~ s/^\./\.\./gm; # handle . as first character
$message =~ s/\r\n/\n/g; # handle line ending
$message =~ s/\n/\r\n/g;
$smtp =~ s/^\s+//g; # remove spaces around $smtp
$smtp =~ s/\s+$//g;
if (!$to)
{
return(-8);
}
if ($SMTP_SERVER ne "")
{
my($proto) = (getprotobyname('tcp'))[2];
my($port) = (getservbyname('smtp', 'tcp'))[2];
my($smtpaddr) = ($smtp =~
/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
? pack('C4',$1,$2,$3,$4)
: (gethostbyname($smtp))[4];
if (!defined($smtpaddr))
{
return(-1);
}
if (!socket(MAIL, AF_INET, SOCK_STREAM, $proto))
{
return(-2);
}
if (!connect(MAIL, pack('Sna4x8', AF_INET, $port, $smtpaddr)))
{
return(-3);
}
my($oldfh) = select(MAIL);
$| = 1;
select($oldfh);
$_ = ;
if (/^[45]/)
{
close(MAIL);
return(-4);
}
print MAIL "helo $SMTP_SERVER\r\n";
$_ = ;
if (/^[45]/)
{
close(MAIL);
return(-5);
}
print MAIL "mail from: <$fromaddr>\r\n";
$_ = ;
if (/^[45]/)
{
close(MAIL);
return(-5);
}
foreach (split(/, /, $to))
{
print MAIL "rcpt to: <$_>\r\n";
$_ = ;
if (/^[45]/)
{
close(MAIL);
return(-6);
}
}
print MAIL "data\r\n";
$_ = ;
if (/^[45]/)
{
close MAIL;
return(-5);
}
}
if ($SEND_MAIL ne "")
{
open (MAIL,"| $SEND_MAIL");
}
print MAIL "To: $to\n";
print MAIL "From: $fromaddr\n";
print MAIL "Reply-to: $replyaddr\n" if $replyaddr;
print MAIL "X-Mailer: Perl Powered Socket Mailer\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$message";
print MAIL "\n.\n";
if ($SMTP_SERVER ne "")
{
$_ = ;
if (/^[45]/)
{
close(MAIL);
return(-7);
}
print MAIL "quit\r\n";
$_ = ;
}
close(MAIL);
return(1);
}