<?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 "../engine/functions.php";
require "../../inc/langs/".$s40[lang].".php";
if (isset($_GET[a]) and $_GET[a] == "delete"){
	if (file_exists("../../media/thumbs/".$_GET[i])){
		unlink ("../../media/thumbs/".$_GET[i]);
		unlink ("../../media/images/".$_GET[i]);
		unlink ("../../media/original/".$_GET[i]);
		header ("location: gallerybrowser.php?f=$_GET[f]&d=$_GET[d]");
	}
}

if($_POST[submit]){
	

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: gallerybrowser.php?f=$_GET[f]&d=$_GET[d]");
	break;

}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $l[admintitle]; ?></title>
<meta name="robots" content="NOFOLLOW,NOINDEX" />
<link href="../style/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#gbrowser .gbitem {
	height: <?php echo $s40[thumbsize];?>px;
	width: <?php echo $s40[thumbsize];?>px;
}
</style>
<script type="text/javascript" src="../libs/tiny_mce/tiny_mce_popup.js"></script>
<script type="text/javascript">
function insert(name){
	var win = tinyMCEPopup.getWin();
	win.document.<?php echo $_GET[d];?>.image<?php echo $_GET[f];?>.value = name;
	tinyMCEPopup.close();
}
</script>
</head>
<body class="popup">
<div id="gbrowser">
<fieldset><legend>Select from uploaded files</legend><?php
$imagedir = "../../media/thumbs/";
$images = glob($imagedir."*");
rsort ($images);
foreach ($images as $image){
	echo "<div class=\"gbitem\">";
	echo "<div class=\"gbactions\">";
	echo "<a href=\"gallerybrowser.php?a=delete&amp;i=".str_replace($imagedir, "", $image)."&amp;f=".$_GET[f]."&amp;d=".$_GET[d]."\" onclick=\"if(! confirm('".$l[deleteconfirm]."')) return false;\"><img src=\"../style/delete.png\" width=\"24\" height=\"24\" border=\"0\" /></a>";
	echo "</div>";
	echo "<a href=\"javascript:void(0);\" onclick=\"insert('".str_replace($imagedir, "", $image)."');\">";
	echo "<img src=\"".$image."\" width=\"".$s40[thumbsize]."\" height=\"".$s40[thumbsize]."\" border=\"0\" />";
	echo "</a>";
	echo "</div>";
}
?></fieldset>
<div class="clear">
<form action="" method="post" enctype="multipart/form-data" id="gbupload"><fieldset><legend>Upload</legend>
  <input name="actions" type="hidden" id="actions" value="upload" />
  <input name="submit" type="hidden" id="submit" value="true" />
<input type="file" name="uploadfile" id="uploadfile"  /><input name="Submit" type="submit" value="Upload" />
<br />
Upload only (.jpg, .gif, .png) files <?php echo maxsize(); ?></fieldset></form>
</div>
</div>
</body>
</html>

