[an error occurred while processing this directive]

technikum29 

 
 

ftplisting

ftplisting ist ein sehr kleines selbstgeschriebenes Perl-Programm, welches sich mit einem beliebigen FTP-Server verbindet und dann für die gewünschten Verzeichnisse, die solchen, die vom Apachen generiert werden, vergleichsweise ähnlich sehen.

Sourcecode

(Version vom 13.10.2005, 18:30 Uhr)

#!/usr/bin/perl

# erstellt automatische Verzeichnislistings von
# FTP-Verzeichnissen

# usage:
#   ./ftplisting.pl /a/b/c a/b verzeichnisxyz
# einfach alle aufzulistende verzeichnislistings auf-
# schreiben

# die generierte indexfile sieht der Apache-Listing-
# Datei sehr ähnlich...

my %icons = ( # some nice icons to use
    'gif' => 'image3.gif',              'png' => 'image2.gif',
    'jpg' => 'image2.gif',              'tex' => 'tex.gif',
    '..'  => 'back.gif',                'pdf' => 'pdf.gif',
    'zip' => 'compressed.gif',          '?'   => 'unknown.gif',
    'blank' => 'blank.gif',             'dir' => 'folder.gif'
);
my @texticons = ('htm', 'html', 'shtm', 'shtml', 'css', 'js', 'txt', 'ps', 'php', 'pl');
foreach (@texticons){ $icons{$_} = 'text.gif'; }

#use strict;
use Net::FTP;

my %conf = (
    # settings for ftp-server
    'server' => 'technikum29.de',
    'user' => '<benutzer>',
    'pwd' => '<passwort>',
    
    # files
    'indexfile' => 'index.html',         # name der zu generierenden datei
    'apacheicons' => '/icons',           # prefix für apache-icons (false zum weglassen)
    'tmpfile' => '/tmp/ftplisting.tmp',  # tempfile
    
    # settings for output HTML
    'namelength' => 40,                  # Länge der dateinamenspalte
    'sizelength' => 16,                  # Länge der Dateigrößenspalte
    'modlength' => 20,                   # last modified
    'space' => ' 'x5,                    # space between cols
    'head' => '<em>This is a non-automatically generated dir-listing. Generated at '.localtime(time).'</em>', # text to display in the top
    'foot' => '<address>generated by <a href="/etc/txt/ftplisting.shtm">ftplisting.sh</a> on '.`uname -sormip`.'</address>' # text to display in the end
);

die "usage: program-name [directory-to-create-index-file] [dir2] [dir3] ..." unless scalar(@ARGV);

print("Connecting to $conf{'server'}... ");
my $ftp = Net::FTP->new($conf{'server'}, Debug => 0) or die "Cannot connect to $conf{'server'}: $@";
$ftp->login($conf{'user'}, $conf{'pwd'}) or die "Cannoct login ",$ftp->message;
print("connected as $conf{'user'}\n");


$titleline = sprintf("%s %-$conf{'namelength'}s$conf{'space'}%$conf{'sizelength'}s$conf{'space'}%-$conf{'modlength'}s", getIcon(''), 'Name', 'Size', 'Last modified');

# now doing directorys
foreach my $d (@ARGV) {
    print("* Creating index-file for $d ");
    $ftp->cwd($d) or die "Can't change into $d: ", $ftp->message;
    my @listing = $ftp->dir() or die "Can't list dir $d: ".$ftp->message;
    print(' (',scalar(@listing),' files)');
    
    my $index = <<"HTML";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of $d</title>
 </head>
 <body>
<h1>Index of $d</h1>
$conf{'head'}
<pre>
$titleline<hr>
HTML
    chomp($index);

    foreach (@listing) {
        # let's think this would be some kind of
        # list like this:
        # "drwxr-xr-x   7 2503-501 ftpusers     4096 May 14 23:04 de"
        die "regex not matching: $_" 
            unless /^.+?\s+(\d+)\s+.+?\s+.+?\s+(\d+)\s+(.{12})\s+(.+)$/;
        
        # $1 = counting (1 = file, x = dir)
        # $2 = filesize in bytes
        # $3 = created time
        # $4 = filename
        
        next if($4 eq '.'); # bei "." = dieses Verzeichnis -> überspringen
        my $name = ($4 eq '..') ? 'parent directory' : $4;
        
        my $addlength = int($conf{'namelength'}-length($name));
        print $addlength;
        $index .= sprintf("%s <a href=\"$4\">$name</a>%s$conf{'space'}%$conf{'sizelength'}s$conf{'space'}%-$conf{'modlength'}s\n",
             getIcon($4, $1 ne '1'),
             ' ' x $addlength,
             ($1 eq '1' ? FileSizeFormat($2) : "$1 files"),
             $3
        );
        
        #print("$1\t$2\t$3\t$4\n");
    }
    
    $index .= "<hr></pre>$conf{'foot'}</body></html>";
    
    
    open(TMPFH, ">$conf{'tmpfile'}") or die "$conf{'tmpfile'} not writable";
    print TMPFH $index;
    close(TMPFH);
    
    $ftp->ascii();
    $ftp->put($conf{'tmpfile'}, $conf{'indexfile'}) or
        die "Could'nt upload tmpfile: ",$ftp->message;
        
    print(" done\n");
}

$ftp->quit;



sub getIcon {
    my $filename = shift;
    my $isdir = shift; # if dir => 1
    $filename =~ /\.([^\.]+)$/;
    my $image = (exists $icons{$1}) ? $icons{$1} : $icons{'?'};
    $image = $icons{'dir'} if $isdir;
    $image = $icons{'..'} if $filename eq '..';
    $image = $icons{'blank'} if $filename eq '';
    return "<img src=\"$conf{'apacheicons'}/$image\" alt=\"[$1]\">";
}

sub FileSizeFormat {
    # this is ported from php ;)
    my $size = shift;
    my $ukb = 1024;
    my $readable, $definition;
    my $kb, $mb, $gb, $tb;
    $kb = $ukb;
    $mb = $ukb*$kb;
    $gb = $ukb*$mb;
    $tb = $ukb*$gb;

      if($size < $kb) {
          $readable = $size;
          $definition = '';
      }elsif($size < $mb) {
          $readable = ($size/$kb);
          $definition = 'KB';
      }elsif($size < $gb) {
          $readable = ($size/$mb);
          $definition = 'MB';
      }elsif($size < $tb) {
          $readable = ($size/$gb);
          $definition = 'GB';
      }else {
          $readable = ($size/$tb);
          $definition = 'TB';
      }
      if ($plain == 1) {
          $definition = "";
      }
      if ($size < $kb) {
          unless (length($readable)) {
              $readable = 0;
          }
          return $readable.$definition;
      } else {
          return sprintf("%.2f $definition",$readable);
      }
}
# EOF