<?php

/*
	Script by Tuomas Airaksinen <email@example.com>
				and Maarten den Braber <email@example.com>

	Ideas from imgconvert.sh by Garrett LeSage <email@example.com>

	Published under GNU General Public Licence (GPL)
	You should get whole license information in COPYING
	file distributed with this file. For more information
	see http://www.gnu.org */

$version = "v1.1.3";

/*
	See http://tuma.stc.cx/gallery.php for more info
*/


require("screens-config.php");

if (! $included )
{
	echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
	echo "<HTML><HEAD><TITLE>Screen shots of runescape</TITLE>\n";
	echo "</HEAD><BODY background=greyback.jpg>\n";
}

if (! $number)
	$number = $defnumber;

if (! $pagenum)
	$pagenum = 1;


/* Some helper functions */

function get_pictinfo($picture)
{
	global $infofile;
	# Read the infofile

	$fp=fopen($infofile,"r");
	$contents=fread($fp, filesize($infofile));
	fclose($fp);
	$start = strpos($contents,$picture) + strlen($picture);
	$tmpstr = substr($contents,$start,strlen($contents));
	$stop = strpos($tmpstr, "\n");
	$pictinfo = substr($contents,$start,$stop);

	return $pictinfo;
}

function num2picture($num)
{
	global $thumbdir;
	$othumbdir = dir($thumbdir);
	for($count=0; $count<=($num+1); $count++)
	{
		$entry=$othumbdir->read();
	}

	$othumbdir->close;
	return $entry;
}

function picture2num($picture)
{
	global $thumbdir;
	$count = 0;

	$othumbdir = dir($thumbdir);

	while($entry=$othumbdir->read())
	{
		$count++;
		if($entry == $picture)
			break;
	}
	$othumbdir->close;
	return $count - 2;
}

/* -- Gallery function ---------------------------------------------------- */

function thumbnails()
{
	global  $number, $pagenum, $languages, $thumbdir, $imgdir, $othumbdir, $oimgdir,
					$thisfile, $help, $backtogallery, $header, $lang, $tablestart,
					$lines_in_row, $pagestr, $included;

	# Define the next and previous pages
	$prev  = $pagenum - 1;
	$next  = $pagenum + 1;

	# Pages start with 0, but we start with 1 so substract 1
//	$pagenum--;

	# Define when a new page starts
	$start = ($pagenum - 1) * $number + 1;
	$stop = $start + $number;

// echo "DEBUG: number: $number, start: $start, stop: $stop\n total: " . ($stop - $start) . "\n";

	# Print language selector
	if (! $included )
	{
		echo "<center><b><font size=5>" . $header[$lang] . " - " . $pagestr[$lang] . " $pagenum</font></b><br>\n";
		echo $help[$lang] . "</center><BR>\n\n";
	}
	$total = 0;
	# Loop through all the images in the directory
	while($entry=$othumbdir->read())
	{
		$total++;

		if(is_file("$thumbdir/$entry"))
		{
			$count = picture2num($entry);
			/*
			echo " DEBUG: count: $count start: $start, stop: $stop, tulos: " .
					($count < $stop && $count >= $start) . "\n";
			*/
			if($count < $stop && $count >= $start)
			{
				$newline = $count - $start;
				$countloop++;

				if ($newline == 0 || $countloop == $lines_in_row)
				{
					$countloop=0;
					if ($newline < ($lines_in_row - 1))
					{
						echo "$tablestart\n";
					}
					else
					{
						echo "</TR><TR>\n";
					}
				}

				echo "<TD valign=top align=center>";
				if(is_file("$imgdir/$entry"))
				{
					echo "<A HREF=\"" . $thisfile . "?action=full&picture=$entry&lang=$lang\">" .
						"<IMG BORDER=0 SRC=\"$thumbdir/$entry\"></A><br>";
					echo "<B>$count. </B><SMALL>" . get_pictinfo($entry) . "</SMALL>";
					echo "<BR></TD>\n";
				}
				else
				{
					echo "<IMG BORDER=0 SRC=\"$thumbdir/$entry\"><br>";
					echo "<B>$count. </B><SMALL>" . get_pictinfo($entry) . "</SMALL>";
					echo "<BR></TD>\n";
				}
				$count++;
			}
		}
	}

	echo "</TD></TR></TABLE>\n";

	$gallery_prev  = "<table border=1><tr><td><input type=button value=&lt;&lt; onclick=\"window.location='$thisfile?action=gallery&pagenum=$prev&number=$number&lang=$lang'\"></td><td>";
	$gallery_next  = "</td><td><input type=button value=&gt;&gt; onclick=\"window.location='$thisfile?action=gallery&pagenum=$next&number=$number&lang=$lang'\"></td></tr></table>";

	$totalpages = ceil(($total - 2) / $number); // -2 = we don't count "." and ".."
	$gallery_pages = 1;

	while ( $totalpages >= $gallery_pages )
	{
		if ($gallery_pages == $pagenum)
			$gallery_number = "$gallery_number$gallery_pages&nbsp;&nbsp;\n";
		else
			$gallery_number = "$gallery_number<A HREF=\"$thisfile?action=gallery&pagenum="
				. "$gallery_pages&number=$number&lang=$lang\">$gallery_pages</A>&nbsp;&nbsp;\n";
		$gallery_pages++;
	}

	if ( $pagenum == 1 )
	{
		$gallery_prev = "<table border=1><tr><td><input type=button value=&lt;&lt; disabled=disabled></td><td>";
	}

	if ( $pagenum == $totalpages )
	{
		$gallery_next = "</td><td><input type=button value=&gt;&gt; disabled=disabled></td></tr></table>";
	}

	echo "\n<P>\n\n";
	echo "$gallery_prev\n&nbsp;&nbsp;&nbsp;\n\n";
	echo "<B>" . $pagestr[$lang] . ":</B>\n\n&nbsp;$gallery_number\n";
	echo "&nbsp;&nbsp;&nbsp;\n$gallery_next\n";

}

/* -- Show full size picture -------------------------------------- */

function fullsize()
{

	global $picture, $infofile, $languages, $thumbdir, $imgdir, $othumbdir,
					$oimgdir, $thisfile, $help, $backtogallery, $lang,
					$tablestart,$number, $included;

	# Find previous and next
	$current = picture2num($picture);

	$count = picture2num("dontfindme.xyz") - 2 ;

	$prev = num2picture($current - 1);
	$next = num2picture($current + 1);


	$pagenum = ceil($current / $number);

	$thumb_prev  = "<table border=1><tr><td><input type=button value=&lt;&lt; onclick=\"window.location='$thisfile?action=full&picture=$prev&lang=$lang'\"></td><td>";
	$thumb_index = "<A HREF=\"$thisfile?action=gallery&number=$number&pagenum=$pagenum&lang=$lang\">"
								 . "<B>" . $backtogallery[$lang] .  "</B></A>";
	$thumb_next  = "</td><td><input type=button value=&gt;&gt; onclick=\"window.location='$thisfile?action=full&picture=$next&lang=$lang'\"></td></tr></table>";

	if ($current == 1)
	{
		$thumb_prev = "<table border=1><tr><td><input type=button value=&lt;&lt; disabled=disabled></td><td>";
	}

	if ($next == "")
	{
		$thumb_next = "</td><td><input type=button value=&gt;&gt; disabled=disabled></td></tr></table>";
	}

	$browsestr = "$thumb_prev $thumb_index $thumb_next";

	echo "<center><P>$browsestr\n";
	echo "<br><font size=5><B>$current.</B> " . get_pictinfo($picture) . "<font><br>";
	echo "<IMG BORDER=1 SRC=\"$imgdir/$picture\"><BR>\n";
	echo "<P>$browsestr\n";

}

/* -- Switches ------------------------------------------------------------ */

function gallery()
{
	global $version, $action, $oimgdir, $othumbdir, $imgdir, $thumbdir;
	$othumbdir	= dir($thumbdir);
	$oimgdir	= dir($imgdir);
	switch($action)
	{
		case "gallery" :
			thumbnails();
			break;
		case "full" :
			fullsize();
			break;
		default :
			thumbnails();
			break;
	}
	echo "<BR><BR><SMALL>Generated by " .
		"<A HREF=\"http://tuma.stc.cx/gallery.php\">photogallery script</A> $version.</SMALL></CENTER>";
	$othumbdir->close();
	$oimgdir->close();
}

if(! $included)
{
	gallery();
	echo "</BODY></HTML>";
}
?>
