#!/usr/local/bin/perl -wT
# File that contains subroutine for parsing form data. <br>Should be called from another script.
# noexec
use strict;

sub parse_form_data {
    my %form_data;
    my $name_value;
    my @name_value_pairs;
    if (defined $ENV{QUERY_STRING}) {
	@name_value_pairs = split /&/, $ENV{QUERY_STRING};
    }
    
    if (defined $ENV{REQUEST_METHOD}) {
	if ( $ENV{REQUEST_METHOD} eq 'POST' ) {
	    my $query = "";
	    read( STDIN, $query, $ENV{CONTENT_LENGTH} ) == $ENV{CONTENT_LENGTH}
	    or return undef;
	    push @name_value_pairs, split /&/, $query;
	}
    }
    
    foreach $name_value ( @name_value_pairs ) {
        my( $name, $value ) = split /=/, $name_value;
        
        $name =~ tr/+/ /;
        $name =~ s/%([\da-f][\da-f])/chr( hex($1) )/egi;
        
        $value = "" unless defined $value;
        $value =~ tr/+/ /;
        $value =~ s/%([\da-f][\da-f])/chr( hex($1) )/egi;
        
	if ( exists $form_data{$name} ) {
	    $form_data{$name} .= "\t$value";
	} else {
	    $form_data{$name} = $value;
	}
    }
    return %form_data;
}

1;

