<?
@ini_set('memory_limit', '128M');

define('ROOT','../../../..');
require_once(ROOT.'/.config.php');
define('CACHE_FM', ROOT_PATH.'/cache/file_manager');

if(!is_dir(CACHE_FM)) mkdir(CACHE_FM, 0755);
if(!is_dir(CACHE_FM.'/jcrop_temp/')) mkdir(CACHE_FM.'/jcrop_temp/', 0755);

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




$_GET['src']=urldecode($_GET['src']);
$_GET['src']=str_replace('/userfiles/'.USR_CK.'/','',$_GET['src']);
$real_src = ROOT_PATH.'/userfiles/'.USR_CK.'/'.$_GET['src'];


$temp_file=CACHE_FM.'/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($_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;
	}

	$_GET['new_name']=urldecode($_GET['new_name']);
	$temp=explode('/',$_GET['new_name']);
	$new_filename=@array_pop($temp);

	require_once('functions.php');
	unset($temp[count($temp)]);
	$new_dir=@implode('/',$temp);

	$ext=@array_pop(explode('.',$new_filename));
	$new_filename=substr($new_filename,0,-(strlen($ext)+1));
	$new_filename = URL_mod($new_filename) . '.'.$ext;

	@rename($temp_file, ROOT_PATH.$new_dir.'/'.$new_filename);
	readfile('img/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);
?>