<?PHP
	session_start();
	require('../../db.php');

	define("FILE", $_GET[file_name]);
	define("THUMBDIR", "thumbs/");
	define("THUMB_Y", 150);

	$supported_files = file('supported_files');

	// make sure the table needed for this plugin exists.
	$query = "create table if not exists images (id int auto_increment primary key, filepath varchar(255), checksum varchar(32))";
	mysql_query($query);
//	if(!mysql_query($query))
//	{
//		
//		echo "we got this: " . mysql_query($query) . "<br>\n";
//		die("Required table not found in database, check user restrictions for db user.");
//	}

	function sort_dirs_first($contents)
	{
		$dirs = array();
		$files = array();
		$n = 0;
		foreach($contents as $file)
		{
			if(is_dir($_SESSION[current_local_dir]."/".$file))
				$dirs[$n++] = $file;
			else
				$files[$n++] = $file;
		}

		return $dirs + $files;
	}

	function show_file($object)
	{
		if(is_dir($object))
		{
			$contents = sort_dirs_first(scandir($object));
			echo "<table border=0 cellpadding=0 cellspacing=0 width=650>\n<tr>\n<td width=650>\n";

			foreach($contents as $item)
			{
				// check that we are dealing with an image and if so:
				// create an image object for the file and check if there allready is a thumbnail, if not, create one.
				$fullpathitem = $_SESSION[current_local_dir]."/".$item;
				$img_type = @exif_imagetype($fullpathitem);
				if($img_type)
				{
					$image_checksum = md5($item . filesize($_SESSION[current_local_dir]."/".$item));
					$thumbfile = $image_checksum . ".jpg";
					if(!is_file(THUMBDIR . $thumbfile))
					{
						if($img_type == IMAGETYPE_JPEG)
						{
							$img_src = imagecreatefromjpeg($fullpathitem);
						}
						elseif($img_type == IMAGETYPE_GIF)
						{
							$img_src = imagecreatefromgif($fullpathitem);
						}
						elseif($img_type == IMAGETYPE_PNG)
						{
							$img_src = imagecreatefrompng($fullpathitem);
						}

						$dimensions = getimagesize($fullpathitem);
						$x_size = floor((THUMB_Y / $dimensions[1]) * $dimensions[0]);
						$thumb_img = imagecreatetruecolor($x_size, THUMB_Y);

						if(imagecopyresampled($thumb_img, $img_src, 0, 0, 0, 0, $x_size, THUMB_Y, $dimensions[0], $dimensions[1]))
						{
							if(imagejpeg($thumb_img, THUMBDIR . $thumbfile, 75))
							{
								// Insert data into the database
								$query ="insert into images values('','$fullpathitem','$image_checksum')";
								mysql_query($query);
							}
						}
					}
					if(is_file(THUMBDIR . $thumbfile))
					{
						echo "<a href=\".?file_name=$item\"><img src=\"" . THUMBDIR . $thumbfile . "\" border=0 vspace=5 hspace=5></a>";
					}
				}
			}
			echo "</td>\n</tr>\n</table>\n";
		}
		else
		{
			// Select data about the file from the database based on the checksum in the filename
			// Show the picture by itself with the possibility to write and read comments attached to the picture
			echo "object is: $object and filesize is: " . filesize($_SESSION[current_local_dir]."/".$object) . "<br>\n";
			$image_checksum = md5($object . filesize($_SESSION[current_local_dir]."/".$object));
			$query = "select filepath from images where checksum = '$image_checksum'";
			echo "checksum for this image is: $image_checksum<br>\n";
			$result = mysql_query($query);
			$line = mysql_fetch_array($result);
			if($line[filepath] != "")
			{
				$line[filepath] = substr($line[filepath], 14);
				echo "<img src=\"$line[filepath]\">";
			}
			else
				echo "no path to image found<br>\n";
		}
	}
PHP?>

<html>
<head>
<title></title>

<link rel=stylesheet href=settings.css type=text/css>

</head>

<body>

<table border=0 cellpadding=0 cellspacing=0 width=100%>
<?PHP	show_file(FILE);	PHP?>
</table>

</body>
</html>
