tulrich.com
photos index
#!/usr/local/bin/perl -Tw

# This code has been written by Thatcher Ulrich <tu@tulrich.com> and 
# donated to the Public Domain.  Do whatever you want with it.

# Screenshot viewer.


use CGI;
my $q = new CGI;


print "Content-type: text/html\n\n";
print "<html><head><title>Thatcher's Photos</title>";
print "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
print "</head>\n";
print "<center><a href=\"http://tulrich.com\">tulrich.com</a><br>\n";

# Get 'dir' parameter.
$dir = $q->param('dir') || '';

# Don't allow weird characters, and don't allow dots in the first
# position.
if ($dir ne '') {
    $dir =~ /^([\w\-]+[\w\-\.]*)$/;
    $dir = $1 || '';
}

# Get 'file' parameter.
$file = $q->param('file') || '';

# Don't allow weird characters, and don't allow dots in the first
# position.
if ($file ne '') {
    $file =~ /^([\w\-]+[\w\-\.]*)$/;
    $file = $1 || '';
}

# print "dir=$dir<br>file=$file<br>\n";


if ($q->param('show_source')) {
    #
    # Show script source.
    #

    print "<a href=\"index.pl\">photos index</a><br>\n";

    print "</center><pre>\n";
    open IN, "index.pl";
    while (<IN>) {
	s/\&/&amp;/g;
	s/\</&lt;/g;
	s/\>/&gt;/g;
	s/\>/&gt;/g;
	print $_;
    }
    close IN;
    print "</pre>\n";
}
elsif ($dir eq '' || ! -d $dir) {
    #
    # Show an overall index.
    #

    print "photos index<br>\n";

    opendir DIR, "." or die "can't open directory: $!";
    @dirlist = grep !/^\./, readdir DIR;
    closedir DIR;

    # Sort by date.
    sub numerical_date {
	my ($dir) = @_;
	my ($year, $month, $day) = ($dir =~ /(\d+)-(\d+)-(\d+)/);
	$year = $year || 0;
	$month = $month || 0;
	$day = $day || 0;
	return sprintf("%04d%02d%02d", $year, $month, $day);
    }
    @dirlist = sort { numerical_date($b) <=> numerical_date($a) } @dirlist;

    print "<table border=0 cellpadding=3 cellspacing=1 bgcolor=#eeeeee>\n";
    my $row = 0;
    my $col = 0;
    for $dir (@dirlist) {
	if (-d $dir) {
	    if ($col == 0) {
		if ($row > 0) {
		    print "</tr>\n";
		}
		print "<tr bgcolor=#ffffff>\n";
	    }

	    print "<td align=center>";

	    # Show the first thumbnail, if there is one.
	    eval {
		opendir DIR, "./$dir" or die "can't open directory: $!";
		my @thumblist = grep /thumb\.jpg$/, readdir DIR;
		closedir DIR;

		@thumblist = sort { numerical_date($b) <=> numerical_date($a) } @thumblist;

		if (length(@thumblist) > 0)
		{
		    my $thumb = $thumblist[0];
		    print "<a href=\"index.pl?dir=$dir\">"
			. "<img src=\"$dir/$thumb\" border=0>"
			. "</a><br>\n";
		}
	    };

	    print "<a href=\"index.pl?dir=$dir\">$dir</a>\n";

	    $col++;
	    if ($col >= 4) {
		$row++;
		$col = 0;
	    }
	}
    }
    if ($col > 0) {
	# Put in some empty cells to fill out the row.
	while ($col < 4)
	{
	    print "<td></td>";
	    $col++;
	}
    }
    print "</tr></table>\n"
}
elsif ($file eq '' || ! -f "$dir/$file") {
    #
    # Show a table of thumbnails.
    #

    print "<a href=\"index.pl\">photos index</a><br>\n";
    print "$dir<br>\n";

    opendir DIR, "./$dir" or die "can't open directory: $!";
    @filelist = grep !/\.\./, grep /\.thumb\./, readdir DIR;
    closedir DIR;

    # Sort by the image number near the end of the filename.
    sub image_number {
	return ($_[0] =~ /\.([0-9]+)\.thumb/)[0];
    }
    @filelist = sort { image_number($a) <=> image_number($b) } @filelist;

    print "<table border=0 cellpadding=3 cellspacing=1 bgcolor=#eeeeee>\n";
    my $row = 0;
    my $col = 0;
    for $thumb (@filelist)
    {
	$thumb =~ /^(.*)\.thumb(.*)$/;
	$fullimage = $1 . $2;

	if ($col == 0) {
	    if ($row > 0) {
		print "</tr>\n";
	    }
	    print "<tr bgcolor=#ffffff>\n";
	}
	print "<td align=center>"
	    . "<a href=\"index.pl?dir=$dir&file=$fullimage\">"
	    . "<img src=\"$dir/$thumb\" border=0>"
	    . "</a></td>\n";

	$col++;
	if ($col >= 4) {
	    $row++;
	    $col = 0;
	}
    }
    if ($col > 0) {
	# Put in some empty cells to fill out the row.
	while ($col < 4)
	{
	    print "<td></td>";
	    $col++;
	}
    }
    print "</tr></table>\n"
}
else {
    #
    # Show the image.
    #

    print "<a href=\"index.pl\">photos index</a><br>\n";
    print "<a href=\"index.pl?dir=$dir\">$dir</a><br>\n";

    $file =~ /^(.*\.)([0-9]+)(\.jpg)$/;
    $prefix = $1;
    $image_number = $2;
    $postfix = $3;

    if ($image_number > 0) {
	$previous = $prefix . ($image_number - 1) . $postfix;
	print "<a href=\"index.pl?dir=$dir&file=$previous\">previous</a>";
    } else {
	print "previous";
    }
    print " | ";
    $next = $prefix . ($image_number + 1) . $postfix;
    if (-f "$dir/$next") {
	print "<a href=\"index.pl?dir=$dir&file=$next\">next</a><br>";
    } else {
	print "next";
    }

    print "<table border=0 cellpadding=3 cellspacing=1 bgcolor=#eeeeee>\n";
    print "<tr bgcolor=#ffffff><td align=center>"
	. "<img src=\"$dir/$file\" style='width: 100%;'>"
	. "</td></tr>\n";
    print "</table>\n"
}

print "</center>\n";

print "<font size=-1><a href=\"index.pl?show_source=1\">Perl script source</a></font>";
print "</body></html>\n";
Perl script source