#####################################################################################
# All information, data, source code, compiled binaries, information, documentation #
# contained within this document is the sole property of "WhiteHat Security, Inc"   #     
# and may not be sold, reused or distributed.                                       #
# Copyright 2002 WhiteHat Security, Inc.  ALL RIGHTS RESERVED                       #
#####################################################################################


my $Debug = 0;


sub table_start {
        my $border = $_[0];
        my $cpad = 3;
        my $cspace = 0;
        print "<TABLE BORDER=\"$border\" CELLPADDING=\"$cpad\" CELLSPACING=\"$cspace\">\n";
        return;
}

sub table_end {print "</TABLE>\n"; return; }

sub table_srow { print "<TR>\n"; return;}
sub table_erow { print "</TR>\n"; return;}

sub table_data {
        my $align = $_[0] || "";
        my $valign = $_[1] || "";
        my $width = $_[2] || "";
        my $data = $_[3] || "";

        print "<TD ALIGN=\"$align\" VALIGN=\"$valign\" WIDTH=\"$width\">$data</TD>\n";
        return;
}

sub printTable { # Create and Print Table
	# Function is used to start and end HTML Tables
	my $t = shift; # start = table start/ end = table end;
	
	if($t eq "start") { 			# Start HTML Table
		my $border = shift;		# BORDER Attribute
		my $cellpadding = shift;	# CELLPADDING Attribute
		my $cellspacing = shift;	# CELLSpacing Attribute
		
		# Print Table HTML with Attributes
		print "<TABLE BORDER=\"$border\" CELLPADDING=\"$cellpadding\" CELLSPACING=\"$cellspacing\">\n";
	} elsif ($t eq "end") { 	# End HTML Table
		print "</TABLE>\n";	# Print End HTML Table
	} # End Control If
	
} # End SubRoutine


sub printTableRow { # Create and Print Table Row
	# Function is used to start and end HTML Tables Rows
	my $t = shift; 			# start = table row start/ end = table row end
	if($t eq "start") { 		# Start HTML Table Row
		print "<TR>\n"; 	# print Start HTML Table Row
	} elsif ($t eq "end") { 	# End HTML Table Row
		print "</TR>\n"; 	# print End HTML Table Row
	} # End Control If
} # End SubRoutine

sub printTableData { # Create and Print TABLE Data
	# Function is used to start and end HTML Tables Datas
	my $t = shift; 			# start = table data start/ end = table data end
	if($t eq "start") {		# Start HTML Table Data
		my $width = shift;	# WIDTH Attribute
		my $align = shift;	# ALIGN Attribute
		my $valign = shift;	# VALIGN Attribute
		my $bgcolor = shift || "FFFFFF";
		# Print HTML Table Data with Attributes
		print "<TD WIDTH=\"$width\" ALIGN=\"$align\" VALIGN=\"$valign\" BGCOLOR=\"$bgcolor\">\n";
	} elsif ($t eq "end") {		# End HTML Table Data
		print "</TD>\n";	# Print End HTML Table Data
	} # End Control If
} # End SubRoutine



#######################
### Enclosing Table ###
#######################
# Enclosing HTML Table.
# Takes in background color input

sub encloseTable {
	my $flag = shift || "start";
	my $bgcolor = shift || "#FFFFFF";
	
	if ($flag eq "start") {
		# Gray Enclosing HTML Block
		print "<TABLE BGCOLOR=\"$bgcolor\" BORDER=0 CELLPADDING=1 CELLSPACING=0><TR><TD ALIGN=LEFT VALIGN=TOP>\n";
	} elsif ($flag eq "end") {	
		# End Folders HTML
		print "</TD></TR></TABLE>\n";
	}
	
	
} # End Enclosing Table SubRoutine








##################
### Print Font ###
##################
# Function will print stylized font text.
# Input Text
# Input Text Size
sub printFont {
	my $text = shift || "";
	my $size = shift || 12;
	
	print "<font style=\"font-size: " . $size . "px; font-family:arial,sans-serif\">$text</font>";
		
	
} # End Print Font SubRoutine

#############################
### Title Bar HTML Block  ###
#############################
# Print an HTML Title Bar block
# Takes in 2 parameters
# Title string
# Description string
sub titlebarHTMLBlock {

	my $title = shift || "";	# Set Title
	my $description = shift || "";	# Set Description
	
	
	# Print HTML Tiltle Bar
	print "<table WIDTH=\"600\" CELLPADDING=\"1\">\n";
	print "<tr><td BGCOLOR=\"#fdbe50\">\n";
	print "<font COLOR=\"000000\" style=\"font-size: 18px\; font-family:arial,sans-serif\">\n";
	print "<b>$title</b>\n";
	print "</font>\n";
	print "</td></tr>\n";
	print "<tr><td>\n";
	print "<font COLOR=\"000000\" style=\"font-size: 12px; font-family:arial,sans-serif\">\n";
	print "$description";
	print "</font>\n";
	print "<hr>\n";
	print "</td></tr>\n";
	print "</table>\n";

} # End Title Bar HTML Block



sub printForm { # Create and Print HTML FORM
	# Function is used to start and end HTML Forms
	my $t = shift; 			# start = form start/ end = form end
	if($t eq "start") {		# Start HTML Form
		my $method = shift;	# METHOD Attribute
		my $action = shift;	# Action Attribute
		my $name = shift;	# Name Attribute
		
		# Print HTML Form with attributes
		print "<FORM METHOD=\"$method\" ACTION=\"$action\" NAME=\"$name\">\n";
	} elsif ($t eq "end") { 	# End FORM HTML
		print "</FORM>\n";	# Print FORM HTML
	} # End Control If
} # End SubRoutine



sub printInput { # Create and Print HTML Input
	# Function is used to start and end HTML Form Input
	my $type = shift || "";		# TYPE Attribute
	my $name = shift || "";		# NAME Attribute
	my $value = shift || "";	# VALUE Attribute
	my $size = shift || "";		# SIZE Attribute
	my $width = shift || "";
	
	if($size eq "0") {	# Set size to zero if input is not supposed to have a size
		# Print HTML FORM INPUT with attributes but without size
		print "<INPUT TYPE=\"$type\" NAME=\"$name\" VALUE=\"$value\">\n";
	} else { # With Size Attribute
		# Print HTML FORM INPUT with attributes with size
		print "<INPUT TYPE=\"$type\" NAME=\"$name\" VALUE=\"$value\" style=\"WIDTH: ${width}px; font-size: 10px; font-family:arial,sans-serif\">\n";
	} # End Control If
} # End SubRoutine

sub dtime {
	my $show_ampm = "yes";
	my $weekday_date_space = ", ";
	my $date_time_space = "at";
	my $month_day_year_space = "/";
	my $offset = "0";

	my @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	my @months =('January','February','March','April','May','June','July','August','September','October','November','December');
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time + (3600*$offset));

	$hour = "0$hour" if ($hour < 10);
	$min = "0$min" if ($min < 10);
	$sec = "0$sec" if ($sec < 10);

	my $var;
	my $ext;

	if ($hour >= 12) { $var = "P.M."; } else { $var = "A.M."; }
	if ($hour == 0) { $hour = 12; }
	if ($hour > 12) { $hour -= 12; }
	if ($show_ampm ne "yes") { $var = ""; }
	$year = 1900 + $year;

	if ($mday eq "1")         { $ext = "st"; }
 	elsif ($mday eq "3")   { $ext = "rd"; }
 	elsif ($mday eq "23") { $ext = "rd"; }
 	elsif ($mday eq "2")   { $ext = "nd"; }
 	elsif ($mday eq "22") { $ext = "nd"; }
 	elsif ($mday eq "21") { $ext = "st"; }
 	elsif 	($mday eq "31") { $ext = "st"; }
 	else { $ext = "th"; }

	my $day = $days[$wday];
	my $month1 = $months[$mon];
	my $time1 = "$hour:$min:$sec $var";
	my $date1 = "$mday$ext";

	my $dtime = $day . $weekday_date_space . $month1 . " " . $date1 . " " . $year . " " . $date_time_space . " " . $time1;
	return $dtime;

} # End SubRoutine

sub cutEnd {
	my $file = $_[0] || exit;
	my $addr;

	open(FH, "+<$file") or die "can't update $file: $!";
	while (<FH>) {
        	$addr = tell(FH) unless eof(FH);
	}
	truncate(FH, $addr) or die "can't truncate $file: $!";

} # End SubRoutine


###################
### SizeConvert ###
###################
# Written by Lex Arquette
# Version 1.2

sub SizeConvert {
# Convert a size in bytes to k's if needed
my $Size = shift ;

if ($Size >= 1024) {

	$Size = ($Size / 1024) ;
	$Size = int($Size) ;
	$Size .= 'k' ;

} else {

	$Size .= 'bytes' # end if
} # end if

# Return the perdy result
return $Size ;

} # end SizeConvert


#=======================================
# WrapString
# Version 1.0
#
# Usage:                              
# Take in a string of text and a length.
# a line break will be added at each
# iteration of given length.
#=======================================
sub WrapString {

my $url = shift || "";
my $wraplength = shift || 40;
my $counter = 0;
my $newurl = '' ;

# Check the length of the string. If it's below our wrap length
# we don't have to process it

if (length $url > $wraplength) {

		# Split the URL into seperate characters
		my @SplitURL = split '', $url ;

		# Step through each character and keep a running total

			foreach $char (@SplitURL) {

				# Add one to our counter
				$Counter++;
			
				# Add a break if we're at our wrap length	
				if ($Counter > $wraplength) {
				
					$char = $char ." "; 
					
					# Reset our counter
					$Counter = 0;
				
				} # end if		

			# Add the character on to our new url
			$newurl .= $char ;

			} # end foreach $char
		
		# Replace the url with our new url
		$url = $newurl ;		

	} # end if length

return $url ;
} # end sub WrapString




sub XMLEsc {
        my $mystring = shift;
        if($mystring) {
                print "STRING: $mystring<BR>\n" if ($Debug == 1);
        } else {
                return;
        }

        if(($mystring) && (length $mystring >= 1)) {
		$mystring =~ s/&/&amp;/g;
                $mystring =~ s/>/&gt;/g;
                $mystring =~ s/</&lt;/g;
                $mystring =~ s/"/&quot;/g;

                my @chars = split(//, $mystring);

                foreach my $c (@chars) {
                        if(ord($c) >= 127) {
                                print "C: " . ord($c) if ($Debug == 1);
                                $c = "&#" . ord($c) . "\;";
                                print "XC: $c<BR>\n" if ($Debug == 1);
                        } else {
                                print "CL: $c " . ord($c) . "<BR>\n" if ($Debug == 1);
                        }
                }
                my $string = join "", @chars;
                print "RSTRING: $string<BR>\n" if ($Debug == 1);
                return $string;
        }

} # end XMLEsc

sub HTMLEsc {
        my $mystring = shift;
        if($mystring) {
                print "STRING: $mystring<BR>\n" if ($Debug == 1);
        } else {
                return;
        }

        if(($mystring) && (length $mystring >= 1)) {
		$mystring =~ s/&/&amp;/g;
                $mystring =~ s/>/&gt;/g;
                $mystring =~ s/</&lt;/g;
                $mystring =~ s/"/&quot;/g;
                return $mystring;
        }

} # end XMLEsc

sub URLDecode { # URL Decoder
	my $url = $_[0];
	$url =~ s/\0//g; # Remove Nulls
	$url =~ tr/+/ /;
	$url =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg;
	$url =~ s/<!--(.|\n)*-->//g;
	$url =~ s/([\&;\`'\\\|"*?~<>^\(\)\[\]\{\}\$\n\r])/\\$1/g; # Escape special characters
	return $url;
} # End SubRoutine


sub errorOut { # Exit Routine
	my $error_string = $_[0];
	print "<B><font COLOR=\"FF0000\" style=\"font-size: 12px\; font-family:arial,sans-serif\">Error</font>: </B>\n";
	print "<font COLOR=\"000000\" style=\"font-size: 12px\; font-family:arial,sans-serif\">$error_string</font><BR>\n";
	exit 1; # Exit
} # End SubRoutine



1;
