#!/usr/bin/perl
# Change the title head.txt and foot.txt files too
        $sitetitle = 'Technical Term Search';
        $searchurl = 'http://www.tpub.com/dictionary';
        $base = '/home/superturd/dictionary/search/base.txt';
        $headfile = '/home/superturd/dictionary/search/head.txt';
        $footfile = '/home/superturd/web/_borders/bottom.htm';
        $respondfile = '/home/superturd/web/dictionary/search/respond.txt';
        $smutfile = '/home/superturd/web/dictionary/search/smut.txt';
        $scripturl = 'http://www.tpub.com/cgi-bin/tech.cgi';
	$font = 'FACE=arial,helvetica COLOR=000000';
        $minword = '3';
	$maxauthor = '50';
        $maxquote = '300';
	$maxkeywords = '50';
        $numnew = '300';
	$uselock = '1';
	$userespond = '1';

# Get the form variables

	if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        	$buffer = $ENV{'QUERY_STRING'};
	}	
	else {
        	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	}

# Break em up into a format the script can read

	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
        	($name, $value) = split(/=/, $pair);
        	$value =~ tr/+/ /;
        	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        	$FORM{$name} = $value;
	}

# Get the heading information

	unless (open (DATA,"$headfile")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 0;
		}
		@headinfo = <DATA>;
		if ($uselock eq '1') {
			flock DATA, 8;
		}
	close (DATA);
	foreach $headline (@headinfo){
		$heading = $heading.$headline;
	}

# Get the footer information

	unless (open (DATA,"$footfile")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 0;
		}
		@footinfo = <DATA>;
		if ($uselock eq '1') {
			flock DATA, 8;
		}
	close (DATA);
	foreach $footline (@footinfo){
		$footer = $footer.$footline;
	}

# Get the smut filter information

	unless (open (DATA,"$smutfile")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 0;
		}
		@smutinfo = <DATA>;
		if ($uselock eq '1') {
			flock DATA, 8;
		}
	close (DATA);
	foreach $smutline (@smutinfo){
		$smutfilter = $smutfilter.$smutline;
		@smutwords = split (/::/,$smutfilter);
	}

# Determine what part of the script we need

	if ($FORM{'action'} eq "showadd") {
		&showadd;
	}

	if ($FORM{'action'} eq "addurl") {
		&addurl;
	}

	$holder = $FORM{'keywords'};
	$position = $FORM{'code'};
	$nosearch = 0;
	$addshow = 0;
	$noshow = 0;
	$match = 0;
	@searchwords = split (/ /, $holder);
	if ($FORM{'safe'} ne "on") {
		$safekey = "off";
	} else {
		$safekey = "on";
	}

# Begin the search process and output the results

	unless (open (DATA,"$base")) {die (&error);}
	if ($uselock eq '1') {
		flock DATA, 2;
		seek DATA, 0, 0;
	}
	@input = <DATA>;
	if ($uselock eq '1') {
		flock DATA, 8;
	}
	close (DATA);

# Routine for 'words' search

	if ($FORM{'mode'} eq "words") {
		$searchstring=$FORM{'keywords'};
		@words = split (/ /,$searchstring);
		foreach $word (@words) {
			$wordlength = length($word);
			if ($wordlength < $minword) {
				&stringshort;
			}
		}
		&heading;
		$entries = @input;
		if ($position == 0) {
			$currentline = $entries;	
		} else {
			$currentline = $position;
		}
		$found="0";
		print "<CENTER><FONT $font SIZE=3><B>Search Results : </B>'$FORM{'keywords'}'<P></FONT></CENTER>";
		print "<HR WIDTH=400>";
		print "<FONT $font SIZE=2>";
		until ($found > 9 || $currentline == 0) {
			foreach $word (@words) {
				if ($input[$currentline] =~ /$word/i) {
					@data = split (/::/,$input[$currentline]);
					if ($data[1] ne "") {
						if ($safekey eq "on" && $match == 0) {
							foreach $smutword (@smutwords) {
								if ($input[$currentline] =~ /$smutword/i) {
									$smut = 1;
								}
							}	
							unless ($smut == 1) {
							print "<B>$data[0]</B></A><BR>";
                                		print "<I>$data[1]<BR></I><P>";
							++$found;
							++$match;
						}
						$smut = 0;
					}
						if ($safekey eq "off" && $match == 0) {
							print "<B>$data[0]</B></A><BR>";
                                		print "<I>$data[1]<BR></I><P>";
							++$found;
							++$match;
						}
					}
				}
			}
			--$currentline;
			$match = 0;
		}  
	}

# Routine for 'phrases' search

	if ($FORM{'mode'} eq "phrases") {
		$searchstring=$FORM{'keywords'};
			$wordlength = length($FORM{'keywords'});
			if ($wordlength < $minword) {
				&phrase;
			}
		&heading;
		$entries = @input;
		if ($position == 0) {
			$currentline=$entries;	
		} else {
			$currentline = $position;
		}
		print "<CENTER><FONT $font SIZE=3><B>Search Results : </B>'$FORM{'keywords'}'<P></FONT></CENTER>";
		print "<HR WIDTH=400>";
		print "<FONT $font SIZE=2>";
		until ($found > 9 || $currentline == 0) {
			if ($input[$currentline] =~ /$FORM{'keywords'}/i) {
				@data = split (/::/,$input[$currentline]);
				if ($data[1] ne "") {
					if ($safekey eq "on") {
							foreach $smutword (@smutwords) {
								if ($input[$currentline] =~ /$smutword/i) {
									$smut = 1;
								}
							}					
							unless ($smut == 1) {
							print "<B>$data[0]</B></A><BR>";
                                		print "<I>$data[1]<BR></I><P>";
							++$found;
						}
						$smut = 0;
					}
					if ($safekey eq "off") {
							print "<B>$data[0]</B></A><BR>";
                                		print "<I>$data[1]<BR></I><P>";
						++$found;
					}
				}
			}
			--$currentline;
		}  
	}
	print "</FONT>"; 
	&footer;

#################   SUBROUTINES   ######################

sub heading {
	print "Content-type: text/html\n\n";
	print "$heading";
}

sub footer {
	$keyencode=$FORM{'keywords'};
	$keyencode =~ tr/ /+/;
	if ($found > 9) {
		$position=$currentline;
                print "<CENTER><FONT $font size=3><A HREF=http://www.directleads.com/display/cd7396><IMG BORDER=0 SRC=http://www.directleads.com/ads/5625/random></A><br><A HREF=\"$scripturl?keywords=$keyencode&code=$position&mode=$FORM{'mode'}&safe=$safekey \"><B>More Results</B></A><BR><HR WIDTH=400></FONT></CENTER>";
	}
	else {
		unless ($addshow == 1) {
			print "<CENTER><FONT $font SIZE=2><B>End of Results.</B><BR><HR WIDTH=400></FONT></CENTER>\n";
		}
	}
	unless ($noshow == 1) {
		unless ($addshow == 1) {
                        print "<CENTER><P><FORM METHOD=post ACTION=$scripturl><TABLE><TR><TD VALIGN=TOP><FONT $font SIZE=3><B>Search For :</B></FONT></TD><TD><INPUT TYPE=TEXT NAME=keywords SIZE=25 VALUE=\"$FORM{'keywords'}\"><BR><FONT $font SIZE=2><B>Mode :</B><INPUT TYPE=\"radio\" NAME=\"mode\" VALUE=\"words\" CHECKED>Words<INPUT TYPE=\"radio\" NAME=\"mode\" VALUE=\"phrases\">Phrase<BR><B>Safe : </B><INPUT TYPE=\"checkbox\" NAME=\"safe\" CHECKED>Omit Offensive Slang</FONT></TD><TD VALIGN=TOP ALIGN=CENTER WIDTH=60><INPUT TYPE=SUBMIT VALUE=\"Search!\"></TD></TR></TABLE></FORM><p></CENTER>\n";
		}
	}
	&generate;
	print "$footer";
	exit;
}

sub error {    
	$noshow = 1;
      print "Content-type: text/html\n\n";
	&heading;
      print "<CENTER><FONT $font><h2>Server Busy</h2><P><B>Busy due to high traffic...please try again in a minute or two.</B></FONT></CENTER><P>\n";
	&footer;
}

sub stringshort {
	$noshow = 1;
      print "Content-type: text/html\n\n";
	&heading;
      print "<CENTER><FONT $font><h2>Word Too Short</h2><P><B>Sorry...each word must be at least $minword characters long.</B></FONT></CENTER><P>\n";
	&footer;
}

sub phrase {
	$noshow = 1;
      print "Content-type: text/html\n\n";
	&heading;
      print "<CENTER><FONT $font><h2>Phrase Too Short</h2><P><B>Sorry...your phrase must be at least $minword characters long.</B></FONT></CENTER><P>\n";
	&footer;
}

sub generate {
}

sub showadd {
	&heading;
	$addshow = 1;
        print "<CENTER><FONT $font SIZE=3><B>Add Term</B></FONT><HR WIDTH=400>\n";
	print "<FONT $font SIZE=2>Please fill out the following information and press the SUBMIT button.<BR>\n";
	print "<B>Please note that all fields are required</B>.</FONT><P>\n";
	print "<FORM METHOD=post ACTION=$scripturl>\n";
	print "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=5>\n";
	print "<TR><TD ALIGN=RIGHT><B><FONT $font SIZE=2>E-mail :</FONT></B></TD><TD><INPUT NAME=email TYPE=text SIZE=35><BR></TD></TR>\n";
        print "<TR><TD ALIGN=RIGHT><B><FONT $font SIZE=2>Term : </B></FONT></TD><TD><INPUT NAME=author TYPE=text SIZE=35 MAXLENGTH=$maxauthor><BR></TD></TR>\n";
        print "<TR><TD ALIGN=RIGHT><B><FONT $font SIZE=2>Definition : </FONT></B></TD><TD><INPUT NAME=quote TYPE=text SIZE=35 MAXLENGTH=$maxquote><BR></TD></TR>\n";
	print "</TABLE>\n";
	print "<INPUT TYPE=checkbox NAME=send CHECKED><FONT $font SIZE=2> I would like to receive occassional news from $sitetitle.</FONT><P>\n";
	print "<INPUT TYPE=hidden NAME=action VALUE=addurl>\n";
	print "<INPUT TYPE=submit VALUE=Submit> <INPUT TYPE=reset VALUE=Clear></FORM></CENTER>\n";
	&footer;
}

sub addurl {
	&heading;
	$noshow = 1;

	if ($FORM{'author'} eq "" || $FORM{'email'} eq "" || $FORM{'quote'} eq "") {
		&empty;
	}

	unless (open (DATA,"$base")) {die (&error);}
	if ($uselock eq '1') {
		flock DATA, 2;
		seek DATA, 0, 0;
	}
	@input = <DATA>;
	if ($uselock eq '1') {
		flock DATA, 8;
	}
	close (DATA);
	$entries = @input;

	$newauthor = substr($FORM{'author'},0,$maxauthor);
	$newquote = substr($FORM{'quote'},0,$maxquote);
	$line = join ("::","$newauthor","$newquote");

	unless (open (DATA,">>$base")) {die (&error);}
		if ($uselock eq '1') {
			flock DATA, 2;
			seek DATA, 0, 2;
		}
		print DATA "$line\n";
		if ($uselock eq '1') {
			flock DATA, 8;
		}
	close (DATA);

	print "<center><HR WIDTH=400><FONT $font SIZE=3><b>Submission Received</b></font><p>\n";
	print "<FONT $font SIZE=2><B>The following submission has been received by $sitetitle :</B></font><br>\n";
	print "<table width=400><tr>\n";
	print "<tr><td align=right><FONT $font SIZE=2><B>Author :</B></font></td><td><FONT $font SIZE=2> $FORM{'author'}</td></tr>\n";
        print "<tr><td align=right><FONT $font SIZE=2><B>Term :</B></font></td><td><FONT $font SIZE=2> $FORM{'quote'}</td></tr>\n";
        print "<tr><td align=right><FONT $font SIZE=2><B>Definition :</B></font></td><td><FONT $font SIZE=2> $FORM{'email'}</font></td></tr></table></center><p>\n";

	&footer;
}

sub samestuff {
	$noshow = 1;  
      print "<CENTER><FONT $font SIZE=2><h2>Recent URL Submission</h2><HR WIDTH=400><TABLE WIDTH=400><TR><TD><B>You have recently submitted an URL with either the exact same title, description, or keywords. Since this is a different URL, please change your title, description and keywords to match this new page.</B></TD></TR></TABLE><P>\n";
	&footer;
}

sub empty {
	$noshow = 1;
      print "<CENTER><FONT $font SIZE=2><h2>Field Empty</h2><HR WIDTH=400><B>Please make sure that you have filled in all fields on the form.</B><P>\n";
	&footer;
}
