#!/usr/bin/perl

# English-Metric Conversion Program
# Version 2.0
# enmeco.pl
# By Mike Calabrese
# http://www.mikecalabrese.com
# 07-16-2003

# This makes sure that the data is coming from our site
$refurl = ("http://www.yourdomain.com/exact/url/path/to/enmeco/enmeco.html");
if ($ENV{'HTTP_REFERER'} !~m#^$refurl#) {
    &bad_referer;
}

# Receive and clean up the  data
$theinput = <>;
chomp ($theinput);

# Split the elements at ampersands
@theinput = split (/&/,$theinput);

# Assign variables to the elements
$amount = substr($theinput[0],7);
$convert = substr($theinput[1],8);

# Check for valid numbers
$amount =~ s/%([0-9|A-F]{2})//g;
$amount =~ s/([;<>\*\|'&\$!#\(\)\[\]\{\}:"])//g;
if ($amount =~ /(\d.\d)/ || $amount =~ /(\d)/ || $amount =~ /(.\d)/)
{

# Remove the capitals
$convert = lc($convert);

# Remove any non alpha-numeric charactors
$convert=~s/\W//g;

# In each case below test for which conversion,
# do the proper calculations,
# then test whether input or the results = 1
# round off the output decimal places to the nearest hundreth,
# and return plural or singular values,
# add commas where needed,
# then call the html subroutine

if ($convert eq "milestokilometers") {
$result = ($amount * 1.609344);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "mile";
} else {
$from_value = "miles";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "kilometer";
} else {
$to_value = "kilometers";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "kilometerstomiles") {
$result = ($amount * 0.621371);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "kilometer";
} else {
$from_value = "kilometers";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "mile";
} else {
$to_value = "miles";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "yardstometers") {
$result = ($amount * 0.9144);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "yard";
} else {
$from_value = "yards";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "meter";
} else {
$to_value = "meters";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "meterstoyards") {
$result = ($amount * 1.093613);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "meter";
} else {
$from_value = "meters";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "yard";
} else {
$to_value = "yards";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "feettometers") {
$result = ($amount * 0.3048);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "foot";
} else {
$from_value = "feet";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "meter";
} else {
$to_value = "meters";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "meterstofeet") {
$result = ($amount * 3.280840);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "meter";
} else {
$from_value = "meters";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "foot";
} else {
$to_value = "feet";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "inchestocentimeters") {
$result = ($amount * 2.54);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "inch";
} else {
$from_value = "inches";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "centimeter";
} else {
$to_value = "centimeters";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

elsif ($convert eq "centimeterstoinches") {
$result = ($amount * 0.393701);
if ($amount eq 1 || $amount eq "1.00") {
$from_value = "centimeter";
} else {
$from_value = "centimeters";
}
$result = sprintf("%.2f", $result);
if ($result eq 1 || $result eq "1.00") {
$to_value = "inch";
} else {
$to_value = "inches";
}
$amount = comify($amount);
$result = comify($result);
&results;
}

sub results {
   print "Content-type: text/html\n\n";
   print "<html><head><title>English-Metric Conversion Program\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>English-Metric Conversion Program</H3>\n";
   print "<BR><HR WIDTH=\"400\">\n";
   print "<B>$amount</B> $from_value is equal to <B>$result</B> $to_value. \n";
   print "<BR><HR WIDTH=\"400\">\n";
   print "</TD></TR></TABLE></CENTER>\n";
   print "<P ALIGN=Center>\n";
   print "<SMALL>This script by \n";
   print "<A HREF=http://www.mikecalabrese.com/>";
   print "Mike Calabrese</A></SMALL>\n";
   print "</BODY></HTML>\n";
}
} else {

# Print out the error page
print "Content-type: text/html\n\n";
print "<html><head><title>ERROR!\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 "<center>\n";
print "<table width=600 cellpadding=0 cellspacing=0>\n";
print "<center>\n";
print "<H3><B>Sorry, you typed in an invalid Number.\n";
print "</B></H3>\n";
print "<BR>\n";
print "Please use your browser back button and \n";
print "try again.\n";
print "</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;
}