
#Can't execute this one, it is the library of files for parsing the query string
#noexec
# File: parse-lib.pl

$escape_chars = " !@#$%^&()+={[}]:;\"'<,>.?/|\\";
@escape_chars = split(/ */,$escape_chars);
@table = qw(0 1 2 3 4 5 6 7 8 9 A B C D E F);
@mon2str = qw(January February March April May June July August September October November December);
@wday2str = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

# ------------------ QUERY PARSING ROUTINE HERE ------------
sub get_cookie {
    local($cookie_env)=$ENV{'HTTP_COOKIE'};
    local($param,$value,%cookie);

    if ($cookie_env)
    {
	$cookie_env=~tr/ //d;
	@pairs=split(';',$cookie_env);
	foreach (@pairs)
	{
	    ($param,$value) = split('=');
	    $param = &unescape($param);
	    $value = &unescape($value);
	    if ($cookie{$param}) {
		$cookie{$param} .= "$;$value";
	    } else {
		$cookie{$param} = $value;
	    }
	}
    }

    return %cookie;
}


sub get_query {
    local($query_string);
    local(@lines);
    local($method)=$ENV{'REQUEST_METHOD'};

    # If method is GET fetch the query from
    # the environment.
    if ($method eq 'GET') {
	$query_string = $ENV{'QUERY_STRING'};

	# If the method is POST, fetch the query from standard in
    } elsif ($method eq 'POST') {
	read(STDIN,$query_string,$ENV{'CONTENT_LENGTH'});
    }

    # No data.  Return an empty array.
    return () unless $query_string;	

    # We now have the query string.
    # Call parse_params() to split it into key/value pairs
    return &parse_params($query_string);
}

sub parse_params {
    local($tosplit) = @_;
	local(@pairs) = split('&',$tosplit);
	local($param,$value,%parameters);

	foreach (@pairs) {
	    ($param,$value) = split('=');
	    $param = &unescape($param);
	    $value = &unescape($value);
	    if ($parameters{$param}) {
		$parameters{$param} .= "$;$value";
	    } else {
		$parameters{$param} = $value;
	    }
	}
	return %parameters;
}

sub escape {
    my($toencode) = @_;

    $toencode =~ s/([ !@#$%^&()+={\[}\]:;\"'<,>.?\/|\\])/escape_one($1)/ge;

    return $toencode;
}

sub escape_one {
    my($char) = @_;
    my($hex_val,$val);
    $val = ord($char);
    $hex_val = $table[$val % 16] . $hex_val;
    $val = int ($val / 16);
    $hex_val = $table[$val % 16] . $hex_val;
    return "%".$hex_val;
}
		   

sub unescape {
    local($todecode) = @_;
    $todecode =~ tr/+/ /;	# pluses become spaces
    $todecode =~ s/%([0-9A-Fa-f]{2})/pack("c",hex($1))/ge;
    return $todecode;
}

#Print title and heading using paramter
sub my_print_head {
    local($Title,$Color) = @_;
    print <<END;
<HTML><HEAD>
<TITLE>$Title</TITLE>
</HEAD>
<BODY $Color>
<H1>$Title</H1>
END
    ;
}

#Print the HTTP header for HTML
sub my_print_HTTP_header {
	print "Content-type: text/html\n\n";
}

#Print the end of the document
sub my_print_tail {
    print <<END;
</BODY></HTML>
END
    ;
}

sub xwday2str {
    my($wday)=@_;
    if ($wday == 0) {return "Sunday"}
    elsif ($wday == 1) {return "Monday"}
    elsif ($wday == 2) {return "Tuesday"}
    elsif ($wday == 3) {return "Wednesday"}
    elsif ($wday == 4) {return "Thursday"}
    elsif ($wday == 5) {return "Friday"}
    elsif ($wday == 6) {return "Saturday"}
    else {return "Error in weekday number"};
}

sub xmon2str {
    my($mon)=@_;
    if ($mon == 0) {return "January"}
    elsif ($mon == 1) {return "February"}
    elsif ($mon == 2) {return "March"}
    elsif ($mon == 3) {return "April"}
    elsif ($mon == 4) {return "May"}
    elsif ($mon == 5) {return "June"}
    elsif ($mon == 6) {return "July"}
    elsif ($mon == 7) {return "August"}
    elsif ($mon == 8) {return "September"}
    elsif ($mon == 9) {return "October"}
    elsif ($mon == 10) {return "November"}
    elsif ($mon == 11) {return "December"}
    else {return "Error in month number"};
}

sub time_gmt {
    my($plus) = @_;
    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
    my($mname,$wname,$time,$gmt);

    $time=time + $plus;
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time);
    $mname=$mon2str[$mon];
    $wname=$wday2str[$wday];
    $year -= 100 if ($year > 99);
    $gmt=$wname . ", " . &dig2($mday) . "-" . $mname . "-" . &dig2($year);
    $gmt.= " " . &dig2($hour) . ":" . &dig2($min) . ":" . &dig2($sec) . " GMT";
    return $gmt;
}

sub dig2 {
    my($str)=@_;
    if ($str < 10)
    {
	$str = "0" . $str;
    }
    return $str;
}

1;
