<?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");
}
$imagedir = "../media/thumbs/";
$imagesdir = "../media/original/";
$images = glob($imagedir."*");
if ($_POST[submit]){
	$up = 0;
	$e1 = 0;
	$e2 = 0;
	$e3 = 0;
	$errnote = "";
	for ($i=0;$i<6;$i++){
		if($_FILES['imageup']['name'][$i]){
			if(!preg_match("/\.jpg|.jpeg|.gif|.png\b/i", $_FILES['imageup']['name'][$i])){
				$error = "wrongfile";
			}
			if (filesize($_FILES['imageup']['tmp_name'][$i]) > ((str_replace(" mb max","",maxsize())*1024)*1024)){
				$error = "oversize";
			}
			if (file_exists("../media/images/".basename($_FILES['imageup']['name'][$i]))){
				$error = "fileexists";
			}
			switch ($error) {
				case "wrongfile":
				$e1 ++;
				$errnote .= $_FILES['imageup']['name'][$i]." ".$l[invalidimage].",";
				break;
				case "oversize":
				$e2 ++;
				$errnote .= "File ".$_FILES['imageup']['name'][$i]." ".$l[bigimage].",";
				break;
				case "fileexists":
				$e3 ++;
				$errnote .= $_FILES['imageup']['name'][$i]." ".$l[existsimage].",";
				break;
				
				default:
			$target_path = "../media/original/";
			$target_path = $target_path . basename( $_FILES['imageup']['name'][$i]);
			
			move_uploaded_file($_FILES['imageup']['tmp_name'][$i], $target_path);
			
			cropimage($s40[thumbsize], $s40[thumbsize], $target_path, findext($_FILES['imageup']['name'][$i]), "../media/thumbs/". basename( $_FILES['imageup']['name'][$i]));
		
			
			$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") or preg_match("/.jpeg/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['imageup']['name'][$i],100);
			$up++;
			break;
			}
			
		}
	}
	$er = ($e1 + $e2 + $e3);
	if ($er == 0){
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=608&a=gallery&up=".$up."\";</script>";
	}else{
		echo "<script language=\"javascript\" type=\"text/javascript\">window.location = \"main.php?n=609&a=gallery&up=".$up."&er=".$er."&errnote=".$errnote."\";</script>";
	}
}
?>

<h1><?php echo $l[gallery];?></h1>
<div id="gallery">
<div id="uploads">
<h2><?php echo $l[galleryupload];?></h2>
<form action="<?php echo $_SERVER['PHP_SELF']."?a=".$_GET[a];?>" method="post" enctype="multipart/form-data" name="galleryuploads" class="form" id="galleryuploads" style="text-align:center">
<?php if(isset($_GET[errnote])){
	echo "<span style=\"color:#CC0000\">".str_replace(",","<br />",$_GET[errnote])."</span>";
}
?><br />

<?php echo $l[galleryuploadnote]; echo maxsize();?> <br />
<input name="addimage" type="button" id="addimage" value="<?php echo $l[imagesaddfield];?>" onClick="addInput()" />
<div id="uploadform">
<input name="imageup[]" type="file" /><br />
</div>
<br />
<input name="submit" type="hidden" value="true" /><input name="actions" type="hidden" value="gallery" /><input name="submit" type="submit" class="save" id="submit" value="<?php echo $l[upload];?>" />
</form>
</div>
<div id="galleryitems">
<h2><?php echo $l[galleryimages];?> (<?php echo count($images); ?>)</h2>
<?php
sort ($images);
foreach ($images as $image){
	echo "<div class=\"mediaitem\">";
	echo "<img src=\"".$image."\" width=\"".$s40[thumbsize]."\" height=\"".$s40[thumbsize]."\" border=\"0\" />";
	echo "<div class=\"mediaactions\">";
	echo "<a rel=\"image\" title=\"".str_replace($imagedir,"",$image)."\" href=\"".str_replace($imagedir,$imagesdir,$image)."\" ><img src=\"style/preview.png\" width=\"24\" height=\"24\" border=\"0\"></a> <a href=\"?a=delete&amp;i=".str_replace($imagedir,"",$image)."\" onclick=\"if(! confirm('".$l[deleteconfirm]."')) return false;\"><img src=\"style/delete.png\" width=\"24\" height=\"24\" border=\"0\"></a>";
	echo "</div></div>";
}
?>

<div class="clear"></div>
</div>
</div>