#!/usr/bin/perl -w
require(5.004);
# use strict;
use strict 'vars';
use strict 'refs';
use strict 'subs';
use POSIX;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Pretty qw(:html3);
# global variables
use vars($Debug, $Sendmail, $UploadMax);
use vars qw($Query $SelfUrl);
$Debug = 0;
$Sendmail = '/usr/sbin/sendmail';
$UploadMax = 5; # max attachment size (in Mbytes)
main();
exit(0);
###############################################################################
# main program
###############################################################################
sub main {
my ($key, $value, $status, $title);
# get inputs from HTML form
$CGI::POST_MAX = 1024*1024*$UploadMax;
$Query = new CGI;
$SelfUrl = $Query->url();
foreach $key ($Query->param) {
$value = $Query->param($key);
$Att1 = $value if ($key eq 'Att1');
}
sanitizeFilename($Att1);
htmlEscape($Att1);
urlEscape($Att1);
# CGI error
if ($Query->cgi_error) {
$Action = 'ERROR';
$title = 'Action Failed';
}
# process new form
elsif ($Action eq 'SUBMIT') {
$Action = 'SUBMIT';
$title = 'Submit File';
}
# unknown action
else {
$Action = $Action;
$title = 'Action Failed';
}
Header($title, undef, $MyStyle);
# CGI error
if ($Action eq 'ERROR') {
htmlError('Cannot process request',
"total size of uploaded files (if any) must be <= " .
"$UploadMax Mbytes");
}
# process a file
elsif ($Action eq 'SUBMIT') {
if ($status = ProcessFile()) {
# an error has occurred
htmlError('Cannot process file', $status);
}
else {
SendMail($MyEmail, 'Error processing file');
# success
htmlNote("Successfully processed file");
}
}
# unknown action
else {
htmlError('Cannot perform action',
"unrecognized action '$Action'");
}
Tailer();
exit(0);
}
# fixme
if ($Action eq 'SUBMIT' && $Att1) {
if (open(FILE, ">$Uatt1")) {
while (read($Query->upload('Att1'), $buffer, 1024)) {
print(FILE $buffer);
}
close(FILE);
}
else {
$status .= "cannot open file '$Uatt1': $!
";
}
}
}
###############################################################################
# make form
###############################################################################
sub MakeForm {
print $Query->start_multipart_form(-action=>"$SelfUrl",
-method=>'POST',
-name=>'main');
beginPanel('cellleft');
print $Query->filefield(-name=>"Att1",
-default=>"$Att1",
-size=>30);
midPanel('cellleft');
print "\n";
print "\n";
endPanel();
print $Query->endform;
}
###############################################################################
# send e-mail
###############################################################################
sub SendMail {
my $subject = shift;
my $address = shift;
my $path = $ENV{PATH};
$ENV{PATH} = '';
if (! open(MAIL, "| $Sendmail $address")) {
htmlError("Cannot send e-mail to $address", $!);
$ENV{PATH} = $path;
return;
}
$ENV{PATH} = $path;
print(MAIL "To: $address\n");
print(MAIL "Subject: $subject\n");
print(MAIL "\n");
print(MAIL "--------------------------------------------------\n");
print(MAIL "your text here\n");
print(MAIL "--------------------------------------------------\n");
close(MAIL);
}
###############################################################################
# start page
###############################################################################
sub Header {
my $title = shift;
my $background = shift;
my $style = shift;
print($Query->header);
print($Query->start_html(-title => $title,
-background => $background,
-style => {src => $style}));
beginPanel('title');
print("$title\n");
endPanel();
print("
\n");
}
###############################################################################
# end page
###############################################################################
sub Tailer {
if ($Debug) {
print("
\n");
beginPanel('header');
print("Debug Information\n");
midPanel('cellleft');
debugPrint();
endPanel();
}
print($Query->end_html);
}
###############################################################################
# print query and environment info
###############################################################################
sub debugPrint {
my $key;
print("Query:\n");
print("
\n"); foreach $key (sort($Query->param)) { print("$key = \"" . $Query->param($key) . "\"\n"); } print("\n"); print("Environment:\n"); print("
\n"); foreach $key (sort(keys(%ENV))) { print("$key = \"" . $ENV{$key} . "\"\n"); } print("\n"); } ############################################################################### # draw panels ############################################################################### sub beginPanel { my $class = shift; print("
\n"); } sub midPanel { my $class = shift; print(" |
\n"); } sub endPanel { print(" |