<?
/*-----------------------------------------------------------------------------
Script Name: showimages
Author:      Trevor Staats © 2004 All Rights Reserved
Purpose:     Build a table of thumbnails and captions to display a photo album.
History:
-----------------------------------------------------------------------------*/
if ( isset( $_SERVER[ 'QUERY_STRING' ] ) ) parse_str( $_SERVER[ 'QUERY_STRING' ] );

function DisplayImages( $Folder )
{
	require 'admin/admin.php';	// Gets the header and footer info.
	require_once('thumb.php');	
	$TableFont = GetTableFont();
	$TextFont = GetTextFont();
	$HeaderFont = GetHeaderFont();
	$CaptionFileName = GetCaptionFileName();
	$FolderName = $Folder;

	
	if ( !is_dir( $FolderName ) )
	{
		printf( "Album is not available at present - please try again later" );
		exit;
	}
	
	$FileFolder = explode( "/", $FolderName );
	// see if we need to create thumbnails first
	if ( !file_exists( $FolderName."/"."files.csv" ) )
	{
		// Create the thumbnail files and reference file 
		$Dir = opendir( $FolderName );
		$Extension = "";
		while( $FileName = readdir( $Dir ) )
			$FileList[] = $FileName;
		closedir( $Dir );
		for ($i = 0; $i < count( $FileList ); $i++ )
		{
			$FileName = $FileList[ $i ];
			if ( strstr( $FileName, "." ) )
			{
				$Extension = explode( ".", $FileName );
				if ( ( $Extension[1] == "jpg" or
				$Extension[1] == "gif" or
				$Extension[1] == "png") and
				( strncmp( $Extension[ 0 ], "th-", 3 ) ) )
				{
					// rename the sucker first
					$OldName = $FolderName."/".$FileName;
					$NewName = strtolower( str_replace(" ", "", $FileName) ); 
					rename( $OldName, $FolderName."/".$NewName );		
					// add it to the captions list
					$FilesCSV[] = $NewName.",".$Extension[0];
					// create the thumbnail
					create_thumb_wfixed( $FolderName."/".$NewName, $FolderName."/".'th-'.$NewName, 100, 70 );
				}
			}
		}
		$fp = fopen( $FolderName."/"."files.csv", "w" );
		for ( $i = 0; $i < count( $FilesCSV ); $i++ )
		{
			fputs( $fp, $FilesCSV[ $i ]."\r\n", strlen($FilesCSV[ $i ])+2 );
		} 
		fclose( $fp );
	}
	unset( $FileList );
	
	// get a list of eligible files in the folder...
	$Dir = opendir( $FolderName );
	$Extension = "";
	while( $FileName = readdir( $Dir ) )
	{
		if ( strstr( $FileName, "." ) )
		{
			$Extension = explode( ".", $FileName );
			if ( ( $Extension[1] == "jpg" or
			$Extension[1] == "gif" or
			$Extension[1] == "png") and
			( !strncmp( $Extension[ 0 ], "th-", 3 ) ) )
			{
				// Only add on graphics files that start with th- (the thumbnails))
				$FileList[] = $FileName;
			}
		}
	}
	closedir( $Dir );
	sort( $FileList );
	
	// Include any html header data that is required.
	$HeaderName = GetHeaderName();
	if ( !readfile( $HeaderName ) );
	{
		print( "<html>\n<body>\n" );
	}
	
	// Is there any descriptive text to go in?  It is in <foldername>.txt
	if ( file_exists( $FolderName."/".$FileFolder[ 1 ].".txt" ) )
	{
		$DescFile = fopen( $FolderName."/".$FileFolder[ 1 ].".txt", "r" );
		if ( $DescFile )
		{
			$buf = fgets ($DescFile, 1000 );
			if ( $buf != "" )
			printf( "<p align=\"center\"><b>%s%s</font></b></p>%s", $HeaderFont, $buf, NewLine() );
			
			while( $buf = fgets ($DescFile, 1000 ) )
			{
				printf( "<p>%s%s</font></p>%s", $TextFont, $buf, NewLine() );
			}
		}
		print( "<hr>" );
	}
	
	// There may also be individual file descriptions.
	// Load up an array of these
	if ( file_exists( $FolderName."/". $CaptionFileName ) )
	{
		$CaptionFile = fopen( $FolderName."/". $CaptionFileName, "r" );
		if ( $CaptionFile )
		{
			while ($buf = fgetcsv ($CaptionFile, 1000, ",") )
			{
				$Captions[ $buf[ 0 ] ] = $buf[ 1 ];
			}
		}
	}
	
	// cycle through all the files and build the html table
	$Count = count( $FileList );
	print( "<table width=\"100%\" Border=\"2\">\r\n" );
	print( "<tr>\r\n" );
	for ( $i = 0; $i < $Count; $i++ )
	{
		if ( $i % 3 == 0 )
		{
			print( "</tr>\r\n<tr>");
		}
		$File = $FileList[ $i ];
		$BigFile = substr ($File, 3 );
		
		if ( isset( $Captions ) && $Captions[ $BigFile ] !== NULL )
		$Caption = $Captions[ $BigFile ];
		else
		$Caption = $BigFile;
		
		//    printf( "<td><a href=\"%s%s%s\">$TableFont<img src=\"%s%s%s\" align=Right> %s</a></td>",
		//	        $FolderName, "/", $BigFile,
		//			$FolderName, "/", $File,
		//			$Caption );
		printf( "<td><a href=\"%s\">$TableFont<img src=\"%s%s%s\" align=Right> %s</a></td>",
		"showsingleimage.php?FolderName=$FolderName&Index=$BigFile",
		$FolderName, "/", $File,
		$Caption );
		
	}
	print( "</tr>\r\n" );
	print( "</table>\r\n" );
	
	// Include any html header data that is required.
	if ( !readfile( GetFooterName() ) )
	{
		print( "</body>\n</html>\n" );
	}
}
//$folder = "ausreports/19810000hville";
if ( isset( $folder ) )
  DisplayImages( $folder );
?>
