<?php
session_name("s".base64_decode($_COOKIE[register]));
session_start ();
$sesspass = "base64_decode($_COOKIE[register])";
if (session_id() == "" and $_SESSION[$sesspass] != session_id()) 
{
	session_unset ();
	session_destroy ();
	header ("location: index.php?n=503");
}
if (!session_is_registered("s".base64_decode($_COOKIE[register]))) 
{
	session_unset ();
	session_destroy ();
	header ("location: index.php?n=503");
}
require "../../../../../inc/config.php";
require "../../../../../inc/langs/".$s40[lang].".php";
?>
<?php
function cropImage($nw, $nh, $source, $stype, $dest) {
	$size = getimagesize($source);
	$w = $size[0];
	$h = $size[1];
	switch($stype) {
		case 'gif':
		$simg = imagecreatefromgif($source);
		break;
		case 'jpg':
		$simg = imagecreatefromjpeg($source);
		break;
		case 'png':
		$simg = imagecreatefrompng($source);
		break;
	}
	$dimg = imagecreatetruecolor($nw, $nh);
	$wm = $w/$nw;
	$hm = $h/$nh;
	$h_height = $nh/2;
	$w_height = $nw/2;
	if($w> $h) {
		$adjusted_width = $w / $hm;
		$half_width = $adjusted_width / 2;
		$int_width = $half_width - $w_height;
		imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
	} elseif(($w <$h) || ($w == $h)) {
		$adjusted_height = $h / $wm;
		$half_height = $adjusted_height / 2;
		$int_height = $half_height - $h_height;
		imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
	} else {
		imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
	}
	imagejpeg($dimg,$dest,90);
}
function findext($sname){
	if(preg_match("/.jpg/i", "$sname") or preg_match("/.jpeg/i", "$sname"))
	{
	return "jpg";
	}
	if (preg_match("/.gif/i", "$sname"))
	{
		return "gif";
	}
	if(preg_match("/.png/i", "$sname"))
	{
		return "png";
	}
}
if(!preg_match("/\.jpg|.jpeg|.gif|.png\b/i", $_FILES['uploadfile']['name'])){
	$error = "wrongfile";
}
if (filesize($_FILES['uploadfile']['tmp_name']) > ((str_replace(" mb max","",maxsize())*1024)*1024)){
	$error = "oversize";
}
if (file_exists("../../../../../media/images/".basename($_FILES['uploadfile']['name']))){
	$error = "fileexists";
}
switch ($error) {
	
	case "wrongfile":
	echo $_FILES['uploadfile']['name']." is not valid file for upload";
	break;
	
	case "oversize":
	echo "File is too big";
	break;
	
	case "fileexists":
	if ($_FILES['uploadfile']['name'] == ""){
		echo "No file for upload";
	}else{
		echo $_FILES['uploadfile']['name']." already exists";
	}
	break;
	
	default:
	$target_path = "../../../../../media/original/";
	$target_path = $target_path . basename( $_FILES['uploadfile']['name']);
	
	move_uploaded_file($_FILES['uploadfile']['tmp_name'], $target_path);
	
	cropImage($s40[thumbsize], $s40[thumbsize], $target_path, findext($_FILES['uploadfile']['name']), "../../../../../media/thumbs/". basename( $_FILES['uploadfile']['name']));

	
	$thumbsize=$s40[imagesize];
	$imgfile = $target_path;
	list($width, $height) = getimagesize($imgfile);
	$imgratio=$width/$height;
	if ($imgratio>1){
	$newwidth = $thumbsize;
	$newheight = $thumbsize/$imgratio;
	}else{
	$newheight = $thumbsize;
	$newwidth = $thumbsize*$imgratio;
	}
	if (function_exists('imagecreatetruecolor')) {
	$newimage = ImageCreateTrueColor($newwidth,$newheight);
	} else {
	$newimage = imagecreate($newwidth,$newheight);
	}
	if(preg_match("/.jpg/i", "$imgfile"))
	{
	$format = 'image/jpeg';
	}
	if (preg_match("/.gif/i", "$imgfile"))
	{
	$format = 'image/gif';
	}
	if(preg_match("/.png/i", "$imgfile"))
	{
	$format = 'image/png';
	}
	
	switch($format)
	{
	case 'image/jpeg':
	$source = imagecreatefromjpeg($imgfile);
	break;
	case 'image/gif';
	$source = imagecreatefromgif($imgfile);
	break;
	case 'image/png':
	$source = imagecreatefrompng($imgfile);
	break;
	}
	if (function_exists('imagecopyresampled')) {
	imagecopyresampled ($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	} else {
	imagecopyresized($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
	}
	imagejpeg($newimage,"../../../../../media/images/".$_FILES['uploadfile']['name'],100);
	header ("location: image.php");
	break;

}
?>
