#!/usr/bin/perl
use strict;
use CGI;
use Mail::Sendmail;
use File::Basename;
use MIME::Base64;
use Time::Local;
$CGI::POST_MAX = 1024 * 2000; # file upload max
$CGI::DISABLE_UPLOADS = 0; # allow uploads
#-----------------------------------------------
#process printing services request form
#-----------------------------------------------
my $SERVER="http://".$ENV{'SERVER_NAME'};
my $form=$SERVER.$ENV{'SCRIPT_NAME'} || $SERVER.$ENV{'PATH_INFO'};
my $datapath=dirname $0;
my $progname=basename $0;
my $logo ='';
my $listimg ='';
my ($sec,$min,$hr,$day,$mon,$yr)=(localtime);
my $date=sprintf("%04d-%02d-%02d %02d:%02d", $yr+1900, $mon+1, $day, $hr, $min);
my $date_sub;
my %in=();
my %str=();
my %check=();
my $typetext="text";
my $typetextarea="textarea";
my $closetextarea="<\/textarea>";
my $border=0;
my $PREVIEWSTR;
my $SENDSTR;
my $FIXSTR;
my $ATTACHSTR;
my $ATTACHERR;
my $submitstr;
my $previewstr;
my %mainbody;
my $formid;
my $formstr;
my $date;
my $error;
my $debug=0;
my $boundary = "====" . time() . "===="; #mail data delimiter
my ($e, $ee, $f, $ff, $ef);
my $MAILTO='bryson.connolly@ottawa.ca'; # printshop@ottawa.ca, Bonnie.Pritchard@ottawa.ca
$MAILTO='bryson.connolly@ottawa.ca' if ($debug);
my ($sec, $min, $hr, $day, $mon, $yr) = localtime();
my $today=sprintf("%04d%02d%02d %02d:%02d", $yr+1900, $mon+1, $day, $hr, $min);
#------------------
#read form input
#------------------
my $query=new CGI;
print "content-type: text/html\n\n";
if ($query->cgi_error()){
print $query->cgi_error();
print " The file you're trying to upload is too large!";
exit;
}
my $action =$query->param('action');
my $lang =$query->param('lang');
my $id="print_bi";
my $tpl="$id.htm";
if ($lang ne "_fr")
{ $lang="_en";
$f="";
$ef=$f;
$PREVIEWSTR="Preview";
$SENDSTR="Submit to Printing Services";
$FIXSTR="Edit";
$ATTACHSTR="*
Document to be Printed
Document to be Printed
Document to be Printed
Document to be Printed ";
$ATTACHERR="Attachment is required";
} else {
$e="";
$ef=$ee;
$PREVIEWSTR="Prévisualiser";
$SENDSTR="Soumettre au services d'imprimerie";
$FIXSTR="éditer";
$ATTACHSTR="*
Document Etre Imprimé
Document Etre Imprimé
Document Etre Imprimé
Document Etre Imprimé ";
$ATTACHERR="Document est requis";
}
$submitstr=" ";
$submitstr.="";
#print "Content-type: text/html\n\n";
&getdata;
if ( $action eq $PREVIEWSTR)
{
$typetext="hidden";
$typetextarea="input type=hidden";
$closetextarea="";
%str=%in;
$listimg="";
$border=1;
$date=$today;
$submitstr= " ";
$submitstr.="" ;
}
if ($action eq "$SENDSTR")
{
if ( $query->param("fileid") || $query->param("c_mailit") )
{
$typetext="hidden";
$typetextarea="input type=hidden";
$closetextarea="";
%str=%in;
$listimg="";
$border=1;
$date=$today;
$submitstr="";
$formid=&newformid;
$formstr="Control #: $formid";
$ATTACHSTR="";
&mailto;
}
$error="$ATTACHERR";
}
my $tmp=&readhtml("$datapath/$tpl");
$tmp=&addinc($tmp);
print $tmp;
exit;
#return htmlfile from template
sub readhtml
{
my $selectname;
my $retfile;
open (DATA, "@_") || print "@_ HTML file not found";
while ()
{
#parse input data
#input type must be specified
#type specified right after ]?name=)"?(\w+)"?(.*?>)|$str{$2}|ig;
s||$closetextarea|ig;
s|((.*)|
package temp;
my $tmp;
if (($check{$selectname.$1}) or ($typetext eq "text"))
{$tmp=" " . $check{$selectname.$1} . $typetext . "text"}
$tmp;
|sgeix;
$retfile.=$_;
}
close (DATA);
return $retfile;
}
sub addinc
{
#------------------------------
#add include file just like ssi
#------------------------------
($_)=@_;
my $BASEDIR=$ENV{'DOCUMENT_ROOT'} || dirname (dirname ($datapath));
undef $/; #slurp whole file ignoring cr/lf
s|
|
package temp;
my $tmp;
my $file = $1;
if (!open(IN,$BASEDIR . "/$file")) {
$tmp = "[ INCLUDE ERROR: Cannot include $BASEDIR $file file ]";
}
else {
$tmp = ;
close(IN);
$tmp;
}
|sgeix;
$/="\n";
return $_
}
sub getdata
{
#--------------
#load form data
#--------------
foreach ($query->param)
{
$in{$_} = $query->param($_);
if ($_=~/^c_/) { $check{$_}="CHECKED" } #checkbox
if ($_=~/^r_/) { $check{$_.$in{$_}}="CHECKED" } #radio
if ($_=~/^s_/) { $check{$_.$in{$_}}="SELECTED" } #Select
$in{$_}=~s/"/"/g;
$in{$_}=~s/'/’/g;
$in{$_}=~s/\r\n/ /g || $in{$_}=~s/ /\r\n/g;
}
$debug++ if ($in{requestor} =~/debug/i);
}
sub mailto
{
#----------
#send email
#----------
$date_sub = "date submitted: ".localtime();
$logo="";
$listimg="";
my $FROM="Forms\@ottawa.ca";
my $SUBJECT= $in{reqdate} . " " . $in{docwords} . " " . $in{doctitle};
my $to=$MAILTO;
my $cc=$in{email};
my $body=&readhtml("$datapath/$tpl");
$body=&doclean($body);
$body.=&attachfile ("fileid"); #the attachments
$body.=&attachfile ("ref1", "ref1_");
$body.=&attachfile ("ref2", "ref2_");
$body.=&attachfile ("ref3", "ref3_");
$body.="--$boundary--";
my %mail=(SMTP => 'appsmtp.ottawa.ca',
To => $to,
cc => $cc,
From => $FROM,
subject => $SUBJECT,
'content-type'=> "multipart/mixed; boundary=\"$boundary\"",
body => "--$boundary\nContent-Type: text/html\n\n $body" );
if (!sendmail(%mail) )
{ print "$Mail::Sendmail::error;";
exit
}
print "This request was mailed to $to $cc
" if ($debug);
$tpl="confirm$lang.htm";
return
}
sub newformid
{
#-----------------------------------
#get next id number and increment it
#-----------------------------------
use File::CounterFile;
$File::CounterFile::DEFAULT_DIR="$datapath";
my $c = File::CounterFile->new("counter.dat");
$c->inc;
$c=sprintf("%06d", $c);
$c = "e".$c;
return $c;
}
sub doclean
{
#-------------------------------------------------
#remove the hidden fields and submit/reset buttons
#-------------------------------------------------
($_)=@_;
undef $/;
s|||imsg;
s|||imsg;
$/="\n";
return $_;
}
sub attachfile
{
#----------------------------------------------------
#create attachment to a mail message of uploaded file
#----------------------------------------------------
#$CGI::POST_MAX = 1024 * 5000; # 5MB file upload max
#$CGI::DISABLE_UPLOADS = 0; # allow uploads
#
my ($uploadfile,$prename)=@_;
my $tmp;
my $ATTACHDIR="$datapath/working";
my $filehandle = $query->upload("$uploadfile");
my $fileid = $query->param("$uploadfile");
return if (!$filehandle) ;
my @file = <$filehandle>;
close ($filehandle);
my $fid=$prename . basename($filehandle);
open (OUTFILE,">$ATTACHDIR/$fid") || print "failed to open $ATTACHDIR / $fid xx";
binmode OUTFILE;
print OUTFILE @file;
close OUTFILE;
open(F, "$ATTACHDIR/$fid") or die("Cannot read $fid: $!");
undef $/;
binmode F;
$tmp = encode_base64();
close F;
$/="\n";
#if ($query->cgi_error()){
#my $error = $query->cgi_error();
#"The file you are uploading is too large! Files are limited to $CGI::POST_MAX KB".$error;
print "...File $fileid uploaded " if ($debug);
return <