#!/usr/bin/perl print "Content-type: text/html\n\n"; ############### Photoalbum.cgi 2-4-07 ############## $version = "v2-4-07"; @e = split /,+/, $ENV{'QUERY_STRING'}; $title = "Photoalbum.cgi"; $subtitle ="A script by Dave Porter"; $pagetitle = 0; $wanthelp = "No"; $nosort = 0; $sort = 0; $captions = "Captions.txt"; $htmlheader = ""; $htmlfooter = ""; $fontface = "Verdana, Arial, Helvetica, sans-serif"; $fontsize = "3"; $backgroundgif = 'http://www.aquatantravel.com/Photoalbum/Background.jpg'; $random = 0; $size = 250; $scanall = 0; $indent = 2; $targetchapter = 1; $chaptercount = 0; $bgcolor = "#C5CED1"; foreach (@e) { #pick up items from query string if (/(^\w+.txt)/i) { $captions = $1; #txt file present = captions file } elsif (/\bHELP\b/i) { $wanthelp = 1; } elsif (/^(\d+)/) {$targetchapter = $1;} #chapter spec } #This section scans the directory into @results #always need this even for noscan, to pick up capitalization opendir(DIR, "."); #open current directory while (my $i = readdir(DIR)) { if ($i !~ /^\.+/ && $i =~ /\.jpg$/i) { #select jpgs $results[@results] = $i; #append to results } if (uc $i eq uc $captions) {$captions = $i} #adjust case of captions } closedir DIR; print < END_OF_TEXT # Load Captions.txt or $q file supplied on command line. # first scan is for title and subtitles # -e "Captions.txt" tests for existence. $lineinput = 0; $headerclear = 0; $footerclear = 0; $suppresschapters = 0; if (-e $captions) { open (F, $captions) || dienice; #Open captions file while () { ++$lineinput; if (/^\*{1}/) { #pick out asterisks if (/\*(CHAPTER)/i) { ++$chaptercount; trim(s/\*$1//); push @chaptertitle, $_; push @chapterstarts, $lineinput; $footerclear = 0; $headerclear = 0; } elsif ($chaptercount == $targetchapter || $chaptercount < 2) { if (/(SUBTITLE)/i) { trim(s/\*$1//); $subtitle = $_; } elsif (/(PAGETITLE)/i) { trim(s/\*$1//); $pagetitle = $_; } elsif (/(TITLE)/i) { trim(s/\*$1//); $title = $_; } elsif (/(INDENT)/i) { trim(s/\*$1//); $indent = $_; } elsif (/(FONTFACE)/i) { trim(s/\*$1//); $fontface = $_; } elsif (/(BGCOLOR)/i) { trim(s/\*$1//); $bgcolor = $_; } elsif (/(FONTSIZE)/i) { trim(s/\*$1//); $fontsize = $_; } elsif (/(SIZE)/i) { trim(s/\*$1//); $size = $_; } elsif (/(BACKGROUNDGIF)/i) { trim(s/\*$1//); s/\\/\//; $backgroundgif = $_; } elsif (/(HTMLHEADER)/i) { trim(s/\*$1//); if (!$headerclear) { $htmlheader = $_; $headerclear = 1; } else { $htmlheader = $htmlheader . $_; } } elsif (/(HTMLFOOTER)/i) { trim(s/\*$1//); if (!$footerclear) { $htmlfooter = $_; $footerclear = 1; } else { $htmlfooter = $htmlfooter . $_; } } elsif (/SCANALL\b/i) { $scanall = 1; } elsif (/NOSORT\b/i) { $nosort = 1; } elsif (/SORT\b/i) { $sort= 1; } elsif (/RANDOM\b/i) { $random = 1; } elsif (/SUPPRESSCHAPTERS\b/i) { $suppresschapters = 1; } } } } close F; } if (!$pagetitle) {$pagetitle = $title;} print "$pagetitle"; print ""; print ""; print ''; for (1..$indent) { print "
"; } print <

Click on any of the pictures to enlarge them to their original size. Photoalbum.cgi page generator script $version (c) 2007 by David Porter.

END_OF_TEXT for (1..$indent) { print "
"; } print '
'; if (length($subtitle) > 45) { #if subhead kinda long, provide width print ''; } else { #otherwise let width float print '
'; } print <

END_OF_TEXT print "$title
"; print "$subtitle"; print <

END_OF_TEXT for (1..$indent) { print "
"; } print ''; print $htmlheader; if (-e $captions) {} else { print "Warning: Captions file $captions not found...now scanning for jpgs
"; $captions = 0; $scanall = 1; $sort = 1; $nosort = 0; } if ($chaptercount > 1 && !$suppresschapters) { $i = 0; print "

Chapters:"; foreach(@chaptertitle) { ++$i; if ($i != $targetchapter) { print '
', $_, ""; } else { print "
", $_, ""; } } print "

"; } # 2nd scan of Captions.txt or $captions file, to pick up JPG names if (-e $captions) { open (F, $captions) || dienice; #Open captions file $lineinput = 0; if ($chaptercount) { $chapterstartsat = $chapterstarts[$targetchapter-1]; } else { $chapterstartsat = 0; } while () { $line = trim($_); ++$lineinput; if (length $line == 0) {} elsif ($lineinput > $chapterstartsat) { if ($line =~ /\*CHAPTER/i) { last; } #hit chapter boundary elsif ($line =~ /^\*{1}/ ) {} #some other command else { #otherwise it's a picture reference if (($j = index(uc $line, ".JPG")) > -1) {#if jpg in it $s[0] = substr($line, 0, $j+4); $s[1] = substr($line, $j+5); } else { @s = split /\s{1}?/, $line; #else split on space foreach (2..$#s){ $s[1] = $s[1]." ".$s[$_]; } $s[0] = $s[0].".jpg" } foreach (@results) { #adjust case if needed if (uc $_ eq uc $s[0]) { $s[0] = $_; last; } } if (exists $H{$s[0]}) {} else { $H{$s[0]} = $s[1]; push @iterate, $s[0]; } } } } close F; } if ($scanall) { #no caption file--scan only. foreach (@results) { $i = $_; if (exists $H{$_}) {} else { $H{$i} = $i; } } } #sorting occurs here if (length $captions) { if ($sort) { @iterate = sort {$aa = substr($a, 0, index(uc $a, ".JPG")); $bb = substr($b, 0, index(uc $b, ".JPG")); if ($aa =~ /^\d*$/ && $bb =~ /^\d*$/) {return $aa <=> $bb;} elsif ($aa !~ /^\d*$/ && $bb =~ /^\d*$/) {return 1;} elsif ($aa =~ /^\d*$/ && $bb !~ /^\d*$/) {return -1;} else {return lc $aa cmp lc $bb;} } keys %H; } else { # @iterate = keys %H; ########### } } else { if ($nosort) { # @iterate = keys %H; ############ } else { @iterate = sort {$aa = substr($a, 0, index(uc $a, ".JPG")); $bb = substr($b, 0, index(uc $b, ".JPG")); if ($aa =~ /^\d*$/ && $bb =~ /^\d*$/) {return $aa <=> $bb;} elsif ($aa !~ /^\d*$/ && $bb =~ /^\d*$/) {return 1;} elsif ($aa =~ /^\d*$/ && $bb !~ /^\d*$/) {return -1;} else {return lc $aa cmp lc $bb;} } keys %H; } } if ($#iterate =~ -1) { print "No jpg files found...ending script."; exit; } print ""; $i = 0; foreach $n (@iterate) { $i++; print "\n

"; print ""; print < END_OF_TEXT $_ = $n; s/\\/\//; print ""; print ''; print ''; $c = $H{$n}; print ''; if (length($c) > 0) { print " $c"; } else { print "No caption supplied for ", $n, "."; } print ""; } print ''; print "

", $htmlfooter; print ""; exit; sub debug{ print "===============
"; print "htmlheader = ", $htmlheader, "
"; print "htmlfooter = ", $htmlfooter, "
"; print "pagetitle = ", $pagetitle, "
"; print "title = ", $title, "
"; print "subtitle = ", $subtitle, "
"; print "indent = ", $indent , "
"; print "size = ", $size , "
"; print "fontsize = ", $fontsize , "
"; print "backgroundgif = ", $backgroundgif , "
"; print "scanall = ", $scanall , "
"; print "sort = ", $sort , "
"; print "nosort = ", $nosort , "
"; print "random = ", $random , "
"; print "Captions.txt file = ", $captions, "
"; print "chaptercount = ", $chaptercount, "
"; print "targetchapter = ", $targetchapter, "
"; print "===============
"; } sub trim { my $string = shift; for ($string) { s/^\s+//; s/\s+$//; } return $string; }