#!/usr/bin/perl 

# Index Finger Pro v4.101
# Developed by Index Finger, Inc. - www.indexfinger.com
# Copyright 1998-99 Index Finger, Inc. - All rights reserved.

$thisprogram = "subscriber.cgi";
$operatingsystem = "unix";

if (($operatingsystem eq "nt") || ($operatingsystem eq "other")) { 
    $ifdirectory = "D:/home/indexfinger/";
    }

$filetorequire = "$ifdirectory" . "config.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "config-admin.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "variables/setup.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "programs.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "subroutes.cgi";
require "$filetorequire";
$filetorequire = "$ifdirectory" . "mailconfig.cgi";
require "$filetorequire";


# Parse incoming information.

&parsequery;
    $inwhere = $QUERY{'where'};
    ($inmaingroupid,$inthisgroupid,$ingroupid) = split(/\-/,$inwhere);

&parseform;
    $inaddress = $FORM{'address'};
    $action = $FORM{'action'};


# Begin the program.

&nonhtmlmainmenu;

$output .= "<font size=\"-1\" face=\"verdana,arial\">\n";
$output .= "<a href=\"$directoryurl/$frontpage\">Home</a> /<p>\n";
$output .= "<b>What's New Mailing List</b><p>\n";


# Check or make required directory and files.

$maildirectory = "$ifdirectory" . "mail";
unless (-e $maildirectory) {
    mkdir ("$maildirectory", 0755);
    chmod(0755, "$maildirectory");
    }

    $mailinglistfile = "$maildirectory/list.cgi";
unless (-e $mailinglistfile) {
    chmod(0755, "$maildirectory");
    open(FILE,">$mailinglistfile");
    close(FILE);
    }

    $removedlistfile = "$maildirectory/removed.cgi";
unless (-e $removedlistfile) {
    chmod(0755, "$maildirectory");
    open(FILE,">$removedlistfile");
    close(FILE);
    }

$databasefile = $mailinglistfile;

# They want action, but no address was entered.

if ($action && $inaddress eq "") {
    $output .= "<b>Status:</b> No Email Address Entered<br>\n";
    &subscribeform;
    &gethtmlpage("variables/mailing-whatisthis.html");
    }

# Subscribe.

elsif ($action eq "subscribe" && $inaddress ne "") {
    $duplicate = &dupcheck($inaddress);
    $valid = &emailcheck($inaddress);
           
    # Already subscribed.

    if ($duplicate eq "yes") {
        $output .= "<b>Status:</b> Already Subscribed<p>\n";
        &subscribeform;
        &gethtmlpage("variables/mailing-whatisthis.html");
        }

    # Bad email address.

    elsif ($duplicate eq "no" && $valid eq "no") {
        $output .= "<b>Status:</b> Bad Email Address<p>\n";
        &subscribeform;
        &gethtmlpage("variables/mailing-whatisthis.html");
        }

    # Add email address.


    else {

        $hostname = $ENV{'REMOTE_ADDR'};
        $date = time;

        $filetoappend = "mail/list.cgi";

        unless ($inmaingroupid ne "") { $inmaingroupid = "all"; }

        open(FILE, ">>$filetoappend");
        flock(FILE,2);
        print FILE "$inmaingroupid|$inaddress|$hostname|$date\n";
        close(FILE);


        $output .= "<b>Status:</b> Email Address Added!<p>\n";
        $output .= "Thank you for subscribing to our what's new mailing list. If you wish to unsubscribe, return to this page and enter your email address.\n";

        open (MAIL,"|$mailprog -t");
        print MAIL "X-Mailer: Index Finger Pro v4.101\n";
        print MAIL "X-Mailer-Info: www.indexfinger.com\n";
        print MAIL "Organization: $directoryname\n";
        print MAIL "To: $inaddress\n";
        print MAIL "From: $directoryname <$fromemail>\n";
        print MAIL "Subject: Subscribe Confirmation\n\n";
        print MAIL ".........................................................\n";
        print MAIL "$directoryname\n";
        print MAIL "$directoryurl\n";
        print MAIL ".........................................................\n";
        print MAIL "This email is to confirm your subscription to our What's\n";
        print MAIL "New Mailing List. We send updates on new sites that have\n";
        print MAIL "been added to our directory.\n\n";
        print MAIL "If you wish to be removed, please go to this url:\n\n";
        print MAIL "    $adminurl/$subscriberprogram\n\n";
        print MAIL ".........................................................\n";
        print MAIL "Powered by Index Finger Pro v4.101\n";
        close (MAIL);

        }
    }

# Unsubscribe

elsif ($action eq "unsubscribe" && $inaddress ne "") {
    $duplicate = &dupcheck($inaddress);

    # Remove email address.

    if ($duplicate eq "yes") {

        &remove($inaddress);
        $output .= "<b>Status:</b> Email Address Removed!<p>\n";
        $output .= "Your email address has been removed. If you get another what's new email, please feel free to contact us.<p>\n";
        }

    # Not listed.

    else {
        $output .= "<b>Status:</b> Not Listed<p>\n";
        &subscribeform;
        &gethtmlpage("variables/mailing-whatisthis.html");
        }
    }

# Give them first page.

else {
    $output .= "<b>Status:</b> Enter Email Address<p>\n";
    &subscribeform;
    &gethtmlpage("variables/mailing-whatisthis.html");
    }


    &nonhtmlpoweredby;


if (-e $mailingtemplate) {
    open(TEMPLATE,"$mailingtemplate");
    flock(TEMPLATE,2);
    @template = <TEMPLATE>;
    close(TEMPLATE);
    foreach $line (@template) { $body = "$body$line"; }
    }


    &template;


sub subscribeform {
    if ($inwhere eq "") { $output .= "<form action=\"$adminurl/$subscriberprogram\" method=\"post\">\n"; }
    else { $output .= "<form action=\"$adminurl/$subscriberprogram?where=$inwhere\" method=\"post\">\n"; }
    $output .= "<font face =\"verdana,arial\" size=\"3\">\n";
    $output .= "<input type=text name=\"address\" size=\"30\" value=\"$inaddress\"><p>\n";
    $output .= "<font face =\"verdana,arial\" size=\"2\">\n";
    $output .= "Subscribe <input type=\"radio\" name=\"action\" value=\"subscribe\" checked> &nbsp\; &nbsp\; Unsubscribe <input type=\"radio\" name=\"action\" value=\"unsubscribe\"><p>\n";
    $output .= "<input type=\"submit\" value=\"Submit\"> <input type=\"reset\" value=\"Reset\">\n";
    $output .= "</form>\n";
    }
