#!/usr/npac/bin/perl
#set the line above this to your perl path. Usually fine as is
# or /usr/local/bin/perl  /sbin/perl
# make certain that the first line stays the first line!
$|=1;

#### PROGRAM CONFIGURATION SECTION ############################
# 
# Modify each item below to meet your needs.
#
# MAIL_PROGRAM should be the path to your sendmail program
# BASEDIR is the unix directory that your greeting cards will
#   be stored in.
# BASEURL is the URL (http address) of the directory your cards
#   will be stored in.
# SITEURL is the home page URL for your site.
# SITENAME is the Name of your site, ie Title
# EXT is the ending name for your card files. NEVER, EVER USE shtml!!
# PROGNAME is the URL of THIS script.
# MY_EMAIL is YOUR e-mail address! BE SURE to put that backslash
#    BEFORE THE \@ sign
#
$MAIL_PROGRAM="/usr/lib/sendmail -t ";
$BASEDIR="/npac/server/foxsden00/sroot/droot/psf/FFE/cards";
$BASEURL="http://www.foxsden.org/psf/FFE/cards";
$SITEURL="http://www.foxsden.org/psf/FFE/ffe.htm";
$SITENAME="FFE";
$EXT=".html";
$PROGNAME="http://boss.npac.syr.edu:8080/cgi-bin/pamfox/card.pl";
$MY_EMAIL="psf\@npac.syr.edu";
###############################################################
#
# Now go thru the program looking for the string "BNB SAYS!"
# to locate other changes you should make, such as wording of
# the notification e-mail and "plug" for the site.
#
# to keep things simple, the field names are hard coded in.
# you can of course modify what you wish.
###############################################################

#This first part of the program splits up our data and gets it
#ready for formatting.
  $i=0;
  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;
    $fields{$key}=$content;
    $i++;
    $item{$i}=$key;
    $response{$i}=$content;
   }

if ($fields{'recip_email'} eq "")
  {
    &no_email;
    exit;
  }

# BNB SAYS! REMOVE THIS BLOCK OF CODE IF YOU DO NOT CARE ABOUT THE 
# SENDER'S EMAIL ADDRESS.
if ($fields{'sender_email'} eq "")
  {
    &no_email;
    exit;
  }
#END OF THE ABOVE BLOCK OF CODE

if ($fields{'action_code'} eq "NEW") 
  {                             
     &do_preview;
  }                             
if ($fields{'action_code'} eq "SENDCARD") 
  {                             
     &get_file_name;
     &create_file;
     &send_mail;
     &thank_you;
  }                             


#Set up our HTML Preview Form
sub do_preview
{
print <<__STOP_OF_PREVIEW__;
Content-type: text/html


<HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
if(navigator.userAgent.indexOf("MSIE") != -1)
document.writeln ('');
else
document.writeln ('<EMBED SRC="$BASEURL/$fields{'midifile'}" AUTOSTART="true" HIDDEN="true" VOLUME="80%">');
//-->
</SCRIPT>
<BGSOUND SRC="$BASEURL/$fields{'midifile'}">

<BODY BGCOLOR="#FFFFFF">
<CENTER>
<P>
<TABLE WIDTH=580 BGCOLOR=$fields{'back_color'} BORDER=5>
 <TR>
 <TD>
<TABLE WIDTH=580 BGCOLOR=$fields{'back_color'}>
 <TR>
 <TD>
  <TABLE WIDTH=200 BORDER=4>
    <TR>
      <TD ALIGN=CENTER VALIGN=CENTER>
       <IMG SRC=$BASEURL/$fields{'pic_select'} HEIGHT=250 WIDTH=250 BORDER=0><P>
      </TD>
    </TR>
  </TABLE>
 </TD>
 <TD WIDTH=380 VALIGN=TOP >
   <CENTER>
   <HR WIDTH=1 ALIGN=LEFT>
   <FONT SIZE=+2 COLOR=$fields{'text_color'}
     FACE=ARIAL><B>$fields{'the_title'}</B></FONT>
   <HR WIDTH=200>
   <TABLE WIDTH=300>
    <TR>
     <TD><FONT FACE=ARIAL COLOR=$fields{'text_color'}>
         $fields{'the_message'}

      <P ALIGN=CENTER>
      <I>$fields{'sig_line'}
      </I>
      </P>
      </FONT>
      </TD>
    </TR>
   </TABLE>
   </CENTER>
 </TD>
 </TR>
</TABLE>
</TD>
</TR>
</TABLE>
<P>
<TABLE WIDTH=500>
  <TR>
  <TD>
  <FONT FACE="ARIAL">
  This card was created by
  $fields{'sender_name'},
  <I><A HREF=mailto:$fields{'sender_email'}>
     $fields{'sender_email'}</A></I>
  expressly for $fields{'recip_name'}. If you would like to
  send a card to a person you really care for, just go to
  <A HREF=$SITEURL>$SITEURL</A>
  and create your own free virtual cards.
  </FONT>
  <CENTER>
<FORM METHOD="POST" ACTION="$PROGNAME">
<INPUT TYPE="HIDDEN" NAME="action_code" VALUE="SENDCARD">
<INPUT TYPE="HIDDEN" VALUE="$fields{'pic_select'}" NAME="pic_select">
<INPUT TYPE="HIDDEN" VALUE="$fields{'sender_name'}" NAME="sender_name">
<INPUT TYPE="HIDDEN" VALUE="$fields{'sender_email'}" NAME="sender_email">
<INPUT TYPE="HIDDEN" VALUE="$fields{'recip_name'}" NAME="recip_name">
<INPUT TYPE="HIDDEN" VALUE="$fields{'recip_email'}" NAME="recip_email">
<INPUT TYPE="HIDDEN" VALUE="$fields{'text_color'}" NAME="text_color">
<INPUT TYPE="HIDDEN" VALUE="$fields{'back_color'}" NAME="back_color">
<INPUT TYPE="HIDDEN" VALUE="$fields{'the_title'}" NAME="the_title">
<INPUT TYPE="HIDDEN" VALUE="$fields{'the_message'}" NAME="the_message">
<INPUT TYPE="HIDDEN" VALUE="$fields{'sig_line'}" NAME="sig_line">
<INPUT TYPE="HIDDEN" VALUE="$fields{'midifile'}" NAME="midifile">
<INPUT TYPE="HIDDEN" VALUE="$ENV{'HTTP_REFERER'}" NAME="parent">
<INPUT TYPE="submit" VALUE="SEND-CARD">   
</FORM>
</CENTER>
 <FONT FACE="ARIAL">
 <B>To send your creation, click on the SEND-CARD button. To return
    the card creation screen without sending, please press your
    browser's BACK button.
 </B>
 <P>
 </FONT>
  <BLOCKQUOTE>
  <I>
  <A HREF=$SITEURL>$SITENAME</A> 
 
  <I> a cyberclub for kids and teens
  </BLOCKQUOTE>
  </TD>
  </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>

__STOP_OF_PREVIEW__
}

#Get the file name and increment the counter
#create the file name based on both the month/day 
#and counter value. Makes for easy cleanup of holding
#directory!
sub get_file_name
{
   if ( -e "$BASEDIR/ccount")
    {
      open(COUNTER,"<$BASEDIR/ccount") ;
      while(<COUNTER>)
        {
         $newnum = $_ + 1;
        }
      close(COUNTER);
     }
     else {$newnum=1;}
   open(COUNTER,">$BASEDIR/ccount");
   print COUNTER "$newnum";
   close (COUNTER);
   $date=localtime(time);  
   ($day, $month, $num, $time, $year) = split(/\s+/,$date); 
   $month=~tr/A-Z/a-z/;
   $PREF = "$month$num-";

$FILE_NAME="$BASEDIR/$PREF$newnum$EXT";
$URL_NAME="$BASEURL/$PREF$newnum$EXT";
}


#Write out our HTML FILE
sub create_file
{
   open(OUTFILE,">$FILE_NAME") ;
print OUTFILE <<__STOP_OF_CRFILE__;

<HTML>
<SCRIPT LANGUAGE="JavaScript">
<!--
if(navigator.userAgent.indexOf("MSIE") != -1)
document.writeln ('');
else
document.writeln ('<EMBED SRC="$BASEURL/$fields{'midifile'}" AUTOSTART="true" HIDDEN="true" VOLUME="80%">');
//-->
</SCRIPT>
<BGSOUND SRC="$BASEURL/$fields{'midifile'}">
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<P>
<TABLE WIDTH=580 BGCOLOR=$fields{'back_color'} BORDER=5>
 <TR>
 <TD>
<TABLE WIDTH=580 BGCOLOR=$fields{'back_color'}>
 <TR>
 <TD>
  <TABLE WIDTH=200 BORDER=4>
    <TR>
      <TD ALIGN=CENTER VALIGN=CENTER>
       <IMG SRC=$BASEURL/$fields{'pic_select'} HEIGHT=250 WIDTH=250 BORDER=0><P>
      </TD>
    </TR>
  </TABLE>
 </TD>
 <TD WIDTH=380 VALIGN=TOP >
   <CENTER>
   <HR WIDTH=1 ALIGN=LEFT>
   <FONT SIZE=+2 COLOR=$fields{'text_color'}
     FACE=ARIAL><B>$fields{'the_title'}</B></FONT>
   <HR WIDTH=200>
   <TABLE WIDTH=300>
    <TR>
     <TD><FONT FACE=ARIAL COLOR=$fields{'text_color'}>
         $fields{'the_message'}

      <P ALIGN=CENTER>
      <I>$fields{'sig_line'}
      </I>
      </P>
      </FONT>
      </TD>
    </TR>
   </TABLE>
   </CENTER>
 </TD>
 </TR>
</TABLE>
</TD>
</TR>
</TABLE>
<P>
<TABLE WIDTH=500>
  <TR>
  <TD>
  <FONT FACE="ARIAL">
  This card was created by
  $fields{'sender_name'} 
  (<I><A HREF=mailto:$fields{'sender_email'}>
     $fields{'sender_email'}</A></I>)
  expressly for $fields{'recip_name'}. If you would like to
  send a card to a person you really care for, just go to
  <A HREF=$SITEURL>$SITEURL</A>
  and create your own free virtual cards.
  </FONT>
 <P>
 </FONT>
  <BLOCKQUOTE>
  <I>
  <A HREF=$SITEURL>$SITENAME</A> 
    a cyberclub for kids and teens
  </BLOCKQUOTE>
<PRE>


</PRE>
<CENTER>
<FONT FACE="ARIAL" SIZE="-1"><B>
Card Creator Script by<A HREF="http://bignosebird.com"/>BigNoseBird.com</A>
</B><BR>
<I>Everything for the webmaster, for free!
</CENTER>
<PRE>


</PRE>
To $fields{'recip_email'}
  </TD>
  </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>

__STOP_OF_CRFILE__
close (OUTFILE)
}

sub thank_you
{
open (ML,">>$BASEDIR/maillog");      
print ML "$fields{'recip_email'}\n";  
print ML "$fields{'sender_email'}\n"; 
close(ML);

print <<__STOP_OF_THANKS__;
Content-type: text/html

<CENTER>
<H1><B>THANKS!</B></H1>
Your card notification has been sent to $fields{'recip_name'}<BR>
The URL of this card is<P>
<A HREF="$URL_NAME">$URL_NAME</A>
<P>
<B><A HREF=$fields{'parent'}>RETURN TO CARD CREATOR</A></B>
<P>

__STOP_OF_THANKS__
}


sub no_email
{
print <<__STOP_OF_NOMAIL__;
Content-type: text/html

<FONT SIZE="+1">
<B>
SORRY! Your request could not be processed because of missing
e-mail address(es). Please use your browser's back button to
return to the card entry page.
</B>
</FONT>
__STOP_OF_NOMAIL__
}

#BNB SAYS! Change the email address in "From:" to your e-mail
#  address. Also, modify the text below to meet your needs.
#  make certain everything is between the __STOP_OF_MAIL2__
#  markers.
sub send_mail
{
open (MAIL_RECIP, "|$MAIL_PROGRAM");
print MAIL_RECIP <<__STOP_OF_MAIL2__;
To: $fields{'recip_email'}
From: $MY_EMAIL
Subject: You have a virtual card waiting for you!

Hi,

$fields{'sender_name'} stopped by my site, $SITENAME
and created a Virtual Card just for you! To pick up your
card, simply point your browser at the page listed below.

   $URL_NAME

The card will remain on the server for about one week, so
please print it out or save it as soon as you can.


__STOP_OF_MAIL2__
close(MAIL_RECIP);
}


