#!/usr/bin/perl
# Change the title head.txt and foot.txt files too
        $sitetitle = 'Integrated Publishing';
        $searchurl = 'http://www.tpub.com/';
        $searchemail = 'search\@tpub.com';
        $mailprogram = '/usr/sbin/sendmail';
        $base = '/home/sites/www.tpub.com/web/search/base.txt';
        $headfile = '/home/sites/www.tpub.com/web/search/head.txt';
        $footfile = '/home/sites/www.tpub.com/web/search/foot.txt';
        $respondfile = '/home/sites/www.tpub.com/web/search/respond.txt';
        $smutfile = '/home/sites/www.tpub.com/web/search/smut.txt';
        $scripturl = 'http://www.tpub.com/cgi-bin/easysearch.cgi';
	$font = 'FACE=arial,helvetica COLOR=000000';
        $minword = '3';
	$maxtitle = '50';
	$maxdescription = '150';
	$maxkeywords = '50';
        $numnew = '999';
	$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;
	}

	if ($FORM{'action'} eq "newurls"){
		&newurls;
	}

	if ($FORM{'action'} eq "randomurl"){
		&randomurl;
	}


	$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[4] ne "") {
						if ($safekey eq "on" && $match == 0) {
							foreach $smutword (@smutwords) {
								if ($input[$currentline] =~ /$smutword/i) {
									$smut = 1;
								}
							}	
							unless ($smut == 1) {
							print "<A HREF=\"$data[0]\"><B>$data[1]</B></A><BR>";
                                		print "$data[4]<BR>";
							print "<I>$data[0]</I><P>";
							++$found;
							++$match;
						}
						$smut = 0;
					}
						if ($safekey eq "off" && $match == 0) {
							print "<A HREF=\"$data[0]\"><B>$data[1]</B></A><BR>";
                                		print "$data[4]<BR>";
							print "<I>$data[0]</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[4] ne "") {
					if ($safekey eq "on") {
							foreach $smutword (@smutwords) {
								if ($input[$currentline] =~ /$smutword/i) {
									$smut = 1;
								}
							}					
							unless ($smut == 1) {
							print "<A HREF=\"$data[0]\"><B>$data[1]</B></A><BR>";
                                		print "$data[4]<BR>";
							print "<I>$data[0]</I><P>";
							++$found;
						}
						$smut = 0;
					}
					if ($safekey eq "off") {
						print "<A HREF=\"$data[0]\"><B>$data[1]</B></A><BR>";
                                	print "$data[4]<BR>";
						print "<I>$data[0]</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 URL</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>Site Title : </FONT></B></TD><TD><INPUT NAME=title TYPE=text SIZE=35 MAXLENGTH=$maxtitle><BR></TD></TR>\n";
	print "<TR><TD ALIGN=RIGHT><B><FONT $font SIZE=2>Url : </FONT></B></TD><TD><INPUT NAME=url TYPE=text SIZE=35 value=http://><BR></TD></TR>\n";
	print "<TR><TD ALIGN=RIGHT><B><FONT $font SIZE=2>Description : </FONT></B></TD><TD><INPUT NAME=description TYPE=text SIZE=35 MAXLENGTH=$maxdescription><BR></TD></TR>\n";
	print "<TR><TD VALIGN=TOP ALIGN=RIGHT><FONT $font SIZE=2><B>Keywords : </B><BR><I>(Commas, no spaces)</I></FONT></TD><TD VALIGN=TOP><INPUT NAME=keywords TYPE=text SIZE=35 MAXLENGTH=$maxkeywords><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;
	unless ($FORM{'url'} =~ /http:\/\//) {
		&submiterror;
	}

	if ($FORM{'url'} eq "" || $FORM{'title'} eq "" || $FORM{'email'} eq "" || $FORM{'description'} eq "" || $FORM{'keywords'} 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;

	$urlsearch = "$FORM{'url'}"."::";
	$urltemp = $FORM{'url'};
	chomp($urltemp);
	chop($urltemp);
	$urlsearchtwo = "$urltemp"."::";
	$urlsearchthree = "$FORM{'url'}"."/::";
	$currentline = 0;
	until ($currentline == $entries) {
		if ($input[$currentline] =~ /$urlsearch/i) {
			&exists;
		}
		if ($input[$currentline] =~ /$urlsearchtwo/i) {
			&exists;
		}
		if ($input[$currentline] =~ /$urlsearchthree/i) {
			&exists;
		}
		++$currentline;
	}  

	$newemail = $FORM{'email'};
	if ($FORM{'send'} ne "on") {
		$newemail = "X"."$newemail"."X";	
	}
	$newtitle = substr($FORM{'title'},0,$maxtitle);
	$newdesc = substr($FORM{'description'},0,$maxdescription);
	$newkeywords = substr($FORM{'keywords'},0,$maxkeywords);
	$line = join ("::","$FORM{'url'}","$newtitle","$newkeywords","$newemail","$newdesc");

	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><td align=right><FONT $font SIZE=2><B>URL :</B></font></td>\n";
	print "<td><a href=\"$FORM{'url'}\"><FONT $font SIZE=2><b>$FORM{'url'}</b></font></a></td></tr>\n";
	print "<tr><td align=right><FONT $font SIZE=2><B>Title :</B></font></td><td><FONT $font SIZE=2> $FORM{'title'}</td></tr>\n";
	print "<tr><td align=right><FONT $font SIZE=2><B>Keywords :</B></font></td><td><FONT $font SIZE=2> $FORM{'keywords'}</td></tr>\n";
	print "<tr><td align=right><FONT $font SIZE=2><B>Description :</B></font></td><td><FONT $font SIZE=2> $FORM{'description'}</td></tr>\n";
	print "<tr><td align=right><FONT $font SIZE=2><B>E-mail :</B></font></td><td><FONT $font SIZE=2> $FORM{'email'}</font></td></tr></table></center><p>\n";

	if ($userespond eq '1') {
		unless (open (DATA,"$respondfile")) {die (&error);}
			if ($uselock eq '1') {
				flock DATA, 2;
				seek DATA, 0, 0;
			}
			@respondinfo = <DATA>;
			if ($uselock eq '1') {
				flock DATA, 8;
			}
		close (DATA);
		foreach $respondline (@respondinfo){
			$respondmessage = $respondmessage.$respondline;
		}

		open (MAIL, "|$mailprogram -t");
		print MAIL "To: $FORM{'email'}\n";
		print MAIL "From: $searchemail\n";
		print MAIL "Subject: Integrated Publishing\n\n";
		print MAIL "Welcome to $sitetitle!\n";
		print MAIL "We are located at $searchurl\n\n";
		print MAIL "YOUR SUBMISSION:\n";
		print MAIL "------------------------------------------------------------------\n";
		print MAIL "URL : $FORM{'url'}\n"; 
		print MAIL "Title : $FORM{'title'}\n";
		print MAIL "Description : $FORM{'description'}\n";
		print MAIL "Keywords : $FORM{'keywords'}\n";
		print MAIL "E-mail : $FORM{'email'}\n";
		print MAIL "------------------------------------------------------------------\n\n";
		print MAIL "$respondmessage";
		print MAIL "------------------------------------------------------------------\n\n";
		print MAIL "Thanks again,\n";
		print MAIL "---------------------------------------------\n";
		print MAIL "$sitetitle\n";
		print MAIL "$searchemail\n";
		print MAIL "$searchurl\n";
		print MAIL "---------------------------------------------\n";

		close (MAIL);
	}
	&footer;
}

sub exists {
	$noshow = 1;
      print "<CENTER><FONT $font SIZE=2><h2>URL Already Exists</h2><BR>$FORM{'url'}<HR WIDTH=400><B>Sorry...Each URL is only allowed one entry.</B><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;
}

sub submiterror {
	$noshow = 1;
      print "<CENTER><FONT $font SIZE=2><h2>Invalid URL</h2><HR WIDTH=400><B>Please make sure that your URL contains <B>http://</B> and is correct.</B><P>\n";
	&footer;
}

sub newurls {
	&heading;
	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;
	print "<FONT $font SIZE=3><CENTER><B>Newest URLs : </B><HR WIDTH=400></CENTER></FONT>";
	$currentline = $entries;
	print "<FONT $font SIZE=2>";
		@data = split (/::/,$input[$currentline]);
        	if ($data[4] ne "") {
			foreach $smutword (@smutwords) {
				if ($input[$currentline] =~ /$smutword/i) {
					$smut = 1;
				}
			unless ($smut == 1) {
				print ("<a href=\"$data[0]\"><B>$data[1]</B></a><br>");
                		print ("$data[4]<br>");
		    		print ("<I>$data[0]</I><P>");
			}
			$smut = 0;
		}
		--$currentline;
	}
	print "</FONT>";
	&footer;
}

sub randomurl {
	&heading;
	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;
	print "<FONT $font SIZE=3><CENTER><B>Random URL : </B><HR WIDTH=400></CENTER></FONT>";
	$count=0;
	while ($count != 1)  {
		srand (time + $$);
		$currentline = int( rand ($entries));
		print "<FONT $font SIZE=2>";
		@data = split (/::/,$input[$currentline]);
		foreach $smutword (@smutwords) {
			if ($input[$currentline] =~ /$smutword/i) {
				$smut = 1;
			}
		}
		unless ($smut == 1) {
			if ($data[4] ne "") {
                			print "<A HREF=\"$data[0]\"><B>$data[1]</B></A><BR>";
                			print "$data[4]<BR>";
		    		print "<I>$data[0]</I></FONT><P>";
			}
		}
		$smut = 0;
	}
	&footer;
}
