#!/usr/bin/perl
######################################################################
#  BEFORE TRYING TO EDIT THIS SCRIPT, READ THE README FILE
###################################################################### 
#
#     Dream Catchers CGI Scripts               Feel free to modify 
#     Free Linkage                             this script to your 
#     Created by Seth Leonard                  needs, but please
#     for Dream Catchers Technologies, Inc.    keep this portion so
#                                              that I get credit.  
#     http://dreamcatchersweb.com/scripts      The same goes for 
#                                              distribution.
#
#     (c)1996/1997 Dream Catchers Technologies, Inc.,
#     All Rights Reserved
#
######################################################################

# ONLY EDIT THIS PART OF THE SCRIPT!!!!

$freelocation = "http://www.foxsden.org/simfanatic/sim/linkage/free.htm";
$freepath = "/tmp_mnt/npac/server/foxsden00/sroot/droot/simfanatic/sim/linkage/free.htm";
$cgilocation = "http://boss.npac.syr.edu:8080/cgi-bin/sim/linkage/free.pl";

# DO NOT EDIT BELOW THIS LINE!!!!
#####################################################################

# Receive Info From Free Link
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ s/<!--(.|\n)*-->//g;
	$value =~ s/<([^>]|\n)*>//g;
	$INPUT{$name} = $value;
}

# Give Error Messages
&no_url unless $INPUT{'url'};
&no_title unless $INPUT{'title'};
&no_category unless $INPUT{'category'};

# Set Up Free Link Page
open (FILE, "$freepath");
@lines = <FILE>;
close(FILE);

# Write To Free Link Page
open (FREE,">$freepath");

foreach $line (@lines) {
	if ($line =~ /<!--$INPUT{'category'}-->/) { 
		print FREE "<!--$INPUT{'category'}-->\n";
		print FREE "<a href=\"$INPUT{'url'}\">$INPUT{'title'}</a><br>\n";
	} else {
		print FREE ("$line");
	} 
}

close (FREE);

&follow;

# Error Pages

sub no_url {

print ("Content-type: text/html\n\n");
print ("<html><head><title>No URL</title></head>\n");
print ("<body><h1>URL appears to be blank</h1>\n");
print ("You didn't add your URL so your entry to the Free Link Page was not added.\n");
print ("Please add your url below.<br>\n");
print ("<form method=POST action=\"$cgilocation\">\n");
print ("Page URL: <INPUT TYPE=\"text\" NAME=\"url\" VALUE=\"http://\" SIZE=\"40\">\n");
print ("<INPUT TYPE=\"hidden\" NAME=\"title\" VALUE=\"$INPUT{'title'}\"><br><br>\n");
print ("<INPUT TYPE=\"hidden\" NAME=\"category\" VALUE=\"$INPUT{'category'}\"><br><br>\n");
print ("<input type=submit VALUE=\"Submit Link\"><hr>\n");
print ("</body></html>\n");

exit;

}

sub no_title {

print ("Content-type: text/html\n\n");
print ("<html><head><title>No Title</title></head>\n");
print ("<body><h1>Title appears to be blank</h1>\n");
print ("You didn't add your Title so your entry to the Free Link Page was not added.\n");
print ("Please add your url below.<br>\n");
print ("<form method=POST action=\"$cgilocation\">\n");
print ("Page Title: <INPUT TYPE=\"text\" NAME=\"title\" SIZE=\"40\">\n");
print ("<INPUT TYPE=\"hidden\" NAME=\"url\" VALUE=\"$INPUT{'url'}\"><br><br>\n");
print ("<INPUT TYPE=\"hidden\" NAME=\"category\" VALUE=\"$INPUT{'category'}\"><br><br>\n");
print ("<input type=submit VALUE=\"Submit Link\"><hr>\n");
print ("</body></html>\n");

exit;

}

sub no_category {

print ("Content-type: text/html\n\n");
print ("<html><head><title>No Category</title></head>\n");
print ("<body><h1>Category appears to be blank</h1>\n");
print ("You didn't add your Category so your entry to the Free Link Page was not added.\n");
print ("Please hit \"BACK\" on your Browser and fix this\n");
print ("</body></html>\n");

exit;

}

sub follow {

print ("Content-type: text/html\n\n");
print ("<html><head><title>Thank You</title></head>\n");
print ("<body><h1>Thank You For Adding a Link</h1>\n");
print ("Here's what you added:<br><br>\n");
print ("<b>Page URL:</b> $INPUT{'url'}<br>\n");
print ("<b>Page Title:</b> $INPUT{'title'}<br><hr>\n");
print ("Back to <a href=\"$freelocation\">The Free Links Page</a>. ");
print ("<b>If you do not see your entry, hit RELOAD<br>\n");
print ("</body></html>\n");

exit;

}
