<?php
/**
* (c) 2000, 2001, 2002, 2003 Remistech Sdn Bhd. All Rights Reserved.
*  Developed by John Lim
*  Licensed to Natsoft (M) Sdn Bhd to distribute and support. Refer to LICENSE document.
*/

/*
	Load a picture from the directory stored in $TMPDIR.
	$TMPDIR must match beginning of the picture directory 
	set in the $lens->imageTmpDir property.
*/
## jmj patch

@include(dirname(__FILE__).'/config/phplens.img.config.inc.php');

define('MAX_BLOB_SIZE',100000);
//-----------------------------------------------------------------------------
// USE THE FOLLOWING TO DEBUG -- uncomment the mail line and set the address 
//-----------------------------------------------------------------------------
function err($msg)
{
	// mail('email@address.here','phplens-img.php error '.date('Y-m-d H:i:s'),$msg);
	die($msg);
}


//-----------------------------------------------------------------------------
// CODE BEGINS
//-----------------------------------------------------------------------------
if (empty($HTTP_GET_VARS['f'])) die();
$fname= $HTTP_GET_VARS['f'];

if (defined('LENS_IMAGETMPDIR')) $TMPDIR = LENS_IMAGETMPDIR;
else if (substr($fname,0,1) == '/') $TMPDIR = '/tmp';  # unix
else $TMPDIR = getenv('TEMP');  # windows
// for windows, is normally 'C:/Documents and Settings/Default User/';

//------------------------------------------------------
// Clean up \ and / handling by changing everything to /
if (strpos($fname,'\\\\') !== FALSE) {
	$fname = str_replace('\\\\','\\',$fname);
}
$TMPDIR = str_replace('\\','/',$TMPDIR);
$fname = str_replace('\\','/',$fname);


//-------------------------------------------------------------
// hacker check 1. Do not allow them to access parent directory
if (strpos($fname,'..') !== false) err('parent directory hack='.$fname);
//---------------------------------------------------------------------
// hacker check 2. We must access a base directory that matches $TMPDIR
if (strtoupper($TMPDIR) != strtoupper(substr($fname,0,strlen($TMPDIR)))) err('base directory hack="'.$fname.'", should be ='.$TMPDIR);
//---------------------------------------
// hacker check 3. It must have len in it
if (strpos($fname,'len') === false) err('filename hack='.$fname);


$f = fopen($fname,'rb');
if ($f) {
	$val = fread($f,MAX_BLOB_SIZE);
	
} else {
	err( "Unable to open file $fname");
	$val = '';
}

$n = (isset($HTTP_GET_VARS['n'])) ? $HTTP_GET_VARS['n'] : false;
	
if (isset($HTTP_GET_VARS['t'])) {
	$ctype = $HTTP_GET_VARS['t'];
} else {
	if (substr($val,0,3) == 'GIF') $ctype = 'image/gif';
	else if (substr($val,6,4) == 'JFIF') $ctype = 'image/jpeg';
	else if (substr($val,1,3) == 'PNG') $ctype = 'image/png'; 
	else {
		$ctype = 'application/octet-stream';
		if (!$n) { 
		// if no name defined, try to autodetect type and give a name
			$suffix = '';
			if (substr($val,0,5) == '%PDF-') $suffix = '.pdf';
			elseif (substr($val,0,5) == '{\rtf') $suffix = '.rtf';
			$n = 'RENAME_FILE'.$suffix;
		}
	}
}

Header("Content-Length: " . filesize($fname));
Header("Content-Type: ".$ctype);
if ($n !== false) Header("Content-Disposition: attachment; filename=\"$n\"");

echo $val;
if ($f) {
	do {
		$val = fread($f,MAX_BLOB_SIZE);
		if (strlen($val) == 0) break;
		echo $val;
	} while(1);
	
	fclose($f);
	$f = false;
}

if (empty($HTTP_GET_VARS['c'])) unlink($fname); // cache
?>
