#!/opt/bin/perl

############################################
##                                        ##
##               WebSearch                ##
##           by Darryl Burgdorf           ##
##       (e-mail burgdorf@awsd.com)       ##
##                                        ##
##             version:  1.11             ##
##         last modified: 2/19/98         ##
##           copyright (c) 1998           ##
##                                        ##
##    latest version is available from    ##
##        http://awsd.com/scripts/        ##
##                                        ##
############################################

# COPYRIGHT NOTICE:
#
# Copyright 1998 Darryl C. Burgdorf.  All Rights Reserved.
#
# This program is being distributed as shareware.  It may be used and
# modified by anyone, so long as this copyright notice and the header
# above remain intact, but any usage should be registered.  (See the
# bottom of this file for registration information.)  By using this
# program you agree to indemnify Darryl C. Burgdorf from any liability.
#
# Selling the code for this program without prior written consent is
# expressly forbidden.  Obtain permission before redistributing this
# program over the Internet or in any other medium.  In all cases
# copyright and header must remain intact.

# VERSION HISTORY:
#
# 1.11  02/19/98  Fixed bug in "hits per page" maintenance
#                 Made use of descriptions optional
#                 Eliminated <SCRIPT> contents from searches
# 1.10  02/12/98  FIRST SHAREWARE RELEASE
#                 Allowed for searching of non-alphanumeric characters
#                 Descriptions are now drawn from page text w/o METAs
#                 Dumped "relevance" for keyword matches & filesize
#                 Added optional "A NAME" tag splits to results listing
#                 Added total kilobytes to main file list display
#                 Made "hits per page" user (rather than admin) option
#                 Added $bodyspec, $header_file and $footer_file
#                 Improved appearance of "search results" page
#                 Corrected year refs from 19XX to 1900+XX
#                 Added comma delimiting to large numbers
#                 Numerous other subtle but worthwhile changes!
# 1.08  12/12/97  Made search/display of METAs optional
#                 Eliminated "minimum length" for search terms
#                 Added path/URL replace to title as well as address
#                 Restored stripping of quote marks from search terms
# 1.07  08/06/97  Squashed "display by date" bug
#                 Fixed "Next X" if last set less than X
# 1.06  08/02/97  Added use of META description tags in search output
#                 Replaced UNIX-specific "ls" with calls to "find.pl"
#                 Allowed distinction of "only HTML" and "all text" dirs
#                 Allowed full recursion in directory searches
#                 Added ability to simply list files in search
#                 Added optional display by date rather than relevance
#                 Corrected $avoid format to allow regexs
# 1.05  05/25/97  Fixed bug in "Next/Previous Set" forms
# 1.04  05/21/97  Added META tags to searched material
#                 Revised search directory definition methodology
#                 Added ability to search within multiple URLs
#                 Set script to display only X matches per page
#                 Added "as a phrase" option to boolean choices
#                 Changed "value" count to "relevance" computation
#                 Eliminated incomplete "choose directory" option
# 1.03  04/03/97  Added ALT text to searched material
#                 Added display of total number of files searched
#                 Added $avoid to designate files not to be searched
#                 Fixed bug in the way titles are obtained
#                 Fixed bug introduced by "minor code shuffling"
# 1.02  02/17/97  Fixed bug in Get_Date subroutine
# 1.01  02/07/97  Minor code shuffling
# 1.00  02/03/97  Initial "public" release

####################
# GENERAL COMMENTS #
####################

# WebSearch allows users to search for key words in documents located
# on your Web site.  It searches the actual documents, rather than a
# master index file.  On the "up" side, that means the results it
# returns are always up-to-the minute.  On the "down" side, of course,
# it means that it takes a bit longer than some other scripts to return
# those results.  It's a tradeoff, but if you're working with relatively
# small file sets, the difference probably won't be too pronounced.
#
# The script scores the match URLs based upon the frequency with which
# the requested key terms appear in the documents, and also lists the
# date on which each file was last modified.  It searches the basic text
# of the documents, as well as ALT text and any information contained in
# META "keywords" and "description" tags.  It does *not* search HTML
# tags or comments, so, for example, a search for "HTML" won't key on
# every "A HREF" tag.

#########
# SETUP #
#########

# The script, of course, must be called from a search form on a Web
# page.  The form should look something like the forms below.  The exact
# structure of the form is not too important, of course, so long as the
# correct fields and options exist.  If you leave out the "boolean" and
# "case" fields, the script will default to a case-insensitive boolean
# "OR" ("any terms") search.  If you leave out the "hits" field, the
# script will default to showing 25 matches per output page.

# EXAMPLE OF COMPLETE FORM:
#
# <FORM METHOD=POST ACTION="http://www.foo.com/cgi-bin/websearch.pl">
#
# <P><CENTER>Terms for which to Search:
# <BR><INPUT TYPE=TEXT NAME="terms" SIZE=60>
#
# <P>Find: <SELECT NAME="boolean"> 
# <OPTION>any terms<OPTION>all terms<OPTION>as a phrase</SELECT> 
# Case: <SELECT NAME="case"> 
# <OPTION>insensitive<OPTION>sensitive</SELECT> 
# Display: <SELECT NAME="hits"> 
# <OPTION>10<OPTION>25<OPTION>50<OPTION>100</SELECT>
#
# <P><INPUT TYPE=SUBMIT VALUE="Search">
#
# </CENTER></FORM></P>

# EXAMPLE OF SIMPLE FORM:
#
# <FORM METHOD=POST ACTION="http://www.foo.com/cgi-bin/websearch.pl">
#
# <P><CENTER>Search For: <INPUT TYPE=TEXT NAME="terms" SIZE=40> 
# <INPUT TYPE=SUBMIT VALUE="Search">
#
# </CENTER></FORM></P>

# If you run the script with *none* of the expected form input values
# (in other words, if you run it from the command line or by calling
# it directly through your browser), it will simply return to you a list
# of the URLs of all the files it's currently set to search.  This can
# be handy to make sure (a) that you're searching all the files that you
# intend to and (b) that you're *not* searching any files that you
# *don't* intend to.

# A variety of variables need to be defined.  First, you should
# define @dirs as shown below with a list of the full (absolute) paths
# to the directories you wish the script to search.  (The absolute path
# of any directory can be found by issuing the UNIX "pwd" command while
# "sitting" in that directory.)

# By default, only HTML files in the specific directories you specify
# will be searched.  If you wish to search *all* text files in a given
# directory, instead of just HTML files, append a "/+" to the directory
# name.  If you want to search files in the listed directory *and* in
# all of its subdirectories, append a "/*" to the directory name.  If
# you want to search *all* text files in the directory *and* in all of
# its subdirectories, append a "/*+" to the directory name.

@dirs = ('/export/vol2/httpd/htdocs/student_info/greeks/sigma_phi_epsilon/*+');

# If there are particular files you *don't* want included in the
# search, define them in the $avoid variable below.  You need only
# include enough of the file names to distinguish them from other files.
# For example, if you want to exclude all ".txt" files from the search,
# you can simply include "\.txt" as part of $avoid.  (The backslash is
# used to tell the script to regard the period as a literal character
# rather than a "wildcard.")

$avoid = '(\.backup|\.cgi|\.pl|\.txt)';

# Define the variable $cgiurl as the URL of the WebSearch script itself.

$cgiurl = 'http://www.auburn.edu/cgi-bin/sigepsearch.pl';

# Define the variables $basepath and $baseurl with the absolute path
# and corresponding URL for a "base" directory under which the various
# directories to be searched all lie.  These variables are used to
# convert the UNIX paths to URLs for the results page.

$basepath = '/export/vol2/httpd/htdocs/student_info/greeks/sigma_phi_epsilon/';
$baseurl = 'http://www.auburn.edu/sigep/';

# If you wish to be able to specify several other possible URLs -- if,
# for example, some of the files you wish to search fall under a
# different virtual domain or have to be referenced "through" a shopping
# cart or other CGI program -- uncomment the lines below and define
# %otherurls with the desired path/URL pairs.  Note that the script will
# check this variable for matches to convert paths to URLs *before* it
# checks the $basepath and $baseurl variables.

# Please also note that this variable, like the $basepath and $baseurl
# variables above, is *only* used for purposes of converting paths to
# URLs.  It does *not* determine what files or directories are searched.
# All directories to be searched *must* be included in the @dirs
# variable!

# %otherurls = (
#   '/usr/www/foo/scripts/dir2/sub1/',
#   'http://www.foo.com/cgi-bin/some.cgi?access=',
#   '/usr/www/foo/scripts/dir2/sub2/',
#   'http://www.foo.com/scripts/dir2/sub2/another.cgi?read='
#   );

# WebSearch will, by default, display search results in order by the
# number of keyword matches on the page.  If, instead, you'd like the
# results displayed in order by date, with the most recently-modified
# files listed first, then set the $DisplayByDate variable to any
# value greater than 0!

$DisplayByDate = 0;

# WebSearch will also, by default, search META tag information and
# display the contents of any META "description" tags as a description
# of the page.  If you don't want this done -- if for example, all your
# pages contain the *same* META information -- then set $NoMETAs to 1.

$NoMETAs = 0;

# If for some reason you don't want *any* descriptions shown for
# the pages in the results list, set $UseDescs to 0.

$UseDescs = 1;

# If you have files with lots of internal location tags (<A NAME="x">
# tags), and want each "piece" of the files to show up separately in the
# results listing, you can set $SplitNames to 1.  If you set it to 0,
# such internal tags will be ignored, and all documents will be viewed
# by the script as single entities.

$SplitNames = 0;

# The $bodyspec variable should be defined as illustrated with any
# attributes (BACKGROUND, BGCOLOR, TEXT, etc.) which you want to have
# assigned to the <BODY> tag on pages created by the script.  The
# $header_file and $footer_file variables should be defined with the
# full paths to text files (optional) containing HTML code to be placed
# at the top and/or bottom of the pages.  This allows you to include
# certain "standard" information on all of them.

$bodyspec = "BGCOLOR=\"#ffffff\" TEXT=\"#000000\"";
$header_file = "/export/vol2/httpd/htdocs/sigep/search/header.txt";
$footer_file = "/export/vol2/httpd/htdocs/sigep/search/footer.txt";

#####################
# THE ACTUAL SCRIPT #
#####################

# CHANGE NOTHING BELOW THIS LINE!

$version = "1.11";

require "find.pl";

@day = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);
@month = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
	($name, $value) = split(/=/, $pair);
	$name =~ tr/+/ /;
	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	if ($FORM{$name}) {
		$FORM{$name} = "$FORM{$name}, $value";
	}
	else {
		$FORM{$name} = $value;
	}
}

if (!$FORM{'boolean'} && !$FORM{'case'}
  && !$FORM{'hits'} && !$FORM{'terms'}) {
	$ListOnly = 1;
}

unless ($FORM{'boolean'}) { $FORM{'boolean'} = "any terms"; }
unless ($FORM{'case'}) { $FORM{'case'} = "insensitive"; }
unless ($FORM{'hits'}) { $FORM{'hits'} = 25; }
unless ($FORM{'terms'}) { $NoTerms = 1; }

$FORM{'terms'} =~ s/\s+/ /g;
$FORM{'terms'} =~ s/^\s//;
$FORM{'terms'} =~ s/\s$//;
$FORM{'terms'} =~ s/([^\w\s])/\\$1/g;
if ($FORM{'boolean'} eq "as a phrase") {
	push (@terms,$FORM{'terms'});
}
else {
	@terms = split(/\s+/,$FORM{'terms'});
}

$matchcount=0;
$filecount=0;

foreach $file (@dirs) {
	undef (@AllFiles,$AllText);
	if ($file =~ s/\+$//) {
		$AllText = 1;
	}
	if ($file =~ s/\*$//) {
		$AllDirs = 1;
		&find ($file);
	}
	else {
		opendir(DIR,$file);
		@AllFiles = readdir(DIR);
		closedir(DIR);
	}
	$file =~ s/\/$//;
	foreach $subfile (@AllFiles) {
		unless ($subfile =~ /^$file/) {
			$subfile = $file."/".$subfile;
		}
		if ((-T "$subfile")
		  && ($AllText || ($subfile =~ /\.(s|p)*htm(l)*$/))
		  && (!$avoid || ($subfile !~ /$avoid/))) {
			$kbytesize{$subfile} = int((((stat($subfile))[7])/1024)+.5);
			$kbytestotal += $kbytesize{$subfile};
			push (@files,"$subfile");
		}
	}
}

if ($ListOnly) {
	@FILES = sort (@files);
	print "Content-type: text/html\n\n";
	&Header("File List");
	print "<PRE>\n\n";
	print "The following files are included in the search:\n\n";
	foreach $FILE (@FILES) {
		if (%otherurls) {
			foreach $path (keys %otherurls) {
				$FILE =~ s/$path/$otherurls{$path}/i;
			}
		}
		$FILE =~ s/$basepath/$baseurl/i;
		print "$FILE\n";
		$filecount ++;
		next;
	}
	print "\nTotal:  ",&commas($filecount)," files\n";
	print "        ",&commas($kbytestotal)," kb\n\n";
	print "</PRE>\n";
	&PrintForm;
	&Footer;
	exit;
}

unless ($NoTerms) {
	foreach $FILE (@files) {
		open (FILE,"$FILE");
		@LINES = <FILE>;
		close (FILE);
		$filecount ++;
		$mtime = (stat($FILE))[9];
		$mtime{$FILE} = $mtime;
		$update{$FILE} = &Get_Date;
		$string = join(' ',@LINES);
		$string =~ s/\n/ /g;
		$string =~ s/<SCRIPT>.*<\/SCRIPT>/ /gi;
		if ($string =~ /<TITLE>([^>]+)<\/TITLE>/i) {
			$title{$FILE} = "$1";
		}
		elsif ($string =~ /SUBJECT>(.+)POSTER>(.+)EMAIL>/i) {
			$title{$FILE} = "$1";
			$poster{$FILE} = "$2";
		}
		else {
			$title{$FILE} = "$FILE";
		}
		if ($SplitNames) {
			@names = split (/<\s*A\s*NAME\s*=\s*"*/i,$string);
		}
		else {
			@names[0] = $string;
		}
		$namescount = @names;
		foreach $key (0..$namescount-1) {
			unless ($key==0) {
				$filename = $names[$key];
				$filename =~ s/^([^">]*).*/#$1/;
				$names[$key] =~ s/^[^">]*"*\s*>(.*)/$1/;
				$filename = "$FILE"."$filename";
			}
			else {
				$filename = $FILE;
			}
			$kbytesize{$filename} = $kbytesize{$FILE};
			$update{$filename} = $update{$FILE};
			$title{$filename} = $title{$FILE};
			$poster{$filename} = $poster{$FILE};
			$val{$filename} = 0;
			$string = $names[$key];
			unless (!($UseDescs) || ($NoMETAs > 0)) {
				if ($string =~ /<[^>]*META[^>]+NAME\s*=[ "]*description[ "]+CONTENT\s*=\s*"(([^>"])*)"[^>]*>/i) {
					$description{$filename} = "$1";
				}
			}
			$title{$filename} =~ s/\s+/ /g;
			$title{$filename} =~ s/^\s*//;
			$title{$filename} =~ s/\s*$//;
			if ($poster{$filename}) {
				$poster{$filename} =~ s/\s+/ /g;
				$poster{$filename} =~ s/^\s*//;
				$poster{$filename} =~ s/\s*$//;
			}
			$string =~ s/<[^>]*\s+ALT\s*=\s*"(([^>"])*)"[^>]*>/$1/ig;
			unless ($NoMETAs > 0) {
				$string =~ s/<[^>]*META[^>]+NAME\s*=[ "]*(description|keywords)[ "]+CONTENT\s*=\s*"(([^>"])*)"[^>]*>/$2/ig;
			}
			unless (!($UseDescs) || ($description{$filename})) {
				$description = $string;
				if ($description =~ /<BODY/) {
					$description =~ s/.*<BODY[^>]*>(.*)/$1/i;
				}
				elsif ($description =~ /LINKURL>/) {
					$description =~ s/.*LINKURL>[^ ]*(.*)/$1/i;
				}
				$description =~ s/<([^>])*>//g;
				$description =~ s/\s+/ /g;
				$description =~ s/^\s*//;
				$description =~ s/\s*$//;
				$description{$filename} = substr($description,0,250);
			}
			$string =~ s/<([^>])*>//g;
			$string =~ s/\s+/ /g;
			$string =~ s/^\s*//;
			$string =~ s/\s*$//;
			if ($FORM{'boolean'} eq 'all terms') {
				foreach $term (@terms) {
					if ($FORM{'case'} eq 'insensitive') {
						$test = ($string =~ s/$term//ig);
						if ($test < 1) {
							$val{$filename} = 0;
							last;
						}
						else {
							$val{$filename} = $val{$filename}+$test;
						}
					}
					elsif ($FORM{'case'} eq 'sensitive') {
						$test = ($string =~ s/$term//g);
						if ($test < 1) {
							$val{$filename} = 0;
							last;
						}
						else {
							$val{$filename} = $val{$filename}+$test;
						}
					}
				}
			}
			else {
				foreach $term (@terms) {
					if ($FORM{'case'} eq 'insensitive') {
						$test = ($string =~ s/$term//ig);
					}
					elsif ($FORM{'case'} eq 'sensitive') {
						$test = ($string =~ s/$term//g);
					}
					$val{$filename} = $val{$filename}+$test;
				}
			}
			if ($val{$filename} > 0) {
				$truval{$filename} = ($val{$filename});
				$matchcount++;
			}
		}
	}
}

foreach $term (@terms) {
	$term =~ s/\\\\/BaCkSlAsH/g;
	$term =~ s/\\//g;
	$term =~ s/BaCkSlAsH/\\/g;
	$term =~ s/&/&amp;/g;
	$term =~ s/"/&quot;/g;
	$term =~ s/>/&gt;/g;
	$term =~ s/</&lt;/g;
}

print "Content-type: text/html\n\n";
&Header("Search Results");

print "<H1 ALIGN=CENTER>Search Results</H1>\n";
print "<P ALIGN=CENTER>Keywords ($FORM{'boolean'}, ";
print "case $FORM{'case'}): <STRONG>";
if ($NoTerms) {
	print "No Search Terms Provided!";
}
else {
	foreach $term (@terms) {
		print "$term ";
	}
}
print "</STRONG></P>\n";
print "<P ALIGN=CENTER><SMALL>";
print "(<STRONG>",&commas($filecount),"</STRONG> files searched; ";
print "<STRONG>",&commas($matchcount),"</STRONG> match";
if ($matchcount == 1) {
	print " found)";
}
else {
	print "es found)";
}
print "</SMALL>\n";

unless ($FORM{'first'}) { $FORM{'first'} = 1; }
unless ($FORM{'last'}) { $FORM{'last'} = $FORM{'hits'}; }

if ($matchcount == 0) {
	print "<P ALIGN=CENTER>No documents match your search criteria!";
	print "<BR>You might want to revise them and try again.</P>\n";
}
else {
	print "<P ALIGN=CENTER><STRONG>Matches $FORM{'first'} ";
	if ($matchcount < $FORM{'last'}) {
		print "- $matchcount</STRONG></P>\n";
	}
	else {
		print "- $FORM{'last'}</STRONG></P>\n";
	}
	$Count = 0;
	print "<P><UL>\n";
	if ($DisplayByDate > 0) {
		foreach $key (sort ByDate keys %truval) {
			&PrintEntry;
		}
	}
	else {
		foreach $key (sort ByValue keys %truval) {
			&PrintEntry;
		}
	}
	print "</UL></P>\n";
	if (($FORM{'first'} > 1) || ($FORM{'last'} < $matchcount)) {
		print "<P><CENTER><TABLE><TR>\n";
	}
	if ($FORM{'first'} > 1) {
		print "<TD><FORM METHOD=POST ACTION=\"$cgiurl\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"terms\" VALUE=\"";
		foreach $term (@terms) {
			print "$term ";
		}
		print "\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"boolean\" ";
		print "VALUE=\"$FORM{'boolean'}\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"case\" ";
		print "VALUE=\"$FORM{'case'}\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"hits\" ";
		print "VALUE=\"$FORM{'hits'}\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"first\" ";
		print "VALUE=\"",($FORM{'first'}-$FORM{'hits'}),"\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"last\" ";
		print "VALUE=\"",($FORM{'last'}-$FORM{'hits'}),"\">\n";
		print "<INPUT TYPE=SUBMIT ";
		print "VALUE=\"Last $FORM{'hits'} Matches\">\n";
		print "</FORM></TD>\n";
	}
	if ($FORM{'last'} < $matchcount) {
		print "<TD><FORM METHOD=POST ACTION=\"$cgiurl\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"terms\" VALUE=\"";
		foreach $term (@terms) {
			print "$term ";
		}
		print "\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"boolean\" ";
		print "VALUE=\"$FORM{'boolean'}\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"case\" ";
		print "VALUE=\"$FORM{'case'}\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"hits\" ";
		print "VALUE=\"$FORM{'hits'}\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"first\" ";
		print "VALUE=\"",($FORM{'first'}+$FORM{'hits'}),"\">\n";
		print "<INPUT TYPE=HIDDEN NAME=\"last\" ";
		print "VALUE=\"",($FORM{'last'}+$FORM{'hits'}),"\">\n";
		print "<INPUT TYPE=SUBMIT VALUE=\"";
		$nextset = $matchcount-$FORM{'last'};
		if ($nextset == 1) {
			print "Next Match";
		}
		elsif ($nextset < $FORM{'hits'}) {
			print "Next $nextset Matches";
		}
		else {
			print "Next $FORM{'hits'} Matches";
		}
		print "\">\n</FORM></TD>\n";
	}
	if (($FORM{'first'} > 1) || ($FORM{'last'} < $matchcount)) {
		print "</TR></TABLE></CENTER></P>\n";
	}
}

&PrintForm;
&Footer;
exit;

sub commas {
	local($_)=@_;
	1 while s/(.*\d)(\d\d\d)/$1,$2/;
	$_;
}

sub Get_Date {
	($mday,$mon,$yr) = (localtime($mtime))[3,4,5];
	$yr += 1900;
	$date = "$mday $month[$mon] $yr";
	return $date;
}

sub PrintEntry {
	$Count++;
	next if ($Count < $FORM{'first'});
	last if ($Count > $FORM{'last'});
	$fileurl = $key;
	if (%otherurls) {
		foreach $path (keys %otherurls) {
			$fileurl =~ s/$path/$otherurls{$path}/i;
			$title{$key} =~ s/$path/$otherurls{$path}/i;
		}
	}
	$fileurl =~ s/$basepath/$baseurl/i;
	$title{$key} =~ s/$basepath/$baseurl/i;
	print "<LI><STRONG><A HREF=\"$fileurl\">";
	print "$title{$key}</A></STRONG>";
	if ($poster{$key}) {
		print " (posted by $poster{$key})";
	}
	print "<BR><SMALL>Keyword Matches: ";
	print "<STRONG>$truval{$key}</STRONG>; ";
	print "Size: <STRONG>$kbytesize{$key} kb</STRONG>; ";
	print "Last Updated: ";
	print "<STRONG>$update{$key}</STRONG></SMALL>\n";
	if ($description{$key}) {
		print "<BR><EM>$description{$key}</EM>\n";
	}
	print "<P>\n";
}

sub ByDate {
	$aval = $mtime{$a};
	$bval = $mtime{$b};
	$bval <=> $aval;
}

sub ByValue {
	$aval = $truval{$a};
	$bval = $truval{$b};
	$bval <=> $aval;
}

sub wanted {
	(push (@AllFiles, $name)) && -f $_;
}

sub PrintForm {
	print "<P><CENTER><TABLE BORDER CELLPADDING=12><TR><TD>\n";
	print "<H2 ALIGN=CENTER>New Search</H2>\n";
	print "<FORM METHOD=POST ACTION=\"$cgiurl\">\n";
	print "<P><CENTER>Terms for which to Search:\n";
	print "<BR><INPUT TYPE=TEXT NAME=\"terms\" SIZE=60 VALUE=\"";
	foreach $term (@terms) {
		print "$term ";
	}
	print "\">\n<P>Find: <SELECT NAME=\"boolean\"> ";
	if ($FORM{'boolean'} eq 'any terms') {
		print "<OPTION SELECTED>any terms<OPTION>all terms";
		print "<OPTION>as a phrase</SELECT> ";
	}
	elsif ($FORM{'boolean'} eq 'all terms') {
		print "<OPTION>any terms<OPTION SELECTED>all terms";
		print "<OPTION>as a phrase</SELECT> ";
	}
	else {
		print "<OPTION>any terms<OPTION>all terms";
		print "<OPTION SELECTED>as a phrase</SELECT> ";
	}
	print "Case: <SELECT NAME=\"case\"> ";
	if ($FORM{'case'} eq 'insensitive') {
		print "<OPTION SELECTED>insensitive";
		print "<OPTION>sensitive</SELECT> ";
	}
	else {
		print "<OPTION>insensitive";
		print "<OPTION SELECTED>sensitive</SELECT> ";
	}
	print "Display: <SELECT NAME=\"hits\"> ";
	print "<OPTION";
	if ($FORM{'hits'} == 10) {
		print " SELECTED";
	}
	print ">10<OPTION";
	if ($FORM{'hits'} == 25) {
		print " SELECTED";
	}
	print ">25<OPTION";
	if ($FORM{'hits'} == 50) {
		print " SELECTED";
	}
	print ">50<OPTION";
	if ($FORM{'hits'} == 100) {
		print " SELECTED";
	}
	print ">100</SELECT>\n";
	print "<P><INPUT TYPE=SUBMIT VALUE=\"Search\">";
	print "</CENTER></FORM></P>\n";
	print "<P ALIGN=CENTER><SMALL>The search terms you input ";
	print "do not have to be complete words.";
	print "<BR>&quot;Wash,&quot; for example, will match ";
	print "occurrences of wash, washer, Washington, etc.";
	print "<BR>Do not include asterisks or other non-alphanumeric ";
	print "characters in your search terms";
	print "<BR>unless you actually want them included (as with ";
	print "&quot;C++&quot;) as part of your search.\n";
	print "<P ALIGN=CENTER>";
	print "Maintained with <STRONG>";
	print "<A HREF=\"http://awsd.com/scripts/websearch/\">";
	print "WebSearch $version</A></STRONG>.</SMALL></P>\n";
	print "</TD></TR></TABLE></CENTER></P>\n";
}

sub Header {
	($title) = @_;
	print "<HTML><HEAD><TITLE>$title</TITLE></HEAD>\n";
	print "<BODY $bodyspec>\n";
	if ($header_file) {
		open (HEADER,"$header_file");
		@header = <HEADER>;
		close (HEADER);
		foreach $line (@header) {
			if ($line =~ /<!--InsertAdvert\s*(.*)-->/i) {
				&insertadvert($1);
			}
			else {
				print "$line";
			}
		}
	}
}

sub Footer {
	if ($footer_file) {
		open (FOOTER,"$footer_file");
		@footer = <FOOTER>;
		close (FOOTER);
		foreach $line (@footer) {
			if ($line =~ /<!--InsertAdvert\s*(.*)-->/i) {
				&insertadvert($1);
			}
			else {
				print "$line";
			}
		}
	}
	print "</BODY></HTML>\n";
}

sub insertadvert {
	require "/u2/www/users/dburgdor/scripts/ads/ads_display.pl";
	$adverts_dir = "/u2/www/users/dburgdor/scripts/ads/ads";
	$display_cgi = "http://awsd.com/scripts/ads/ads.pl";
	$advertzone = $_[0];
	$ADVUseBackup = 1;
	$ADVUseLocking = 1;
	$NonSSI = 0;
	$ADVNoPrint = 1;
	&ADVsetup;
}

# REGISTERING WEBSEARCH:
#
# WebSearch is distributed as shareware.  While you are free to modify
# and use it as you see fit, any usage should be registered.  The
# registration fee is just $25 (US).  Payment should be sent via check
# or money order to Darryl C. Burgdorf, Affordable Web Space Design,
# 3524 Pacific Street, Omaha NE 68105.
#
# (If you happen to live in a country other than the United States, you
# can write a check in your local currency for the equivalent of $32.50.
# That will cover the $25 registration fee and the $7.50 service fee
# which my bank charges.  Please do not write me a check in US funds
# drawn on a non-US bank; the service charge for those can be anywhere
# from $10 to $25!)
#
# Thank you for your support!

