#!/usr/bin/perl
# #!C:/Programs/Perl/bin/Perl.exe

#######################################################################################
# This is copyright 2002, William Gunther. There are no terms of service, except that #
# the comment at the bottom of script remains. I'd also like you to keep the file name#
# the same, but if not that's okay. Anyway, remember this script is SO simple, and I  #
# put a lof of comments in them. To save space, you may erase them.                   #
#######################################################################################
# Use this by linking to www.domain.com/path/to/printer.pl?file=html/path/to/file.html#
# Other suggestions of use are using a link tag for auto printing. (Refer to article  #
# at www.webmutant.com). Enclose area in <!--begincontent--> and <!--endcontent-->    #
# JavaScript Tags.                                                                    #
#######################################################################################
#Complete path to base folder (NOT URL!), WITHOUT / at end. Only thing you HAVE to change

$path = "/home/www/web644/html";
#Franky-Internet: $path = "C:/Dokument/web/frames";
@referers = ('localhost','frank-dieterle.de','frank-dieterle.com','drdieterle.de','drdieterle.com','chemometrie.de','chemometrie.com','127.0.0.1');




   print "Content-type: text/html\n\n";

# Check Referring URL to prevent hacking
&check_url;

#Calls up Parsing subroutine at bottom of script...Do not change
&parseForm;
#opens the file specified in the $path

open(FILE,"$path/$FORM{file}") ;
@FILE = <FILE>;
close(FILE);
#opens up footer. May be changed if you change footer.html's name or directory.
open(FOOTER,"print_footer.html") ;
@FOOTER = <FOOTER>;
close(FOOTER);
#Print equals 0, pretty self explanitory....
$print = 0 ; 
#open the header please! Same applies as in the footer
open(HEADER,"print_header.html") ;
@HEADER = <HEADER>;
close(HEADER);
#print it! Put a # before print if you don't want a header printed...
print "@HEADER";
#Okay, below is says foreach line in the file do the following. Print = 1 if it's after begin content, and it's 0 if its
#after endcontent.

$Pfad=$FORM{file} ;





# ersetzt von hinten aus gesehen /*.html durch / um den Pfad zur Datei festzulegen
$Pfad =~ s/\/(\w+)\.html\Z/\//gi ;

foreach $line(@FILE) {
	if ($line =~ s/src\s*=\s*\"/src=\"$Pfad/gi) {
	}
# ersetzt src*=*" durch src="Pfad
	if ($line =~ s/background\s*=\s*\"/background=\"$Pfad/gi) {
	}
# ersetzt background*=*" durch background="Pfad
	if ($line =~ s/\"style_diss.css/\"\/phd\/style_diss.css/gi) {
	}
# ersetzt "style_diss.css durch "/phd/style_diss.css
	if ($line =~ s/\"style_metabonomics.css/\"\/tutorials\/metabonomics\/style_metabonomics.css/gi) {
	}
# ersetzt "style_metabonomics.css durch "/tutorials/metabonomics/style_metabonomics.css
	if ($line =~ s/<A[^>]*>//gi) {
	}

# loescht alle Links
        if ($line =~ /begincontent/) { 
                print $line ; 
                $print = 1;
                next ;
        }

        if ($line =~ /endcontent/) { $print = 0 ; }
#print the 1's! Now!
        if ($print eq "1") { 
                print $line; 
        }

}
#print that footer, put # infront it if you don't want it.
print "@FOOTER";

#here's that subroutine we called in the beginning. It parses the form. I could have used CGI.PM, but 
#that limits the people who can use it sometimes. C'est tres facile!!!
sub parseForm {
      
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
        # Split the name-value pairs
        @pairs = split(/&/, $ENV{'QUERY_STRING'});
    }
elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {

      
 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   
   # Split the name-value pairs
   @pairs = split(/&/, $buffer);
}
   foreach $pair (@pairs) {
      ($name, $value) = split(/=/, $pair);
      
      # Un-Webify plus signs and %-encoding
      $value =~ tr/+/ /;
      $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
#      $value =~ s/<!--(.|\n)*-->//g;

      $FORM{$name} = $value;
      }
      
	# Wenn die URL einen Blankspace oder %20 oder | oder .. enthält, dann besteht gefahr eines Hackversuches
	if ($FORM{file} =~ m/\s/i) {
		die('URL contains encoded blank space - could be a hack attempt')
	 	}
	if ($FORM{file} =~ m/\%20/i) {
		die('URL contains encoded blank space - could be a hack attempt')
	 	}
	if ($FORM{file} =~ m/\|/i) {
		die('URL contains pipe signal - could be a hack attempt')
	 	}
 	
	if ($FORM{file} =~ m/\.\./i) {
		die('URL contains .. - could be a hack attempt')
	 	}

	# Wenn die URL ohne / anfaengt dann potentieller Hackversuch
	if (not $FORM{file} =~ m/\A\//i) {
		die('URL does not start with an / - could be a hack attempt')
	 	}
	
}


sub check_url {

    # Localize the check_referer flag which determines if user is valid.     #
    local($check_referer) = 0;

    # If a referring URL was specified, for each valid referer, make sure    #
    # that a valid referring URL was passed to FormMail.                     #

    if ($ENV{'HTTP_REFERER'}) {
        foreach $referer (@referers) {
            if ($ENV{'HTTP_REFERER'} =~ m|https?://([^/]*)$referer|i) {
                $check_referer = 1;
                last;
            }
        }
    }
    else {
        $check_referer = 0;
    }

    # If the HTTP_REFERER was invalid, send back an error.                   #
    if ($check_referer != 1) { die('Bad referer used for printing file - could be a hacking attempt') }
}




#Copyright 2002, William Gunther ;)
#Copyright 2003, Modifications by Franky ;)