<?php

######################################################################
#
# Author: Jason Packer - jhpacker/at/lazyhacker.com
# Version: 1.0 
#
# printable.php -- create a bare-bones printable HTML page 
#
# usage: printable.php?printable=filename.html
#
# very basic script to create a minimal printable version
# of an HTML page. if you modify this take care to not expose
# other parts of your filesystem or code. YMMV.

# some input pulled from: php.net/manual/en/function.strip-tags.php
#
# There are no restrictions on the use of this code other
# than to include my name in any derived work.  There are
# no warranty for this obviously, but you are welcomed
# to modify, correct, or adapt the code to your needs.  The
# author appreciates if you are willing to submit corrections
# or suggestions for improvement.


# http://www.lazyhacker.com
#
######################################################################

// what tags we're keeping
$allow_tags "<html>,<body>,<b>,<a>,<i>,<u>,<p>,<br>,<hr>,<script>,<table>,<td>,<tr>,<input>";

// what attributes we're dumping
$strip_attributes "bgcolor,background,text,vlink,alink,javascript:,onclick,ondblclick,onmousedown,onmouseup,onmouseover,onmousemove,onmouseout,onkeypress,onkeydown,onkeyup";

if (empty(
$printable)){
  
error("usage: printable.php?printable=file.html");
}

// we don't want to expose the "file" variable name 
// to keep people from finding this by google search &
// attempting to exploit

$file $printable;
$docroot getenv("DOCUMENT_ROOT");

// do some security checks
if (!preg_match("/\.html?$/i",$file)){
  
// only allow this with HTML files
  
error("only HTML allowed");
}
if (
preg_match("/(\.\.)/",$file) || empty($docroot)){
  
// don't allow trying to escape document root
  
error("directory not allowed");
}

// read the file
$fd fopen ("$docroot/$file""r");
if (
$fd){
  
$contents fread ($fdfilesize($file));
  
fclose($fd);
}else{
  
error();
}

// turn bare special characters into html entities
// could cause problems with tags like < br >, so don't do that!
// php => 4.05 for this one.
if (phpversion()>=4.05){
  
$contents preg_replace_callback("/\s(<|>)(=|<|>)?\s/"
                    
create_function(
                            
'$matches',
                            
'return htmlspecialchars($matches[0]);'
                            
),
                    
$contents);
}

$clean strip_tags($contents$allow_tags);
$attributes explode(",",$strip_attributes);
foreach (
$attributes as $attribute){
  
$clean preg_replace("/(<.+)$attribute(\=\"\S+\")?(.+>)/i","$1$3",$clean);
}

// create link to regular version
if (preg_match("/href=\"\S?printable.php\?printable=(\S+\")/i",$clean,$matches)){
  
$backlink $matches[1];
}else{
  
// fall back to referer if we can't find the printable.php link
  
$backlink getenv("HTTP_REFERER");
}

$backlink "<a href=\"$backlink\">&lt;-Back to regular version</a><br><br>";

echo 
$backlink;
echo 
$clean;

function 
error($error_msg){
  if (empty(
$error_msg)){
    
$error_msg "sorry, unknown page";
  }
  echo 
"error: <b>$error_msg</b>";
  exit;
}

?>