<?php
session_name(base64_decode($_COOKIE[register]));
session_start ();
if (! session_is_registered ( "authorized-".base64_decode($_COOKIE[register]) ) ) 
{
session_unset ();
session_destroy ();
	header ("location: ../../system.php?n=503");
}
?>
<?php
function getExtension($str) {
	$i = strrpos($str,".");
	if (!$i) { return ""; }
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return $ext;
}
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);
}


if ($_FILES['uploadfile']['tmp_name'] == ""){
	$e1 = "error";
}else{
	$e1 = true;
}


$uploadedfile = $_FILES['uploadfile']['tmp_name'];
$uploadedimage = $_FILES['uploadfile']['name'];

if ($uploadedimage){
	$filename = stripslashes($_FILES['uploadfile']['name']);
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	if (($extension != "jpg") && ($extension != "jpeg")){
		$e2 = "error";
	}else{
		$e2 = true;
	}
	if (filesize($_FILES['file']['tmp_name']) > "1400000"){
		$e3 = "error";
	}else{
		$e3 = true;
	}
	$e4 = true;
}else{
	$e4 = "error";
}
if ($e1 and $e2 and $e3 and $e4){	
	$src = imagecreatefromjpeg($uploadedfile);
	list($width,$height)=getimagesize($uploadedfile);
	if ($height < 520){
		$newheight=$height;
		$newwidth=($width/$height)*$height;
		$tmp=imagecreatetruecolor($newwidth,$newheight);
		imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
		}else{
	$newheight=520;
	$newwidth=($width/$height)*520;
	$tmp=imagecreatetruecolor($newwidth,$newheight);
	imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
	}
	$date = date ("YmdHiu");
	$rand = rand (1000, 9999);
	$imgname = $date.$rand.".".base64_decode($_COOKIE[register]).".l.jpg";
	$filename = "../../../../engine/uploads/".$imgname;
	imagejpeg($tmp,$filename,100);
	
	cropImage(100, 100, "../../../../engine/uploads/".$imgname, "jpg", "../../../../engine/uploads/".$date.$rand.".".base64_decode($_COOKIE[register]).".s.jpg");
	
	imagedestroy($src);
	imagedestroy($tmp); 
	header ("location: image.php");
	
}else{?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Insert/edit image</title>
<link href="css/advimage.css" rel="stylesheet" type="text/css" />
</head>
<body id="advimage" >
    <div id="errors" class="panel">
        
        	<?php if($e1 == "error"){ ?>
            <fieldset>
            <legend>Empty field!</legend>
            </fieldset>
            <?php } ?>
            <?php if($e2 == "error"){ ?>
            <fieldset>
            <legend>Non JPG file!</legend>
            </fieldset>
            <?php } ?>
            <?php if($e3 == "error"){ ?>
            <fieldset>
            <legend>Oversized file!</legend>
            </fieldset>
            <?php } ?>
             <?php if($e4 == "error"){ ?>
             <fieldset>
            <legend>Error uploading file!</legend>
            </fieldset>
            <?php } ?>
            <a href="image.php">go back and try again</a>
        
    </div>
</body>
</html>
<?php } ?>