#!/usr/bin/perl -w # This CGI script gives a friendly user interface # to the directory where ATT dumps the call detail # reports. It allows you to view the files as text, download # the files, download them as CSV files, or clean up # old files. use CGI qw/:standard/; use strict; # testing on bitsy.sub-atomic.com (-d "/home/liveops/ssh") && chdir("/home/liveops/ssh"); # live config (-d "/home/adl/users/liveops/cdr") && chdir("/home/adl/users/liveops/cdr"); my $i = 0; my $dmax = param('dmax'); if (defined $dmax) { foreach $i (0 .. $dmax) { my $f; if ($f = param("d$i")) { unlink($f); Delete("d$i"); } } Delete('dmax'); } # show the file they are asking for, if and only if they ask # for a simple filename my $f = param('file'); if (defined $f && $f =~/^[a-z0-9_.]*$/i) { if (open(O, "<$f")) { if (param('dl')) { if (param('cs')) { printCSV(); exit; } print header(-type => "application/binary", '-Content-Disposition' => "attachment; filename=\"$f\""); } else { print header("text/plain"); } print ; close(O); } else { print header(-type=> "text/plain", -status=>404); print "File $f not found."; } exit; } print header("text/html"); print ""; my @files = <*.rpt>; $i = 0; print start_form(-name => 'listform'); print "\n"; print ""; print "
", checkbox(-name => 'allbox', -label=>'', onclick => "CheckAll(document.listform);"); print "FilenameSummary\n"; foreach my $file (@files) { my $summary = getSummary($file); param('file', $file); my $furl = self_url(); param('dl', 1); my $dlurl = self_url(); Delete('dl'); my $csurl = ''; if ($summary ne 'Bulk Data Download') { param('dl', 1); param('cs', 1); $csurl = self_url(); Delete('cs'); Delete('dl'); } print "
", checkbox(-name=>"d$i", -value=> $file, -label=>'', onclick => "CheckCheckAll(document.listform);"); print "$file"; print " "; print " " unless $csurl eq ''; print "$summary"; print "\n"; $i++; } print "
\n"; $i--; print ""; print ""; print end_form(); # try to make a one-line description of the file sub getSummary { my ($file) = @_; if (! open(F, "<$file")) { return '(not found)'; } my $name = 'unknown name'; my $date = 'unknown date'; my $debug = ''; my $end; while () { chomp; s/^\s*//; # clean off front s/SCD: /SCD\t/; # make it properly tab delimited s/: \t?/\t/; if (/Bulk Data Download/) { close F; return "Bulk Data Download"; } my($k, $v) = split(/\t/); $debug .= "$k, "; if (defined $k && $k eq 'SCD') { $name = $v; $name =~ s/^Standard Call Detail //; } if (defined $k && $k eq 'Requested Date(s)') { $date = $v; last; } } close F; return "$name, $date"; #$debug"; } sub printCSV { my($file) = param('file'); if (! open(F, "<$file")) { print header(-type=> "text/plain", -status=>404); print "File $f not found."; exit; } $file =~ s/.rpt$/.csv/; print header(-type => "text/csv", '-Content-Disposition' => "attachment; filename=\"$file\""); my $mode; $_ = ; if (/^SCD/) { $mode = 1; $_ = ; $_ = ; } else { $mode = 2; while () { last if (/^----/); } $_ = ; $_ = ; $_ = ; $_ = ; } my @line; while () { if ($mode == 1) { @line = split(/\t/); shift @line; shift @line; $line[3] =~ s/^\s*//; } elsif ($mode == 2) { s/ +/\t/g; @line = split(/\t/); shift @line; } print $line[3], ",", $line[4], "\n" if (defined $line[3] && $line[3] ne ''); } close F; }