#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
use strict;
use warnings;
use CGI;
use File::Find;
use HTML::Parser;
my $cgi   = CGI->new();
print $cgi->header(-type => 'text/html');
my %params  = $cgi->Vars();
my $string  = '';
my $title    = '';
my $basedir = '/home/netzgrafik/www.marktgemeinde-seibersdorf.at/';
my $base_url = 'http://www.marktgemeinde-seibersdorf.at/';
my $hp_title = 'Startseite';
# hier noch den Pfad zur Vorlage eintragen
my $template = '/home/netzgrafik/www.lottermoser.at/vorlage/vorlage.htm';
my @files = ();
find(\&find_files,$basedir);
my ($includes) = search($params{terms},\@files,$basedir);
print_html($base_url,$template,$hp_title,$params{terms},$includes);
sub find_files{
 push(@files,$File::Find::name) if(-f $File::Find::name && $_ =~ /\.htm$/);
}
sub print_html{
 my ($base,$template,$title,$terms,$hashref) = @_;
 my $content = '';
 {
   local $/;
   open(my $fh,'<',$template) or die $!;
   $content = <$fh>;
   close $fh;
 }
 my $html = '
\n
      Such Information:
     Â
\n- Suchbegriff: ~;
 $html.= join(", ",split(/\s+/,$terms));
 $html .= qq~
- $title\n
       Â
\n~;
       Â
        #netzgrafik.com.\n~;
 $content =~ s/\$Titel/$title/;
 $content =~ s/\$Message/$html/;
 print $content;
}
sub search{
 my ($termsstring,$files,$basedir) = @_;
 my @terms = split(/\s+/,$termsstring);
 my $parser = HTML::Parser->new(
             api_version => 3,
             start_h   => [\&start,"self,tagname,attr"],
             text_h    => [\&text,"self,dtext"],
             end_h    => [\&end,"self,tagname"]);
 $parser->{divs} = 0;
 my %include;
 for my $html_file(@$files){
   $string = '';
   $title = '';
   $parser->parse_file($html_file);
   foreach my $term (@terms) {
     $term = umlauts($term);
     # hier wurde noch das "i" angef?gt f?r "ignore case" also
     # matching ohne Ber?cksichtigung von Gro?- und Kleinschreibung
     if ($string =~ /$term/i or $title =~ /$term/i) {
      (my $local_file =  $html_file) =~ s/\Q$basedir\E//;
      $include{$local_file} = $title;
      last;
    }
    Â
   }
 }
 return \%include;
}
sub start{
 my ($self,$tag,$attr) = @_;
 if($tag eq 'div' && $attr->{class} eq 'scroll'){
   $self->{search} = 1;
 }
 if($tag eq 'div' and $self->{search}){
   $self->{divs}++;
 }
 if($tag eq 'title'){
   $self->{title} = 1;
 }
}
sub text{
 my ($self,$dtext) = @_;
 $string .= $dtext if($self->{search});
 $title = $dtext if($self->{title});
}
sub end{
 my ($self,$tag) = @_;
 if($tag eq 'div' and $self->{search}){
   $self->{divs}--;
 }
 if($self->{divs} == 0){
   $self->{search} = 0;
 }
 if($tag eq 'title'){
   $self->{title} = 0;
 }
} Â
Â
sub umlauts{
 my ($term) = @_;
 $term=~ s/&â/?\;/g;
 $term=~ s/x
/?\;/g;
 $term=~ s/&â/?\;/g;
 $term=~ s/?/?\;/g;
 $term=~ s/&¸/?\;/g;
 $term=~ s/?/?\;/g;
 $term=~ s/?/?\;/g;
 return $term;
}