#!/usr/bin/perl -w
# ----------------------------------------------------------------
# $Id: quar_display.pl,v 1.2 2004/04/06 09:18:56 root Exp root $
#
# quar_display.pl - Display contents of a MIMEdefang quarantine directory
# Version:	$Revision: 1.2 $
# Date:		$Date: 2004/04/06 09:18:56 $
# Author:	Paul Murphy, Ionix Pharmaceuticals - pmurphy@ionixpharma.com
# Copyright 2004 - permission granted for non-profit use.
# ----------------------------------------------------------------

# Given a directory to browse, this will create a web page with a 
# navigation header, and a section per file in the quarantine directory.
# Note that it takes care to ensure that all file contents are _not_ 
# interpreted as HTML, since you quarantined the message for a reason...

# For quarantined parts rather than whole messages, only shows text files.

use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
$CGI::POST_MAX=1024 * 100;  # max 100K posts
$CGI::DISABLE_UPLOADS = 1;  # no uploads

my ( $dir, $scriptname );
my ( $q, $folder, @filenames );
my ( $qdirfile, $cmd );
  
# CONFIG SECTION - may be changed
$dir = "/var/spool/MD-Quarantine";
$scriptname= '/cgi-bin/quar_display.pl';
# END OF CONFIG


$q = new CGI;
chdir $dir;
if (! $q->param())    #warn about usage
  {
  # send HTML headers and page heading
  print $q->header;
  print $q->start_html('Quarantine Display'),
        $q->h1('Quarantine Display');

  print "<b>Cannot use this program directly - contact Support</b><br>\n";
  print $q->end_html;
  print "\n\n";
  exit(-1);
  }
else
  {
  #process the inputs
  chdir $dir;
  # start by getting the folder parameter
  $folder= $q->param("qdir");
  # start the output
  print $q->header;
  print $q->start_html(-title=>'Quarantine Viewer'); 

  print "<a name=\"top\"></a><b>Folder=$folder</b>";

  $qdir=$dir."/".$folder;

  # get list of files we can view
  @filenames= &myreaddir($qdir);

  # show the navigation header
  print $q->start_table({border => 1});
  print "<tr>";
  foreach $qdirfile ( @filenames )
    {
    if ( $qdirfile !~ /BODY/ )	# not a binary file
      {
      print "<td width=\"10%\"><a href=\"#$qdirfile\">$qdirfile</a></td>";
      }
    }
  print "</tr>";
  print $q->end_table;


  # show the individual parts
  foreach $qdirfile ( @filenames )
    {
    if ( $qdirfile !~ /BODY/ )	# not a binary file
      {
      print "<hr><a name=$qdirfile></a><h1>$qdirfile</h1><a href=\"#top\">Back to top</a>";
      print $q->start_table({border => 1});
      print "<tr>";
      # use <xmp> block rather than <pre> since this disables HTML parsing
      print "<td width=\"90%\"><xmp>";

      # nasty hack to view the file - basic shell redirect 
      # stripping just in case
      if ( $qdirfile eq "ENTIRE_MESSAGE" )
	{
	print "First 100 lines of ENTIRE_MESSAGE:\n\n\n";
        $cmd='head -100 '.$folder.'/'.$qdirfile.'|sed -e "s/<//"|sed -e "s/>//"';
	}
      else
	{
        $cmd='cat '.$folder.'/'.$qdirfile.'|sed -e "s/<//"|sed -e "s/>//"';
	}
      system($cmd);

      print "</xmp></td>";
      print "</tr>";
      print $q->end_table;
      }
    }

  print $q->end_html;
  }


sub myreaddir
{
my ( $dir ) = @_;
my ( @list );
my ( $FDIR );
my ( $count,@new );

opendir (FDIR, $dir) || die("Unable to open directory $dir");
@list=readdir(FDIR);

foreach $file ( @list )
  {
  if (  $file =~ /^[A-z]/ )
    {
    push(@new,$file);
    }
  }
return @new;
}
1;
