#!/usr/bin/perl # # Author: Trever Furnish, trever AT wondious D0T com, 2002-03-29. # Released as public domain code with absolutely no warranty for any purpose. # # This script is meant to be called from a web browser and executed on # a web server. It provides a form for entering a bandwidth measurement and # a set of units to convert to. Required Perl, probably 5.6 or later. # This kind of thing really should just be pure javascript, but I don't know # javascript very well, so... :-) It also works best if you link it using # javascript to have it open in its own little window, like so: #bandwidth converter # $error_contact="t\@wondious.com"; $my_url="/bwconvert/index.cgi"; $workdir="/var/www/html/bwconvert"; use HTML::Entities; # for encode/decode_entities() use CGI; # CGI::Carp for making our errors more useful. # fatalsToBrowser: fatal errors go to the browser. # set_message: Define the message to send to the browser for errors. # warningsToBrowser: warnings go to browser as comments. # warningsToBrowser(0) to disable temporarily # warningsToBrowser(1) to enable again. use CGI::Carp qw(fatalsToBrowser warningsToBrowser set_message); # Limit the max post data we'll accept at once for security. $CGI::POST_MAX=1024*1024*1024; # 1 megabyte # Disable CGI upload acceptance for security. $CGI::DISABLE_UPLOADS=1; # Disable uploads. BEGIN { sub handle_errors { my $msg = shift; print "

Thundercats Whoa!!!

"; print "Got an error message:\n

",
			encode_entities("$msg"), "\n
"; } set_message(\&handle_errors); } # Parse the client data into %in. CGI::ReadParse(); print < EOT #-#print <Blah #-# #-#Foo #-#a is $in{"a"}. #-# #-#EOT #-##exit 0; if ($in{"a"} eq "c") { # Convert something. unless (defined $in{"oldunits"} and defined $in{"newunits"}) { print "You are a bad person. Tell me what units to use!\n"; exit 0; } my $amount=&clean_data($in{"amount"}); my $newval=&myconvert($amount, $in{"oldunits"}, $in{"newunits"}); unless (defined $newval) { print "You really suck, huh?\n"; exit 0; } my %nicename = ( "bs" => "bits per sec", "Bs" => "bytes per sec", "kbs" => "kilobits per sec", "kBs" => "kilobytes per sec", "mbs" => "megabits per sec", "mBs" => "megabytes per sec", "gbs" => "gigabits per sec", "gBs" => "gigabytes per sec", ); print <Bandwidth Measurement Converter $amount $nicename{$in{oldunits}} equals $newval $nicename{$in{newunits}}\n

Back EOT exit 0; } elsif ($in{"a"} eq "a") { # Show an "about" box. print "I like you.\n"; exit 0; } else { # Default - show a blank form. print <Bandwidth Measurement Converter

Bandwidth Converter:
From:
to:
EOT exit 0; } sub myconvert { my ($amount, $oldunits, $newunits) = (@_); my %factors = ( "bs" => 1, "kbs" => 2**10, "mbs" => 2**20, "gbs" => 2**30, "Bs" => 8, "kBs" => 8*2**10, "mBs" => 8*2**20, "gBs" => 8*2**30 ); unless (exists ($factors{$oldunits}) and exists ($factors{$newunits})){ return undef; } # Convert to bits/sec... $result = ($amount * $factors{$oldunits}) / $factors{$newunits}; return $result; } sub clean_data { my $word=shift; $word=~s/[^1-90\.]//g; if ($word=~/\..*\./) { # $word has multiple periods in it. Screw that. $word=~s/\.//g; } return $word; }