#!/usr/local/bin/perl5
#######################################################################
###########################  Link Redirect  ###########################
###########################   Version 1.0    ##########################
#######################################################################
#                                                                     #
#         This release includes the following files:                  #
#                                                                     #
#        * readme.txt       - The readme file                         #
#        * link.pl          - This file                               #
#        * links.db         - The database file                       #
#        * log.txt          - The log file                            #
#                                                                     #
#######################################################################

# The following variables should be set to define the locations
# and URLs of various files, as explained in the readme.txt.

# The Server path to the database
$database_file = "/usr/www/users/travelpr/portmacquarie/cgi-bin/links.db";

# The Server path to keywords file to log Links search terms. You do NOT 
# need to create this file in advance - the script will create the file 
# if it is not found. The file should be chmod to 0666 (rw_rw_rw_).
$log_file = '/usr/www/users/travelpr/portmacquarie/cgi-bin/log.txt';

# The email address of the Site Administrator.
$admin = 'admin@travelpromote.com.au';

# The difference in seconds of your time zone to your servers. 
$zone = '50400';

# NOTHING BELOW THIS LINE NEEDS TO BE ALTERED!

$url = '';

&getinput;
&checkinput;
&opendatabase;
&error("$id");

sub getinput {
	
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
 	 read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
	}

	if ( $ENV{'REQUEST_METHOD'} eq "GET")  {
	  	
		if($input)  {

			$input .= "\&";
		}
	
		$input .= $ENV{'QUERY_STRING'};
	} 

	@input = split (/&/, $input);
	foreach (@input) {
		  s/\+/ /g;
		  ($name, $value) = split (/=/,$_);

  		 $name =~s/%(..)/pack("c",hex($1))/ge;
 		 $value =~s/%(..)/pack("c",hex($1))/ge;
 		 $name =~ s/\+/ /g;
	 	 $value =~ s/\+/ /g;
 		 $input{$name} .= "\0" if (defined($input{$name}));
 		 $input{$name} .= $value;
	}
}

sub checkinput {

	if(defined($input{'id'})) {
		$input{'id'} =~ s/\s//g;
		$input{'id'} =~ s/\n//g;
		$input{'id'} =~ s/(^\D\D)/\L$1/g;

		if ($input{'id'} =~ s/(^\D\D\d\d\d\d)/$1/g) {
		}

		else  {

			&error("The link value is not set to a correct value");
		}
	}
	
	else  {
	
		&error("No value has been specified");	
	}
}

sub opendatabase  {

	if(-e $database_file)  {
		
		open(DATABASE, $database_file) || &error("The database could not be opened");
		while ($post = <DATABASE>)  {
			if ($post =~ /^$input{'id'}\|\|/) {
				$url = (split (/\|\|/, $post))[1];
				last;
			}
		}
		close(DATABASE);

		if($url ne '')  {
			($sec, $min, $hour, $mday, $mon, $year) = localtime(time + $zone);
		    if ($sec < 10) { $sec = "0$sec"; }
		    if ($min < 10) { $min = "0$min"; }
		    if ($hour < 10) { $hour = "0$hour"; }
		    if ($mon++ < 10) { $mon = "0$mon"; }
		    if ($mday < 10) { $mday = "0$mday"; }
			open(LOG, ">>$log_file") || &error("The logfile could not be opened");
			print LOG "$mday/$mon/$year $hour:$min:$sec $url\n";
		   	close(LOG);
			print "Location: $url \n\n";
			die;
		}
		else  {
			&error("The link value does not exist in the database");
		}
	}
	else {
		&error("The link database could not be found");
	}
}
	
sub error {
	
	$error = @_[0];
	if (!$error)  {
		$error = "An unexplained error has occurred";
	}
	print "Content-type: text/html\n\n";
	print <<"HTML code";	
	<HTML>
	<HEAD>
	<TITLE>An error has occurred!</TITLE>
	</HEAD>
	<BODY>
	<br>
	<br>
	<CENTER><TABLE BORDER=0>
	<TR>
	<TD>
	<P>
	</TD>
	<TD WIDTH=450>
	<P><TABLE BORDER=0>
	<TR>
	<TD BGCOLOR="#FFFFFF" ALIGN="CENTER">
	<FONT FACE="Arial" COLOR="#FF0000"><B>An error has occurred</B></FONT>
	</TD>
	</TR>
	<TR>
	<TD ALIGN="CENTER"><FONT FACE="Arial">
	<P>While processing your request, the following error occurred:</FONT>
	<UL>
	<LI><FONT FACE="Arial" COLOR="#FF0000">$error $!</FONT></LI>
	</UL>
	<FONT FACE="Arial">
	<P>Please report the error to <A HREF="MAILTO:$admin">$admin</a></FONT>
	</TD>
	</TR>
	</TABLE>
	</TD>
	</TR>
	</TABLE>
	</CENTER>
	</BODY>
	</HTML>

HTML code
die;

}