# MTT's PERL and CGI libary functions
# Michael Toppa - mike@toppa.com
# v1 June 1998
#
# v2 March 1999
# * CleanInput: added ability to replace linefeeds with spaces;
# handling for additional special characters
# * GetDate: complete re-write to supply multiple date formats
# * FileRead: added ability to send data to CGIError
#
# v3 February 2000
# General code cleanup and...
# * ParseInput: changed default separator to the more conventional comma;
# accepts GET and POST only in capitals (conforms to HTTP)
# * CleanInput: added ability to explicitly request conversion of special
# characters to entities
# * DateTime: complete rewrite of former GetDate routine
# * FileRead: improved error message handling
# Here you'll find the following routines:
# ParseInput - Parses name-value pairs from GET or POST input into the
# associative array %in. Optionally accepts a string for use as a seperator
# for multiple values associated with a single name. Defaults to "," if
# no string is defined.
#
# CleanInput - Removes Control-M's from TEXTAREA input, and accepts values
# for these local variables:
# 1. $input = "the input text" (required)
# 2. $linefeed = '0' : leave linefeeds as is (default)
# $linefeed = 'br' : replace single linefeeds with
tags
# $linefeed = 'br_feed' : add
tags to single linefeeds
# $linefeed = 'p' : replace double linefeeds with
tags, delete single linefeeds # $linefeed = 'p_feed' : add
tags to double line feeds # $linefeed = 'strip' : replace all linefeeds with spaces # 3. $special = '0' : leave special characters as is (default) # $special = '1' : convert special characters to HTML entities (incl. MS Word) # 4. $hypertext = '0' : do not add hyperlinks (default) # $hypertext = '1' : add hyperlinks to URLs and mailto hyperlinks to email addresses # 5. $nohtml = '0' : leave <, >, &, and " characters as is (default) # $nohtml = '1' : replace <, >, &, and " characters with entities # # HTMLTop - Returns the top of an HTML documet, including the Content-type. # Optionally accepts values for these local variables: # 1. $title = e.g. 'A Title' # default = none # 2. $body = e.g. '
' # default = '' # 3. $font = e.g. "' # default = '' # 4. $header = e.g. '/g if ($linefeed eq "p"); $input =~ s/\n//g if ($linefeed eq "p"); $input =~ s/\n[\r\t\f ]*\n/\n\n
/g if ($linefeed eq "p_feed");
$input =~ s/\n/ /g if ($linefeed eq "strip");
# Add hyperlinks to URLs and mailto's, if requested.
if ($hypertext) {
$input =~ s/\bhttp:\/\/\S*\.\S*\b/$&<\/A>/g;
$input =~ s/\b\S*\@\S*\.\S*\b/$&<\/A>/g;
}
return $input;
}
sub HTMLTop {
local ($title, $body, $font, $header) = @_;
$body = '' unless ($body);
$font = '' unless ($font);
print "Content-type: text/html\n\n";
print "\n\n";
print "$header
\n";
print "$message\n";
# Print the errors messages, if any.
if (@errors) {
print "\n";
foreach $error (@errors) {
print "
\n";
}
&HTMLBottom;
# Since there was an error, stop all execution.
exit;
}
sub GetDate {
local ($format) = @_;
local ($sec,$min,$milhour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
local (@days) = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
local (@months) = ('January','February','March','April','May','June',
'July','August','September','October','November','December');
local ($date);
if ($milhour == 0) {
$hour = 12;
$am = 1;
$pm = 0;
}
elsif (($milhour > 0) && ($milhour < 12)) {
$hour = $milhour;
$am = 1;
$pm = 0;
}
elsif ($milhour == 12) {
$hour = $milhour;
$am = 0;
$pm = 1;
}
elsif ($milhour > 12) {
$hour = $milhour - 12;
$am = 0;
$pm = 1;
}
$year = $year - 100 if ($year > 99); # adjust if a 3 digit year
$year = ("0" . "$year") if ($year =~ /^\d{1}$/);
$longYear = "20" . $year;
$realMonth = $mon + 1;
$realMonth = ("0" . "$realMonth") if ($realMonth =~ /^\d{1}$/);
$mday = ("0" . "$mday") if ($mday =~ /^\d{1}$/);
$hour = ("0" . "$hour") if ($hour =~ /^\d{1}$/);
$min = ("0" . "$min") if ($min =~ /^\d{1}$/);
$sec = ("0" . "$sec") if ($sec =~ /^\d{1}$/);
if ($format eq "datelong") {
$date = "$months[$mon] $mday, $longYear";
$date = "$months[$mon] $mday, $longYear";
}
elsif ($format eq "day") {
$date = "$days[$wday]";
}
elsif ($format eq "timeshort") {
$date = "$hour:$min AM" if ($am);
$date = "$hour:$min PM" if ($pm);
}
elsif ($format eq "timelong") {
$date = "$hour:$min:$sec AM" if ($am);
$date = "$hour:$min:$sec PM" if ($pm);
}
elsif ($format eq "milshort") {
$date = "$milhour:$min";
}
elsif ($format eq "millong") {
$date = "$milhour:$min:$sec";
}
else {
$date = "$realMonth/$mday/$year";
}
return $date;
}
sub FileRead {
local ($file, $message, $header, $body, $font) = @_;
local (@FileRead);
unless ($file) {
&CGIError ($message, $header, $body, $font);
}
unless (open (FILE, "$file")) {
&CGIError ("Unable to open $file", $header, $body, $font);
}
@FileRead =