#!/usr/bin/perl

#websearch.pl/cgi
#for MKTMKT.com
#james@webdepot.net
#August 1997


# Define Variables
$basedir = '/home/users/web/b2624/pow.delwest/htdocs/';
$baseurl = 'http://mail.webdepot.net/~james/mktmkt/';
@files = ('*.html','*.htm','webcart2/*.htm','webcart2/html');

$title = "WebSearch";
$template = '/home/users/web/b2624/pow.delwest/htdocs/search/results_template.html';
$res=0;
$start = time;

$FORM{'boolean'} = "OR";

# Parse Form Search Information
&parse_form;

$tt = $FORM{'terms'};



$FORM{'case'} = "Insensitive";

# Get Files To Search Through
&get_files;

# Search the files

if ($FORM{'terms'} ne '')
{
&search;
}


$end = time;

$elap = $end - $start;

# Print Results of Search
&return_html;
exit 0;

##########PARSE FORM#
sub parse_form {

$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
else
{
$buffer = $ENV{'QUERY_STRING'};
}
@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;
   $name =~ tr/+/ /;
   $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
}

}

sub get_files {

   chdir($basedir);
   foreach $file (@files) {
      $ls = `ls $file`;
      @ls = split(/\s+/,$ls);
      foreach $temp_file (@ls) {
         if (-d $file) {
            $filename = "$file$temp_file";
            if (-T $filename) {
               push(@FILES,$filename);
            }
         }
         elsif (-T $temp_file) {
            push(@FILES,$temp_file);
         }
      }
   }
}

sub search {

   @terms = split(/\s+/, $FORM{'terms'});

   foreach $FILE (@FILES) {

      open(FILE,"$FILE");
      @LINES = <FILE>;
      close(FILE);

      $string = join(' ',@LINES);
      $string =~ s/\n//g;

##remove tags
#$string =~ s/<p (.*)>//gi;
#$string =~ s/<([^>]|\n)*>//gi;
##end remove tags

      if ($FORM{'boolean'} eq 'AND') {
         foreach $term (@terms) {
            if ($FORM{'case'} eq 'Insensitive') {
               if (!($string =~ /(\W)$term(\W)/i)) {
                  $include{$FILE} = 'no';
                  last;
               }
               else {
                  $include{$FILE} = 'yes';
               }
            }
            elsif ($FORM{'case'} eq 'Sensitive') {
               if (!($string =~ /(\W)$term(\W)/)) {
                  $include{$FILE} = 'no';
                  last;
               }
               else {
                  $include{$FILE} = 'yes';
               }
            }
         }
      }
      elsif ($FORM{'boolean'} eq 'OR') {
         foreach $term (@terms) {
            if ($FORM{'case'} eq 'Insensitive') {
               if ($string =~ /(\W)$term(\W)/i) {
                  $include{$FILE} = 'yes';
                  last;
               }
               else {
                  $include{$FILE} = 'no';
               }
            }
            elsif ($FORM{'case'} eq 'Sensitive') {
               if ($string =~ /(\W)$term(\W)/) {
                  $include{$FILE} = 'yes';
                  last;
               }
               else {
                  $include{$FILE} = 'no';
               }
            }
         }
      }
      if ($string =~ /<title>(.*)<\/title>/i) {
         $titles{$FILE} = "$1";
      }
      else {
         $titles{$FILE} = "$FILE";
      }
   }
}

sub return_html {
   foreach $key (keys %include) {
      if ($include{$key} eq 'yes') {
       $res++;

$l= length($titles{$key});

if ($l > 50)
{
$titles{$key} = substr($titles{$key},0,50);
$titles{$key} .= "...";
}

$tz = $FORM{'terms'};
$tz =~ s/ /+/g;


$titles{$key} =~ s/s+ //g;
if ($titles{$key} eq '')
{
$results .=  "<font size=-2>$res.</font><a href=\"page.cgi?page=$key&terms=$tz\">Untitled Document</a><br>\n";
} else
{
$results .=  "<font size=-2>$res.</font><a href=\"page.cgi?page=$key&terms=$tz\">$titles{$key}</a><br>\n";
}
      }
   }

if ($FORM{'terms'} ne ''){
$terms = "$FORM{'terms'}";
}


if ($FORM{'boolean'} eq 'OR') {
$terms =~ s/ / <b>or<\/b> /ig;
} else {

$terms =~ s/ / <b>and<\/b> /ig;
}


if ($FORM{'terms'} eq '')
{
$terms = "no search words entered";
}

open(HTML, "<$template") || exit(0);
@html = <HTML>;
close(HTML);
foreach $line (@html){
$line =~ s/<RESULTS>/$results/i;
$line =~ s/<KEYWORDS>/$terms/i;
$line =~ s/<TIME>/$elap/i;
$line =~ s/<NUMR>/$res/i;
}

print "Content-type: text/html\n\n";
print "@html";
}
