#!/usr/bin/perl -wT use strict; use diagnostics; use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; $ENV{PATH} = '/bin:/usr/bin:/home/ed/bin:/vol/linux/apps/bin'; my $VIEW = 'Convert to PDF'; my $SAVE = "$VIEW for saving"; if (not param()) { die "usage: topdf.cgi?input="; } # Should find some way to incorporate this credit message, if we ever # display a plain HTML page. # # p('This was implemented using ' . # CGI::a({ href => 'http://www.cs.waikato.ac.nz/~singlis/' }, 'TIC' ) . # ' and ' . # CGI::a({ href => 'http://www.ghostscript.com/' }, 'GhostScript') . # '.'), # address(CGI::a({ href => 'mailto:ed@membled.com' }, 'Edward Avis')), my $f = param('input'); if (not defined $f) { die "expected parameter 'input'" } # Sanitize filename if ($f =~ m!^/!) { die "absolute filenames not permitted" } my @f = split m!/+!, $f; my @newf; foreach (@f) { if (/^\./) { die "path components beginning with . are not permitted: $_"; } unless (/^([a-zA-Z0-9._-]+)$/) { die "path component contains bad chars: $_"; } push @newf, $1; } my $newf = join('/', @newf); undef $f; if (not -f $newf) { die "not a regular file: $f"; } # Could check for readability, but race condition; just assume any # .tic98 or .ps files under the current directory should be # readable. # my $type = 'application/pdf'; if ($newf =~ /\.tic98$/) { print header(-type => $type); system "tic98topdf <$newf"; } elsif ($newf =~ /\.ps$/) { print header(-type => $type); system "pip -io ps2pdf -sPAPERSIZE=a4 -r600x600 -.ps -.pdf <$newf"; } elsif ($newf =~ /\.ps.bz2$/) { print header(-type => $type); system "bzip2 -d <$newf | pip -io ps2pdf -sPAPERSIZE=a4 -r600x600 -.ps -.pdf"; } elsif ($newf =~ /\.pdf$/) { print header(-type => $type); system "cat $newf"; } elsif ($newf =~ /\.pdf.bz2$/) { print header(-type => $type); system "bzip2 -d <$newf"; } else { die "unsupported file type" }