#!/usr/bin/perl
#
##### Begin: header information and copyright notice #####
#
# MasterPFP Version 1.2
#     by William Bontrager,
#         a programmer at willmaster.com.
# Version 1.0 completed: February 7, 2000
# Version 1.2 completed: September 2, 2000
#  Programmer's Website: http://www.willmaster.com/
#    Programmer's Email: programmer@willmaster.com
# 
# Copyright 2000 by William Bontrager. All rights reserved.
#
# This program is designed to display a printer friendly version of your page.
#
# This program may be used and modified free of charge by anyone so long as
# this header information, including copyright notice, remains intact. By using 
# and/or modifying this program you agree to indemnify William Bontrager and his
# associates from any liability that might arise from its use and/or modification.
#
# Selling the code for this program without prior written consent from the
# copyright owner is expressly forbidden. You must have permission before
# redistributing this program. In all cases, this header information and
# copyright notice must remain intact.
#
#
# Before you install this program, you must agree to the License Agreement.
#     The License Agreement is linked from the index page at http://willmaster.com/master
#
#
##### End: header information and copyright notice #####

#
# HTML page instructions:
#
#
# Before you install this program, you must agree to the License Agreement.
#     The License Agreement is linked from the index page at http://willmaster.com/master
#
#
# (1)
# The program will display everything up to the first <body...> tag. It
#    will replace your <body...> tag with <body bgcolor="#FFFFFF"><blockquote> 
#    Then it will display whatever is between any
#       <!-- BEGIN_PRINTER_FRIENDLY_COPY -->
#    and
#       <!-- END_PRINTER_FRIENDLY_COPY -->
#    tag sets. You may have as many of those tag sets as you wish, allowing 
#    you to be selective about what the user will print. Last, the program 
#    will finish the page with </blockquote></body></html>
#
# (2)
# Any images that you will display on the printer friendly page must use 
#    the complete http:// URL rather than a relative URL. Examples --
#           this: <img src="http://www.domain.com/images/picture.jpg">
#       not this: <img src="images/picture.jpg">
#
# (3)
# The link to MasterPFP must end with:
#       ?doc=thispage.html
#    where "thispage.html" is the page the program is to display a printer 
#    friendly version of. You can specify the page as either a complete 
#    http://... URL or as a file on your server.
#
#    If you elect to use the complete http://... URL, it would look something 
#    like this:
#       ?doc=http://www.domain.com/stuff/thispage.html
#
#    If you elect to specify the page as a file on your server, then if 
#    the page is in a subdirectory, the subdirectory must be specified. 
#    Example, http://www.domain.com/stuff/thispage.html would be:
#       ?doc=stuff/thispage.html
#
# (4)
# Optionally, you can direct the program to display the URL at the top 
#    and/or bottom of the printer friendly page. You do this by appending 
#    additional data to the link. The addition data can be
#       &top=http://www.domain.com/stuff/thispage.html
#    and/or
#       &bottom=http://www.domain.com/stuff/thispage.html
#    The URL in the additional data is to the regular page.
#
#    The following examples show the several different valid link formats.
#    Just replace the URL, program name/location, and web page name with
#    what is appropriate for you:
#
#       <a href="MasterPFP.cgi?doc=http://www.domain.com/thispage.html">Click 
#       for printer friendly page</a>
#
#           <a href="MasterPFP.cgi?doc=thispage.html">Click 
#           for printer friendly page</a>
#
#       <a href="cgi-bin/MasterPFP.cgi?doc=http://www.domain.com/stuff/thispage.html">Click 
#       for printer friendly page</a>
#
#           <a href="cgi-bin/MasterPFP.cgi?doc=stuff/thispage.html">Click 
#           for printer friendly page</a>
#
#       <a href="../cgi-bin/MasterPFP.cgi?doc=http://www.domain.com/thispage.html">Click 
#       for printer friendly page</a>
#
#           <a href="../cgi-bin/MasterPFP.cgi?doc=thispage.html">Click 
#           for printer friendly page</a>
#
#       <a href="http://www.domain.com/cgi-bin/MasterPFP.cgi?doc=http://www.domain.com/stuff/thispage.html">Click 
#       for printer friendly page</a>
#
#           <a href="http://www.domain.com/cgi-bin/MasterPFP.cgi?doc=stuff/thispage.html">Click 
#           for printer friendly page</a>
#
#       <a href="MasterPFP.cgi?doc=http://www.domain.com/thispage.html">Click 
#       for printer friendly page</a>
#
#           <a href="MasterPFP.cgi?doc=thispage.html">Click 
#           for printer friendly page</a>
#
#       <a href="MasterPFP.cgi?doc=http://www.domain.com/master/pfp/index.html&top=http://willmaster.com/master/pfp/index.html&bottom=http://willmaster.com/master/pfp/index.html">Click 
#       for printer friendly page</a>
#
#           <a href="MasterPFP.cgi?doc=master/pfp/index.html&top=http://willmaster.com/master/pfp/index.html&bottom=http://willmaster.com/master/pfp/index.html">Click 
#           for printer friendly page</a>
#
# Script installation instructions:
#
# (1) Ensure the first line of this file points to Perl 5+
#
# (2) The $imagefix = '__________'; line, below, tells the program what to do, 
#     if anything, with the images on the printer friendly version of your page. 
#     Any of the following values can be between the apostrophes (single quotes):
#         $imagefix = '';
#         $imagefix = '-';
#         $imagefix = '[IMAGE]';
#         $imagefix = '<b>[Image]</b>';
#         $imagefix = '[Image Location]';
#         $imagefix = 'anything else';
#
#     The first example (nothing between the apostrophes) tells the program 
#     to do nothing with the images on the printer friendly version of your page.
#
#     The second example (a hyphen or minus sign between the apostrophes) tells
#     the program to remove the images on the printer friendly version of your page.
#
#     The third through sixth examples (anything other than the first two examples 
#     between the apostrophes) each tells the program to replace the images on  
#     the printer friendly version of your page with whatever is between the  
#     apostrophes. In other words, if it is not either the first or second example  
#     then the program will replace the images on your page with whatever you put  
#     between the apostrophes. And you can include HTML code (demonstrated in the  
#     fourth example).
#
#     The live $imagefix = '__________'; line is the next line below.

$imagefix = '';

# (3) Upload the program to your server as ASCII/plain text. (A reminder: All 
#     edits must be done with an ASCII/plain text word processor.)
#
# (4) CHMOD the program to 755 (does not apply to NT servers).
#
# That's it. You're ready to test it!
#


###                                        ###
### DO NOT CHANGE ANTHING BELOW THIS POINT ###
###                                        ###


require 5;
use LWP::Simple;
@doc = split(/&/,$ENV{QUERY_STRING});
for (@doc)
{
        my ($n,$v) = split(/=/,$_);
        $n =~ tr/+/ /;
        $v =~ tr/+/ /;
        $n =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
        $v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
        $v =~ s/\<script/\<_s_c_r_i_p_t\(mailer_robot_edited\)/ig;
        $In{$n} = $v;
}
($topURL,$bottomURL) = ('','');
$topURL = "<a href=\"$In{top}\">Click here to return to $In{top}\</a><p>" if $In{top};
$bottomURL = "<p><a href=\"$In{bottom}\">Click here to return to $In{bottom}\</a>" if $In{bottom};
$In{doc} = $ENV{HTTP_REFERER} unless $In{doc};
if($In{doc} =~ /\Ahttp[s]*\:\/\/.+\..+/i)
{
        my $p = get $In{doc};
        @doc = split /\n/,$p;
        my $tt;
        for(@doc) { $tt .= "\n"; }
}
else
{
        unless(open R,"$ENV{DOCUMENT_ROOT}\/$In{doc}")
        {
                print "Content-Type: text/plain\n\nSORRY, $In{doc} COULD NOT BE LOCATED ON THE SERVER.";
                exit;
        }
        @doc = <R>;
        close R;
}
for $t (@doc) { $t =~ s/bgcolor\=\"{0,1}.*?\"{0,1}//ig; }
for $t (@doc)
{
        last if $t =~ /\<\s*body/i;
        push @top,$t;
        $t = '';
}
$top = join('',@top);
$doc = join('',@doc);
@doc = split(/\<\!\-\-\s*BEGIN[\_\s]PRINTER[\_\s]FRIENDLY[\_\s]COPY\s*\-\-\>/i,$doc);
$doc[0] = "<body bgcolor\=\"\#FFFFFF\">$topURL<blockquote>";
for $t (@doc) { $t =~ s/\<\!\-\-\s*END[\_\s]PRINTER[\_\s]FRIENDLY[\_\s]COPY\s*\-\-\>.*//si; }
$doc = join('',@doc);
$imagefix = '' if $imagefix =~ /\A\s*\Z/;
if($imagefix)
{
        $imagefix = '' if $imagefix =~ /\A\s*\-\s*\Z/;
        $doc =~ s/\<\s*img\s+src\s*\=\s*\".+?".*?\>/$imagefix/sgi;
}
print "Content-Type: text/html\n\n$top$doc</blockquote>$bottomURL</body></html>\n";
exit;

