#!/usr/bin/perl

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



# 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 = ('/usr/local/apache/htdocs/xxx/','/usr/local/apache/htdocs/xxx/*');


# 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://xxx.com.tr/cgi-bin/tarama.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 = '/usr/local/apache/cgi-bin/';
$baseurl = 'http://xxx.com.tr/';

# 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=\"#FFFF80\" TEXT=\"#000000\"";
#$header_file = "/usr/www/foo/header.txt";
#$footer_file = "/usr/www/foo/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("Tarama Sonuçlari");

print "<H1 ALIGN=CENTER>Tarama Sonuclari</H1>\n";
print "<P ALIGN=CENTER>Anahtar Kelimeler ($FORM{'boolean'}, ";
print "case $FORM{'case'}): <STRONG>";
if ($NoTerms) {
        print "Aranan Sözcük Bulunamadi!";
}
else {
        foreach $term (@terms) {
                print "$term ";
        }
}
print "</STRONG></P>\n";
print "<P ALIGN=CENTER><SMALL>";
print "(<STRONG>",&commas($filecount),"</STRONG> Dosya Tarandi; ";
print "<STRONG>",&commas($matchcount),"</STRONG> Kayda Rastlandi";
if ($matchcount == 1) {
        print " )";
}
else {
        print " )";
}
print "</SMALL>\n";

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

if ($matchcount == 0) {
        print "<P ALIGN=CENTER>Aradiginiz kritere uyan bir sözcük bulunamadi!";
        print "<BR>Taramayi yeniden yapmak için buraya 
tiklayiniz.</P>\n";
}
else {
        print "<P ALIGN=CENTER><STRONG>Kayit: $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 {
}

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!



