#!/usr/local/bin/perl
#----------------------------------------------------------------------------#
#                                                                            #
# db_TalkToMe.cgi    Version 2.0 Lite                                        #
#                                                                            #
#                       						    						 #
# Database version of the talktome script                                    #
# Inspired by the TalkToMe.cgi written by Sue Braiden			     		 #
#                                                                            #
# Written by: Brian Helle  webmaster@expertsolutions.net                     #
# Date Released: 2/14/2000                                                   #
#                                                                            #
# Description:	User definable single forum threaded message board using     #
#               flat file databases.                                         #
#		More information and the FULL version available              		 #
#		at www.expertsolutions.net                      	   			 	 #
#									     									 #
# Required files:							    							 #
#			db_TalkToMe.cgi					    							 #
#			dbttm_admin.cgi					    							 #
#			main_forum.setup.cgi  				    						 #
#			date.pl         				    							 #
#			cgi-lib.pl      				    							 #
#----------------------------------------------------------------------------#

#----------------------------------------------------------------------------#
# COPYRIGHT NOTICE                                                           #
# Copyright 2000 Brian Helle   All Rights Reserved.                          #
#                                                                            #
# This program may be used free of charge by anyone so long as               #
# the following requirements are met.                                        #
# This copyright notice, and all comments remain intact.                     #
# The link back to me remains intact in the footer section of all pages.     #
#                                                                            #
# By using this code you agree to indemnify Brian Helle from any liability   #
# that might arise from it's use.                                            #
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden.  In other words, please ask first before you try and  #
# make money off of my program.                                              #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium.  In all cases copyright and header must remain intact.#
#                                                                            #
# This Copyright is in full effect in any country that has International     #
# Trade Agreements with the United States of America.                        #
#----------------------------------------------------------------------------#

#----------------------------------------------------------------------------#
# Acknowledgments:                                                           #
#                                                                            #
# The following people have had significant impact on this project.          #
# Sue Braiden: Author of the original TalkToMe script.                       #
# Calle Johannesson: Original artwork and inspiration.                       #
# Chris Hunt: Author of the function random_password()                       #
# Last, but in no way least, AnnaMary Helle, my wife.                        #
#	For listening to me ramble on and on about code and checking and     #
#	re-checking the program -- Thank you.                                #
#----------------------------------------------------------------------------#
if (length ($ENV{'QUERY_STRING'}) > 0){
	$buffer = $ENV{'QUERY_STRING'};
	 @pairs = split(/&/, $buffer);
	foreach $pair (@pairs){
		($name, $value) = split(/=/, $pair);
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$in{$name} = $value; 
		}
	}

$forumy = ($in{'forum_name'});

if ($forumy eq "") {
$forumy = "main";
	}

#if ($forumy eq "main") {
#require ("main_forum.setup.cgi") || die ("Cant require Forum setup file");
#	}
#if ($forumy eq "burn") {
#require ("burn_forum.setup.cgi") || die ("Cant require Forum setup file");
#	}

$|=1;

print "Content-type: text/html\n\n";



# REQUIRE LIBRARY FILES
require ("cgi-lib.pl") || die ("Cant require cgi-lib.pl");
require ("date.pl") || die ("Cant require date.pl");
#require ($forumy.".setup.cgi") || die ("Cant require Forum setup file");
require ("all_forum.setup.cgi") || die ("Cant require Forum setup file");


# USER CONFIG
# YOU MUST SET THE FOLLOWING VARIABLES TO YOUR SPECIFIC SERVER.
$date_command = "/usr/bin/date";
$mail_program = "/usr/lib/sendmail -t -n";
$error_admin = "d\@schmacme.com";
$databoard_url ="http://www.schmacme.com/cgi-bin/discussion/thread.cgi";



##############################################################
# NO NEED TO EDIT ANYTHING BELOW THIS LINE

# SET THE DATE IN A VARIABLE
# This date command allows for Y2K compliance. The year is 4 digits
$shortdate = `$date_command +"%m/%d/%Y %T %Z"`;
chop($shortdate);

# PROGRAM FLOW #############################################

# READ THE INCOMMING DATA AND ADD TO AN ARRAY
&ReadParse(*form_data);
&clean_input;
# option logic
if ($form_data{'message_number'} ne "") {
	&page_header;
	&nav_table_detail;
	&view_message_detail;
 	}
 if ($form_data{'mode'} eq "preview") {
	&page_header;
	&verify_fields;
	&nav_table;
	&preview_message;
	&preview_page_footer;
	exit;
	} 
if ($form_data{'form_action'} eq "") {
	  &display_default;
	}
if ($form_data{'form_action'} eq "post") {
	&post_message;
	}
if ($form_data{'form_action'} eq "follow_up") {
	&post_follow_up;
	}
if ($form_data{'form_action'} eq "search") {
	&forum_power_search;
	} # end if

exit;


# FUNCTIONS

#---------------------------------------------------------------------#
# Display default page                                                #
# after a post or followup post, show the user the main disply output #
#---------------------------------------------------------------------#
sub display_default
  {
	  &page_header;
	  &nav_table;
	  &message_display_all;
	  &post_new_message;
	  &page_footer;
} # end function


#------------------------------------------------------------------------------#
# load_datafile                                                                #
# Takes a file name as an argument and returns an array with the file contents #
# Modified 8/10/99 to allow for blank lines, also creates a new array of the   #
# comment lines for easier updating of a file.                                 #
#------------------------------------------------------------------------------#
sub load_datafile
  {
local ($file_to_get) = @_;
local ($line);
local (@clean_records);
print ("<br>Can't open $file_to_get \n") unless (open(DATAFILE, "$file_to_get"));
flock(DATAFILE, 2);
  @all = <DATAFILE>;
flock(DATAFILE, 8);
close(DATAFILE);
$rec_count = 0;
# set the comment records in it's own list
foreach (@all) {
	if ($_ =~ /^COMMENT:/) {
		push(@comment_records, $_);
	}
}
# Strip out any comments or blank lines
foreach $line (@all) {
	if ($line !~ /^COMMENT:/ && $line ne "\n") {
		push(@clean_records, $line);
		$rec_count++;
	} # end if
} # end foreach
if ($rec_count == 0) {
	push(@clean_records, "none");
} 
	
return (@clean_records);
} # end function

#--------------------------------------------#
# Output a threaded list of all the messages #
#--------------------------------------------#
sub message_display_all
  {

# Get all of the messages
if (-e "$data_file_path") {
	@all_messages = &load_datafile($data_file_path);
} else {
	$all_messages[0] = "none";
}

# See if the data file is empty, if so, report this and exit
if ($all_messages[0] eq "none") {
	print qq~
	<center>
	<table border=1 bgcolor="000000" width="200">
	<tr><td align="center"><font face="Verdana" size=2><br><b>there are no messages yet</b><br></font></td></tr>
	</table></center>
	~;
&post_new_message;
&page_footer;
exit;
	
} 


# Set up an ordered list of messages and the parent messages
foreach (@all_messages) {
	@message_fields = split(/\|/, $_);
	$ordered_messages[$message_fields[0]] = $_;
	if ($message_fields[11] eq "" ) {
		# record has no parent
	  	push (@parent_messages, $_);
	}
}

# Start the message table
print qq~
<table border=0 bgcolor="#000000" width="95%" align=center>
<tr><td bgcolor="$message_area_color"><font size=2><ul>
~;
# Listing ordered oldest to newest.
if ($display_order == 1) {
	foreach (@parent_messages) {
		@pr_fields = split(/\|/, $_);
		print qq~
		<li>
		<img src="$icon_path$pr_fields[7]">
<a href="$databoard_url?forum_name=$forum_name&message_number=$pr_fields[0]">$pr_fields[4]</a>
 - <b>$pr_fields[2]</b> $pr_fields[1] ($pr_fields[13])<br>
~;
		if ($pr_fields[12] ne "") { # this record has children
		  @list_to_send = split(/,/, $pr_fields[12]);
		  &print_all_children(@list_to_send);
		} 
	}
} else {
	
	for ($i = $#parent_messages; $i >= 0; $i--) {
		@pr_fields = split(/\|/, $parent_messages[$i]);
		print qq~
		<li>
		<img src="$icon_path$pr_fields[7]">
<a href="$databoard_url?forum_name=$forum_name&message_number=$pr_fields[0]">$pr_fields[4]</a>
 - <b>$pr_fields[2]</b> $pr_fields[1] ($pr_fields[13])<br>
~;
		if ($pr_fields[12] ne "") { # this record has children
		  @list_to_send = split(/,/, $pr_fields[12]);
		  &print_all_children(@list_to_send);
		} 
	}
	
}

$tm = $#all_messages + 1;
@lrf = split(/\|/, $all_messages[$tm-1]);
print qq~
</ul><br>&nbsp;</font></td></tr>
<td bgcolor="$message_total_color" align="center"><font face="Verdana" size=2>
<hr width="50%">
total messages = $tm &nbsp;&nbsp;&nbsp; last entry date = $lrf[1]
<hr width="50%">
</font></td></tr>
</table>

~;
} # end display messages all function

#--------------------------------------------#
# Print all children messages function       #
# This is a recursive function               #
#--------------------------------------------#
sub print_all_children
  {
local (@children_list) = @_;
print ("<ul>"); # start a nested list item
foreach $this_child (@children_list) {
	@this_child_fields = split(/\|/, $ordered_messages[$this_child]);
	$message_number = "message_number\=$this_child_fields[0]"; # Sets the variable for use in the href link
		print qq~
		<li>
		<img src="$icon_path$this_child_fields[7]">
<a href="$databoard_url?forum_name=$forum_name&message_number=$this_child_fields[0]">$this_child_fields[4]</a>
 - <b>$this_child_fields[2]</b> $this_child_fields[1] ($this_child_fields[13])<br>
~;

	if ($this_child_fields[12] ne "") { # this child has children
	  @list_to_send = split(/,/, $this_child_fields[12]);
	  &print_all_children(@list_to_send);
	  } # end if
} # end foreach
	
print ("</ul>"); # End the nested list items

} # end function




##############################################################################
# Display message details
##############################################################################
sub view_message_detail
  {

@record_data = &load_datafile($data_file_path);
# Create a new array with the subsets corrisponding to the record number
foreach $line (@record_data) {
	@new_line = split(/\|/, $line);
	$record_number = $new_line[0];
	$ordered_records[$record_number] = $line;
	} # end foreach

$message = $form_data{'message_number'};
@message_data = split(/\|/, $ordered_records[$message]);

print qq~
<table border=1 bgcolor="000000">
<tr><td>
~;

print ("<img src=\"$icon_path$message_data[7]\"> <b>posted by: </b><a href=\"mailto:$message_data[3]\"> $message_data[2]</a> on: $message_data[1]");
print ("<br><b>Subject:</b> $message_data[4]");
print qq~
</td></tr></table>
<br>
<b>message detail:</b><br><hr>
<table border=0 cellpadding=3 width="95%" bgcolor="000000">
<tr><td height="100" width="95%" valign="top">
~;

if ($message_data[10]){
	print ("<center><img src=\"$message_data[10]\"></center><br><br>");
	} # end if

print ("$message_data[5]");
print qq~
</td></tr></table>
~;

if ($message_data[9]){

print qq~
<b>message link:</b>
<table border=1 width="100%" bgcolor="000000">
<tr><td><br><a href="$message_data[8]">$message_data[9]</a><br></td></tr>
</table>
~;
} elsif ($message_data[8]) {

print qq~
<b>message link:</b>
<table border=1 width="100%" bgcolor="000000">
<tr><td><br><a href="$message_data[8]">message has a link</a><br></td></tr>
</table>
~;
} # end if

# supply a link to this records parent if any
if ($message_data[11] ne "") {
print qq~
<table border=1 width="100%" bgcolor="000000">
<tr><td><center>
<a href=\"$databoard_url\?$options\&message_number\=$message_data[11]\">view parent message</a>
</center></td></table>
~;
} # end if

# List messages children if any

if ($message_data[12]) {
  @reply_list = split(/,/, $message_data[12]);
print qq~
<b>follow ups:</b>
<table border=1 width="100%" bgcolor="000000">
<tr><td><ul>
~;
# Create a new array with the subsets corrisponding to the record number
foreach $line (@record_data) {
	@new_line = split(/\|/, $line);
	$record_number = $new_line[0];
	$ordered_records[$record_number] = $line;
	} # end foreach

foreach $item (@reply_list) {
	@this_child_fields = split(/\|/, $ordered_records[$item]);
	$message_number = "message_number\=$this_child_fields[0]"; # Sets the variable for use in the href link
	$post_type_img = "<img src=\"$icon_path$this_child_fields[7]\">";
	print ("<li>");
	print ("$post_type_img ");
	print (" <a href=\"$databoard_url\?$options\&$message_number\">$this_child_fields[4]</a>");
	print (" - <b>$this_child_fields[2]</b> $this_child_fields[1] \($this_child_fields[13]\)\n");
} # end foreach
} # end if
print ("</ul></table>");

&follow_up_input($message);
&page_footer;
exit;

} # end function


#-------------------------#
# post a followup message #
#-------------------------#
sub post_follow_up
  {

$record_to_modify = $form_data{'parent'};

$replies = 0; # The default is zero, record updates will alter this

open(CF, "$counter_file");
flock(CF, 2);
 while (<CF>)
  {
   $uid = "$_";
  }
flock(CF, 8);
close(CF);
$new_number = $uid + 1;

open(CF, ">$counter_file");
flock(CF, 2);
 print CF "$new_number";
flock(CF, 8);
close(CF);

open(DF, ">>$data_file_path");
flock(DF, 2);

	$new_line .= "$uid\|";
	$new_line .= "$shortdate\|";
	$new_line .= "$form_data{'name'}\|";
	$new_line .= "$form_data{'email'}\|";
	$new_line .= "$form_data{'subject'}\|";

      $form_data{'body'} =~ s/\n/<BR>/g;
	$form_data{'body'} =~ s/\r/ /g;
      $form_data{'body'} =~ s/\r\r/<P>/g;
      $form_data{'body'} =~ s/\|/:/g;


	$new_line .= "$form_data{'body'}\|";
	$new_line .= "$form_data{'notify_me'}\|";
	$new_line .= "$form_data{'post_type'}\|";
	$new_line .= "$form_data{'link_url'}\|";
	$new_line .= "$form_data{'link_title'}\|";
	$new_line .= "$form_data{'image_url'}\|";
	$new_line .= "$form_data{'parent'}\|";
	$new_line .= "$form_data{'children'}\|";
	$new_line .= "$replies\|";
	$new_line .= "$forum_name\n";


print DF "$new_line";

@notify = $new_line; # set an array with the reply record
flock(DF, 8);
close(DF);

# here we need to modify the record of the message this is a reply to. 
# fields updated are the children and replies fields.

unless (open(DATAFILE, "$data_file_path")) {
die ("cannot open database file $data_file_path \n");
} # end unless
flock(DATAFILE, 2);

while (($data_line = <DATAFILE>))
   {
	
	@fields = split(/\|/, $data_line); # Splits the record into fields
	$match_number = $fields[0]; 
	
	if ($match_number eq $record_to_modify) {
	  $fields[12] .= "$uid,"; # sets the child field to the follow up message number
	  $fields[13] += 1; # increment this number by one
	  push(@notify, join("\|", @fields));
	 } 
      push(@database_rows, join("\|", @fields)); # just rewrite the record to the database array
      
   } # end while
flock(DATAFILE, 8);
close(DATAFILE);


# now write the new database array to the datafile 

unless (open(DATAFILE, ">$data_file_path")) {
die ("cannot open database file $data_file_path \n");
}
flock(DATAFILE, 2);
foreach $record (@database_rows) {
  print DATAFILE "$record";
} # end foreach
flock(DATAFILE, 8);
close(DATAFILE);

# send the email notification
&send_notification(@notify);

&display_default; # goes back to the main screen

} # end of function


#--------------------#
# Post a new message #
#--------------------#
sub post_message
   {


$replies = 0; # The default is zero, record updates will alter this

# get the current counter number and index it
if (-e "$counter_file") {
	open(CF, "$counter_file");
	flock(CF, 2);
	 while (<CF>)
	  {
	   $uid = "$_";
	  }
	flock(CF, 8);
	close(CF);
} else {
	$uid = 0;
}

$new_number = $uid + 1;

open(CF, ">$counter_file");
flock(CF, 2);
 print CF "$new_number";
flock(CF, 8);
close(CF);

open(DF, ">>$data_file_path");
flock(DF, 2);

	$new_line .= "$uid\|";
	$new_line .= "$shortdate\|";
	$new_line .= "$form_data{'name'}\|";
	$new_line .= "$form_data{'email'}\|";
	$new_line .= "$form_data{'subject'}\|";

      $form_data{'body'} =~ s/\n/<BR>/g;
	$form_data{'body'} =~ s/\r/ /g;
      $form_data{'body'} =~ s/\r\r/<P>/g;
      $form_data{'body'} =~ s/\|/:/g;

	$new_line .= "$form_data{'body'}\|";
	$new_line .= "$form_data{'notify_me'}\|";
	$new_line .= "$form_data{'post_type'}\|";
	$new_line .= "$form_data{'link_url'}\|";
	$new_line .= "$form_data{'link_title'}\|";
	$new_line .= "$form_data{'image_url'}\|";
	$new_line .= "$form_data{'parent'}\|";
	$new_line .= "$form_data{'children'}\|";
	$new_line .= "$replies\|";
	$new_line .= "$forum_name\n";


print DF "$new_line";

flock(DF, 8);
close(DF);

&display_default; # goes back to the main display
} # end function


#-----------------------------------------------------------------------#
# send email notification                                               #
# Send the origonal poster an email telling them someone posted a reply #
#-----------------------------------------------------------------------#
sub send_notification 
  {
local (@arg_list) = @_;
($follow_up, $parent) = @arg_list;
@parent_fields = split(/\|/, $parent);
@follow_up_fields = split(/\|/, $follow_up);
$follow_up_number = "message_number\=$follow_up_fields[0]";
$options = "forum_name\=$forum_name";
# does the parent want to receive a notification
if ($parent_fields[6] == 1) {

open(MAIL, "|$mail_program -t")|| die "Can't open $mail_program!\n";

print MAIL "From: $admin_email ($admin_name)\n";
print MAIL "Reply-To: $admin_email ($admin_name)\n";
print MAIL "Errors-to: $admin_email ($admin_name)\n";
print MAIL "To: $parent_fields[3]\n";
print MAIL "Subject: $notify_subject\n\n";
print MAIL "I just wanted to let you know that $follow_up_fields[2] ($follow_up_fields[3])\n";
print MAIL "has posted a reply at the $forum_title dialogue to the posting entitled ";
print MAIL "$parent_fields[4], which was originally posted by $parent_fields[2] ($parent_fields[3]) ";
print MAIL " on $parent_fields[1].\n\n";
print MAIL "You can read this reply right now by going to this URL:\n";
print MAIL "$databoard_url?$options&$follow_up_number\n \n";
print MAIL "Regards,\n";
print MAIL "$admin_name.\n";

close (MAIL);
} # end if

} # end function

#---------------------------------------------------#
# forum power search                                #
# this searches forums from the db_TalkToMe program #
#---------------------------------------------------#
sub forum_power_search
  {
# Make sure the user supplied reasonable data in the entry fields.
# Also set the how many types variable.
$how_many_types = 0;
if($form_data{'limit_view'}) {
	if ($form_data{'limit_view'} =~ /\d/) {
		$how_many_types += 1;
	} else {
		&error_control("Invalid Character - Field requires numeric characters only.");
	} # end else
} # end if
if ($form_data{'poster_name'}) {
	if ($form_data{'poster_name'} =~ /[\w]+/) {
		$how_many_types += 1;
	} else {
		&error_control("Invalid Character - Field requires alpha characters only.");
	} # end else
} # end if
if ($form_data{'keywords'}) {
	if ($form_data{'keywords'} =~ /[\w]+/) {	
		@words = split(/\s+/, $form_data{'keywords'});
		$how_many_types += 1;
	} else {
		&error_control("Invalid Character - Field requires alpha characters only.");
	} # end else
} # end if
# do we want to search
if ($how_many_types == 0) {
	&error_control("Invalid - No search cryteria was specified.");
} # end if
# Now we need to decide where we are going to search.
push (@forums_to_search, $form_data{'forum_name'});

# Start results output.
&page_header;
print qq~
<center>
<table border=1 bgcolor="$search_main_color" cellpadding=2>
<tr><td>
<table cellpadding=5 cellspacing=0 width=600 border=0 bgcolor="$search_main_color">
<tr><td bgcolor="$search_middle_color" align="center"><font face="Verdana">
	<h2><u>Power Search Results</u></h2></font></td></tr>
<tr><td bgcolor="$search_middle_color" align="right"><font face="Verdana" size="2">
    The search results are grouped by forum.<br>Click the message link to
    view the message in a new window.<br></font></td></tr>
</center>
~;
# Start the initial searching logic
# Begin looping through the list of forums to search.
# For each forum we will create a two dimensional array to use as the search target.
@name_result = ();
@date_result = ();
@keyword_result = ();
foreach (@forums_to_search) {
	$this_file = "$_.data";
	$current_name = "$_";
	open (FDF, "$this_file" );
	flock(FDF, 2);
	while ( <FDF> ) {
		if ($_ !~ /^COMMENT:/) {
		@tmp = split(/\|/);	
		push @DFA, [ @tmp ];
		}
	}
	flock(FDF, 8);
	close(FDF);
	# Search for a name
	if ($form_data{'poster_name'}) {
		for $i ( 0 .. $#DFA) {
			if ($DFA[$i][2] =~ /$form_data{'poster_name'}/i) {
				push (@name_result, $i);
			}
		}
	}
	if ($form_data{'limit_view'} ne "") {
		$i = 0;
		$oldest = (&today() - $form_data{'limit_view'});
		for $i ( 0 .. $#DFA) {
			$day_jv = &jday(substr($DFA[$i][1],0,2),substr($DFA[$i][1],3,2),substr($DFA[$i][1],6,4));
			if ($day_jv >= $oldest) {
				push (@date_result, $i);
			}
		}
	}
	if ($form_data{'keywords'}) {
	@keywords = split(/\s+/, $form_data{'keywords'});
	if ($form_data{'boolean'} eq "any") {
		$i = 0;
		for $i ( 0 .. $#DFA) {
			foreach $word (@keywords) {
				if ($form_data{'case'}) {
					# Case sensitive
					if ("@{$DFA[$i]}" =~ /\b$word\b/) {
						push (@keyword_result, $i);
						last;
					}
				} else {
					#case insensitive
					if ("@{$DFA[$i]}" =~ /\b$word\b/i) {
						push (@keyword_result, $i);
						last;
					}
				}	
			}
		}
	} else {
	# All keywords
	$i = 0;
	for $i ( 0 ..$#DFA) {
		$match = 1;
		foreach $word (@keywords) {
			if ($match == 1) {
				if ($form_data{'case'}) {
					# Case sensitive
					if ("@{$DFA[$i]}" =~ /\b$word\b/) {
						$match = 1;
						next;
					} else {
						$match = 0;
						last;
					}
				} else {
					# case insensitive
					if ("@{$DFA[$i]}" =~ /\b$word\b/i) {
						$match = 1;
						next;
					} else {
						$match = 0;
						last;
					}
				}
			}
		}
		if ($match == 1) {
			push (@keyword_result, $i);
		}
	}
}
}
# Now we need to process the results depending if this was a single search
# or a multiple cryteria search.
if ($how_many_types == 1) {
	# This was a single type of search. Populate the Results array with
	# any matches from any single type of search.
	push (@Results, @date_result);
	push (@Results, @name_result);
	push (@Results, @keyword_result);
} elsif ($how_many_types == 2) {
	# This was a multiple type of search. We need to filter the single
	# results to find those records that matched in each type of search.
	print "starting the 2 type logic<br>";
	if ($form_data{'poster_name'} && $form_data{'limit_view'}) {
		foreach $N (@name_result) {
			foreach $D (@date_result) {
				if ($N eq $D) {
					push (@Results, $N);
				}
			}
		}
	} elsif ($form_data{'poster_name'} && $form_data{'keywords'}) {
		foreach $N (@name_result) {
			foreach $K (@keyword_result) {
				if ($N eq $K) {
					push (@Results, $N);
				}
			}
		}
	} elsif ($form_data{'limit_view'} && $form_data{'keywords'}) {
		foreach $D (@date_result) {
			foreach $K (@keyword_result) {
				if ($D eq $K) {
					push (@Results, $D);
				}
			}
		}
	} 
} else {
	# All Three cryteria
	foreach $N (@name_result) {
		foreach $D (@date_result) {
			if ($N eq $D) {
				push (@Hold, $N);
			}
		}
	}
	# before we continue, if nothing matched we can exit here.
	if (@Hold) {
		foreach $H (@Hold) {
			foreach $K (@keyword_result) {
				if ($H eq $K) {
					push (@Results, $H);
				}
			}
		}
	}
}
# We should have now populated the array Results IF we matched all the requested
# cryteria. if it's blank, we didn't pass all the tests.
# output the matching records
print qq~
<tr><td align="center" bgcolor="$search_detail_color"><font face="Verdana" size=2>
    <hr noshade width="90%"><b>$current_name</b></font></td></tr>
    ~;
if ($Results[0] ne "") {
        foreach (@Results) {
       		print qq~
       		<tr><td align="left" bgcolor="$search_detail_color"><font face="Verdana" size=2>
	    	<img src="$icon_path$DFA[$_][7]">
       		<a href="$databoard_url?forum_name=$current_name&message_number=$DFA[$_][0]" target="results">$DFA[$_][4]</a>
       		- <b>$DFA[$_][2]</b> $DFA[$_][1] ($DFA[$_][13])</font></td></tr>
       		~;
       	}
} else {
       	print qq~
       	<tr><td align="center" bgcolor="$search_detail_color"><font face="Verdana" size=2>
       	[ No matching records ]</font></td></tr>
       	~;
}
# Reset the arrays to no elements
        undef (@name_result);
        undef (@date_result);
        undef (@keyword_result);
        undef (@Results);
        undef (@Hold);
        undef (@DFA);
} # end of main foreach forum
# End the output, finish up the table
print qq~
<tr><td bgcolor="$search_middle_color" align="center"><font face="Verdana" size=2>
    <br>[<a href="$databoard_url?forum_name=$form_data{'forum_name'}"> return to $forum_title </a>]<br>&nbsp;</font></td></tr>
</td></tr></table></td></tr></table>
</center>
~;
# page footer here
&page_footer;
} # end forum power search function

######################################
#
# verify_fields
sub verify_fields
  {
if ($form_data{'name'} eq "" || $form_data{'email'} eq "" || $form_data{'subject'} eq "" || $form_data{'body'} eq "") {
	&error_control("Required Field Missing - Minimum fields are Name, Email, Subject, Message.");
} # end if
} # end verify fields

######################################
#
# Clean the incomming field data to remove unwanted characters.
# Added 8-2-99
sub clean_input
  {

@field_names = ("name",
		"email",
	        "subject",
	        "body",
	        "notify_me",
	        "post_type",
	        "link_url",
	        "link_title",
	        "image_url",
	        "parent",
	        "children",
	        "forum_name" );
	        

foreach (@field_names) {
	$form_data{$_} =~ s/\|/&#124;/g;
	$form_data{$_} =~ s/\{/&#125;/g;
	$form_data{$_} =~ s/\}/&#123;/g;
	$form_data{$_} =~ s/\(/&#040;/g;
	$form_data{$_} =~ s/\)/&#041;/g;
	$form_data{$_} =~ s/\[/&#091;/g;
	$form_data{$_} =~ s/\]/&#093;/g;
	$form_data{$_} =~ s/\~/&#126;/g;
	$form_data{$_} =~ s/\$/&#036;/g;
	$form_data{$_} =~ s/\|/&#124;/g;
}

} # end function



#---------------------------------------------------------------#
# Error Control Function                                        #
# Provides a standard error reporting utility to display errors #
# to the browser.                                               #
#---------------------------------------------------------------#
sub error_control
  {
local ($error_msg) = @_;
&page_header;
print qq~
<center>
<table border=1 cellpadding=2 cellspacing=2 bgcolor="000000" width="400">
<tr><td align="center"><font face="Verdana" size=2>
    <b>error control</b></font></td></tr>
<tr><td align="left"><font face="Verdana" size=2>
    something screwed up.<br>please use your browsers back
    button and correct the problem.</font></td></tr>
<tr><td align="center"><font face="Verdana" size=2>
    <b>details:</b><br>
    $error_msg<br><br></font></td></tr>
<tr><td align="center"><font face="Verdana" size=2>
    if you continue to have trouble<br>
    <a href="mailto:$error_admin">e-mail the administrator</a></font></td></tr>
</table>
~;
&page_footer;
exit;
} # end error control function




