#!/srvtools/http/bin/perl
#############################################################################
#
#   File: cyberpals_high.pl
#
#   History:
#    Ver   Date      Who               Comments
#    -----------------------------------------------------------------------
#    2.0   29-Feb-96 Falke Bruinsma (falke@innersource.com)
#                                      Added the ability to EDIT ads using
#			               passwords for protection
#    1.1   02-Feb-96 Craig Bloom       Minor revisions to HTML output.
#
#    1.0   30-Jan-96 Mike Reading      Changed database structure to colon and 
#                                      new-line delimited flat file. 
#                                      Added search and preview capabilities. 
#                                      Added "poor man's database locking".
#
#    0.2   09-Dec-95 Paul Coddington   Initial Release 
#
#
require 'cgi-lib.pl';

#############################################################################
#
#   CONFIGURABLE PARAMATERS
# 
#############################################################################

require "../cgiconf.pl";  # Contains the server variables

# URL for Student Center home directory
$home = "$ENV{'INFOMALL_URL'}";

#Directories that are writable by the server
$rw = "$ENV{'INFOMALL_DOCROOT_RW'}";
$rw = "$rw/studentcenter";

#
# The fully qualified path to the database file.
# NOTE: The this file and the directory in which it is located 
#       MUST be readable/writeable by the httpd deamon.
#   

$DB_FILE="$rw/cyberpals.db";
$SECRET="4Pals";
$SCRIPT="cyberpals_high.pl";

#############################################################################
#
#   MAIN FUNCTION
#
#############################################################################

#
# Initialize the random number generator
#
srand;

#
# Parse arguments passed by http server
#
  &ReadParse(*input);

#
# Figure out what we're supposed to do and do it
#
$preview_button = $input{"preview_button"};
$post_button    = $input{"post_button"};
$search_button  = $input{"search_button"};
$search2_button = $input{"search2_button"};
$edit_button 	= $input{"edit_button"};
$add_button	= $input{"add_button"};
$passwd_button  = $input{"passwd_button"};
$delete_button  = $input{"delete_button"};

if ($preview_button) {
  &Preview_Personal();
} elsif ($post_button) {
  &Post_Personal();
} elsif ($search_button) {
  &Display_Search_Page();
} elsif ($search2_button) {
  &Search_Database();
} elsif ($edit_button) {
  &Edit_Personal(0);
} elsif ($add_button) {
  &Edit_Personal(1);
} elsif ($passwd_button) {
  &Ask_For_Passwd();
} elsif ($delete_button) {
  &Delete_Personal();
} else {
  &Display_Info();
}


#############################################################################
#  
#   SUBROUTINES 
#
#############################################################################



sub Pack_Record {
  #
  # INPUTS:  A list of field values
  # OUTPUTS: A single string containing the fields joined by colons
  #          and "special" characters replaced by their Hex equivalents 
  #

  local(@fields) = @_;
  local($field);

  # 
  # Convert all percent signs to %25, all colons to %3A and all new-lines to %0A   
  #
  foreach $field (@fields) {
    $field =~ s/%/%25/g;
    $field =~ s/:/%3A/g;
    $field =~ s/\n/%0A/g;
  }

  #
  # Return the fields joined by colons
  #
  join(":",@fields);
}




sub Unpack_Record {
  #
  # INPUT:   A single string containing the fields joined by colons and
  #          "special" characters encoded with their hex equivalents
  # OUTPUTS: A list of unencoded field values
  #

  local($record) = @_;
  local($field,@fields);

  # take off the last \n
  chop($record);
  #
  # Split the records into individual fields using : as a delimeter
  #
  @fields = split(/:/,$record);

  #
  # Convert all %3A's to colons, all %0A's to new-lines, and all %25's to percents.
  #
  foreach $field (@fields) {
    $field =~ s/%3A/:/g;
    $field =~ s/%0A/\n/g;
    $field =~ s/%25/%/g;
  }

  #
  # Return the "unpacked" list 
  #
  @fields;
}


sub Delete_Personal {
  local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate, $passwd);
  local($tmpclass,$tmpname,$tmpage,$tmpsex,$tmptown,$tmpstate,$tmpcountry,$tmpemail, $tmphomepage, $tmpentry,$tmppostdate,$tmppasswd);
  local($record);
  local($tmprecord);

  $postdate = $input{'postdate'};
  open(DB,"$DB_FILE");
  open(DBTMP, ">$DB_FILE.tmp");
  while ($tmprecord = <DB>) {
   ($tmpclass,$tmpname,$tmpage,$tmpsex,$tmptown,$tmpstate,$tmpcountry,$tmpemail,$tmphomepage,$tmpentry,$tmppostdate,$tmppasswd) = &Unpack_Record($tmprecord);
   if ($postdate ne $tmppostdate) { print DBTMP $tmprecord; }
  }
  close(DB);
  close(DBTMP);
  system("cp $DB_FILE.tmp $DB_FILE");
  print &PrintHeader;
  print "
<HTML>
<HEAD>
<TITLE>Personal ad has been removed</TITLE>
</HEAD>
<BODY>
Your personal ad has been successfully removed
<P>
Click <A HREF=\"$SCRIPT\">here to continue</A>
</BODY>
</HTML>
";
}

sub Post_Personal {          
  # PURPOSE: Prepend a new "packed" record to the database
  # INPUTS:  Data supplied by the assoc array %input
  # OUTPUTS: An HTML Document displaying some feedback along with a printout
  #          of how the personal will look.
  
  local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);
  local($tmpclass,$tmpname,$tmpage,$tmpsex,$tmptown,$tmpstate,$tmpcountry,$tmpemail,$tmphomepage,$tmpentry,$tmppostdate,$tmppasswd);
  local($record);

  # Pull out the variables we're interested in
  $class = $input{'class'};
  $name = $input{'name'};
  $age = $input{'age'};
  $sex = $input{'sex'};
  $town = $input{'town'};
  $state = $input{'state'};
  $country = $input{'country'};
  $email = $input{'email'};
  $homepage = $input{'homepage'};
  $entry = $input{'entry'};
  $passwd = $input{'the_passwd'};
  $oldpostdate = $input{'postdate'};
  if (not $input{'postdate'}) { $add = 1; } 
  # Get the date from the system clock
  $postdate = `date`;
  chop($postdate);
  
  # Prepend a record to the database file by:
  #   1. "Locking" the database file
  #   2. Writing the new record to a temporary file
  #   3. Appending the existing database to the temp file
  #   4. Copying the temp file back to the database file
  #   5. "Unlocking" the database file
  $record = &Pack_Record($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);

  # Loop until we can get a sucessful "lock" on the database
  while (!&Lock_Database) {
    # Sleep a random amount of time between 0 and 1 seconds
    select(undef,undef,undef,rand);
  }

  # Prepend the record
  open (DBTMP,">$DB_FILE.tmp") || die "Unable to open $DB_FILE.tmp\n";
  print DBTMP "$record\n";
  close DBTMP;
  if ($add) { system("cat $DB_FILE >> $DB_FILE.tmp"); } 
  else {
    open(DB,"$DB_FILE"); 
    open(DBTMP, ">>$DB_FILE.tmp");
    while ($tmprecord = <DB>) {
     ($tmpclass,$tmpname,$tmpage,$tmpsex,$tmptown,$tmpstate,$tmpcountry,$tmpemail,$tmphomepage,$tmpentry,$tmppostdate,$tmppasswd) = &Unpack_Record($tmprecord);
     if ($oldpostdate ne $tmppostdate) { print DBTMP $tmprecord; }
    }
    close(DB);
    close(DBTMP);
  }
  system("cp $DB_FILE.tmp $DB_FILE");

  # Unlock the database
  &Unlock_Database;
  # Display a message telling the user that the post was sucessful
  print &PrintHeader; 
  print "
    <HTML>
      <HEAD>
        <TITLE>Sucessful Post</TITLE>
      </HEAD>

      <BODY>
        <H1>Your Cyberpals Personal Ad was sucessfully added to the 
database!</H1>
        <P><BR>
  ";
  
  &Display_Record($record);

  print "
	<P><BR>
        <HR WIDTH=50% SIZE=5 ALIGN=CENTER>
	<BR><BR><BR>
        <FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">
	   <H4>Perform a 
<INPUT TYPE=\"submit\" NAME=\"search_button\" VALUE=\"Search\"></H4>
</FORM>
	  <P>
	  |<A HREF= \"$home/index.html\">The Center</A>|<A HREF= \"$home/comment.html\">Your Comments And Ours</A>|<A HREF= \"$home/blurb.html\">About The Student Center</A>|
	</BODY>
    </HTML>
  ";

  return 1;
}

sub Edit_Personal {
local local($add) = @_;
local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);
local($tmpclass,$tmpname,$tmpage,$tmpsex,$tmptown,$tmpstate,$tmpcountry,$tmpemail,$tmphomepage,$tmpentry,$tmppostdate,$tmppasswd);
  $class = $input{'class'};
  $name = $input{'name'};
  $age = $input{'age'};
  $sex = $input{'sex'};
  $town = $input{'town'};
  $state = $input{'state'};
  $country = $input{'country'};
  $email = $input{'email'};
  $homepage = $input{'homepage'};
  $entry = $input{'entry'};
  $passwd = $input{'the_passwd'};
  $postdate= $input{'postdate'};

  print &PrintHeader;
  print "<HTML><HEAD>";
  if ($add) { print"<TITLE>High School Cyberpals Add Form</TITLE>"; }
  else { print "<TITLE>High School Cyberpals Edit Form</TITLE>"; }
  print "</HEAD><BODY>";
  if ($add) { print "<H1>High School Cyberpals Add Form</H1>"; }
  else { 
    print "<H1>High School Cyberpals Edit Form</H1>"; 
    open(DB, "$DB_FILE");
    while ($record = <DB>) {
      ($tmpclass,$tmpname,$tmpage,$tmpsex,$tmptown,$tmpstate,$tmpcountry,$tmpemail,$tmphomepage,$tmpentry,$tmppostdate,$tmppasswd) = &Unpack_Record($record);
      if ($postdate eq $tmppostdate) {
        if (($passwd ne $tmppasswd || $tmppasswd eq "") && $passwd ne $SECRET) { 
         print "You entered the wrong password!  </BODY> </HTML>";
         die;
        }
      } 
    }
    close DB;
  }
  print "<FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">";
  if (not $add) { print "<INPUT TYPE=\"hidden\" NAME=\"postdate\" VALUE=\"$postdate\" SIZE=100>"; }
  print "
<INPUT TYPE=\"hidden\" NAME=\"class\" VALUE=\"high\" SIZE=100 VALUE=\"$class\">

<TABLE>
  <TR>
    <TD>Full Name (or Nickname):</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"name\" SIZE=40 VALUE=\"$name\"></TD>
  </TR>

  <TR>
    <TD>Sex:</TD>
    <TD>
";
   if ($sex eq  "M") {
      print "<INPUT TYPE=\"Radio\" NAME=\"sex\" VALUE=\"M\" CHECKED> Male";
   } else { 
      print "<INPUT TYPE=\"Radio\" NAME=\"sex\" VALUE=\"M\"> Male";
   } 
   if ($sex eq "F") {
      print "<INPUT TYPE=\"Radio\" NAME=\"sex\" VALUE=\"F\" CHECKED> Female";
   } else {
      print "<INPUT TYPE=\"Radio\" NAME=\"sex\" VALUE=\"F\"> Female"; 
   }
print "
    </TD>
  </TR>

  <TR>
    <TD>Age: </TD>
    <TD><INPUT TYPE=\"text\" NAME=\"age\" SIZE=10 VALUE=\"$age\"></TD>
  </TR>

  <TR>
    <TD>City/Town:</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"town\" SIZE=40 VALUE=\"$town\"></TD>
  </TR>

  <TR>
    <TD>State (use 2-letter code if USA):</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"state\" SIZE=20 VALUE=\"$state\"></TD>
  </TR>

  <TR>
    <TD>Country:</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"country\" SIZE=20 VALUE=\"$country\"></TD>
  </TR>

  <TR>
    <TD>E-mail address:</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"email\" SIZE=40 VALUE=\"$email\"></TD>
  </TR>

  <TR>
    <TD>Enter the URL of your homepage (if you have one).  <BR>
Please include the \"http://\":<BR>
        </TD>
    <TD><INPUT TYPE=\"text\" NAME=\"homepage\" SIZE=40 VALUE=\"$homepage\"></TD>
  </TR>
  <TR>
    <TD>Password for your personal ad: (this allows you to edit your ad later)</TD>
    <TD><INPUT TYPE=\"password\" NAME=\"the_passwd\" SIZE=10 VALUE=\"$passwd\"></TD>
  </TR>
</TABLE>

Enter your personal ad:<BR>
<TEXTAREA TYPE=\"text\" NAME=\"entry\" ROWS=12 COLS=60>$entry</TEXTAREA>
<p>

<CENTER>
<INPUT TYPE=\"submit\" NAME=\"preview_button\" VALUE=\"Preview Personal\">";
  if ($add) { print "<INPUT TYPE=\"submit\" NAME=\"post_button\"    VALUE=\"Post Personal\">"; }
  else { 
    print "<INPUT TYPE=\"submit\" NAME=\"post_button\" VALUE=\"Update Personal\">"; 
    print "<INPUT TYPE=\"submit\" NAME=\"delete_button\" VALUE=\"Delete Personal\">";
  }
  print "
<INPUT TYPE=\"reset\"  VALUE=\"Reset Form\">
</CENTER>
<P>

<P>
<HR>
<P>
|<A HREF= \"$home/index.html\">The Center</A>|<A HREF= \"$home/comment.html\">Your Comments And Ours</A>|<A HREF= \"$home/blurb.html\">About The Student Center</A>|

</BODY>
</HTML>
";
}



sub Preview_Personal {
  # PURPOSE: Show the user what the Personal will look like and allow him
  #          her to Submit the Personal to the database 
  # INPUTS:  Data supplied by the assoc array %in
  # OUTPUTS: An HTML Document displaying a preview of how the personal
  #          along with a form with embeded data allowing the user to Submit
  #          the personal.
  local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);
  local($record);
  # Pull out the variables we're interested in
  $class = $input{'class'};
  $name = $input{'name'};
  $age = $input{'age'};
  $sex = $input{'sex'};
  $town = $input{'town'};
  $state = $input{'state'};
  $country = $input{'country'};
  $email = $input{'email'};
  $homepage = $input{'homepage'};
  $entry = $input{'entry'};
  $passwd = $input{'the_passwd'};
  # Get the date from the system clock
  $postdate = $input{'postdate'};
  $oldpostdate = $input{'postdate'};
  if (not $input{'postdate'}) { $add = 1; }
  $postdate = `date`;
  chop($postdate);
  # Print out a Preview of the Personal
  print &PrintHeader;
  print "
    <HTML>
      <HEAD>
        <TITLE>Cyberpals personal Ad Preview</TITLE>
      </HEAD>
      <BODY><H1>Cyberpals Personal Ad Preview</H1><P><BR>
  ";
  
  $record = &Pack_Record($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);
  &Display_Record($record);
  # Embed the posted data into the output and provide a Submit button
  # to allow the user to Post the data.
  print "<P><BR>
        <FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">
          <INPUT TYPE=\"hidden\" NAME=\"class\" VALUE=\"$class\">
          <INPUT TYPE=\"hidden\" NAME=\"name\"     VALUE=\"$name\">
          <INPUT TYPE=\"hidden\" NAME=\"age\"      VALUE=\"$age\">
          <INPUT TYPE=\"hidden\" NAME=\"sex\"      VALUE=\"$sex\">
          <INPUT TYPE=\"hidden\" NAME=\"town\"     VALUE=\"$town\">
          <INPUT TYPE=\"hidden\" NAME=\"state\"    VALUE=\"$state\">
          <INPUT TYPE=\"hidden\" NAME=\"country\"  VALUE=\"$country\">
          <INPUT TYPE=\"hidden\" NAME=\"email\"    VALUE=\"$email\">
          <INPUT TYPE=\"hidden\" NAME=\"homepage\" VALUE=\"$homepage\">
          <INPUT TYPE=\"hidden\" NAME=\"entry\"    VALUE=\"$entry\">
          <INPUT TYPE=\"hidden\" NAME=\"the_passwd\"   VALUE=\"$passwd\">
          <INPUT TYPE=\"hidden\" NAME=\"postdate\" VALUE=\"$oldpostdate\" SIZE=100>
          <H4>Press <INPUT TYPE=\"submit\" NAME=\"post_button\" VALUE=\"Submit\">
          to add your Cyberpals Personal to the database or use your browser's 
          \"Back\" button to go back and edit your posting.</H4>
	  <BR><BR>
	  <P>
	  |<A HREF= \"$home/index.html\">The Center</A>|<A HREF= \"$home/comment.html\">Your Comments And Ours</A>|<A HREF= \"$home/blurb.html\">About The Student Center</A>|
        </FORM>
      </BODY>
    </HTML>
  ";      

}



sub Display_Record {
  # PURPOSE: Display a Personal
  # INPUTS:  A "packed" record
  # OUTPUTS: An HTML Document displaying the personal

  local($record) = @_;
  
local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);

   
  
($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd) = &Unpack_Record($record);

  print "
    Last updated: $postdate
    <TABLE border=1 width=100%>
      <TR>
        <TD WIDTH=40%>From ";

  if ($homepage eq "") {
     print "<B>$name</B> ";
  } 
  else {
     print "<B><A HREF=\"$homepage\">$name</A> </B>";
  } 

  if ($email eq "") {
     print "(<i>$email</i>) ";
  } 
  else {
     print "(<A HREF=\"mailto:$email\"><i>$email</i></A>) ";
  } 


  print "
        </TD>
        <TD WIDTH=15%>Sex: $sex</TD>
        <TD WIDTH=15%>Age: $age</TD>
        <TD WIDTH=30%>$town, $state, $country</TD>
      </TR>
      <TR>
        <TD COLSPAN=4>$entry</TD>
      </TR>
    </TABLE>
  ";
}

sub Display_HiddenRecord {
  # PURPOSE: Display Hidden info for a Personal
  # INPUTS:  A "packed" record
  # OUTPUTS: An HTML Document displaying the hidden personal 

  local($record) = @_;

local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);

($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd) = &Unpack_Record($record);
print "
          <INPUT TYPE=\"hidden\" NAME=\"class\" VALUE=\"$class\">
          <INPUT TYPE=\"hidden\" NAME=\"name\"     VALUE=\"$name\">
          <INPUT TYPE=\"hidden\" NAME=\"age\"      VALUE=\"$age\">
          <INPUT TYPE=\"hidden\" NAME=\"sex\"      VALUE=\"$sex\">
          <INPUT TYPE=\"hidden\" NAME=\"town\"     VALUE=\"$town\">
          <INPUT TYPE=\"hidden\" NAME=\"state\"    VALUE=\"$state\">
          <INPUT TYPE=\"hidden\" NAME=\"country\"  VALUE=\"$country\">
          <INPUT TYPE=\"hidden\" NAME=\"email\"    VALUE=\"$email\">
          <INPUT TYPE=\"hidden\" NAME=\"homepage\" VALUE=\"$homepage\">
          <INPUT TYPE=\"hidden\" NAME=\"entry\"    VALUE=\"$entry\">
	  <INPUT TYPE=\"hidden\" NAME=\"postdate\" VALUE=\"$postdate\">
";
  if ($_[1]) { print "<INPUT TYPE=\"hidden\" NAME=\"the_passwd\"   VALUE=\"$passwd\">"; }
}

sub Display_Search_Page {
print &PrintHeader;
print "
<HTML>
<HEAD>
<TITLE>Cyberpals Search Form</TITLE>
</HEAD>
<BODY>
<H1>Cyberpals Search Form</H1>
Fill in as many fields as you wish and press the SEARCH button to start your search.
All searches are case-insensitive.  Wildcard characters (ie *,%,#,$ etc.) are not required.
<P>
<STRONG>To view all listings, just hit the search button!</STRONG>
<HR><P>

<FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">

<INPUT TYPE=\"hidden\" NAME=\"search_class\" VALUE=\"high\" SIZE=100>

<TABLE>
  <TR>
    <TD>Name:</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"search_name\" SIZE=40></TD>
  </TR>

  <TR>
    <TD>Sex:</TD>
    <TD>
      <INPUT TYPE=\"Radio\" NAME=\"search_sex\" VALUE=\"\" CHECKED> Male or Female
      <INPUT TYPE=\"Radio\" NAME=\"search_sex\" VALUE=\"M\"> Male
      <INPUT TYPE=\"Radio\" NAME=\"search_sex\" VALUE=\"F\"> Female
    </TD>
  </TR>

  <TR>
    <TD>Age Range:</TD>
    <TD>
      <INPUT TYPE=\"text\" NAME=\"search_age_min\" SIZE=10>
      To
      <INPUT TYPE=\"text\" NAME=\"search_age_max\" SIZE=10>
    </TD>
  </TR>

  <TR>
    <TD>Town:</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"search_town\" SIZE=20></TD>
  </TR>

  <TR>
    <TD>State (use 2-letter code if USA):</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"search_state\" SIZE=20></TD>
  </TR>

  <TR>
    <TD>Country:</TD>
    <TD><INPUT TYPE=\"text\" NAME=\"search_country\" SIZE=20></TD>
  </TR>

  <TR>
    <TD>
      Keyword(s) <BR>
      (Separate Keywords with spaces):
    </TD>
    <TD><INPUT TYPE=\"text\" NAME=\"search_entry\" SIZE=40></TD>
  </TR>

  <TR>
    <TD>Search Type:</TD>
    <TD>
      <INPUT TYPE=\"Radio\" NAME=\"search_boolean\" VALUE=\"AND\">
      Only Return Records Matching ALL Keywords
      <BR>
      <INPUT TYPE=\"Radio\" NAME=\"search_boolean\" VALUE=\"OR\" CHECKED>
      Return Records Matching ANY of the Keywords
    </TD>
  </TR>

  <TR>
    <TD>Limit the number of <BR>personals displayed to:</TD>
    <TD> <INPUT TYPE=\"text\" NAME=\"search_match_max\" SIZE=20 VALUE = \"10\"></TD>
  </TR>
</TABLE>


<CENTER>
<INPUT TYPE=\"submit\" NAME=\"search2_button\" VALUE=\"Search\">
<INPUT TYPE=\"reset\">

</CENTER>
<P>

<P>
<HR>
<P>
|<A HREF= \"$home/index.html\">The Center</A>|<A HREF= \"$home/comment.html\">Your Comments And Ours</A>|<A HREF= \"$home/blurb.html\">About The Student Center</A>|

</BODY>
</HTML>
";
}

sub Search_Database {          
  # PURPOSE: Scan through the database and pick out the records that match
  #          the search criteria.  Display a maximum of $search_match_max 
  #          matching records staring with match number $search_match_start. 
  # INPUTS:  Search data supplied by the assoc array %in
  # OUTPUTS: An HTML document displaying the matches along with a form (with)
  #          embedded search criteria) that allows the user to continue the
  #          search if more matching records exist.
  local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);
  local($search_class,$search_name,$search_age,$search_sex,$search_town,$search_state,$search_country,$search_entry);
  local($record,$match_count);

  #
  # Pull out the variables we're interested in
  #
  $search_class = $input{'search_class'};
  $search_name = $input{'search_name'};
  $search_age_min = $input{'search_age_min'};
  $search_age_max = $input{'search_age_max'};
  $search_sex = $input{'search_sex'};
  $search_town = $input{'search_town'};
  $search_state = $input{'search_state'};
  $search_country = $input{'search_country'};
  $search_entry = $input{'search_entry'};
  $search_boolean = $input{'search_boolean'};
  $search_match_start = $input{'search_match_start'};
  $search_match_max = $input{'search_match_max'};
  
  $search_match_start = 1  if ($search_match_start eq "");
  $search_match_max   = 10 if ($search_match_max   eq "");

  print "Content-type: text/html\n\n";
  print "
    <HTML>
      <HEAD>
      </HEAD>
      <BODY>
	<H1>Cyberpals Search Results</H1><P> 
 ";
  

  open(DB,"$DB_FILE");
  $match_count = 0;

  #
  # Loop through the records comparing each field to the search criteria.
  # Immediately drop out of the loop if any of the fields do not pass the 
  # search criteria.
  #
  while ($record = <DB>) {

    
($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd) = &Unpack_Record($record);
    
    next if ( !($class    =~ /$search_class/i)    && ($search_class ne ""));
    next if ( !($name     =~ /$search_name/i)     && ($search_name     ne ""));
    next if (  ($age      <   $search_age_min )   && ($search_age_min  ne ""));
    next if (  ($age      >   $search_age_max )   && ($search_age_max  ne ""));
    next if ( !($sex      =~ /$search_sex/i)      && ($search_sex      ne ""));
    next if ( !($town     =~ /$search_town/i)     && ($search_town     ne ""));
    next if ( !($state    =~ /$search_state/i)    && ($search_state    ne ""));
    next if ( !($country  =~ /$search_country/i)  && ($search_country  ne ""));
   
    if (($search_boolean eq "OR") && ($search_entry ne "")) {
      #
      # Boolean OR case - ANY key must match
      #
      $match_found = 0 ;
      @keywords = split(/\s/,$search_entry);
      foreach $keyword (@keywords) {
        $match_found = 1 if ($entry =~ /$keyword/i);
      }
      next if !$match_found;

    } else {
      #
      # Boolean AND case -- all keywords MUST match 
      #
      $found_mismatch = 0;
      @keywords = split(/\s/,$search_entry);
      foreach $keyword (@keywords) {
        $found_mismatch = 1 if !($entry =~ /$keyword/i);
      }
      next if $found_mismatch;
    }
    
    #
    # If we made it this far then we must have a match
    #
    ++$match_count;

    #
    # Display the record if it falls within the proper range
    #
    if (  ($match_count >= $search_match_start)
       && ($match_count < ($search_match_start + $search_match_max))) {
      &Display_Record($record);
      print "<FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">";
      &Display_HiddenRecord($record, 0);	
      print "<INPUT TYPE=\"submit\" NAME=\"passwd_button\" VALUE=\"Edit\">";
      print "</FORM><HR>";
    }

    # 
    # Separate records with a New-line
    #
    print "<P>";
  }

  close(DB);

  #
  # Figure out if there are more records to be displayed
  #

  if (($search_match_start + $search_match_max - 1) >= $match_count) {
    #
    # We must have displayed all of them. 
    #
    print "<H3>Found $match_count Matching Personals.</H3>";

	if ($match_count==0) {
	  #
	  # If no matches found then say so
	  #
   	   print "0 matches are displayed.";
	} else {

	   print "Matches $search_match_start - $match_count are displayed."; 
	}
  } else {
    #
    # There must be more
    #
    $max_match_displayed = $search_match_start + $search_match_max - 1;
    print "
        <H3>Found $match_count Matching Personals.</H3>";  
	if ($match_count==0) {
	  #
	  # If no matches found then say so
	  #
   	   print "0 matches are displayed.";
	}  else  {

	print "
	Matches $search_match_start - $max_match_displayed are displayed. <BR>
        <FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">
        <H4>Press <INPUT TYPE=\"submit\" NAME=\"search2_button\" VALUE=\"More\"> to see more matching Personals.</H4>
	    ";
    	}
    #
    # Embed the data from the previous search and increment $search_match_start
    # to allow the next search to display the next set of matching records
    #
    print "
          <INPUT TYPE=\"hidden\" NAME=\"search_class\"       VALUE = \"$search_class\">
          <INPUT TYPE=\"hidden\" NAME=\"search_age_min\"     VALUE = \"$search_age_min\">
          <INPUT TYPE=\"hidden\" NAME=\"search_age_max\"     VALUE = \"$search_age_max\">
          <INPUT TYPE=\"hidden\" NAME=\"search_sex\"         VALUE = \"$search_sex\">
          <INPUT TYPE=\"hidden\" NAME=\"search_town\"        VALUE = \"$search_town\">
          <INPUT TYPE=\"hidden\" NAME=\"search_state\"       VALUE = \"$search_state\">
          <INPUT TYPE=\"hidden\" NAME=\"search_country\"     VALUE = \"$search_country\">
          <INPUT TYPE=\"hidden\" NAME=\"search_entry\"       VALUE = \"$search_entry\">
          <INPUT TYPE=\"hidden\" NAME=\"search_boolean\"     VALUE = \"$search_boolean\">
          <INPUT TYPE=\"hidden\" NAME=\"search_match_start\" VALUE = \"";
    print $search_match_start+$search_match_max;
    print "\">
          <INPUT TYPE=\"hidden\" NAME=\"search_match_max\"   VALUE = \"$search_match_max\">
        </FORM>
    "; 
  }
    
  #
  # Print a closing footer
  #
  print " <P>
	  |<A HREF= \"$home/index.html\">The Center</A>|<A HREF= \"$home/comment.html\">Your Comments And Ours</A>|<A HREF= \"$home/blurb.html\">About The Student Center</A>|

      </BODY>
    </HTML>
  ";
}


#########################################################################
# Database Locking functions
#########################################################################

sub Lock_Database {
  #
  # PURPOSE: Attempt to "lock" the database by creating a lock file.
  # INPUTS:  <NONE>
  # OUTPUTS: A boolean telling if the lock was sucessful
  #

  if (-e "$DB_FILE.lock") {  # DB is already locked
    0;
  } else {
    system "touch","$DB_FILE.lock";
    1;
  }
}

sub Unlock_Database {
  #
  # PURPOSE: Attempt to "unlock" the database by deleting the  lock file.
  # INPUTS:  <NONE>
  # OUTPUTS: A boolean telling if the unlock was sucessful
  #
  
  unlink("$DB_FILE.lock");
}

sub Display_Info {
 print &PrintHeader;
 print "
<HTML>
<HEAD> 
<TITLE>High School Cyberpals</TITLE>
</HEAD>
<BODY>
<CENTER>
<IMG SRC=$home/graphics/CYBER_P.GIF>
<BR>
<FONT SIZE=\"-2\">
Graphics by    
<A HREF=\"mailto\:rcgray\@mailbox.syr.edu\">Digital Cuisine</A>.
</FONT>
<H1>High School Cyberpals</H1>
<H3>
The Best Way to Meet High School Students With Similar Interests
</H3>
</CENTER>

<P><HR>
THE STUDENT CENTER is not responsible for the contents of the entries.    
We love the 1st Amendment, but please keep the public stuff clean.
<HR><P>
<H3>
<FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">
<INPUT TYPE=\"submit\" NAME=\"add_button\" VALUE=\"Add\">
</FORM>
Add your own Cyberpals personal to the database</A>
</H3>
<P><HR><P>

<H3>
<FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">
<INPUT TYPE=\"submit\" NAME=\"search_button\" VALUE=\"Search\">
</FORM>
Search for other Cyberpals with common interests</A>
</H3>

<P>
<HR>
<P>
|<A HREF= \"$home/index.html\">The Center</A>|<A HREF= \"$home/comment.html\">Your Comments And Ours</A>|<A HREF= \"$home/blurb.html\">About The Student Center</A>|

</BODY>
</HTML>
";
}

sub Ask_For_Passwd {
  local($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);
  local($record);
  # Pull out the variables we're interested in
  $class = $input{'class'};
  $name = $input{'name'};
  $age = $input{'age'};
  $sex = $input{'sex'};
  $town = $input{'town'};
  $state = $input{'state'};
  $country = $input{'country'};
  $email = $input{'email'};
  $homepage = $input{'homepage'};
  $entry = $input{'entry'};
  $postdate = $input{'postdate'};
  $record = &Pack_Record($class,$name,$age,$sex,$town,$state,$country,$email,$homepage,$entry,$postdate,$passwd);

print &PrintHeader;
print "
<HTML>
<HEAD>
<TITLE>Verification</TITLE>
</HEAD>
<BODY>
<FORM ACTION=\"$SCRIPT\" METHOD=\"POST\">
";
  &Display_HiddenRecord($record);
print "
Please enter the password to edit this personal ad:
<INPUT TYPE=\"password\" NAME=\"the_passwd\" SIZE=10><P>
<INPUT TYPE=\"submit\" NAME=\"edit_button\" VALUE=\"Edit\">
</FORM>
</BODY>
</HTML>
";
}

# Verify_Passwd {
#  local($record) = @_;
# 
#  1;
# }
