<?php

//$main_dir = "C:\\Documents and Settings\\LucasTDS\\Meus documentos";
$main_dir = getcwd();
$gal_name = "Pictures";
$ext = array("jpg", "jpeg", "png", "gif");
$tbl_cols = 4;

// Tamanhos das miniaturas
$max_w = 140;
$max_h = 105;

// Formato dos links
$link_gal = "index.php?d=%s";
$link_img = "index.php?op=img&f=%s";
$link_tn = "index.php?op=tn&f=%s";


$op = (isset($_GET['op'])) ? $_GET['op'] : "";

switch ($op) {

case "tn":

	$img_src = $main_dir."/".base64_decode($_GET['f']);
	
	$img_ext = strtolower(pathinfo($img_src, PATHINFO_EXTENSION));
	
	header("Content-type: image/jpeg");
	
	// Obtém dados do arquivo para gerar miniatura
	list($larg, $alt) = getimagesize($img_src);
	
	// Obtém o tamanho da imagem reduzida
	if ($alt >= $larg || ($alt * ($max_w / $max_h)) > $larg) {
		$n_alt = $max_h;
		$n_larg = $max_h * ($larg / $alt);
	} else {
		$n_larg = $max_w;
		$n_alt = $max_w * ($alt / $larg);
	}
	
	// Cria os recursos de imagem
	switch($img_ext) {
	case "gif":
		$im1 = imagecreatefromgif($img_src);
		break;
	case "png":
		$im1 = imagecreatefrompng($img_src);
		break;
	default:
		$im1 = imagecreatefromjpeg($img_src);
		break;
	}
	$im2 = imagecreatetruecolor($n_larg, $n_alt);
	$bg = imagecolorallocate($im2, 255, 255, 255);
	imagefill($im2, 0, 0, $bg);
	
	// Redimensiona a foto original
	imagecopyresampled($im2, $im1, 0, 0, 0, 0, $n_larg, $n_alt, $larg, $alt);
	
	// Salva a miniatura no diretório adequado
	imagejpeg($im2, "", 80);
	
	// Fecha os recursos de imagem
	imagedestroy($im1);
	imagedestroy($im2);

	break;



case "img":

	$img = $main_dir."/".base64_decode($_GET['f']);
	header("Content-type: image/jpeg");
	readfile($img);
	
	break;



default:

?>
<html>

<head>
<title>Galeria</title>
<style type="text/css">
body, table, td, input, textarea, option, select {font-family: Verdana, Bitstream Vera Sans, Arial, Helvetica, sans-serif; font-size: 11px;}
td {vertical-align: top;}
body {margin: 12px;}
img {border: 0;}

.tabela {width: 100%;}
.tabela td {padding: 3px; vertical-align: middle; text-align: center;}

.titulo {background-color: #666666; padding: 4px; color: #FFFFFF; font-weight: bold; margin-bottom: 2px;}
.conteudo {background-color: #EEEEEE; padding: 4px;}

.fora {padding: 2px; border: 1px solid #666666;}

a:link, a:visited, a:active {text-decoration: none; color: #003366;}
a:hover {text-decoration: underline; color: #003366;}

#dhtmltooltip {
position: absolute;
width: 300px;
border: 1px solid black;
padding: 3px;
background-color: #DDDDDD;
visibility: hidden;
z-index: 100;
/*Remove below line to remove shadow. Below line should always appear last within this CSS*/
filter: progid:DXImageTransform.Microsoft.Shadow(color=black,direction=135);
}

.tipinfo {
margin-top: 3px;
padding: 2px;
border: 1px solid #999999;
background-color: #FFFFFF;
}

</style>

</head>

<body>

<div id="dhtmltooltip"></div>

<script type="text/javascript">

/***********************************************
* Cool DHTML tooltip script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetxpoint=0 //Customize x offset of tooltip
var offsetypoint=20 //Customize y offset of tooltip
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thecolor, thewidth){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20

var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth)
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=ie? ietruebody().scrollLeft+event.clientX-tipobj.offsetWidth+"px" : window.pageXOffset+e.clientX-tipobj.offsetWidth+"px"
else if (curX<leftedge)
tipobj.style.left="5px"
else
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetxpoint+"px"

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight)
tipobj.style.top=ie? ietruebody().scrollTop+event.clientY-tipobj.offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-tipobj.offsetHeight-offsetypoint+"px"
else
tipobj.style.top=curY+offsetypoint+"px"
tipobj.style.visibility="visible"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip

</script>

<?php

function info ($img_path, $img_name) {
	global $main_dir;
	$img = $main_dir."/".$img_path;
	//onmouseover=\"return overlib('E-mail válido para contato.',CAPTION,'Imagem');\" onmouseout=\"nd();\"
	//onMouseover=\"fixedtooltip('Comprehensive JavaScript tutorials and over 400+ <b>free</b> scripts.', this, event, '150px')" onMouseout="delayhidetip()"
	$str_info = "<b>".$img_name."</b>";
	$str_info .= "<div class=&quot;tipinfo&quot;>";
	$str_info .= "<b>Data:</b> ".date("d/m/Y H:i:s", filemtime($img));
	if (function_exists("exif_read_data")) {
		$exif = @exif_read_data($img, 0, true);
		if (isset($exif["WINXP"]["Title"])) {
			$str_info .= "<br><b>Título:</b> ".htmlentities(htmlentities($exif["WINXP"]["Title"]));
		}
		if (isset($exif["WINXP"]["Comments"])) {
			$str_info .= "<br><b>Comentários:</b> ".htmlentities(htmlentities($exif["WINXP"]["Comments"]));
		}
		if (isset($exif["WINXP"]["Author"])) {
			$str_info .= "<br><b>Autor:</b> ".htmlentities(htmlentities($exif["WINXP"]["Author"]));
		}
		if (isset($exif["WINXP"]["Keywords"])) {
			$str_info .= "<br><b>Palavras-chave:</b> ".htmlentities(htmlentities($exif["WINXP"]["Keywords"]));
		}
		if (isset($exif["WINXP"]["Subject"])) {
			$str_info .= "<br><b>Assunto:</b> ".htmlentities(htmlentities($exif["WINXP"]["Subject"]));
		}
	}
	$str_info .= "<div>";
	return " onmouseover=\"ddrivetip('$str_info');\" onmouseout=\"hideddrivetip();\" ";
}

// Novo subdiretório
$sub_dir = isset($_GET['d']) ? base64_decode($_GET['d']) : "";

$curr_dir = $sub_dir ? $sub_dir."/" : "";

$dh = opendir($main_dir."/".$curr_dir);

$img_list = array();
$dir_list = array();

// Gera lista de diretorios e imagens
while (false !== ($f = readdir($dh))) {
	$fp = $main_dir."/".$curr_dir.$f;
	if (in_array(strtolower(pathinfo($fp, PATHINFO_EXTENSION)), $ext)) {
		$img_list[] = $f;
	}
	if (filetype($fp) == "dir" && $f != "." && $f != "..") {
		$dir_list[] = $f;
	}
}

sort($img_list);
// Total de imagens
$total_imgs = count($img_list);

echo "<div class=\"fora\"><div class=\"conteudo\">";

echo "<a href=\"".sprintf($link_gal, "")."\"><b>$gal_name</b></a>";

$dir_array = explode("/", $sub_dir);
$dir_link = "";

$dir_array_total = count($dir_array);
if ($dir_array_total > 0) {
	for ($i = 0; $i < ($dir_array_total - 1); $i++) {
		$dir_link = $dir_link ? $dir_link."/".$dir_array[$i] : $dir_array[$i];
		$dir_link64 = base64_encode($dir_link);
		echo " / <a href=\"".sprintf($link_gal, $dir_link64)."\">$dir_array[$i]</a>";
	}
	
	echo " / $dir_array[$i]";
}
/**/
echo "</div></div><br>";

// Mostra lista de diretorios
echo "<div class=\"fora\"><div class=\"titulo\">Sub-pastas</div>\n";
echo "<div class=\"conteudo\">";
foreach ($dir_list as $d) {
	$dir64 = base64_encode($curr_dir.$d);
	echo "<a href=\"".sprintf($link_gal, $dir64)."\">$d</a><br>\n";
}
$back64 = base64_encode($dir_link);
echo "<a href=\"".sprintf($link_gal, $back64)."\">(voltar)</a>\n";
echo "</div></div><br>";
/**/

//calculando a porcentagem
$td_w = floor(100 / $tbl_cols);

// Mostra tabela de imagens
echo "<div class=\"fora\"><div class=\"titulo\">Imagens</div>\n";
echo "<div class=\"conteudo\"><table cellspacing=\"0\" class=\"tabela\">";


$k = 1;
foreach ($img_list as $d) {
	$img_info = info($curr_dir.$d, $d);
	$img64 = base64_encode($curr_dir.$d);
	if ($k == 1) {
		echo "<tr>\n";
	}
	echo "  <td width=\"$td_w%\"><a href=\"".sprintf($link_img, $img64)."\"><img src=\"".sprintf($link_tn, $img64)."\" $img_info></a><br></td>\n";
	if ($k == $tbl_cols) {
		echo "</tr>\n";
		$k = 1;
	} else {
		$k++;
	}
}

while ($k > 1 && $k <= $tbl_cols) {
	echo "  <td>&nbsp;</td>\n";
	if ($k == $tbl_cols) {
		echo "</tr>\n";
		$k = 1;
	} else {
		$k++;
	}
}
echo "</table></div></div>\n";

?>

</body>

</html>

<?php

break;
}

?>