#!/home/merlyn/bin/perl -Tw use strict; use CGI::Carp "fatalsToBrowser"; use CGI ":standard"; my $HTDOC = "/home/merlyn/Html"; my $PICSPERPAGE = 10; my $ODD = "#dddddd"; # bgcolor for odd rows my $EVEN = "#ffffff"; # bgcolor for even rows { my $dir; ($ENV{"SCRIPT_URL"} || "") =~ /(.*)/s and $dir = "$HTDOC$1" and chdir $dir or die "cannot chdir $dir: $!"; } my $title = "Picture index for ".get_title("."); print header, start_html("-title" => $title, -dtd => "-//W3C//DTD HTML 4.0 Transitional//EN"), h1($title), p(get_info(".")); my @files = do { local *DIR; opendir DIR, "." or die "cannot readdir '.': $!"; readdir DIR; }; my @other; my @pics; for (sort "..", grep !/^\.|~$/, @files) { if (-d) { push @other, ["$_/", get_title($_)]; next; } if (/\.(gif|jpg)$/ and -r "$_.thumb.jpg") { push @pics, [$_, "$_.thumb.jpg", get_info($_)]; next; } next if /\.thumb\.jpg$/; push @other, [$_, "unexpected file"]; } { print h2("Links"); my $flip = 0; print table({cellspacing => 0, cellpadding => 10}, (map { my ($path,$desc) = @$_; Tr({bgcolor => (($flip = 1 - $flip) ? $ODD : $EVEN)}, td(a({href => my_uri_escape($path)}, $path)), td($desc)) } @other)); } if (@pics) { my $start = 0; $start = $1 if (param("start") || "") =~ /(-?\d+)/; $start = 0 if $start < 0; my $low = $start; $low = $#pics if $low > $#pics; my $high = $low + $PICSPERPAGE - 1; $high = $#pics if $high > $#pics; print h2("Pictures",$low+1,"through",$high+1,"of",$#pics+1, "total"); my $flip = 0; print table({cellspacing => 0, cellpadding => 10}, ($low > 0 ? Tr(td({colspan => 3}, a({href => "$ENV{SCRIPT_URI}?start=".($low - $PICSPERPAGE)}, "preview earlier pictures"))) : ()), (map { my ($path,$paththumb,$desc) = @$_; Tr({bgcolor => (($flip = 1 - $flip) ? $ODD : $EVEN)}, td(a({href => my_uri_escape($path)}, img({src => my_uri_escape($paththumb), alt => "[thumbnail for $path]"}))), td(int((1023 + -s $path)/1024)."K"), td($desc)) } @pics[$low..$high]), ($high < $#pics ? Tr(td({colspan => 3}, a({href => "$ENV{SCRIPT_URI}?start=".($high + 1)}, "preview later pictures"))) : ()), ); } print end_html; sub my_uri_escape { my $text = shift; $text =~ s/([^A-Za-z0-9_.\-\/])/sprintf "%%%02X", ord $1/ge; $text; } sub get_title { my $dir = shift; local *F; open F, "$dir/.title" and =~ /(.+)/ and return $1; $dir eq ".." and return "Go up"; $dir eq "." and return "The $ENV{SCRIPT_URL} directory"; "The $dir directory"; } BEGIN { my %info = (); sub get_info { my $path = shift; unless (%info) { local (*F, $/, $_); if (open F, ".info" and defined($_ = )) { s/^\s*\#.*\n//mg; # toss comments s/[ \t]*\n[ \t]+/ /g; # fold continuation lines %info = /^(\S+)\s+(.*)/mg; } $info{"."} ||= " "; } $info{$path} || "Description not provided for $path"; } }