#!/usr/bin/perl -w use POSIX qw(ceil floor); # Keep output buffer flushed to update web page as we generate it $|++; # No matter what, we need to output a web page of SOME kind! html_start("Media Size to Pages"); # Characters allowed in user inputs # These include the alpha number, security code, lab number, # instructor name, and most importantly, the URLs! # We allow ONLY these characters to be present in inputs. # everything else gets converted to an underscore. # See the function find_param for details # Remember: # http://foo.com/~bob/my-pa%41ge_is.cool2#links.html is legal! # Spaces must be allowed for student comments # Maybe in the future we could specify one set of "allowed" characters # for URLs and another for comments? (jk 18 May 04) $ok_chars = "-_A-z0-9\:\/\.\#\~\$\% "; # Determine which method we were called with and use that to find # the query string. If this is a GET request, just look at the # environment variable QUERY_STRING. If it's a POST request, read # from standard input. $ENV{'REQUEST_METHOD'} =~ /GET/ && ($query_string = $ENV{'QUERY_STRING'}); $ENV{'REQUEST_METHOD'} =~ /POST/ && ($query_string = ); sub commafy { my $text = reverse $_[0]; $text =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; return scalar reverse $text; } sub commify { my $num = shift; # RBF - There has to a better way to do this $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; $num =~ s/([0-9]+)([0-9]{3})/$1,$2/g; return $num; } sub units_text { my $v = shift; $v eq 0 && return "KB"; $v eq 1 && return "MB"; $v eq 2 && return "GB"; $v eq 3 && return "TB"; $v eq 4 && return "PB"; $v eq 5 && return "EB"; return "XX"; } sub html_start { my $title = shift; print "Content-type:text/html\n\n"; print "\<\?xml version=\"1.0\"?>\n"; # print "\n"; print "\n\n"; print "\n"; print ""; print " $title\n"; print "\n\n\n"; } sub html_stop { print "\n\n"; } sub html_error { my $text = shift; print "

ERROR

\n"; print "

$text

\n"; print "

\n

\n"; print " Go back and try again
\n"; print " Send e-mail to the admin: $admin\n"; print "

\n"; html_stop(); exit 1; } # Find a given tag's value in the query string. That is, from # foo=bar&cow=moo&jack=friend, if asked to find "cow", return "moo" sub find_param { my $tag = shift; # If the identifier isn't in the query string, quit now. # THIS IS NOT A GOOD CHECK! Need to add checks for &'s and ='s before # and after the tag. That is, check for &foo= instead of just foo. # This is left as an exercise for the code maintainer. # With the current (18 May 04) # submission form, it shouldn't be a problem though. $query_string =~ /$tag/ || return ""; my $param = $query_string; # Remove everything up to and including identifier and the equals sign $param =~ s/^(.*)($tag\=)(.*)$/$3/; # Remove everything after the first & sign (if it exists) $param =~ s/\&.*$//; # Translate from hex into human format # Turn pluses into spaces $param =~ tr/+/ /; # Change hex values back into ASCII $param =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg; # Remove HTML comments. (I can't remember why I put this in here. # I suspect it was part of something I copied. It's probably safe # to remove it. -jk 18 May 04) $param =~ s///g; # Turn all not allowed characters into underscores $param =~ s/[^$ok_chars]/_/og; return $param; } sub atof { my $t = 0; my $count = 1; my $decimal = 0; foreach my $d (split(//, shift())) { if ($d eq "\.") { $decimal = 1; } else { if ($decimal) { $t += ($d / (10 ** $count)); $count++; } else { $t = $t * 10 + $d; } } } return $t; } $_size = find_param("size"); $_units = find_param("units"); $size = atof($_size); $units = atof($_units); $original_size = $size; $original_units = $units; # Our golden rule: one page equals 4KB while ($units > 0) { $size *= 1024; $units--; } $pages = $size / 4; print "

Media to Pages Converter

\n\n"; print "
\n\n"; print "

A piece of media \n"; print $original_size . units_text($original_units); print " is eqivalent to " . commafy($pages) . " pages of paper.\n"; print "

"; if ($pages > 500) { $reams = floor($pages/500); print "

That's about " . commafy($reams) . " reams of paper

"; } if ($reams > 18000) { $semis = ceil($reams/18629); print "

You would need about " . commafy($semis) . " semi trucks to carry that.

"; } html_stop(); exit 0; #----------------------------------------------------------------------