web2printer 5"; include("configuration.php"); // start time $timingStart = ""; // url to filename to process $urlToPrint = ""; // file content $content = ""; // host name $scripthost = ""; // image processing flag $clearImages = 0; // array with hyperlinks $links = Array(); // number of links $linkCount = 0; // array with images $images = Array(); // number of images $imageCount = 0; // hyperlink processing flag $resolveLink = 0; // send a E-Mail $email = 0; // send a E-Mail stage $stage = 1; // array with hyperlinks // array with parsed meta tags $metaTags = Array(); // number of parsed meta tags $metaCount = 0; // copyright string parsed from meta tags $copyright = ""; // meta tag processing flag $preserveMetaTags = 0; // stylsheet name string // empty or not set: stylsheet informations supressed // otherwise : stylsheet used for printing $stylesheet = ""; // page title $title = ""; // original page $authentyPage = ""; $urlToPrint = ""; // check setting form web2printer_conf.inc, if not set choose defaults if (!isset ($pageUrlHeader)) { $pageUrlHeader = "This page URL:
"; } if (!isset ($pageXRefURLHeader)) { $pageXRefURLHeader = "Links:"; } if (!isset ($pageXRefImages)) { $pageXRefImages = "Images:"; } if (!isset ($activeLinks)) { $activeLinks = 0; } if (!isset ($footer)) { $footer = 1; } if ($footer == 2 && !isset ($customFooter)) { $customFooter = "please define a footer"; } web2printer (); // initialze prepare(); // format ($stage == 1)?display():sendMail(); // display or send as e-mail exit(); function web2printer () { global $timingStart; global $clearImages; global $scripthost; global $resolveLink; global $stage; global $to; global $from; global $name; global $comment; global $preserveMetaTags; global $stylesheet; global $email; // start from here :-) $timingStart = explode(' ', microtime()); if (isset ($_GET["page"])) { $page = $_GET["page"]; // $page = filter_var($_GET["page"], FILTER_SANITIZE_STRING); } if (isset ($_GET["img"])) { $clearImages = $_GET["img"]; // $clearImages = filter_var($_GET["img"], FILTER_VALIDATE_INT); } if (isset ($_GET["lnk"])) { $resolveLink = $_GET["lnk"]; // $resolveLink = filter_var($_GET["lnk"], FILTER_VALIDATE_INT); } if (isset ($_GET["tgs"])) { $preserveMetaTags = $_GET["tgs"]; // $preserveMetaTags = filter_var($_GET["tgs"], FILTER_VALIDATE_INT); } if (isset ($_GET["style"])) { $stylesheet = $_GET["style"]; // $stylesheet = filter_var($_GET["style"], FILTER_VALIDATE_STRING); } // Process the filename // if we nether not have an referer nor an http get -> error if (!isset ($page)) { if (isset ($_SERVER["HTTP_REFERER"])) { $page = $_SERVER["HTTP_REFERER"]; // $page = filter_var($_SERVER["HTTP_REFERER"], FILTER_VALIDATE_URL); } else { if (isset ($_ENV["HTTP_REFERER"])) { $page = $_ENV["HTTP_REFERER"]; // $page = filter_var($_ENV["HTTP_REFERER"], FILTER_VALIDATE_URL); } else { $page = ""; } } // if ($page != "") { $page = parse_url($page, PHP_URL_PATH); } } if ( strlen($page) == 0) { $page = $GLOBALS['defaultPage']; } if ($page[0] != '/') { $page="/".$page; } $GLOBALS['urlToPrint'] = apache_getenv('DOCUMENT_ROOT').$page; } function get_current () { global $timingStart; $stop_time = explode(' ', microtime()); $current = $stop_time[1] - $timingStart[1]; $current += $stop_time[0] - $timingStart[0]; return sprintf("%.6f seconds",$current); } // convert an local filename to uri function makeUrl ($input) { $retval = ""; $input = strtr ($input, "\\", "/"); $input = str_replace (apache_getenv('DOCUMENT_ROOT'), "", $input); $tokens = parse_url ($input); if (isset ($tokens["host"])) { $retval = $input; } else { $input = str_replace ( apache_getenv('SITE_NAME'), "", $input); $retval = apache_getenv('SERVER_NAME').(($input{0} == '/')?"":"/").$input; } return $retval; } // prepare the file content function prepare () { global $clearImages; global $content; global $resolveLink; global $preserveMetaTags; global $stylesheet; global $email; global $stage; global $title; global $authentyPage; global $copyright; if (!is_readable ($GLOBALS['urlToPrint'])) { die ("could not open:".$GLOBALS['urlToPrint']); } $plainFile = file_get_contents ($GLOBALS['urlToPrint']); if (!isset($plainFile) || $plainFile == "") { die ("could not read:".$$GLOBALS['urlToPrint']); } // preserve title tag if (preg_match("/()(.*)<\/title>/i", $plainFile, $regs)) { $title = trim($regs[2]); if ($title == "") { $title = "web2printer generated file"; } } if ($email == 1) { if ($stage == 1) { $content = insertForm($plainFile); return; } $authentyPage = $plainFile; } // parse copyright if (preg_match ("/<meta([ ]?)*name([ ]?)*\=([ ]?)*\"copyright\"([ ]?)*content([ ]?)*\=([ ]?)*\"([^\"])*\"/i", $plainFile, $regs)) { $copyright = preg_replace ("/<meta([ ]?)*name([ ]?)*\=([ ]?)*\"copyright\"([ ]?)*content([ ]?)*\=([ ]?)*\"/i", "", $regs[0]); $copyright = substr ($copyright, 0, strpos($copyright, "\"")); } getMeta($plainFile); // clean up embedded styles $plainFile = preg_replace ("/style[ ]*=\"[^\"]*\"/i", "", $plainFile); // cut off header $plainFile = preg_replace ("/<(\/)?head[ ]*>/i", "<CUTOFFHEADER>", $plainFile); $start = strpos ($plainFile, "<CUTOFFHEADER>"); if ($start) { $end = strpos ($plainFile, "<CUTOFFHEADER>", $start+1); if ($end && $end > 0) { $left = substr($plainFile, 0, $start); $right = substr($plainFile, $start+$end+15); $plainFile = $left.$right; } else { die ("unbalanced header tags"); } } $offset = 0; $loop = true; while ($loop) { $start = strpos ($plainFile, "<!-- web2printer:start -->", $offset); if ($start) { $end = strpos ($plainFile, "<!-- web2printer:end -->", $offset); if ($end) { $content = $content.substr ($plainFile, $start, $end-$start); } else { die ("Missing <!-- web2printer:end -->"); } } else { $loop = false; } $offset = $end+1; } // while if ($clearImages > 0) { clearImages(); } if (0 < $resolveLink) { solveLinks(); } } function insertForm($file) { global $myEMailForm; global $form; $fp = fopen ($myEMailForm, "r"); $form = fread($fp, 64000); return eregi_replace("<!-- web2printer:email !-->", $form, $file); } // image processing // 0 : leave images // 1 : supress image printing // 2 : replace images with [IMAGE] // 3 : replace images with alt function clearImages() { global $content; global $images; // number of images global $imageCount; global $clearImages; $offset = 0; $content = preg_replace ("/<img$/i", "<img", $content); switch ($clearImages) { // leave images case 0: break; // remove images case 1: $content = preg_replace ("/<img([^>]?)*>/i", "", $content); break; // replace images with [IMAGE] case 2: $content = preg_replace ("/<img([^>]?)*>/i", "[Image]", $content); break; // replace images with alt case 3: while ($start = strpos ($content, "<img", $offset)) { $offset = $start+1; $end = strpos ($content, ">", $offset); $entry =substr ($content, $start, $end - $start); if (preg_match ("/alt([^=]?)*=([^\"]?)*\"([^\"]?)*/i", $entry, $regs)) { $alt = preg_replace("/alt([^=]?)*=([^\"]?)*\"/i", "", $regs[0]); $left = substr($content, 0, $start); $right = substr($content, $end+1); $subst = "<b>[Image:".$alt."]</b> "; $content = $left.$subst; $content = $content.$right; $offset = $end+strlen($subst); } $value = ""; } // while $content = preg_replace ("/<img([^>]?)*>/i", "[Image]", $content); break; // generate image crossreference case 4: while ($start = strpos ($content, "<img", $offset)) { $offset = $start+1; $end = strpos ($content, ">", $offset); $entry =substr ($content, $start, $end - $start); if (preg_match ("/src([^=]?)*=([^\"]?)*\"([^\"]?)*/i",$entry, $regs)) { $src = preg_replace("/src([^=]?)*=([^\"]?)*\"/i", "", $regs[0]); $src = makeUrl($src); $left = substr($content, 0, $start); $right = substr($content, $end+1); $found = false; $count = 0; // check if we have this image already while ($count < $imageCount) { if (0 == strcmp ($images[$count], $src)) { $found = true; break; } ++$count; } if (!$found) { $images[$imageCount] = $src; ++$imageCount; } $subst = "[IMAGE No:<b>".($count+1)."</b>]"; $content = $left.$subst; $content = $content.$right; $offset = $end+strlen($subst); } $value = ""; } // while break; default: die ("bad parameter img !"); } } // REMOVE FOR BASIC ------------------------------------------------------------------- // link processing // 1 : create crossreference // 2 : create crossreference with removed get params // 3 : convert hyperlinks to text function solveLinks() { global $content; global $links; global $linkCount; global $activeLinks; global $resolveLink; $offset = 0; $value = ""; $len = strlen ($content); $content = preg_replace ("/<a/i", "<a", $content); $content = preg_replace ("/<\/a/i", "</a", $content); while ($start = strpos ($content, "<a", $offset)) { $offset = $start+2; $end = strpos ($content, "</a", $offset); if ($end == false) { die ("html error: missing </a> starting at:<br>".substr($content, $start, 255)); } $entry =substr ($content, $start, $end - $start+4); if (preg_match ("/href([^=]?)*=([^\"]?)*\"([^\"]?)*/i",$entry, $regs)) { $link = strtolower(preg_replace("/href([^=]?)*=([^\"]?)*\"/i", "", $regs[0])); if ($resolveLink == 2) { if ($pos = strpos ($link, "?")) { $link = substr ($link, 0, $pos); } } if (0 < strlen ($link)) { if ($resolveLink < 3) { $count = 0; $found = false; // check if we have this href already while ($count < $linkCount) { if (0 == strcmp ($links[$count], $link)) { $found = true; break; } ++$count; } // while if (!$found) { $links[$linkCount] = $link; ++$linkCount; } ++$count; } if (0 == $activeLinks) { $entry = preg_replace("/<a([^>]?)*>/i", "", $entry); $entry = preg_replace("/<\/a([^>]?)*>/i", "", $entry); } } $left = substr($content, 0, $start); $right = substr($content, $end); if ($resolveLink < 3) { $content = $left."<b>".$entry."[".$count."]</b>"; $content = $content.$right; } else { $content = $left.$entry.$right; } } // if preg_match $offset = $start+strlen ($entry); if ($offset >= $len) { break; } } // while } function getMeta($plainFile) { $offset = 0; $end = 0; global $metaTags; global $metaCount; global $copyright; $plainFile = preg_replace ("/<(:space:)*meta/i", "<meta", $plainFile); while ($start = strpos ($plainFile, "<meta", $offset)) { $offset = $start+1; $end = strpos ($plainFile, ">", $offset); $metaTags[$metaCount] = substr ($plainFile, $start, $end - $start+1); if (preg_match ("/<meta(:space:)*name(:space:)*=(:space:)*\"copyright\"/i",$metaTags[$metaCount])) { $copyright = preg_replace ("/<meta(:space:)name(:space:)*=(:space:)*\"copyright\"(:space:)*content(:space:)*=\"/i", "", $metaTags[$metaCount]); $copyright = substr ($copyright, 0, strpos($copyright, "\"")); } ++$metaCount; } } function checklog($address) { global $urlToPrint; global $from; global $name; global $comment; global $maxip; global $logfile; if (!isset ($REMOTE_ADDR)) { $ip = getenv("REMOTE_ADDR"); } else { $ip = $REMOTE_ADDR; } $retval = true; $filename = $logfile."/web2printer".date("Ymd").".log"; $filearray = @file ($logfile); if (is_array ($filearray)) { $counter = 0; $boundary = 0; while ($counter < count($filearray) && $retval == true) { if (eregi ($ip, $filearray[$counter])) { ++$boundary; } if ($maxip < $boundary) { $retval = false; break; } ++$counter; } } if ($retval == true) { $fp = @fopen ($filename, "a"); @fputs ($fp, date ("d.m.Y h:m:s", time()). " ;".$urlToPrint." ;".$from." ;".$name." ;".$comment." ;".$address." ;".$ip."\n"); @fclose ($fp); } return $retval; } function SendMail () { global $copyright; global $title; global $content; global $urlToPrint; global $to; global $from; global $name; global $comment; global $email; global $linkCount; global $links; global $authentyPage; global $pageUrlHeader; global $pageXRefURLHeader; global $customFooter; global $web2printerVersion; global $pageXRefImages; global $footer; $ok = true; if (!eregi("^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]+$", trim($from))) { $ok = false; } $tok = strtok($to,","); $i = 0; while ($tok && $i < 5) { if (!eregi("^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]+$", trim($tok))) { $ok = false; } $tok = strtok(","); $i++; } // about if the email adresse not valid syntax if (!$ok) { echo $authentyPage; return; } $headers = "From: ".$from."\r\n"; //specify MIME version 1.0 $headers .= "MIME-Version: 1.0\r\n"; //unique boundary $boundary = uniqid("web2printer"); //tell e-mail client this e-mail contains//alternate versions $headers .= "Content-Type: multipart/alternative; boundary =".$boundary."\r\n\r\n"; //message to people with clients who don't //understand MIME $headers .= "This is a MIME encoded message.\r\n\r\n"; switch ($footer+2) { case 2: $finalfooter = "</table></body></html>"; break; case 3: $finalfooter = "<tr><td><font size=-1><p><hr size=1 noshade>This Page was generated with ".$web2printerVersion." in: ".get_current()." <a href=http://www.printer-friendly.com>http://www.printer-friendly.com</a><br></font></p></td></tr></table></body></html>"; break; case 5: $finalfooter = "<tr><td><p><hr size=1 noshade>".$customFooter."</p></td></tr></table></body></html>"; break; } //HTML version of message $headers .= "--$boundary\r\n"."Content-Type: text/html; charset=ISO-8859-1\r\nContent-Transfer-Encoding: base64\r\n\r\n"; $body ="<html><head><title>".$title.""; $body .= "
"; if (strlen ($comment) > 1) { $body .= "=============================================================
"; $body .= $comment; $body .= "
=============================================================
"; } $body .= $content; $body .= "
"; $body .= "


".$pageUrlHeader.makeUrl($urlToPrint)."

"; $body .= "
"; if ( 0 < $linkCount) { $body .= "

".$pageXRefURLHeader."
"; for ($i = 0; $i < $linkCount; $i++) { $link = makeUrl($links[$i]); $body .= "[".($i+1)."] ".$link."
"; } $body .= "

"; } if (strlen ($copyright) > 0) { $body .= "


Copyright:".$copyright."
"; } $body .= $finalfooter; $headers .= chunk_split(base64_encode($body)); //send message $tok = strtok($to,","); $i = 0; while ($tok && $i < 5) { mail($tok, "Your article:".$title, "", $headers); checklog($tok); $tok = strtok(","); $i++; } echo $authentyPage; } // print the procceced file function display() { global $content; global $urlToPrint; global $title; global $email; global $preserveMetaTags; global $metaCount; global $metaTags; global $pageUrlHeader; global $pageXRefURLHeader; global $customFooter; global $web2printerVersion; global $pageXRefImages; global $footer; echo ""; if (1 == $preserveMetaTags) { for ($i = 0; $i < $metaCount; $i++) { echo $metaTags[$i]; } } if ($email == 1) { echo $content; return; } global $stylesheet; if (strlen($stylesheet) > 0) { echo ""; } switch ($footer+3) { case 3: $finalfooter = "
"; break; case 4: $finalfooter = "


This Page was generated with ".$web2printerVersion." in: ".get_current()." http://www.printer-friendly.com

"; break; case 5: $finalfooter = "


".$customFooter."

"; break; } echo "".$title; echo ""; echo ""; global $copyright; if (strlen ($copyright) > 0) { echo ""; } echo $finalfooter; } ?>
"; echo $content; echo "
"; $link = makeUrl($urlToPrint); echo "


".$pageUrlHeader."".$link; echo "

"; global $linkCount; global $links; if ( 0 < $linkCount) { echo $pageXRefURLHeader."
"; for ($i = 0; $i < $linkCount; $i++) { $link = makeUrl($links[$i]); echo "[".($i+1)."] ".$link."
"; } echo "
"; } global $imageCount; global $images; if ( 0 < $imageCount) { echo "

Images:
"; for ($i = 0; $i < $imageCount; $i++) { echo "[".($i+1)."] ".makeUrl($images[$i])."
"; } } echo "


Copyright:".$copyright."