#!/usr/bin/perl
# Temperature Conversion Program
# Version 2.0
# By Mike Calabrese
# http://www.mikecalabrese.com
# 07-22-2003

# 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.

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

#Receive & 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],7);

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

if ($amount =~ /\d{1,20}?/g)
{

# Perform the necessary computations dependent
# upon which radio button (variable $convert) was selected
if ($convert eq "farcon") {

# Fahrenheit Formulas
$temperature = (5 * ($amount - 32)) / 9; # To Celsius
$kelvins = (5 * ($amount - 32)) / 9 + 273.15; # To Kelvins
$rankine = $amount + 459.67; # To Rankine
$reamur = (($amount - 32) * 4) / 9; # To Reaumur

# Round off output to the nearest tenth where needed
$temperature = sprintf("%.1f", $temperature);
$kelvins = sprintf("%.1f", $kelvins);
$rankine = sprintf("%.1f", $rankine);
$reamur = sprintf("%.1f", $reamur);

# Comify the output
$amount = comify($amount);
$temperature = comify($temperature);
$kelvins = comify($kelvins);
$rankine = comify($rankine);
$reamur = comify($reamur);

# Insert an "F" for fahrenheit after $amount on the results page
$celcius = "F";

# Specify Celsius on the output page
$fahrenheit = "Celsius";

# output the results
&results;

} elsif ($convert eq "celcon") {

# Celsius Formulas
$temperature = ((9 * $amount)/ 5) + 32; # To Fahrenheit
$kelvins = $amount + 273.15; # To Kelvins
$rankine = $temperature + 459.67; # To Rankine
$reamur = ($amount * 4) / 5; # To Reaumur

# Round off the output to the nearest tenth
$temperature = sprintf("%.1f", $temperature);
$kelvins = sprintf("%.1f", $kelvins);
$rankine = sprintf("%.1f", $rankine);
$reamur = sprintf("%.1f", $reamur);

# Comify the output
$amount = comify($amount);
$temperature = comify($temperature);
$kelvins = comify($kelvins);
$rankine = comify($rankine);
$reamur = comify($reamur);

# Specify Fahrenheit on the output page
$celcius = "C";

$fahrenheit = "Fahrenheit";
&results;
}

sub results {

print "Content-type: text/html\n\n";
print "<html><head><title>Temperature Conversion Program\n";
print "</title></head>\n";
print "<body bgcolor=#ffffff text=#000000 link=#008000 vlink=#400000>\n";
print "<center>\n";
print "<table width=\"400\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "<TR><TD>\n";
print "<center><H3>Temperature Conversion Program</H3>\n";
print "<P><B>$amount&#176; $celcius </B>converts\n";
print "to:<HR>\n";
print "$fahrenheit:  <B>$temperature&#176;</B><BR>\n";
print "Kelvin:  <B>$kelvins&#176; ($kelvins kelvins)</B><BR>\n";
print "Rankine:    <B>$rankine&#176;</B><BR>\n";
print "Reaumur:    <B>$reamur&#176;</B><BR>\n";
print "<HR>\n";
print "</center>\n";
print "</TD></TR></TABLE></CENTER>\n";
print "<P ALIGN=Center>\n";
print "This script by: \n";
print "<A HREF=\"http://www.mikecalabrese.com/\">\n";
print "Mike Calabrese</A>\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 "<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\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;
}