#!/usr/bin/perl

my ($thePage) = "Location: http://prop.hfradio.org\nURI: http://prop.hfradio.org\n\n";
print $thePage;
exit;


$sendEmail = 0;

# -- in.cgi -- acts upon new client form submissions

#$html_header = "Content-type: text/html\n\n";
#$copyrightLine = "\n<!-- This software is copyright, 1999 by Tomas Hood.  All rights reserved. http://accessnow.com -->\n";

# --- 
#
# copyright, 1999, Tomas Hood.  Script written by Tomas Hood
# ---

# --- initialize environment ---

$redirToURL = "prop.hfradio.org";

my $mail_prog = "/usr/lib/sendmail";
my $email_ID_to = "nw7us\@hfradio.org";
my $email_ID_from = "nw7us\@hfradio.org";
my $email_subject = "Redir to External Site";

$theDate = `date +%m-%d-%Y`;
$theDate =~ tr/\n\r//d;
$theTime = `date +%H:%M:%S`;
chop $theTime;
 
$remoteHost      = $ENV{'REMOTE_HOST'};
$http_from       = $ENV{'HTTP_FROM'};
$http_user       = $ENV{'HTTP_USER_AGENT'};
$http_refr       = $ENV{'HTTP_REFERER'};
$remote_address  = $ENV{'REMOTE_ADDR'};

if ($remote_address =~ m|24\.234\.0\.80|i) {
	$thePage = "Location: http://hfradio.org\nURI: http://hfradio.org\n\n";
	print $thePage;
	exit;
} elsif ($http_refr =~ m|swiss|i) {
	$thePage = "Location: http://hfradio.org/about.html\nURI: http://hfradio.org/about.html\n\n";
	print $thePage;
	exit;
}

%form_data = &get_form_data(*form_data);        # Get form data

if (exists ($form_data{'redir'})) {
	$redirToURL = $form_data{'redir'};
}

open (LOG, ">> /hfradio/logs/redir.log");

if ($sendEmail) {

	open (MAIL, "|$mail_prog -t -oi") || die "Can't open mail_prog: $mail_prog\n";

	print MAIL "To: $email_ID_to\n";
	print MAIL "Subject: $email_subject - $redirToURL\n";
	print MAIL "Reply-To: $email_ID_to\n";
	print MAIL "From: $email_ID_from\n\n";
	print MAIL "Referred on $theDate at $theTime\n\n";
	print MAIL <<mrr;
TO:   http://$redirToURL
WHO:  $remoteHost
REFR: $http_refr
FROM: $http_from
USER: $http_user
RADD: $remote_address

mrr
	close (MAIL);
}

print LOG "[$theDate $theTime]\n";

print LOG <<rrr;
TO:   http://$redirToURL
WHO:  $remoteHost
REFR: $http_refr
FROM: $http_from
USER: $http_user
RADD: $remote_address

rrr
close (LOG);

&load_page();

print "$thePage";

exit;

#######################################

sub load_page {

	$thePage = "";

	
	$thePage = "Location: http://$redirToURL\nURI: http://$redirToURL\n\n";
	$thePage;
}

sub get_form_data {

        if (@_) {
                local (*in_) = @_;
        }
        local ($i, $loc, $key, $val, $key_value);

        if ($ENV{'REQUEST_METHOD'} eq "GET") {
                $in = $ENV{'QUERY_STRING'};
        } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
                for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
                        $in .= getc;
                }
        } else {
                exit;
        }

        @input = split(/&/,$in);

        foreach $key_value (@input) {
                ($key, $value) = split (/=/, $key_value);
                $value =~ tr/+/ /;
                $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex($1))/eg;
                if (defined($in_{$key})) {
                        $in_{$key} = join("\0", $in{$key},$value);
                } else {
                        $in_{$key} = $value;
                }
        }
        %in_;
}
