<?
define('ADMIN',true);
@ini_set('memory_limit', '128M');
define('ROOT_PATH', realpath((getenv('DOCUMENT_ROOT') && preg_match('#^'.preg_quote(realpath(getenv('DOCUMENT_ROOT'))).'#', realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', dirname(__FILE__))));
define('CACHE', ROOT_PATH.'/cache/file_manager');
if(!is_dir(CACHE)) mkdir(CACHE, 0755);
if(!is_dir(CACHE.'/jcrop_temp/')) mkdir(CACHE.'/jcrop_temp/', 0755);

if($_GET['jcrop_first_load']==1) {
	if($cache_dir=opendir(CACHE.'/jcrop_temp/')) {
		while(($cache_file=readdir($cache_dir))!=false) {
			if($cache_file!='.' && $cache_file!='..') @unlink(CACHE.'/jcrop_temp/'.$cache_file);
		}
		closedir($cache_dir);
	}
}



$_GET['src']=urldecode($_GET['src']);

if($_GET['jcrop_in_temp']==1) {
	$real_src = ROOT_PATH.'/'.$_GET['src'];
} else {
	$_GET['src']=str_replace('/userfiles','',$_GET['src']);
	$real_src = ROOT_PATH.'/userfiles'.$_GET['src'];
}


$temp_file=CACHE.'/jcrop_temp/'.urlencode($real_src);
$src=$real_src;
if(file_exists($temp_file) && $_GET['jcrop_first_load']!=1) $src=$temp_file;
$ext=@array_pop(explode('.',$src));
if(stristr($ext,'_tmp')) $ext=str_replace('_tmp','',$ext);


if($_GET['action']=='save') {
	switch($ext) {
		default:
		case 'jpg':		header('Content-type: image/jpeg');		break;
		case 'gif':		header('Content-type: image/gif');		break;
		case 'png':		header('Content-type: image/png');		break;
	}

	if(file_exists($temp_file)) {
		@unlink($real_src);
		@rename($temp_file, $real_src);
	}
	readfile('../../images/spacer.gif');
	exit;
}


switch($ext) {
	default:
	case 'jpg':		$img_r = imagecreatefromjpeg($src);		break;
	case 'gif':		$img_r = imagecreatefromgif($src);		break;
	case 'png':		$img_r = imagecreatefrompng($src);		break;
}


if($_GET['action']=='resize') {
	$targ_w = $src_w = $_GET['w'];
	$targ_h = $src_h = $_GET['h'];

	list($src_w2, $src_h2) = getimagesize($src);
	$src_x = 0;
	$src_y = 0;


	if($targ_w > $src_w2 || $targ_h > $src_h2) {
		switch($ext) {
			default:
			case 'jpg':		$img_r = imagecreatefromjpeg($real_src);		break;
			case 'gif':		$img_r = imagecreatefromgif($real_src);		break;
			case 'png':		$img_r = imagecreatefrompng($real_src);		break;
		}
		list($src_w2, $src_h2) = getimagesize($real_src);
	}
} else {
	$src_x = $_GET['x'];
	$src_y = $_GET['y'];
	$src_x2 = $_GET['x2'];
	$src_y2 = $_GET['y2'];

	list($src_w2, $src_h2) = getimagesize($src);
	$src_w = $src_w2;
	$src_h = $src_h2;

	$targ_w = $src_x2-$src_x;
	$targ_h = $src_y2-$src_y;

	if($targ_w==0 || $targ_h==0) { 
		switch($ext) {
			default:
			case 'jpg':		header('Content-type: image/jpeg');		break;
			case 'gif':		header('Content-type: image/gif');		break;
			case 'png':		header('Content-type: image/png');		break;
		}
		readfile($src);
		exit;
	}
}


$dst_r=imagecreatetruecolor($targ_w, $targ_h);
imagecopyresampled($dst_r, $img_r, -$src_x, -$src_y, 0, 0, $src_w, $src_h, $src_w2, $src_h2);


switch($ext) {
	default:
	case 'jpg':
		header('Content-type: image/jpeg');
		imagejpeg($dst_r, $temp_file, 90);
	break;
	case 'gif':
		header('Content-type: image/gif');
		imagegif($dst_r, $temp_file);
	break;
	case 'png':
		header('Content-type: image/png');
		imagepng($dst_r, $temp_file);
	break;
}


header("Cache-Control: private, max-age=10800, pre-check=10800");
header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 2 day")));
readfile($temp_file);
?>