#!/usr/bin/perl
# An example of a search script to accept search keys
# from a form, look for them in a DB type flat file, and
# return a HTML page with the matches in a table.
# This script generates a HTML FORM, with itself as the "ACTION"
# When this script is called it checks the STDIN for data it may
# be passing to itself. If there is none, it generates the FORM
# plus some help text on how to use it. If it does see search
# keys in STDIN ($keys) it will again print the FORM, skip the
# help text, open the data file, search for matches, and print
# them as a HTML table.
#******************************************************************
#******************** Subroutines ********************************
#******************************************************************
#******************************************************************
#**************** Get Passed data from form ***********************
#******************************************************************
sub GET_STATE_INFO {
read(STDIN, $save_string, $ENV{CONTENT_LENGTH}); # Yes- Use it
@prompts = split(/&/,$save_string);
foreach (@prompts)
{
($tmp1, $tmp2) = split(/=/,$_);
$tmp2 =~ s/\x2b/\x20/g;
$tmp2 =~ s/%2C/\x2c/g;
$tmp2 =~ s/%28/\x28/g;
$tmp2 =~ s/%29/\x29/g;
$fields{$tmp1}=$tmp2;
}
$keys = $fields{'keys'};
$search_type = $fields{'search_type'};
}
#******************************************************************
#********** Search file for lines with all search keys ************
#******************************************************************
sub SEARCH_FOR_MATCH {
@search_key = split(/\x20/,$keys);
$kounter =0;
open(MYFILE,"phone_book.dat") || print "
Error: Can't open phone book file<\H3>";
while() # Our Read loop
{
$in_line = $_;
if ($search_type eq "and")
{ $found_flag = "Y"; }
else
{ $found_flag = "N"; }
foreach (@search_key) #check for each search key in the file line
{
if ( $in_line =~ m/$_/i) # Is key in the data line ?
{
if ($search_type eq "or") # Yes-
{
$found_flag = "Y"; # If its a OR search all we
last; # need id one match
}
}
else #No-
{
if ($search_type eq "and") # If its a AND search all we
{ # need is one non-match
$found_flag = "N";
last;
}
}
} #end of ForEach Loop
if ( $found_flag eq "Y" )
{
$kounter++;
$in_line = $_;
# split line in phone book data file into seperate fields
# for printing in our table
($email, $name, $phone_num, $dep ) = split(/:/,$in_line);
print "| ", $name, " | \n";
print "", $phone_num, " | \n";
print "", $email, " | \n";
print "", $dep, " | \n";
print "\n";
$kounter++;
if ( $kounter > $max_hits) { last; } # break out of read loop
}
} # end of Read loop
}
#******************************************************************
#**************** Main Rtn ***********************
#******************************************************************
$max_hits = 100; #Set the maximum number of matches to be returned
print("Content-Type: text/html\n\n");
print("Results\n");
print("\n");
&GET_STATE_INFO;
print "Phone Book Look Up
\n";
print "Enter Search terms seperated by spaces. \n";
print " Search terms can be Name, Phone Number, Email, or Department";
print "
\n";
if ( $keys eq "")
{
print "
";
print " Note: \x22OR\x22 search finds lines that match any search terms.";
print " \x22AND\x22 search finds lines that match all search terms.";
print "
Examples: ";
print "
Searching on \x22john smith programming\x22";
print "
- With a AND search- will look for any John Smiths in the programming department. ";
print "
- With a OR search- will look for any Johns, or Smiths, or anyone in the programming department";
}
else
{
print "
";
print "Search Results";
print "\n";
print "| Name | \n";
print "Phone Number | \n";
print "Email Address | \n";
print "Department | \n";
&SEARCH_FOR_MATCH($keys);
if ($kounter > $max_hits )
{
print "| Limit of ", $max_hits;
print " matches exceeded. | \n";
print "
\n";
}
print "
\n";
# Reset the phone_book.dat file to its default
close(MYFILE);
open(MYFILE,">phone_book.dat");
print MYFILE "SSMITH\@BOGUS.COM:STEVE SMITH:555-1234:SALES\n";
print MYFILE "GJONES\@BOGUS.COM:GEORGE JONES:999-444-5689:SALES\n";
print MYFILE "JSMITH\@FOO.COM:John Smith:999-555-2345:PROGRAMMING\n";
print MYFILE "AJONES\@BOGUS.COM:ALEX Jones:999-555-1122:MARKETING\n";
print MYFILE "JJONES\@FOO.COM:JOHN JONES:999-555-3322:PROGRAMMING\n";
print MYFILE "PSMITH\@BOGUS.COM:Paul Smith:999-333-1122:PAYROLL\n";
close(MYFILE);
}
print "";
print "Go to the ";
print " Add a record \n";
print " to the phone book, or See the ";
print " Perl Source Code\n";
print " for this page.";
print "
";
print "Back to Examples Page\n";
print("\n");
print("