#!/usr/bin/perl
# Be sure that the line above points to where perl 5 is
# on your system.
##################################################################
# subscribe.cgi: subscription e-mail collector with subscribe
# and unsubscribe features.
# Release 1.1 on 02/03/02
# (C) 1999-2002 BigNoseBird.Com, Inc. This program is freeware and may
# be used at no cost to you (just leave this notice intact).
# Feel free to modify, hack, and play with this script.
# It is provided AS-IS with no warranty of any kind.
# We also cannot assume responsibility for either any programs
# provided here, or for any advice that is given since we have no
# control over what happens after our code or words leave this site.
# Always use prudent judgment in implementing any program- and
# always make a backup first!
#
##################################################################
# When calling the script, you must provide the following INPUTs
#
# datafile (name of file that will contain the addresses.)
# email (the e-mail address of the person subscribing/unsubscribing
# action (subscribe or unsubscribe)
#### USER CONFIGURATION SECTION ##################################
# set BASEDIR to the directory that will hold your letter and
# mailling list files. Be certain that the script has permission
# to write to this directory. This must be set to the same value
# you declare in nmmdadmin.cgi (no trailing slash!)
$BASEDIR="/www/mydomain/cgi-bin/nomodomo";
# remove the # mark before okaydomains to restrict subscription
# requests to your web site. This prevents others from calling
# your script from elsewhere. If you encourage others to offer
# your newsletter from their sites, do NOT remove the # mark.
# @okaydomains=("http://mydomain.com", "http://www.mydomain.com");
# $delimiter is the special character that is used to separate the
# items of information about each e-mail address. To use the TAB
# character, uncomment (remove the # mark) the line that says TAB
# and place a # mark at the start of the line that says PIPE.
$delimiter="\\|"; #PIPE
# $delimiter="\\t"; #TAB
##################################################################
$lockfile="/tmp/subscribe.lck";
&valid_page;
&decode_vars;
$datafile=~s/[^\s\w\.\/]//g;
$return_to=$ENV{'HTTP_REFERER'};
$the_date=localtime();
$ip_addr=$ENV{'REMOTE_ADDR'};
$datafile="$fields{'datafile'}\.mbz";
$email=$fields{'email'};
$action=$fields{'action'};
if ($datafile eq "")
{ print "Content-type: text/html\n\n";
print "Configuration Error: No datafile specified\n";
exit;
}
if ($action eq "")
{ print "Content-type: text/html\n\n";
print "Configuration Error: No action specified\n";
exit;
}
if (&valid_address($email) == 0)
{
&bad_email;
exit;
}
&write_data;
&thank_you;
sub thank_you
{
if ($action eq "unsubscribe")
{ $whichaction = "removed from";}
else { $whichaction = "added to";}
print "Content-type: text/html\n\n";
print <<__END_THANKS__;
|
Thank You ;-)
Your e-mail address has been $whichaction our mailing list.
Please click on the link below to return
to the page you were last on.
$return_to
Another FREE script from
BigNoseBird.Com
Will be available for download soon!
|
|
__END_THANKS__
}
##################################################################
sub write_data
{
&get_datetime;
$delim=$delimiter;
$delim=~s/\\//g;
&get_the_lock;
open(IDBFILE,"<$BASEDIR/$datafile");
@mailing=;
close(IDBFILE);
open(ODBFILE,">$BASEDIR/$datafile");
foreach $line (@mailing)
{
chop $line;
($thismail,$thisip,$thisdate)=split(/$delimiter/,$line);
if ($email ne $thismail)
{
print ODBFILE "$line\n";
}
}
if ($action eq "subscribe")
{
print ODBFILE "$email$delim$ip_addr$delim$rd$delim\n";
}
close (ODBFILE);
# This may seem a silly place to check to see if we were able to
# create the file, but a lot of people don't have access to their
# error logs to find message written by "die"
&drop_the_lock;
if (!-w "$BASEDIR/$datafile")
{ print "Content-type: text/html\n\n";
print "Configuration Error: could not create datafile
please check path and permissions!\n";
exit;
}
}
##################################################################
sub decode_vars
{
$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;
$content=~s/\t/ /g;
$content=~tr/A-Z/a-z/;
$fields{$key}=$content;
}
}
##################################################################
sub valid_address
{
local($testmail) = @_;
if ($testmail eq "")
{return 0;}
if ($testmail =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
$testmail !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)
{ return 0;}
else
{ return 1;}
}
##################################################################
sub bad_email
{
print <<__STOP_OF_BADMAIL__;
Content-type: text/html
SORRY! Your request could not be processed because of an
improperly formatted e-mail address. Please use your browser's
back button to return to the form entry page.
__STOP_OF_BADMAIL__
}
##################################################################
sub get_the_lock
{
local ($endtime);
$endtime = 60;
$endtime = time + $endtime;
while (-e $lockfile && time < $endtime)
{
# Do Nothing
}
open(LOCK_FILE, ">$lockfile");
}
##################################################################
sub drop_the_lock
{
close($lockfile);
unlink($lockfile);
}
##################################################################
sub get_datetime
{
%mos = ( "jan","01", "feb","02", "mar","03", "apr","04",
"may","05", "jun","06", "jul","07", "aug","08",
"sep","09", "oct","10", "nov","11", "dec","12");
$date=localtime(time);
($day, $month, $num, $time, $year) = split(/\s+/,$date);
@time_temp=split(/\:/,$time);
$month=~tr/A-Z/a-z/;
$rd="$mos{$month}\/$num\/$year $time_temp[0]\:$time_temp[1]\:$time_temp[2]";
}
##################################################################
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 from here!";
exit;
}
}