#! /usr/bin/perl
use Socket;
$|=1;
##################################################################  
#  birdcast.cgi Version 2.0
#  updated May 2, 1999
#  (C)1998, 1999 Bignosebird.com                                          
#  This software is FREEWARE! Do with it as you wish. It is yours   
#  to share and enjoy. Modify it, improve it, and have fun with it! 
#  It is distributed strictly as a learning aid and bignosebird.com 
#  disclaims all warranties- including but not limited to:          
#  fitness for a particular purpose, merchantability, loss of       
#  business, harm to your system, etc... ALWAYS BACK UP YOUR        
#  SYSTEM BEFORE INSTALLING ANY SCRIPT OR PROGRAM FROM ANY          
#  SOURCE!
##################################################################  

# CONFIGURATION NOTES 

#
# $SCRIPT_NAME is the full URL of this script, including the 
# http part, ie, "http://domainname.com/cgi-bin/spreadcast.cgi";
#
# $SITE_NAME is the "name" of your web site.
# $SITE_URL is the URL of your site (highest level)
# $END_LINE is the very last line printed in the e-mail.
#
# $MAXNUM is the number of possible people a person can refer
# your URL to at one time. If you call the script using the
# GET method, then this is also the number of entry blanks
# created for recipient names and addresses.
#
# $SMTP_SERVER is the name of your e-mail gateway server, or
# SMTP host. On most systems, "localhost" will work just fine.
# If not, change "localhost" to whatever your ISP's SMTP
# server name is, ie, smtp.isp.net or mail.isp.net

# $SEND_MAIL is the full path to your server's sendmail program
# If you do not wish to use Sockets for some reason and need
# to use sendmail, uncomment the $SEND_MAIL line and comment
# the $SMTP_SERVER line.

# okaydomains is a list of domains from which you want to allow
# the script to be called from.  Leave it commented to leave the
# script unrestricted. If you choose to use it, be sure to list
# your site URL with and without the www.

#  Use either $SMTP_SERVER 
# $SMTP_SERVER="mail.qfever.com";
#
#     OR
#
   $SEND_MAIL="/usr/sbin/sendmail"; 
#
#      BUT NEVER BOTH!!!!!!

#   @okaydomains=("http://yourdomain.com", "http://www.yourdomain.com");

   $SCRIPT_NAME="http://64.176.146.230/cgi-bin/spreadcast.cgi";
   $SITE_NAME="Q Fever! Your #1 Source For Healthcare Bogosity";
   $SITE_URL="http://www.qfever.com";
   $ENDLINE="";
   $MAXNUM=5;
   $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;
        }

#BNB SAYS! You can modify the Subject line below.

$subject = "Suggestion from $fields{'send_name'}";

#BNB SAYS! Modify the lines below between the lines marked
# with __STOP_OF_MAIL__ to customize your e-mail message
# DO NOT remove the lines that contain __STOP_OF_MAIL__!
# If you enter any hardcoded e-mail addresses, BE SURE TO
# put the backslash before the at sign, ie, me\@here.net

$msgtxt = <<__STOP_OF_MAIL__;            
Hey, $fields{$recipname}!

$fields{'send_name'} stopped by $SITE_NAME 
and suggested that you visit the following URL:

   http://www.qfever.com

__STOP_OF_MAIL__

      if ($fields{'message'} ne "")
       {
         $msgtxt .= "A Message...\n";
         $msgtxt .= "$fields{'message'}\n\n";
       }
       $msgtxt .= "$SITE_NAME\n";
       $msgtxt .= "$ENDLINE\n";
       $msgtxt .= "$SITE_URL\n\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";

#BNB SAYS! Here is the part that draws the page that asks the 
#reader to enter e-mail addresses and names. Tailor it to meet
# your needs if necessary. DO NOT disturb the lines with
# __REQUEST__ on them.

print <<__REQUEST__;
<body bgcolor="#FFFFFF">
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"> </font><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
</font>
<table width="100%" border="0">
  <tr> 
    <td><!-- BEGIN LINKEXCHANGE CODE --> 
      <center>
        <iframe src="http://leader.linkexchange.com/2/X1377200/showiframe?" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no> 
        <a href="http://leader.linkexchange.com/2/X1377200/clickle" target="_top"><img width=468 height=60 border=0 ismap alt="" src="http://leader.linkexchange.com/2/X1377200/showle?"></a></iframe><br>
        <a href="http://leader.linkexchange.com/2/X1377200/clicklogo" target="_top"><img src="http://leader.linkexchange.com/2/X1377200/showlogo?" width=468 height=16 border=0 ismap alt=""></a><br>
      </center>
      <!-- END LINKEXCHANGE CODE --></td>
    <td width="75%" valign="top"><img src="/images/youradhere.gif" width="234" height="60"></td>
  </tr>
</table>
<!-- #BeginLibraryItem "/Library/Title Bar.lbi" --> 
    <table width="100%" border="0" cellspacing="5" cellpadding="5" bordercolor="#CCCCFF">
      <tr bgcolor="#9999FF"> 
        
    <td valign="middle" colspan="3" width="25%"><font face="Verdana, Arial, Helvetica, sans-serif" size="1">&nbsp;</font><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#CCCCFF"></font></td>
      </tr>
      <tr bgcolor="#9999FF"> 
        <td valign="middle" colspan="3" width="25%"> 
          <div align="left"> 
            <table width="100%" border="0" bgcolor="#FFFFFF">
              <tr> 
                <td valign="middle" width="100%" bgcolor="#FFFFFF" bordercolor="#FFFFFF" colspan="3"> 
                  <div align="left"></div>
                  
              <div align="center"><a href="index.html" target="_top"><img src="/images/qfeverlogo-long.gif" width="707" height="59" border="0"></a> 
              </div>
              </td>
              </tr>
            </table>
          </div>
        </td>
      </tr>
      <tr bgcolor="#9999FF"> 
        <td valign="middle" colspan="3" width="25%"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#9999FF">&nbsp;</font></td>
      </tr>
    </table><!-- #EndLibraryItem --> 
<table width="100%" border="0" cellpadding="10">
  <tr>
    <td bgcolor="#9999FF" width="25%">&nbsp;</td>
    <td width="50%"><!-- #BeginEditable "story" --> 
      <table width="100%" border="0" cellpadding="1" bgcolor="#000000">
        <tr bgcolor="#000000" align="center"> 
          <td> 
            <table width="100%" border="0" cellpadding="5">
              <tr bgcolor="#CCCCFF" align="center"> 
                <td><font face="Verdana, Arial, Helvetica, sans-serif" size="4">Help 
                  Spread <b>Q Fever!</b></font></td>
              </tr>
            </table>
          </td>
        </tr>
      </table>

      <p><font face="Verdana, Arial, Helvetica, sans-serif"> Thanks for spreading 
        the word about <b>Q&nbsp;Fever!</b>, Your #1 Source For Healthcare Hubris!</font></p>
      <p><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#000000"> 
        Simply fill in the e-mail address of the person(s) you wish to tell about 
        <b>Q Fever!</b>, your name and e-mail address (so they can reply to you 
        should the need arise), and click the <b>SEND</b> button. If you want 
        to, you can also enter a message that will be included on the e-mail. 
        </font> 
      <p><font face="Verdana, Arial, Helvetica, sans-serif" size="2" color="#000000"> 
        After sending the e-mail, you will be brought back to the page you recommended! 
        </font>
      <form method="POST" action="$SCRIPT_NAME">
        <input type="HIDDEN" name="call_by" value=$ENV{'HTTP_REFERER'}>
        <table border="0" align="center">
          <tr> 
            <td><font face="Arial, Helvetica, sans-serif"></font></td>
            <td><font face="Arial, Helvetica, sans-serif" size="2"><b>Name</b></font></td>
            <td><font face="Arial, Helvetica, sans-serif" size="2"><b>E-Mail Address</b></font></td>
          </tr>
          <tr> 
            <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><b>Your 
              Info:</b></font></td>
            <td><font face="Arial, Helvetica, sans-serif" size="2"> 
              <input type="TEXT" name="send_name" size="10">
              </font></td>
            <td><font face="Arial, Helvetica, sans-serif" size="2"> 
              <input type="TEXT" name="send_email" size="20">
              </font></td>
          </tr>
__REQUEST__
    for ($i=1;$i<$MAXNUM+1;$i++)
     {
    print <<__STOP_OF_ROW__;            
             <tr> 
            <td align="center"><font face="Arial, Helvetica, sans-serif" size="2"><b>Recipient 
              $i</b></font></td>
            <td><font face="Arial, Helvetica, sans-serif" size="2"> 
              <input type="TEXT" name="recipname_$i" size="10">
              </font></td>
            <td><font face="Arial, Helvetica, sans-serif" size="2"> 
              <input type="TEXT" name="recipemail_$i" size="20">
              </font></td>
          </tr>
__STOP_OF_ROW__
     }
    print <<__REQUEST2__;            
<tr align="center"> 
            <td colspan="3"><font face="Arial, Helvetica, sans-serif"></font><font face="Arial, Helvetica, sans-serif" size="2"><b>Your 
              Message<br>
              </b> 
              <textarea name="message" wrap=virtual rows=5 cols=30></textarea>
              <br>
              <input type="submit" value="SEND" name="submit">
              </font></td>
          </tr>
        </table>
      </form>
      <center>
<table width="75%" border="0" bgcolor="#9999FF" cellpadding="5" align="center">
          <tr>
            <td><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><b>Privacy 
              Policy:</b> Despite appearances to the contrary, <b>Q Fever!</b> 
              is an establishment of high moral and ethical standards. </font><font face="Verdana, Arial, Helvetica, sans-serif" size="1">Be 
              it hereby established that <b>Q Fever!</b> shall under no circumstances 
              ever divulge, bequeath, make for sale, or otherwise partake in the 
              distribution the personal information entrusted to us by our readers.</font><font size="-1"><i><i> 
              </i></i></font></td>
          </tr>
        </table>
      </center>
      <!-- #EndEditable --> 
      <p align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><font size="3" face="Courier New, Courier, mono"><b><a href="/index.html" target="_top">[Home]</a></b></font></font> 
      </p>
      <hr noshade width="50%">
      <!-- #BeginEditable "Table" -->
      <table border="0" align="center">
        <tr> 
          <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="/cgi-bin/cgiwrap/qfever/sendcast.cgi">Send</a> 
            this page to someone else!<br>
            <a href="/submit.html">Submit</a> an idea to <b>Q Fever!</b><br>
            <a href="/slogancontest.html">Enter</a> <b>Q Fever!</b>'s slogan 
            contest!<br>
            </td>
        </tr>
      </table>
      <!-- #EndEditable --> 
      <hr noshade width="50%">

    </td>
    <td bgcolor="#9999FF" width="25%">&nbsp;</td>
  </tr>
</table>
<table width="100%" border="0" cellspacing="5" cellpadding="5">
  <tr valign="middle"> 
    <td bgcolor="#CCCCFF"> 
      <div align="center"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#333333">Editor-In-Chief: 
        <a href="Mailto">M. Furfur, MD</a> | Editor-At-Large: <a href="mailto">B. 
        Cereus, MD, PhD</a> </font><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#666666"><br>
        </font><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#333333">All 
        rights reserved.</font><font face="symbol" size="1" color="#333333"> Ó<font face="Verdana, Arial, Helvetica, sans-serif">qfever.com 
        2000</font></font><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#333333"> 
        </font></div>
    </td>
  </tr>
</table>
<hr width="100%" noshade>
<table width="100%" border="0">
  <tr>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>Disclaimer 
      : </b>The material on this site represents a parody meant solely for entertainment 
      purposes, and is not intended to recommend or advise regarding the prevention, 
      diagnosis, or treatment of any medical illness or condition. Stories and 
      articles are meant only to provide a brief, fleeting distraction from the 
      wretchedness of reality, and are not intended to be insensitive, callous, 
      or offensive, or to otherwise belittle the plight of those affected with 
      any medical disease, condition, or illness. All names and descriptions of 
      people are fictitious except for those of well-known public figures, who 
      are the subject of satire. Any resemblance to actual persons or events is 
      purely coincidental.</font></td>
  </tr>
</table>
<hr width="100%" noshade>
<br>
<p>&nbsp; </p>
</body>
__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 "<H2>$ts[0] NOTFOUND. PLEASE CHECK YOUR SCRIPT'S MAIL_PROGRAM VARIABLE</H2>";
   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);
}

###################################################################
#Sendmail.pm routine below by Milivoj Ivkovic 
###################################################################
sub sendmail  {

# error codes below for those who bother to check result codes <gr>

# 1 success
# -1 $smtphost unknown
# -2 socket() failed
# -3 connect() failed
# -4 service not available
# -5 unspecified communication error
# -6 local user $to unknown on host $smtp
# -7 transmission of message failed
# -8 argument $to empty
#
#  Sample call:
#
# &sendmail($from, $reply, $to, $smtp, $subject, $message );
#
#  Note that there are several commands for cleaning up possible bad inputs - if you
#  are hard coding things from a library file, so of those are unnecesssary
#

    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);

    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-4);
    }

    print MAIL "helo $SMTP_SERVER\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-5);
    }

    print MAIL "mail from: <$fromaddr>\r\n";
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-5);
    }

    foreach (split(/, /, $to))
    {
	print MAIL "rcpt to: <$_>\r\n";
	$_ = <MAIL>;
	if (/^[45]/)
	{
	    close(MAIL);
	    return(-6);
	}
    }

    print MAIL "data\r\n";
    $_ = <MAIL>;
    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 "")
  {
    $_ = <MAIL>;
    if (/^[45]/)
    {
	close(MAIL);
	return(-7);
    }

    print MAIL "quit\r\n";
    $_ = <MAIL>;
  }

    close(MAIL);
    return(1);
}

