#!/usr/bin/perl
#
# dirls.cgi     --crb3 26sep01
#
# Invoked by an #exec-cgi SSI call, this Apache CGI program:
# 1. figures out the calling page's location in the directory tree,
# 2. scans that directory for HTML pages,
# 3. opens each such page in turn long enough to grab the title, and
# 4. lists each such suitable page as a hyperlink.
#
# This program was originally written to allow fanfics to be converted
# to HTML and simply dropped into the appropriate directory, and
# automatically added to the browsed index page. It currently ignores all
# non-HTML filetypes and editing backups (*~), so its presentation
# is clean despite whatever else is in that directory.
#
# It is Apache-specific in that it depends on extended ENV settings
# which Apache may be unique in providing.
#
#
#
$basedir=$ENV{REQUEST_URI};
chomp $basedir;
substr($basedir,0,1)="" if index($basedir,"/"eq 0;    #remove leading /
$basedir =~ s/index\.\w?htm\w?$//i;
($fore,$aft)=split(/\//,$basedir,2);
if(index($fore,"~"eq 0){
  substr($fore,0,1)="/home/";
  $basedir = "$fore/public_html/$aft";
}else{
  $basedir="/home/httpd/html/$fore/$aft";
}

open(DIRLIST, "ls $basedir|");

print <<EOF;
<html><head></head><body>
EOF

#print "\n<br>basedir: $basedir<br>\nfore: $fore, aft: $aft<br>\n";

#print "$ENV{REQUEST_URI}<BR>";

print <<EOF;
<center><p align="center">
<table align="center" width="85%">
EOF

while(defined($file=<DIRLIST>))
{
  chomp $file;
  next if $file =~ /^\./;               # ignore dotfiles
  next if $file =~ /\~$/;               # ignore unix backups
  next if $file =~ /^index\./;          # ignore the calling page
  next unless $file =~ /\.\w?htm\w?$/i# ignore non-HTML files

  if(open(IFIL,"<$file")){
    $bgline="";
    while(defined($inline=<IFIL>)){     # look for <title>whatitis</title>
      chomp $inline;                    # allow for multiline source text
      $bgline = "" unless $bgline =~ /\<title\>/i;
      $bgline .= $inline;               # grab what's between those tags
      last if $inline =~ /\<\/title\>/i;
    }                                   # quit when you've found it
    close(IFIL);
    $bgline =~ s/\<\/?title\>//gi;
  }else{
    $bgline="&nbsp;";                   # not found? leave it blank.
  }
  $title=$file;
  print <<EOF;
<tr><td align="right">
<a href="$file">$file</a>
</td><td>
$bgline
</td></tr>
EOF
#
#  print "<tr><td align=\"right\"><a href=\"$file\">$title</a></td></tr>\n";
#  ..display the hyperlinked filename as well as the title.
#

}

print <<EOF;
</table>
</p>
</center>
</body></html>
EOF


Grab a
gzipped
copy
here
 
Syntax highlighting using Syntax::Highlight::Engine::Kate