#!/usr/bin/perl
###########################################################
#####################  Link Redirect  #####################
#####################  Version 1.0    #####################
#####################                 #####################
#####################  # # # # # # #  #####################
#####################                 #####################
#####################    Made by:     #####################
#####################    NetMade      #####################
#####################                 #####################
###########################################################
#                  http://www.weblication.net             #
###########################################################
###########################################################
#                                                         #
# Link Redirect, is a program, which enables you to hide  #
# the actual URL from all nosy users in your HTML code.   #
# The way this works, is that you send a special code to  #
# the program, which in turn finds this code in a         #
# database, and redirects the user to a location found in #
# the database. In this way, it is not possibly for       #
# anybody to steel all of your links, simply by copying   #
# your HTML.                                              #
#                                                         #
###########################################################
#                                                         #
#   This release includes the following files:            #
#                                                         #
#   * readme.txt       - The readme file                  #
#   * link_redirect.pl - This file                        #
#   * linkdata         - The database file                #
#                                                         #
###########################################################
#                     Copyright                           #
###########################################################
#                                                         #
#  The software is licensed on a non-exclusive , right to #
#  use basis. All title and copyright in and to the       #
#  software product (including but not limited to any     #
#  images , photographs, animations, video, audio, music  #
#  and text incorporated into the software product , the  #
#  accompanying printed materials and any copies of the   #
#  software product are owned by NetMade . The software   #
#  is protected by copyright laws and international       #
#  treaty provisions.                                     #
#                                                         #
###########################################################
#                 Limitation of Liability                 #
###########################################################
#                                                         #
#  To the maximum extent permitted by applicable law,     #
#  Netmade disclaim all warranties, either express or     #
#  implied, including, but not limited to implied         #
#  warranties of merchantability and fitness for a        #
#  particular purpose with regard to the software and the #
#  accompanying written materials.                        #
#                                                         #
#  To the maximum extent permitted by applicable law,     #
#  in no event will NetMade be liable for any damages     #
#  whatsoever (including without limitation, special,     #
#  incidental, consequential , or indirect damages for    #
#  personal injury, loss of business profits, business    #
#  interruption, loss of business information , or any    #
#  other pecuniary loss) arising out of the use of or     #
#  inability to use this product , even if NetNade has    #
#  been advised of the possibility of such damages.       #
#                                                         #
###########################################################

# Please modify this variable accorgingly to the instructions
# in the readme file!


$DATABASE_FILE = "/home/gold/public_html/cgi-bin/linkdata.db";
$logfile = '/home/gold/public_html/cgi-bin/redir.txt';
$url = '';

&hent_input;
&tjek_input;
&hent_database;
&fejl("$link");

sub fejl  {
	
	$fejl_besked = @_[0];
	if (!$fejl_besked)  {
		$fejl_besked = "A unexplainable error has occurred";
	}
	
	print "Content-type: text/html\n\n";
	# Så printer vi lige en content-type, så browseren ved hvad vi sender

	print <<"(HTMLSLUT)";	
	 
	<HTML>
<HEAD>
   <TITLE>Link Redirect - A error has occurred!</TITLE>
</HEAD>
<BODY BGCOLOR="#999999">
<P>

<P>&nbsp;

<CENTER><TABLE BORDER=0>
   <TR>
      <TD>
         <P>
      </TD>
      <TD WIDTH=40%>
         <P><TABLE BORDER=0 BGCOLOR="#CCCCCC">
            <TR>
               <TD BGCOLOR="#666666">
                  <CENTER><FONT COLOR="#FFFFFF"><B>A error has occurred</B></FONT></CENTER>
               </TD>
            </TR>
            <TR>
               <TD>
                  <P>While processing your request, the following error occurred 
                  
                  <UL>
                     <LI>$fejl_besked ($!)</LI>
                  </UL>
                  
                  <P>Please report the error to the webmaster of this site

                  <P>&nbsp;
                  <CENTER>Yours faithfully
                  
                  <P><B><I>Link Redirecter</I></B></CENTER>
               </TD>
            </TR>
         </TABLE>
      </TD>
      <TD>
         <P>
      </TD>
   </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>

(HTMLSLUT)

die;

}

sub hent_input {

	
	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 tjek_input {

	if(defined($input{'link'})) {
	
		# Hvis der er defineret hvilken kunde, som forsøger at bruge formularen.

		$input{'link'} =~ s/\s//g;
		$input{'link'} =~ s/\n//g;
		$input{'link'} =~ s/(^\D\D)/\L$1/g;

		if ($input{'link'} =~ s/(^\D\D\d\d\d\d\d\d)/$1/g) {

		# Først fjerner vi mellemrum og newlines. Derefter søger vi efter to små
		# bogstaver, og 6 tal, hvilket repræsenterer MV-Gruppens medlemsnummer.
		# Vi er nu klar til at hente data ind fra filen.
	
		}

		else  {

			&fejl("The link value is not set to a correct value");

			# Der er sket en fejl med kunde nummeret, hvilket
			# vi fortæller
		}
	}
	
	else  {
	
		&fejl("No value has been specified");	
	}
}


sub hent_database  {

	if(-e $DATABASE_FILE)  {
		
		open(DATABASE, $DATABASE_FILE) || &fejl("The database could not be opened");
	
		# Her åbner vi databasen, efter at vi har tjekket at den eksisterer.
		# Hvis ikke, sender vi en fejlmeddelelse.

		while ($post = <DATABASE>)  {

			if ($post =~ /^$input{'link'}\|\|/) {
			
				# Vi har nu fundet den kunde, som det drejer sig om!
				$url = (split (/\|\|/, $post))[1];
				# print $url;
				last;
			}
		}
		close(DATABASE);
		{
		($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time + 50400);
	    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, ">>$logfile");
		print LOG "$mday/$mon/$year $hour:$min:$sec $url\n";
		}
    	close(LOG);

		if($url ne '')  {
			print "Location: $url \n\n";
			die;
		}
		else  {
			&fejl("The link value does not exist in the database");
		}
	}
	else {
		&fejl("The database could not be found");
	}
}
