#!/usr/local/bin/perl # dirindex.cgi by David Efflandt tech-ph@de-srv.com # last updated 5/12/00 # # Since the default index of files is broken on virtualave I wrote # this substitute to automatically lists links to all files in a # directory (except hidden and @xfiles list). # # Rename it 'index.cgi' if you want it to be the default index # (other hosts may require a server directive to use it as default) # # See http://www.apache.org/ for more info on following (case sensitive): # Inserts 'HEADER.html' or 'HEADER' (text) file as header. # Inserts 'README.html' or 'README' (text) file as footer. # # Parses '.htaccess' or an alternate file for: # AddDescription "description" filespec # Note: Any of the usual apache directives related to indexing that I # try to put in .htaccess causes a server error on virtualave.net. # So put AddDescription lines in an alternate file (see $descfile). ### User Variables ### # File to search for AddDescription (.htaccess or another file): $descfile = '.desclist'; # begin with dot to hide file # Note: Hidden (dot) files are automatically excluded. # Exclude files (quoted, comma separated list): my @xfiles = ( 'HEADER', 'README', 'HEADER.html', 'README.html', ); ### End Variables ### use CGI qw/:standard/; use vars qw/$url @insert @lines $desc $types @types @match @links $key %desc/; $url = url(-absolute=>1); # path to script $url =~ s|/[^/]+$||; # prune script name print header,start_html("Index of $url"),"\n"; unless (opendir DIR,'./') { print "Can't open dir $url: $!\n"; exit; } my @all = sort grep !/^\./, readdir DIR; # skip dot files closedir DIR; my @insert = grep /^(HEADER|HEADER.html)$/, @all; if ($_ = pop @insert) { if (open IN,"$_") { flock(IN,2); seek(IN,0,0); if (/^HEADER.html$/) { print ; } else { print pre(); } } close IN; } else { print h1("Index of $url"); } print "\n"; if (open IN,"$descfile") { flock(IN,2); seek(IN,0,0); @lines = ; close IN; while ($_ = pop @lines) { if (/AddDescription\s*"([^"]+)"\s*(\S.*)$/) { $desc = $1; $types = $2; @types = split(' ',$types); foreach $key (@types) { @match = glob($key); push @match,grep(/$key/,@all) unless $key =~ /[\*\?\{\$]/; foreach (@match) { $desc{$_} = $desc; } } } } } dolink('..'); # parent dir foreach $key (@all) { chomp $key; next if grep(/$key/,@xfiles); # skip excluded files dolink($key); } print p,table({cellpadding=>2},Tr([ td([font({face=>'courier'},'Name'), font({face=>'courier'},'Last Modified'), font({face=>'courier'},'Size'), font({face=>'courier'},'Description')]), td({colspan=>4},[hr]),@links])),"\n",p; my @insert = grep /^(README|README.html)$/, @all; if ($_ = pop @insert) { if (open(IN,"$_")) { flock(IN,2); seek(IN,0,0); if (/^README.html$/) { print ; } else { print pre(); } } } print end_html,"\n"; sub dolink { my($uri) = @_; my $update = scalar localtime((stat($uri))[9]); if ($update =~ /^(\w+) (\w+)\s+(\d+) (\d+:\d+):\d+ (\d+)$/) { if ($3 < 10) { $_ = "0" } else { $_ = ''; } $update = "$_$3-$2-$5 $4"; } my $size = -s $uri; $size = int(($size + 1024) / 1024); $size .= 'K'; ($desc = $desc{$uri}) || ($desc = ' '); if (-d $uri) { $uri .= '/'; $size = '-'; } my $link = $uri; $link = 'Parent Directory' if $uri eq '../'; push @links, td([font({face=>'courier'},a({href=>"$uri"},$link)), font({face=>'courier'},$update)]). td({align=>'right'},[font({face=>'courier'},$size)]). td([font({face=>'courier'},$desc)]); }