<?php 
include "include/utils.inc.php";
include "include/images/images.class.php";

//print_rr($_REQUEST);
$old_path = $_REQUEST['img_old'];
$undo =  $_REQUEST['undo'];
$name_save_as = remove_acentos_espacos($_REQUEST['nome_img']);

$dir_old = dirname($old_path);
$img_old = basename($old_path);

$ext = substr($img_old, -4);
$img_name = substr($img_old, 0, -4);

if($undo){
	
	$old_name = $dir_old . "/" . $img_name . "_old" . $ext;
	unlink($old_path);
	rename($old_name, $old_path);
	
	$old_thumb_path = $dir_old . "/thumb/" .$img_name . "_old" . $ext;
	$new_thumb_path = $dir_old . "/thumb/" .$img_old;
	unlink($new_thumb_path);
	rename($old_thumb_path , $new_thumb_path);
	
	echo "Voltou à imagem anterior.<br>";
}else {
	$new_file = file_get_contents($_REQUEST['img']);
	
	//situacao de save as
	if($name_save_as){
		$ext1 = substr($name_save_as, -4);
		$ponto = substr($name_save_as, -4, 1);
		if($ponto == "." || $ext1 == ".jpg" || $ext1 == ".gif" || $ext1 == ".png" || $ext1 == ".bmp"){
			$new_path_name = $dir_old . "/" .$name_save_as;
			$new_path_name_thumb = $dir_old . "/thumb/" . $name_save_as;
			$nome_bd = $name_save_as;
		}else{
			$new_path_name = $dir_old . "/" .$name_save_as . $ext;
			$new_path_name_thumb = $dir_old . "/thumb/" . $name_save_as. $ext;
			$nome_bd = $name_save_as. $ext;
		}
		
		if(file_put_contents($new_path_name, $new_file)){
			$img = new image;
			$img->thumbnail2($new_path_name, $new_path_name_thumb, 100,100);
			
			//meter na BD porque é uma imagem nova
			include_once("db/dbElemento.class.php");
			$db = new dbElemento;
			$sql="select idCat from imagem where imagem='$img_old'";
			$result = mysql_query($sql);
			list($idCat) = mysql_fetch_row($result);
			
			$sql="insert into imagem (idCat, imagem) values ('$idCat', '$nome_bd')";
			$result = mysql_query($sql);
			
			echo "Guardou a nova imagem e o thumbnail. <br>";	
			$_REQUEST['img_old'] = $new_path_name;
		}else{
			echo "Erro ao guardar a imagem.<br>";
		}

	}else {//situaçao de save normal por cima da imagem actual
		
		//mete a imagem antiga com _tmp
		$tmp_name = $dir_old . "/" .$img_name . "_tmp" . $ext;
		rename($old_path, $tmp_name);
		
		//mete o thumb antigo com _tmp
		$old_thumb_path = $dir_old . "/thumb/" .$img_old;
		$old_thumb_path_tmp =  $dir_old . "/thumb/" .$img_name . "_tmp" . $ext;
		rename($old_thumb_path , $old_thumb_path_tmp);
		
		if(file_put_contents($old_path, $new_file)){
			$old_name = $dir_old . "/" . $img_name . "_old" . $ext;
			if(is_file($old_name))
				unlink($old_name);
			rename($tmp_name , $old_name);
			
			$img = new image;
			$img->thumbnail2($old_path, $old_thumb_path, 100,100);
			$old_thumb_path_old = $dir_old . "/thumb/" .$img_name . "_old" . $ext;
			if(is_file($old_thumb_path_old))
				unlink($old_thumb_path_old);
			rename($old_thumb_path_tmp , $old_thumb_path_old);
			
			echo "Guardou a nova imagem e o thumbnail. <br>";
		}else{
			rename($tmp_name , $old_path);
			rename($old_thumb_path_tmp , $old_thumb_path);
			echo "Erro ao guardar a imagem.<br>";
		}
	}
}

?>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

	$('#botao_fechar').click(function () {
		idCat = "<?=$_SESSION['idCat'] ?>";
		offset= "<?=$_SESSION['offset'] ?>";
		opener.$("#main").load("main.php?tipo=imagem&id=" + idCat + "offset="+offset);
		window.close();
	});

});
</script>
<div><img src="<?=$_REQUEST['img_old'] ?>"></div>
<div style="margin-top: 15px"><input id="botao_fechar" type="button" value="Fechar"></div>
