<?php
/*****************************************************************
**																**
**	   Directory-Picture-Thumbnail-Script						**
**																**
**	   Scripted in PHP 5/XHTML 1.0 Transitional					**
**	   (c) 2007 by Nico Ritschel and Jannis Schnitzer			**
**																**
**	   Generates a listing of all pictures in a directory		**
**	   Original name: "showpic.php"								**
**	   v. 2.1 final		Last change: 2007/09/06					**
**																**
*****************************************************************/

/*
 This document is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This document is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this document.  If not, see [http://www.gnu.org/licenses/].
*/

/* TODO:
 *
 * [X] Make resize function work (though I suspect it's because of my Apple Apache).
 */

/* CHANGELOG
* before v2.0:
** [X] Don't know, ask Nico.
* v2.0:
** [X] Having a Range function to show only a specific range of pictures.
** [X] Having a array-reverse-function to show the newest picture on top.
** [X] Having a style change to show the newest picture bigger than the others.
* v2.1:
** [X] Having better parameter control and management.
** [X] Having Show-Single function.
** [X] Did several enhancements.
* v2.2:
** [ ] Creating "nice-looking" HTML.
** [ ] Using Cookie.
*/

$settings = array ();

function print_link ($nr, $count, $all, $actualcount)
{
	if ($count >= $all)
		$nr = 0;
	
	if ($count == $actualcount)
		echo $actualcount." ";
	else /* blubb */
		echo "<a href=\"?start=".$nr."&amp;count=".$count."\">".$count."</a> ";
}

function print_numberslist ($piclist, $count, $start)
{
	echo "<div class=\"numberslist\">\n";
	// The numbers list.
	$piccount = count ($piclist);
	$nr_of_links = $piccount / $count; // $count is the number of pictures to show.
	if ($piccount % $count)
		$nr_of_links ++;
	
	echo "Page ";
	
	for ($i = 1; $i <= $nr_of_links; $i++)
	{
		if ($start / $count == $i - 1)
			echo $i." ";
		else /* blo */
			echo "<a href=\"?start=".($i - 1) * $count."&amp;count=".$count."\">".$i."</a> ";
	}
	
	echo "<br />\n";
	
	echo "Show ";
	if ($count < 10) echo $count." ";
	print_link ($start, 10, $piccount, $count);
	if ($count > 10 && $count < 20) echo $count." ";
	print_link ($start, 20, $piccount, $count);
	if ($count > 20 && $count < 50) echo $count." ";
	print_link ($start, 50, $piccount, $count);
	if ($count > 50 && $count < 100) echo $count." ";
	print_link ($start, 100,$piccount, $count);
	if ($count > 100) echo $count." ";
	echo "items.<br />\n";
	echo "</div>\n";
}


// Configuration options:



// Path to list:
$settings['picdir'] = "./";
// Pictures in a row:
$settings['rowcount'] = 3;
$settings['w'] = 250;
$settings['h'] = 250;
$settings['title'] = "Screenshots.";
$settings['start'] = 0;
$settings['count'] = 10;
// Possible settings of showname: 0 = only show the picture; 1 = show the name of the picture;
//	2 = show the name and the extension of the picture; 3 = show the full (relative) path of the picture;
$settings['showname'] = 1;
$settings['border'] = 0;
$settings['stylesheet'] = "http://itisme.org/Screenshots/style.css";
$settings['shownewest'] = true;
$settings['onlynewest'] = false;
$settings['onlylatest'] = false;

$settings = array_merge ($settings, $_GET);

$settings['stylesheet'] = htmlspecialchars ($settings['stylesheet']);

if (!is_int (intval (intval ($settings['start']))))
	$settings['start'] = 0;
if (!is_int (intval ($settings['count'])))
	$settings['count'] = 10;
if (!is_int (intval ($settings['w'])))
	$settings['w'] = 250;
if (!is_int (intval ($settings['h'])))
	$settings['h'] = 250;
if (!is_int (intval ($settings['rowcount'])))
	$settings['rowcount'] = 3;
if (!is_int (intval ($settings['border'])))
	$settings['border'] = 0;
if (!is_int (intval ($settings['shownewest'])))
	$settings['shownewest'] = true;
if (!is_int (intval ($settings['onlynewest'])) || !is_bool ($settings['onlylatest']))
{
	$settings['onlynewest'] = false;
	$settings['onlylatest'] = false;
}

if (!isset ($settings['showdir']))
	$settings['showdir'] = $settings['picdir'];

if ($settings['onlynewest'] || $settings['onlylatest'])
{
	if (!isset ($_GET['w']))
		unset ($settings['w']);
	if (!isset ($_GET['h']))
		unset ($settings['h']);
}

if ($settings['count'] == 0)
	$settings['count'] = 10;

if (isset($settings['pic'])) {
  error_reporting(1);
  $file = fopen($settings['pic'], "r");
  error_reporting(7);
  if ($file) {
	if ($oldsize = getimagesize($_GET['pic'])) {
	  if ($oldsize[0] <= $settings['w'] AND $oldsize[1] <= $settings['h'] ) {
	$newwidth = $oldsize[0];
	$newheight = $oldsize[1];
	  }
	  else {
	if ($oldsize[0] > $oldsize[1]) {
	  $newwidth = $settings['w'];
	  $newheight = $oldsize[1] / $oldsize[0] * $settings['w'];
	}
	else {
	  $newwidth= $oldsize[0] / $oldsize[1] * $settings['h'];
	  $newheight = $settings['h'];
	}
	  }
	  if ($oldsize[2] == 1) {
	header("Content-type: image/gif");
	$oldpic = imagecreatefromgif($settings['pic']); 
	$newpic = imagecreate($newwidth, $newheight);
	imagecopyresized($newpic,$oldpic,0,0,0,0,$newwidth,$newheight,$oldsize[0],$oldsize[1]);
	imagegif($newpic);
	  }
	  
	  if ($oldsize[2] == 2) {
	header("Content-type: image/jpeg");
	$oldpic = imagecreatefromjpeg($settings['pic']); 
	$newpic = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($newpic,$oldpic,0,0,0,0,$newwidth,$newheight,$oldsize[0],$oldsize[1]);
	imagejpeg($newpic);
	  }
	  
	  if ($oldsize[2] == 3) {
	header("Content-type: image/png");
	$oldpic = imagecreatefrompng ($settings['pic']); 
	$newpic = imagecreatetruecolor($newwidth, $newheight);
	imagecopyresized($newpic,$oldpic,0,0,0,0,$newwidth,$newheight,$oldsize[0],$oldsize[1]);
	imagepng($newpic);
	  }
	}
  }
 }
 else /* No pic to resize */
 {
 	echo "<!--\n"; print_r ($settings); echo "\n-->\n";
 
 	 $another_c = 0;
	 $fh = opendir($settings['picdir']);
	 $piclist = array();
	 while ($file = readdir($fh))
	   {
	 if ((substr (strtolower ($file), -3) == "jpg") or
		 (substr (strtolower ($file), -3) == "gif") or
		 (substr (strtolower ($file), -3) == "png"))
	   {		
			$piclist[$another_c] = $file;
		   	$another_c++;
		}
	}
	
	$piclist2 = array ();
	$temp_array = array ();
	
	foreach ($piclist as $theFile)
	{
		$t = filemtime ($theFile)."$theFile";
		$piclist2[$t] = $theFile;
		$temp_array[] = $t;
	}
	sort ($temp_array);

	$temp_s = count ($temp_array);
	for ($i = 0; $i < $temp_s; $i++)
		$piclist3[$i] = $piclist2[$temp_array[$i]];
	
	$piclist = array_reverse ($piclist3);
	// comment
	unset ($piclist2);
	unset ($piclist3);
	unset ($temp_array);
	unset ($temp_s);
	
	echo "<!--\n";
	print_r ($piclist);
	echo "-->\n";

	?>
		
		<!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-Script-Type" content="text/javascript" />
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		
		<?php
		if ($settings['stylesheet'] != "")
		{
			?>
			<link href="<? echo $settings['stylesheet'] ?>" rel="stylesheet" type="text/css" />
			<?
		}
	?>
		
		<title>
		<?php
		if (isset ($settings['showbig']))
		{
			switch ($settings['showname'])
			{
				case 2:
					$name = (string) $settings['showbig'];
					break;
				case 3:
					$name = (string) $settings['showdir'].$settings['showbig'];
					break;
				default: // is case 1
					$name = (string) substr($settings['showbig'], 0, -4);
					break;
			}
			echo $name;
		}
	else if ($settings['onlylatest'])
	{
		switch ($settings['showname'])
		{
			case 2:
				$name = (string) $piclist[0];
				break;
			case 3:
				$name = (string) $settings['showdir'].$piclist[0];
				break;
			default: // is case 1
				$name = (string) substr($piclist[0], 0, -4);
				break;
		}
		echo $name;		
	}
	else
	{
		echo $settings['title'];
	}
?>
</title>
</head>
<body>
<?php
		
		
echo "<!--\n";
print_r ($piclist3);
echo "\n";
echo "-->\n";
	
	if ($settings['onlynewest'] || $settings['onlylatest'])
	{
		switch ($settings['showname'])
		{
		case 2:
			$name = (string) $piclist[0];
			break;
		case 3:
			$name = (string) $settings['picdir'].$piclist[0];
			break;
		default: // is case 1
			$name = (string) substr($piclist[0], 0, -4);
			break;
		}
		
		echo "<h1>".$name."</h1>\n<hr noshade='noshade' />\n";
		if (isset ($settings['w']) && isset ($settings['h']))
		{	echo "<img border='0' src='http://s.itisme.org/?pic=".$settings['showdir'].$piclist[$settings['start']]."&amp;w=".$settings['w']."&amp;h=".$settings['h']."' />\n";
		}
		else
			echo "<img border='0' src='".$settings['showdir'].$piclist[$settings['start']]."' />\n";
			
	}
	else /* usual list */
	{
		print_numberslist ($piclist, $settings['count'], $settings['start']);
		echo "<hr noshade=\"noshade\" />\n";
		
		$i = $settings['start'];
		while ($i < count($piclist) && $i < $settings['start']+$settings['count'])
		{
			if ($i == $settings['start'] && $settings['shownewest'])
			{
				switch ($settings['showname'])
				{
					case 2:
						$name = (string) $piclist[$i];
						break;
					case 3:
						$name = (string) $settings['showdir'].$piclist[$i];
						break;
					default: // is case 1
						$name = (string) substr($piclist[$i], 0, -4);
						break;
				}
			
				echo "<div class=\"topmost\">\n";
				// The first picture. It has to be bigger and such.
				echo "<a href=\"".$settings['picdir'].$piclist[$i]."\">\n";
				echo "<h1>".$name."<br />\n";
				echo "<img border=\"0\" src=\"?pic=".
						$settings['picdir'].$piclist[$i]."&amp;w=800&amp;h=600\" />\n";
				echo "</h1>\n";
				echo "</a>\n";
				echo "</div>\n";
				echo "<hr noshade=\"noshade\" />\n";
				$i++;
				
				echo "<table cellpadding=\"40\" border=\"".$settings['border']."\">\n";
			}
			else /* not the first one */
			{
				if ($i == $settings['start'])
					echo "<table cellpadding=\"40\" border=\"".$settings['border']."\">\n";
				echo "\t<tr>\n";
				$j = 0;
				
				while ($j < $settings['rowcount'])
				{
					if ($i < count($piclist) && $i < $settings['start']+$settings['count']) {
						switch ($settings['showname'])
						{
							case 2:
								$name = (string) $piclist[$i];
								break;
							case 3:
								$name = (string) $settings['showdir'].$piclist[$i];
								break;
							default: // is case 1
								$name = (string) substr($piclist[$i], 0, -4);
								break;
						}
						
						echo "\t\t<td valign='top' >\n";
						echo "\t\t\t<a href='".$settings['showdir'].$piclist[$i]."'>\n";
						echo "\t\t\t\t".$name."\n";
						echo "\t\t\t\t<br />\n";
						echo "\t\t\t\t<img border='0' src='?pic=".$settings['showdir'].$piclist[$i]."' />\n";
						echo "\t\t\t</a>\n";
						echo "\t\t</td>\n";
						$i++;
					}
					$j++;
					
				}
				echo "\t</tr>\n";
			}
		}
		?> 
</table>
		  <?php 
echo "<hr noshade=\"noshade\" />\n";
		print_numberslist ($piclist, $settings['count'], $settings['start']);
	}
 }
?>
</body>
</html>
