http://qs321.pair.com?node_id=121033
Category: CGI Programming
Author/Contact Info Ranna - ranna@flatirons.org, Ranna@FluffMUCK, Ranna@FurryMUCK, RannaFox@SPR
Description: I claimed that my site was just a bunch of general content, and...well, it is. No specific theme or anything. Because of this, I just sorted my public_html directory (sorta...still really messy) and let this thing loose on certain parts of it. The nice part is, it skips all those private directories that the world really would be better without. :o)

This is the final (I hope) version, but input is welcome. Example: Available code directory

#!/usr/bin/perl
# RedFox! Productions: DirList
# All coding done by one Ranna Fox

use strict;
use CGI qw( :standard );

my $file = param('d') || "prefix/";
if ($file =~ /^\//) {
  print "<b>Ahem...you may not list files with absolute paths.  Especi
+ally not <em>that</em> absolute path</b>\n";
  $r->print_end;
  exit;
} elsif ($file =~ /\.\./) {
  print "<b>'Scuse me, I won't list directories with .. in their path.
+  Better fix that.</b>\n";
  $r->print_end;
  exit;
} elsif (not $file =~ /^prefix\//) {
  print "<b>See, the way things work is that if you're gonna list a di
+rectory, you gotta put a 'prefix/' before it, which means it has to b
+e in my directory.<b>\n";
  $r->print_end;
  exit;
}

my $filen;
($filen = $file) =~ s{^prefix/}{/path/to/home/dir/};
if (not -d $filen) {
  print "<b>That...uh...doesn't appear to exist... o.O</b>\n";
  $r->print_end;
  exit;
}

&parse_dir($filen, $file);

sub parse_dir {
  my $dir = $_[0];
  my $req = $_[1];
  opendir(PARSEDIR, "$dir") || die "Couldn't open directory $dir: $!\n
+";
  my $currdir;
  $dir =~ s/(.*)\/$/$1/;
  ($currdir = $dir) =~ s{.*/([^/]*)$}{$1}; 
  print "<li><b>$currdir/</b>\n<ul>\n";
  foreach my $file (sort(readdir(PARSEDIR))) {
    my $fullname = $dir . "/" . $file;
    next if ($file =~ /^\..*/ || ! -r $fullname);
    if (-d $fullname) {
      $req = $req . "/" . $file;
      &parse_dir($fullname, $req);
    } else {
      my $dirn;
      ($dirn = $dir) =~ s{/path/home/to/dir}{};
      ($filen = $file) =~ s/ /\%20/g;
      print "<li>[<a href=$dirn/$filen>Bare link</a>]";
      if (-T $fullname) {
        # showtext.php is basically an fpassthru() enclosed in <pre> t
+ags.
        print "[<a href=/showtext.php?file=prefix$dirn/$filen>Online</
+a>]"; 
      } else {
        print "[<a href=$dirn/$filen>Download</a>]";
      }
      print " - $file</li>\n";
    }
  }
  closedir(PARSEDIR);
print "</ul>\n";
}