#!/usr/bin/perl # # webls.pl # Paul Venezia, pvenezia@jpj.net # 08.20.02 # v0.8 - 02.16.04 # # This script is designed to be run from /perl or /cgi-bin on an Apache # webserver. Given a root directory, it will create dynamic pages based # on the contents of the directory that's referenced. As it navigates # the directory hierarchy via user choices, it determines if it's # already created a page for that dir, and creates a cache page in that # directory, pushing the cached page to the next request unless the # directory contents changes, when it will regenerate the cache page. # # In short, it gets faster the more it's used. # # Enhanced data display for MP3 files via MP3::Info (can be found at CPAN). # # Note: If changes are made to the webls.pl script, run # # $ find . -name \*pindex\* -exec rm {} \; # # from the root of the tree. Also, all dirs in the tree need to be chgrp to the Apache user, and mode 0776. # # Code should be self explanatory, and needs work to be more portable and modular. # # use CGI qw/:standard *table start_ul/; use File::Basename; use MP3::Info; use Fcntl ':mode'; #use strict; undef @PATTERN; undef @FILEPATTERN; undef @HEADERS; undef @DETAILS; undef $cwd; undef $lscwd; undef $debug; $version = "0.8"; # Version string #------------------------------------------------------------------------------------ # Configuration #------------------------------------------------------------------------------------ $title = "groove.jpj.net resources"; # Default Page title $tbbgcolor = "#C0C0C0"; # Title bar background $tbfcolor = "black"; # Title bar text $bgcolor = "#cccccc"; # Table item background $fcolor = "black"; # Table text $icondir = "/icons/small"; # Apache Icons directory $indexfile = ".pindex.html"; # Cache file name $notesfile = ".pnotes.txt"; # Cache file name $titlefile = ".ptitle.txt"; # Cache file name $url="http://groove.jpj.net/pub"; # Abolut URL $PATH = "/var/www/html/pub"; # Equivalent FS path $FILEPATTERN = "*"; # Pull every file #$debug = "1"; # Turn on debugging #------------------------------------------------------------------------------------ # Error msg sub #------------------------------------------------------------------------------------ sub error_msg { print header('text/html'), start_html("Action Denied"), h2("Action Denied"), h3("$_[0]"), h3(a{href=>"$script_url"},"Return"), end_html; exit; } #------------------------------------------------------------------------------------ # Get stats on files in $cwd #------------------------------------------------------------------------------------ sub getfiles { undef $htype; undef $size; undef $date; undef $link; undef $icon; undef $typename; undef $file; $count = 0; $countdirs = 0; $countfiles = 0; foreach $file (<@PATTERN>) { $count++; next if ($file =~ /($indexfile|$notesfile|$titlefile)/); ($type, $size, $sec) = (stat($file)) [2,7,9]; if (S_ISDIR($type)) { $typename = "Directory"; $icon = "$icondir/dir.png"; $countdirs++ } if (S_ISREG($type)) { $typename = "File"; $icon = "$icondir/unknown.gif"; $countfiles++ } if (S_ISLNK($type)) { $typename = "Link"; $icon = "$icondir/link.png"; } $icon = "$icondir/sound.gif" if ($file =~ /\.(wav|wma)$/i); $icon = "$icondir/image2.gif" if ($file =~ /\.(bmp|jpg|gif|png)$/i); $icon = "$icondir/compressed.gif" if ($file =~ /\.(gz|tar|zip|bz|sit|sea|hqx|iso|tgz)$/i); $icon = "$icondir/rpm.png" if ($file =~ /\.rpm$/i); $icon = "$icondir/text.gif" if ($file =~ /\.(txt|pl|sh)$/i); $icon = "$icondir/movie.gif" if ($file =~ /\.(mov|avi|wmf|fli|asf)$/i); $icon = "$icondir/index.gif" if ($file =~ /\.(html)$/i); #------------------------------------------------------------------------------------ # Call MP3::Info if file is an MP3 #------------------------------------------------------------------------------------ if ($file =~ /\.mp3$/i && $size > 0) { $info = new MP3::Info $file;; ($length, $bitrate) = split (" ", sprintf "%s %s", $info->TIME, $info->BITRATE); $icon = "$icondir/sound2.gif"; $htype = "Length/Type"; $typename = "$length\m - $bitrate\kbps"; } if ($file =~ /\.ogg$/i && $size > 0) { use Ogg::Vorbis; $ogg = Ogg::Vorbis->new; open(INPUT, "< $file"); $ogg->open(INPUT); $info = $ogg->info; ($length, $bitrate) = split (" ", sprintf "%.2f %.0f", $ogg->time_total/60, $ogg->bitrate/1000); $icon = "$icondir/sound2.gif"; $htype = "Length/Type"; $typename = "$length\m - $bitrate\kbps"; close(INPUT); } $date = (localtime($sec)); $date =~ s/:\d+//; #------------------------------------------------------------------------------------ # Do type/size calc #------------------------------------------------------------------------------------ if (S_ISDIR($type)) { $psize = "--"; } elsif ($size < 1024 ) { $psize = "$size bytes" } elsif ($size < 1024000 ) { $psize = sprintf "%.1f kb", $size / 1024; } else { $psize = sprintf "%.2f MB", $size / 1024 / 1024; } $name = basename $file; $link = "/pub/$lscwd/$name"; $link =~ s/\/\//\//g; #------------------------------------------------------------------------------------ # Push data for files list table into @DETAILS #------------------------------------------------------------------------------------ if (S_ISDIR($type)) { push (@DETAILS,a({href=>"$script_url?page=$lscwd/$name"},img({-border=>undef,src=>$icon}),"$name"), $psize, $date, "$typename