#*****************************************************************#
  # ALLGEMEIN:                                                      #
  # Ordner physikalisch durchsuchen                                 #
  #*****************************************************************#
  
  use includedatei;

  # Suchbegriff

  $BEGRIFF = "murphy";

  $STRING = includedatei->start_suche($BEGRIFF);

    
  #*****************************************************************#
  # Subroutinen                                                     #
  #*****************************************************************#  
  
  my $pfad = "d:/server/html/";

  sub start_suche
  {
     ($self, $suchwort) = @_;
     $suchwort =~ s/\./\\\./g;
     $suchwort =~ s/\$/\\\$/g;
     $suchwort =~ s/\^/\\\^/g;
     $suchwort =~ s/\(/\\\(/g;
     $suchwort =~ s/\)/\\\)/g;
     $suchwort =~ s/\[/\\\[/g;
     $suchwort =~ s/\]/\\\]/g;
     $suchwort =~ s/\|/\\\|/g;

     my $ergebnis = "";
     my $anzahl_ergebnis = 0;

     # DATEILISTE
     opendir(ORDNER, $pfad);
     @array = readdir(ORDNER);
     closedir(ORDNER);

     # Quelltext durchsuchen
     foreach(@array) {
       &suche($_);
     }

     # Ergebnisse sortieren
     while (keys(%WICHTIGKEIT)) {
           $key = &sortieren(%WICHTIGKEIT);

           $temp = $ergebnis."Keyword-Haeufigkeit: ";
           $temp .= $WICHTIGKEIT{$key};
           $temp .= "\n".$resultat{$key}."\n";

           $ergebnis = $temp;
           delete $WICHTIGKEIT{$key};
     }
     if ($ergebnis eq '') { $ergebnis = "nix"; }

     return $ergebnis;
}

  #*****************************************************************#

  sub suche
  {
    my $datei = shift;
    my $dateipfad = $pfad.$datei;

    if (-f "$dateipfad" && -T "$dateipfad") 
    {
      $title = &holeDaten($dateipfad);
      if (open (FILE, $dateipfad)) 
      {
        while ($line = <FILE>) 
        {
          if ($line =~ m/$suchwort/i) 
          {
            $line =~ s/</&lt;/g;
            $line =~ s/>/&gt;/g;
            $line =~ s/$suchwort/<b>&<\/b>/gi;
            $dateipfad =~ s/\\/\//gi;
            if ($WICHTIGKEIT{$dateipfad} == 0) 
            {
              $resultat{$dateipfad}  = "Dateiname: ".$datei;
              $resultat{$dateipfad} .= $title."\n";
            }
            $WICHTIGKEIT{$dateipfad}++;
          }
        }
        if ($WICHTIGKEIT{$dateipfad} > 0) 
        {
           $anzahl_ergebnis++;
        }
        close (FILE);
      }
    }
  }

 #*****************************************************************#

  sub holeDaten
  {
    my ($dateipfad) = @_;

    if (open (FILE, $dateipfad)) 
    {
      #das ganze File wird als eine Zeile gelesen
      undef $/;
      $contents = <FILE>;

      $contents =~ m/<title>.*<\/title>/i;
      $title = $&;
      $title =~ s/<title>//ig;
      $title =~ s/<\/title>//ig;

      $contents =~ m/<meta\s+name\s*=\s*"*keywords"*\s+content\s*=\s*"+(.*?)"+\s*>/i;
      $meta1 = $1;
      $meta1 =~ s/<meta\s+name\s*=\s*"*keywords"*\s+content\s*=\s*"+//ig;
      $meta1 =~ s/"+\s*>//ig;

      $contents =~ m/<meta\s+name\s*=\s*"*description"*\s+content\s*=\s*"+(.*?)"+\s*>/i;
      $meta2 = $1;
      $meta2 =~ s/<meta\s+name\s*=\s*"*description"*\s+content\s*=\s*"+//ig;
      $meta2 =~ s/"+\s*>//ig;

      #Line-Separator wieder richtig setzen
      $/ = "\n";
    }

    $output  = "\nTitel: ".$title;
    $output .= "\nKeywords: ".$meta1 ;
    $output .= "\nDescription: ".$meta2;

    return $output;
  }

  #*****************************************************************# 

  sub sortieren
  {
    my (%LISTE) = @_;
    $max = 0;
    $max_key = "";
    foreach $key (%LISTE)
    {
      if ($LISTE{$key} > $max)
      {
        $max = $LISTE{$key};
        $max_key = $key;
       }
    }
    return $max_key;
  }