#!/usr/bin/perl -w

#************************************************************
#** This Perl example is provided "as is".  This code is   **
#** not supported, but I will try to answer questions as   **
#** time allows.  Email: wizjd@panix.com                   **
#** Visit: <URL:http://www.panix.com/~wizjd/> for updated, **
#** and new examples.                                      **
#************************************************************
# 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);

open(MYFILE,"jugl") || print "<H3>Error: Can't open phone book file<\H3>";
while(<MYFILE>)  # 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" )
  {
    $in_line = $_;
    # split line in phone book data file into seperate fields
    # for printing in our table
    ($status, $name, $h_num, $w_num, $c_num, $email, $webpage ) = split(/\+/,$in_line);
 

    print "<TD  ALIGN=\x22center\x22>", $status, "</TD>\n";
    print "<TD  ALIGN=\x22center\x22>", $name, "</TD>\n";
    print "<TD  ALIGN=\x22center\x22>", $h_num, "</TD>\n";
    print "<TD  ALIGN=\x22center\x22>", $w_num, "</TD>\n";
    print "<TD  ALIGN=\x22center\x22>", $c_num, "</TD>\n";
	 if ( $email eq "&nbsp;" )
		{
	    print "<TD  ALIGN=\x22center\x22>", $email, "</TD>\n";
		}
	 else
		{
	    print "<TD  ALIGN=\x22center\x22><A href=\x22mailto:", $email, "\x22>", $email, "</A> </TD>\n";
		}

	if ($webpage eq "&nbsp;")
		{
    		print "<TD  ALIGN=\x22center\x22>", $webpage, "</TD>\n";
		}
	else
		{
		print "<TD  ALIGN=\x22center\x22><A href=\x22", $webpage, "\x22>", $webpage, "</A> </TD>\n";
		}

    print "<TR>\n";
    $kounter++;
    if ( $kounter > $max_hits) { last; }  # break out of read loop
  }


 }  # end of Read loop


}




#******************************************************************
#****************  Main Rtn                 ***********************
#******************************************************************


$max_hits = 500;  #Set the maximum number of matches to be returned
$kounter = 0;     #Intialize counter that counts hits returned

print("Content-Type: text/html\n\n");
print("<HEAD><TITLE>Juggling Roster</TITLE></HEAD>\n");
print("<BODY bgcolor=\"#FFFFFF\">\n");

&GET_STATE_INFO;

print "<H1>Phone List Search</H1>\n";

print "<P>Enter Search terms separated by spaces. \n";
print "<BR>To see the whole list, do a search for \"juggler\" without the quotes.";
print "<FORM METHOD=POST ACTION=\"http://juggling.place.org/psearch.cgi\">\n";
print "<INPUT TYPE=\x22text\x22 NAME=\x22keys\x22 SIZE=40 ";
print "  VALUE=\x22", $keys, "\x22>\n";

print "<BR>Select Search type: \n";

print "\n<INPUT TYPE=\x22radio\x22 Name=\x22search_type\x22 VALUE=\x22and\x22";
if ($search_type eq "and" || $search_type eq "" ) { print " CHECKED"; };
print "> AND";

print "\n<INPUT TYPE=\x22radio\x22 Name=\x22search_type\x22 VALUE=\x22or\x22";
if ($search_type eq "or" ) { print " CHECKED"; };
print "> OR";

print "\n<INPUT TYPE=\x22submit\x22 Name=\x22submit\x22 Value =\x22Search\x22>\n";
print "\n</FORM>\n";


if ( $keys eq "")
 {
  print "<HR>";
  print " Note: \x22OR\x22 search finds lines that match any search terms.";
  print "       \x22AND\x22 search finds lines that match all search terms.";
  print "<HR>";
  print "If you would like to be added to the roster,<BR> ";
  print "change your current entry, or be removed from the roster,<BR>";
  print "then please send email to <A href=\"mailto:juggler\@place.org\">";
  print "juggler\@place.org</A><BR>describing your needs.<BR>";
  print "<HR>";
  print "<A href=\"links.html\">Back</A> ";
  print "<A href=\"index.html\">TJS Home Page</A> ";
  print "<HR>";
  print "<font size = 3>";
  print "<EM><B>";
  print "Texas Juggling Society<BR>";
  print "Last Updated: Now<BR>";
  print "Send Questions or Comments to <A href=\"mailto:juggler\@place.org\">";
  print "juggler\@place.org</A><BR>";
  print "<A href=\"http://www.utexas.edu/\">UT Austin</A><BR>";
  print "</B>";
  print "</font>";
  print "<font size = 0>";
  print "Our web page is published by the Texas Juggling Society, a registered student";
  print " organization.  This page is not an official publication of the University of";
  print " Texas at Austin, and does not represent the views of the University or its";
  print " officers.";
  print "</font></EM>";
  print "</body>";
  print "</html>";
 }
 else
 {
  print "<HR>";
  print "<B>Search Results</B>";
  print "<TABLE BORDER = 7>\n";
  print "<TH ALIGN=\x22center\x22>Status</TH>\n";
  print "<TH ALIGN=\x22center\x22>Name</TH>\n";
  print "<TH ALIGN=\x22center\x22>Home Ph. #</TH>\n";
  print "<TH ALIGN=\x22center\x22>Work Ph. #</TH>\n";
  print "<TH ALIGN=\x22center\x22>Cell Ph. #</TH>\n";
  print "<TH ALIGN=\x22center\x22>Email Address</TH>\n";
  print "<TH ALIGN=\x22center\x22>Web Page</TH><TR>\n";
  &SEARCH_FOR_MATCH($keys);
  


  if ($kounter > $max_hits )
  {
   print "<TD  ALIGN=\x22center\x22><B>Limit of ", $max_hits;
   print " matches exceeded.</B></TD>\n";
   print "<TR>\n";
  }
  print "</TABLE>\n";
  print "Found ",$kounter," matches";

print "<HR>";
print "If you would like to be added to the roster,<BR> ";
print "change your current entry, or be removed from the roster,<BR>";
print "then please send email to <A href=\"mailto:juggler\@place.org\">";
print "juggler\@place.org</A><BR>describing your needs.<BR>";


print "<HR>";
print "<A href=\"links.html\">Back</A> ";
print "<A href=\"index.html\">TJS Home Page</A> ";
print "<HR>";
print "<font size = 3>";
print "<EM><B>";
print "Texas Juggling Society<BR>";
print "Last Updated: Now<BR>";
print "Send Questions or Comments to <A href=\"mailto:juggler\@place.org\">";
print "juggler\@place.org</A><BR>";
print "<A href=\"http://www.utexas.edu/\">UT Austin</A><BR>";
print "</B>";
print "</font>";
print "<font size = 0>";
print "Our web page is published by the Texas Juggling Society, a registered student";
print " organization.  This page is not an official publication of the University of";
print " Texas at Austin, and does not represent the views of the University or its";
print " officers.";
print "</font></EM>";
print "</body>";
print "</html>";


}
