#!/usr/local/bin/oraperl

# Oracle environment.
$ENV{'ORACLE_HOME'} = '/usr/oracle';
$ENV{'ORACLE_SID'} = 'nodb';

# Input form data.
read (STDIN, $incoming, $ENV{'CONTENT_LENGTH'});
&cgi_decode;

# Assign input variable.
$name_sought = $FORM{'NAME'};

# Open database connection.
$userid = "mispirli";
$password = "mispirli";
$lda = &ora_login ($ENV{'ORACLE_SID'}, $userid, $password) || die $ora_errstr;

# Open query.
$query = "select a.last_name, a.first_name, b.phone_no, b.phone_type
          from person_info_table a, phone_list_table b
          where ((lower (a.last_name)) = (lower (:1))) 
            and (a.person_id = b.person_id)";
$csr = &ora_open ($lda, $query);
&ora_bind ($csr, $name_sought);

print qq|Content-type: text/html\n\n|;

# Retrieve query records.
while (($a_first_name, $a_last_name, $a_phone_no, $a_phone_type) = &ora_fetch ($csr))
{
        print qq|First Name: $a_first_name<BR>Last Name: $a_last_name<BR>Phone Number: $a_phone_no<BR>Phone Type: $a_phone_type<P>|;
}

# Close query.
&ora_close ($csr);
# Close database connection.
&ora_logoff ($lda);

# Parse input form data.
sub cgi_decode 
{
    @pairs = split(/&/, $incoming);

    foreach (@pairs) 
    {
        ($name, $value) = split(/=/, $_);

        $name  =~ tr/+/ /;
        $value =~ tr/+/ /;
        $name  =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
        $value =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;

        #### Strip out semicolons unless for special character
        $value =~ s/;/$$/g;
        $value =~ s/&(\S{1,6})$$/&\1;/g;
        $value =~ s/$$/ /g;

        $value =~ s/\|/ /g;
        $value =~ s/^!/ /g; ## Allow exclamation points in sentences

        #### Skip blank text entry fields
        next if ($value eq "");

        #### Check for "assign-dynamic" field names
        #### Mainly for on-the-fly input names, especially checkboxes
        if ($name =~ /^assign-dynamic/) {
            $name = $value;
            $value = "on";
        }

        #### Allow for multiple values of a single name
        $FORM{$name} .= ", " if ($FORM{$name});

        $FORM{$name} .= $value;
    }
}
