#! /usr/bin/perl

#
#                  WEB-IT Internet Services 
#	  	        Add Link v 1.1 
#-------------------------------------------------------------------#
# Installation Instructions: 
#-----------------------------
#
# 1: change the settings below to what your settings are!!!	
# 2: Copy this file link.pl to your cgi-bin
# 3: Copy the links.html to a separate directory
#	eg /links/
# 4: chmod links.pl to 755
# 5: chmod links.html to 777
# 6: chmod /links/ to 777
# 7: change all of the settings below to your servers settings
# 8: make sure to set links.html to call link.pl
#-------------------------------------------------------------------# 
# This program is Freeware and shall be distributed freely
# Do not change the Header/Credits in any way.
# 
#	WEB-IT Internet Services 
#	http://webit.ml.org/
#	e-mail webit@iname.com 
#
#	WEB-IT CGI Scripts: 
#	http://webit.ml.org/scripts/
#	e-mail webit@iname.com       
#

#################################### Settings ###################################

# UNIX PATH TO directory.html

$file = "/data1/hypermart.net/direct/links/links.html";

# URL TO directory.html

$gto = "http://direct.hypermart.net/links/links.html";

# ADD LINKS TO TOP OR BOTTOM (1 = top   2 = bottom)

$top = "1";

#################################################################################

# Get the input data
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Parse the data
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
      	($name, $value) = split(/=/, $pair);

	# get rid of the % encoding
      	$value =~ tr/+/ /;
      	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      	$name =~ tr/+/ /;
      	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
      	$value =~ s/<!--(.|\n)*-->//g;
	
	if(! $allow_html) {
      		$value =~ s/<([^>]|\n)*>//g;
	}

      	$FORM{$name} = $value;
}

open(FILES, "$file") || die "Can't open FILE: $file\n";
@files =<FILES>;
close(FILES);

open(FILES, ">$file") || die "Can't open FILE: $file\n";
foreach $line (@files) {
	if($line =~ /<!--Begin Directory-->/i) {
		if($top) {
			print FILES "<!--Begin Directory-->\n";
		}
		
		print FILES "<br><li><a href=$FORM{'url'}>$FORM{'name'}</a>\n";		

		if(! $top) {
			print FILES "<!--Begin Directory-->\n";
		}
	}
	else {
		print FILES "$line";
	}
}
close(FILES);

print "Location: $gto\n\n";


