#!/usr/local/bin/perl 
# kphoto.cgi - build virtual photoalbums from directories of images
#
# by Kirk Israel
# This script is offered as e-mail-me-ware: if you use it, send 
# e-mail to kisrael@alienbill.com . Enhancements welcome.
#
#
# When placed in a directory (either literally or as a symbolic link)
# makes a clickthrough slideshow for all the 
# *gif, *jpg, *jpeg (case insensitive), with the options of viewing
# all images on a page. Hint: call the symbolic link index.cgi,
# and on many servers it will act as the default "index.html"
# for that directory.
#
# if the file index.txt exists in that directory, it is used 
# as the center section of the title page- this can be HTML
#
# if the file title.txt exists in that directory, its contents 
# (one line of non-HTML text) are used to title the pages,
# and on the title page if index.txt doesn't exist
#
# There is also an oddly implemented preview mode option.
# If the directory full/ exists in the directory, it is 
# assumed that the images in the main directory are thumbnails,
# and that some or all of them have fullsize versions under the 
# same name in the "full" directory (the logic being that, to save
# diskspace, you would want to load all the small images to the 
# server, and then maybe the fullsize version of just a few particularly
# good ones.)  If a fullsize image exists, a "ZOOM" link is created.
# clicking on this brings up the image in full/ .  Clicking on the next 
# image button keeps the user in "zoomed" mode unless they click
# "UNZOOM"
#
# But really, all you need to do is toss a symbolic link to this 
# script in any directory of images, and you're good to go.
#

print "Content-type: text/html\n\n";   
		use Cwd;
chugCGI(*input);



$photocount = 0;
opendir(READ,".");
while($nextfile=readdir(READ)){
    if($nextfile =~ /\.jpg$/i || $nextfile =~ /\.gif$/i || $nextfile =~ /\.jpeg$/i){

	$pixname[$photocount++] = $nextfile;
    }

}
closedir READ;
@sortpix = sort @pixname;

open(READTITLE,"title.txt");
$title = <READTITLE>;
close READTITLE;
$showtitle=$title;
if($showtitle eq "") {
    $showtitle = "K/photo";
}

print<<__EOQ__;
<HEAD><TITLE>$showtitle</TITLE></HEAD><BODY>

__EOQ__

$view = $input{"view"};
$full = $input{"full"};
# if nothing viewed print title page
if($view eq ""){
	open(READFILE,"index.txt");
	while(defined($next=<READFILE>)){
	    $text .= $next;
	}
	close READFILE;
	if($text eq ""){
	    if($title eq ""){

		$dir = getcwd;
		@dirparts = split(/\//,$dir);
		$title = $dirparts[$#dirparts];
	    }
		$text = "<CENTER><FONT SIZE=6>$title</FONT></B></CENTER><BR><BR>";
	}
      
	print $text;
    }


#previous is the one before  next the one after, wrapping around

$prevview = $view - 1;
$nextview = $view + 1;
if($prevview <= 0) {
    $prevview = $photocount;
}
if($nextview > $photocount) {
    $nextview = 1;
}

print<<__EOQ__;
<div align="center"><B>
__EOQ__
    if($view ne ""){
	if($view ne "All"){
	    $viewname =  $sortpix[$view-1];
	}
	print<<__EOQ__;
Viewing <I>$viewname</I>
$view of $photocount
    &nbsp;    &nbsp;    &nbsp;    &nbsp;
<A HREF="index.cgi?view=$prevview&full=$full">&lt;&lt;PREV</A>
    &nbsp; &nbsp;
__EOQ__
} else {
    print "<I>Click NEXT to start with the first image (recommended for slower connections)<BR>Click ALL to see all the photos on one page</I><BR>$photocount Images &nbsp;&nbsp;";
}
print<<__EOQ__;
<A HREF="index.cgi?view=All">[[ALL]]</A>
    &nbsp; &nbsp;
<A HREF="index.cgi?view=$nextview&full=$full">NEXT&gt;&gt;</A>

<BR>
__EOQ__





if($view eq "All"){
    for($i = 0; $i < $photocount; $i++){
	$j = $i+1;
	
	print<<__EOQ__;
	<A HREF="index.cgi?view=$j&full=on"><IMG BORDER=0 SRC="$sortpix[$i]"></A><br>$sortpix[$i]<P>
__EOQ__
}

} else {

    if($view ne ""){
	if($view eq "start") {$view = 1;}
	if($tryfull ne ""){
	    
	}

	if( -e "./full/".$sortpix[$view-1]){
	    $fullhere = "on";
	} else {$fullhere = "";}

	if($full eq "on" && $fullhere eq "on") {
		$imgsrc = "full/".$sortpix[$view-1];
		$fullused = "on";
	    }  else {
		$imgsrc = $sortpix[$view-1];
		$fullused="";
	    }
	$imgsrc =~ s/ /\%20/g;
	print"<A HREF=\"index.cgi?view=$nextview&full=$full\"><IMG SRC=\"$imgsrc\" BORDER=0></A>\n";

	if($fullused eq "on") {
	    print <<__EOQ__;
	    <BR><A HREF = "index.cgi?view=$view">UNZOOM</A>
__EOQ__
	} else {
	    if($fullhere eq "on") {
		print <<__EOQ__;
	    <BR><A HREF = "index.cgi?view=$view&full=on">ZOOM</A>
__EOQ__
	    }

	}


    }

}
    print<<__EOQ__;
<BR><FORM action="index.cgi">
Jump To: <SELECT NAME="view">
__EOQ__
    for($i = 1; $i <= $photocount;$i++){
	$selected = "";
	if($i eq $view) {
	    $selected = " SELECTED ";
	}
	print "<OPTION $selected VALUE=$i>$i ".$sortpix[$i-1]."\n";
    }
    print<<__EOQ__;
</SELECT>
<INPUT TYPE="SUBMIT" VALUE="GO">

__EOQ__

    open(FOOTER,"footer.html");
while(defined($footer = <FOOTER>)){
    print $footer;
}
close FOOTER;
print "</FORM>";


sub chugCGI {
  local (*in) = @_ if @_;
  local ($i, $key, $val);
  # Read in text
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }
  @in = split(/&/,$in);
  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;
    # Split into key and value.
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;
    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }
  return length($in);
}                
