Show view_attachment.html syntax highlighted
% # vim: set syntax=mason:
<%args>
$file => -1
$type => 'application'
$subtype => 'octet-stream'
$mode => "normal"
</%args>
<%init>
my $user;
my $prefix;
eval {
$user = Apache2::SiteControl->getCurrentUser($r);
$prefix = $config->getScratchDir($user);
};
if($@) {
$logger->error($user->getUsername() . ": Unable to get attachment: $@");
$m->redirect(build_url("$base/mail/index.html", { message => "Request failed" }));
}
my $sandbox = $config->get("sandbox_util");
my $sandbox_dir = $config->get("sandbox_dir");
my $sandbox_user = $config->get("sandbox_user", 'sandbox');
my $jpegtopnm = $config->get("jpegtopnm");
my $pnmtojpeg = $config->get("pnmtojpeg");
my $pnmscale = $config->get("pnmscale");
my $giftopnm = $config->get("giftopnm");
my $tifftopnm = $config->get("tifftopnm");
my $pngtopnm = $config->get("pngtopnm");
my $cat = $config->get("cat");
my $iconsize = $config->get("iconsize");
my $username = $user->getUsername();
my $cachetime = $config->get("message_tmpdir_cachetime", 10) . ' min';
$file =~ s/\\/\\\\/g; # Un-escape escapes
if($file == -1) {
$m->redirect(build_url("$base/mail/index.html", { message => "No filename for attachment." }));
} elsif($file =~ m!(^|/)\.\./!) {
$logger->error($user->getUsername() . ": Security Violation! $username attempted to view $file. Prefix was $prefix");
$m->redirect(build_url("$base/mail/index.html", { message => "Security violation on filename $file" }));
}
$file = "$prefix/$file"; # Full path to tmp file.
# make sure the file is still there...
if(!-r $file) {
# We probably renewed an old session (see bug 425)...force them to re-click
# on the message.
$m->redirect(build_url("$base/mail/index.html", { message => "Please try again (decoded attachment was cleaned up due to session inactivity). If it fails again then contact support." }));
}
$subtype = lc($subtype);
$subtype = "tiff" if $subtype eq 'tif';
my $line;
my %converters = ( jpeg => $jpegtopnm,
gif => $giftopnm,
tiff => $tifftopnm,
png => $pngtopnm,
pnm => $cat,
);
my $converter = $converters{$subtype};
$logger->debug($user->getUsername() . ": Running in $mode mode with $type/$subtype on $file");
if($mode eq "shrink" && $type eq "image" && $converter) {
$r->content_type("$type/$subtype");
# cache icons
my $key = "${file}_${type}_${subtype}";
my $icon = $m->cache->get($key);
if(!defined($icon)) {
$logger->debug($user->getUsername() . ": tring to convert using: $sandbox $sandbox_user $sandbox_dir $converter < '$file' | $sandbox $sandbox_user $sandbox_dir $pnmscale -ysize $iconsize | $sandbox $pnmtojpeg|");
# The redirection happens before sandbox runs. That is OK, since the fds
# are passed along for the ride...no need to use the tmp dir in sandbox.
open DATA, "$sandbox '$sandbox_user' '$sandbox_dir' $converter < '$file' | $sandbox '$sandbox_user' '$sandbox_dir' $pnmscale -ysize $iconsize | $sandbox '$sandbox_user' '$sandbox_dir' $pnmtojpeg|" or $logger->error($user->getUsername() . ": problem running: $sandbox '$sandbox_user' '$sandbox_dir' $converter < $file | $sandbox '$sandbox_user' '$sandbox_dir' $pnmscale -ysize $iconsize | $sandbox '$sandbox_user' '$sandbox_dir' $pnmtojpeg|");
while($line = <DATA>) {
$icon .= $line;
$m->print($line);
}
close DATA;
$m->cache->set($key, $icon, $cachetime);
$logger->debug($user->getUsername() . ": Cached $file icon for later use.");
} else {
$logger->debug($user->getUsername() . ": Using cached icon for $file.");
$m->print($icon);
}
} elsif($type eq "image" && $converter && $subtype =~ m/^(tiff|png)$/) {
# Convert to better format for browser
$logger->debug($user->getUsername() . ": Converting $file to JPEG for browser compatibility");
$r->content_type("image/jpeg");
open DATA, "$sandbox '$sandbox_user' '$sandbox_dir' $converter < '$file' | $sandbox '$sandbox_user' '$sandbox_dir' $pnmtojpeg|" or $logger->error($user->getUsername() . ": problem converting file: $sandbox '$sandbox_user' '$sandbox_dir' $converter < $file | $sandbox '$sandbox_user' '$sandbox_dir' $pnmtojpeg|");
while($line = <DATA>) {
$m->print($line);
}
close DATA;
} else {
$logger->debug($user->getUsername() . ": Sending $file unmodified");
$r->content_type("$type/$subtype");
open DATA, "<$file" or $logger->error($user->getUsername() . ": No such file: $file");
while($line = <DATA>) {
$m->print($line);
}
close DATA;
}
</%init>
<%once>
use AlphaMail::HTMLHelper qw(build_url);
our $logger = Log::Log4perl->get_logger('alphamail');
our $base = $config->get('base_uri');
</%once>
See more files for this project here