#!/usr/bin/perl
# Classic Appalachian Trail Distance Calculator
# atdist.pl
# Version 3.1
# By Mike Calabrese
# http://www.mikecalabrese.com/index.shtml
# 01-09-2006
# This program is available for free on an AS-IS basis. I am not
# responsible for consequential damages, loss of income or whatever
# else that may happen as a result of the use of this program.
# ***PLEASE NOT THAT THIS APPLICATION IS NO LONGER BEING UPDATED ***
# *** AS OF 01-01-2007 ***
# This makes sure that the data is coming from your site
$refurl = ("yourdomain.com");
if ($ENV{'HTTP_REFERER'} !~ /$refurl/g) {
&bad_referer;
}
# Receive and clean up the data
$theinput = <>;
chomp ($theinput);
$theinput=~s/\,//g;
$theinput=~s/\+//g; # Convert plus signs to spaces
$theinput=~s/%(..)/pack("c",hex($1))/ge; # keep the @ from showing as %40
# Split the elements at ampersands
@theinput = split (/&/,$theinput);
# Assign variables to the elements
$starting_point = substr($theinput[0],6);
$ending_point = substr($theinput[1],6);
# Remove the capitals
$starting_point = lc($starting_point);
$ending_point = lc($ending_point);
# Remove the comma stuff
$starting_point=~s/%2c//g;
$ending_point=~s/%2c//g;
# Remove any non alpha-numeric charactors
$starting_point=~s/\W//g;
$ending_point=~s/\W//g;
# This is the exact UNIX path to the data files
$points_directory = "/exact/UNIX/path/to/points/directory";
# Open the starting point files and get start distance info
chdir($points_directory);
open(MYFILE, "$starting_point");
$starting_file = <MYFILE>;
@starting_file = split (/,/, $starting_file);
$starting_file_point = $starting_file[0]; # Start point
$starting_file_mileage = $starting_file[1]; # Start mileage
# Open the ending point files and get end distance info
chdir($points_directory);
open(MYFILE, "$ending_point");
$ending_file = <MYFILE>;
@ending_file = split (/,/, $ending_file);
$ending_file_point = $ending_file[0]; # End point
$ending_file_mileage = $ending_file[1]; # End mileage
# Compute the distance between the 2 points
$thedistance = abs($ending_file_mileage-$starting_file_mileage);
# Round off distances to nearest tenth where needed
$thedistance = sprintf("%.1f", $thedistance);
# Place commas in the distance where needed
$thedistance = comify($thedistance);
# Give out the results
print "Content-type: text/html\n\n";
print "<html><head><title>Classic Appalachian Trail Distance Calculator\n";
print "</title></head>\n";
print "<BODY bgcolor=#ffffff text=#000000 link=#008000 vlink=#400000>\n";
print "<center>\n";
print "<table width=600 cellpadding=0 cellspacing=0>\n";
print "<TR><TD>\n";
print "<center>\n";
print "<H3>Classic Appalachian Trail Distance Calculator</H3>\n";
print "<BR><HR WIDTH=\"450\">\n";
print "The distance on the Appalachian Trail between <BR>\n";
print "<B>$starting_file_point</B> and <BR><B>$ending_file_point</B> \n";
print "is <BR><B>$thedistance</B> miles.\n";
print "<BR><HR WIDTH=\"450\">\n";
print "The <I>Data Book</I> and A.T. maps can be purchased at<BR> \n";
print "<A HREF=http://www.atctrailstore.org>The Ultimate Trail \n";
print "Store</A> of the <A HREF=http://www.appalachiantrail.org>\n";
print "Appalachian \n";
print "Trail Conservancy</A>.\n";
print "<P><SMALL>This script by <A HREF=\"http://www.mikecalabrese.com\">\n";
print "Mike Calabrese</A></SMALL></CENTER>\n";
print "</TD></TR></TABLE></CENTER>\n";
print "</BODY></HTML>\n";
sub bad_referer {
print "Content-Type: text/html\n\n<html><head>\n";
print "<title>403 Forbidden</title></head>\n";
print "<body><h2>403 Forbidden</h2></body></html>\n";
exit;
}
sub comify {
my $text = reverse $_[0];
$text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g;
return scaler reverse $text;
}