#!/usr/bin/perl -T
use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

$|=1;

# Init Variable
my $BASE = $ENV{'DOCUMENT_ROOT'};   # alle Suche nur auf den Web-Dateien

# Parameter aus dem Formular holen
my $query = new CGI;
my $searchvalue = $query->param('searchvalue');
my $type = $query->param('type');
my @search = ();
my $numberreturned = 0;

# Suche einschraenken auf ...
my $dirs = $query->param('dirs');
my @directories = ();
my $listdirs = 0;

# Ausschliessen von ...
my $exdirs = $query->param('exdirs');
my @excldirs = ();
my $exdirs = 0;

my $dir = '';

# Parameter in Suchbegriffe zerlegen
@search = split(/ /, $searchvalue);
$numberreturned = 0;

$listdirs = 0;
if (defined($dirs)) 
  { 
  @directories = split(/;/,$dirs);
  $listdirs = 1;
  }

$exdirs = 0;
if (defined($exdirs)) 
  {
  @excldirs = split(/;/,$exdirs);
  $exdirs = 1;
  }

# Print HTML-Header
print "Content-type: text/html", "\r\n\r\n";
print "<HTHL>", "\n";
print "<HEAD><TITLE>Webserver-Suche</TITLE></HEAD>", "\n";
print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#0000FF\" VLINK=\"#0000CC\" ALINK=\"#FF0000\">", "\n";
print "<FONT face=\"Arial,Helvetica,Geneva,Swiss,SunSans-Regular\">\n";
print "<H1 ALIGN=CENTER>Suchergebnis</H1>\n";

&Formular;

print "<UL>\n";
if ($listdirs) { 
  foreach $dir (@directories) 
    { &CheckFiles($BASE."$dir"); }
  } 
else 
  { &CheckFiles($BASE); }
print "</UL>\n";

if ($numberreturned == 0) 
  { print "<P><B>Leider nichts gefunden!</B>\n"; } 
else {
  print "<P><B>$numberreturned Fundstelle";
  if ($numberreturned != 1) { print "n"; };
  print "</B>\n";
  }
print "</FONT></BODY></HTML>\n";
exit(0);


sub Formular 
  {
  print "<FORM METHOD=\"post\" ACTION=\"/cgi-bin/such.pl\">\n";
  print "<INPUT TYPE=\"hidden\" NAME=\"dirs\" VALUE=\"$dirs\">\n" if (defined($dirs));
  print "<INPUT TYPE=\"hidden\" NAME=\"exdirs\" VALUE=\"$exdirs\">\n" if (defined($exdirs));
  print "Suchbegriff(e): ";
  print "<INPUT TYPE=\"text\" NAME=\"searchvalue\" VALUE=\"$searchvalue\">\n";
  print "Verkn\&uuml;pfung: ";
  print "<SELECT NAME=\"type\">\n";
  print "<OPTION VALUE=\"all\"";
  print " SELECTED" if ($type eq "all");
  print ">alle \n";
  print "<OPTION VALUE=\"any\"";
  print " SELECTED" if ($type eq "any");
  print ">mindestens einer \n";
  print "</SELECT> der Begriffe.\n";
  print "<INPUT TYPE=\"submit\" VALUE=\"Suchen\">\n";
  print "<INPUT TYPE=\"reset\" VALUE=\"Löschen\"></FORM>\n";
  print "<P>\n";
  }

sub CheckFiles 
  {
  # read files in directory, passed as first paramater
  my $path = '';
  my $fullFilename = '';
  my @files = ();
  my $line = '';
  my $title = '';
  my $header = '';
  my $search = '';
  my $tmp = '';
  my $tmpath = '';
  my $recurse = 0;
  my $found = 0;

  $path = shift;
  return unless (-d $path); 
  opendir(ROOT,$path) or return;
  @files = readdir(ROOT);
  closedir(ROOT);

  foreach (@files) {
    next if /^\.|\.\.$/;
    $fullFilename = "$path/$_";
    if (-d $fullFilename) {
      $recurse = 1;
      if ($exdirs) {
        $tmp = $fullFilename;
        $tmp =~ s/$BASE//;
        foreach $tmpath (@excldirs) {
          if ($tmp eq $tmpath) { $recurse = 0; last; }
          }
        }
      if ($recurse) { CheckFiles($fullFilename); }
      next;
      }

    if ($fullFilename =~ m/\.htm/i) {
      open(FILE, $fullFilename) or return;
      $line = join(' ',<FILE>);    # Ganze Datei wird zu einer Zeile
      close(FILE);

      $line =~ s/\n//g;            # Newlines weg
      $line =~ s/&auml;/ä/g;       # Umlaute konvertieren
      $line =~ s/&ouml;/ö/g;
      $line =~ s/&uuml;/ü/g;
      $line =~ s/&Auml;/Ä/g;
      $line =~ s/&Ouml;/Ö/g;
      $line =~ s/&Uuml;/Ü/g;
      $line =~ s/&szlig;/ß/g;  

      $title = "No Title";
      if($line =~ m!<title>(.+)</title>!i) { $title = $1; };

      if ($type eq 'all' ) {
        $found = 1;
        foreach $search (@search) {
          if ($line !~ m/$search/i) { $found = 0; last; }
          }
        } 
      else # type = any    
        {
        $found = 0;
        foreach $search (@search) {
          if ($line =~ m/$search/i) { $found = 1; last; }
          }
        }
    
      if ($found) {
        $tmp = $fullFilename;
        $tmp =~ s/$BASE//;
        print "<LI><A HREF=\"$tmp\">$title</a>\n";
        $numberreturned++;
        }
      }
    }
  }

