X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2FMisc.pm;h=936f94a6e30dee4ab7f63025d190da36d1123702;hp=54467a1fb75eea2dca3068fa549f343819fd003d;hb=7153190ee1bfeb6d3ad9e6da270a41a949333a7e;hpb=c648976f0b7975f2328ebd7ba8c711fad0ca4195 diff --git a/FS/FS/Misc.pm b/FS/FS/Misc.pm index 54467a1fb..936f94a6e 100644 --- a/FS/FS/Misc.pm +++ b/FS/FS/Misc.pm @@ -5,15 +5,17 @@ use vars qw ( @ISA @EXPORT_OK $DEBUG ); use Exporter; use Carp; use Data::Dumper; +use IPC::Run qw( run timeout ); # for _pslatex +use IPC::Run3; # for do_print... should just use IPC::Run i guess #do NOT depend on any FS:: modules here, causes weird (sometimes unreproducable #until on client machine) dependancy loops. put them in FS::Misc::Something #instead @ISA = qw( Exporter ); -@EXPORT_OK = qw( send_email send_fax +@EXPORT_OK = qw( generate_email send_email send_fax states_hash counties state_label card_types - generate_ps do_print + generate_ps generate_pdf do_print ); $DEBUG = 0; @@ -38,29 +40,173 @@ but are collected here to elimiate code duplication. =over 4 +=item generate_email OPTION => VALUE ... + +Options: + +=item from + +Sender address, required + +=item to + +Recipient address, required + +=item subject + +email subject, required + +=item html_body + +Email body (HTML alternative). Arrayref of lines, or scalar. + +Will be placed inside an HTML tag. + +=item text_body + +Email body (Text alternative). Arrayref of lines, or scalar. + +=back + +Returns an argument list to be passsed to L. + +=cut + +#false laziness w/FS::cust_bill::generate_email + +use MIME::Entity; +use HTML::Entities; + +sub generate_email { + my %args = @_; + + my $me = '[FS::Misc::generate_email]'; + + my %return = ( + 'from' => $args{'from'}, + 'to' => $args{'to'}, + 'subject' => $args{'subject'}, + ); + + #if (ref($args{'to'}) eq 'ARRAY') { + # $return{'to'} = $args{'to'}; + #} else { + # $return{'to'} = [ grep { $_ !~ /^(POST|FAX)$/ } + # $self->cust_main->invoicing_list + # ]; + #} + + warn "$me creating HTML/text multipart message" + if $DEBUG; + + $return{'nobody'} = 1; + + my $alternative = build MIME::Entity + 'Type' => 'multipart/alternative', + 'Encoding' => '7bit', + 'Disposition' => 'inline' + ; + + my $data; + if ( ref($args{'text_body'}) eq 'ARRAY' ) { + $data = $args{'text_body'}; + } else { + $data = [ split(/\n/, $args{'text_body'}) ]; + } + + $alternative->attach( + 'Type' => 'text/plain', + #'Encoding' => 'quoted-printable', + 'Encoding' => '7bit', + 'Data' => $data, + 'Disposition' => 'inline', + ); + + my @html_data; + if ( ref($args{'html_body'}) eq 'ARRAY' ) { + @html_data = @{ $args{'html_body'} }; + } else { + @html_data = split(/\n/, $args{'html_body'}); + } + + $alternative->attach( + 'Type' => 'text/html', + 'Encoding' => 'quoted-printable', + 'Data' => [ '', + ' ', + ' ', + ' '. encode_entities($return{'subject'}), + ' ', + ' ', + ' ', + @html_data, + ' ', + '', + ], + 'Disposition' => 'inline', + #'Filename' => 'invoice.pdf', + ); + + #no other attachment: + # multipart/related + # multipart/alternative + # text/plain + # text/html + + $return{'content-type'} = 'multipart/related'; + $return{'mimeparts'} = [ $alternative ]; + $return{'type'} = 'multipart/alternative'; #Content-Type of first part... + #$return{'disposition'} = 'inline'; + + %return; + +} + =item send_email OPTION => VALUE ... Options: -I - (required) +=over 4 + +=item from + +(required) + +=item to -I - (required) comma-separated scalar or arrayref of recipients +(required) comma-separated scalar or arrayref of recipients -I - (required) +=item subject -I - (optional) MIME type for the body +(required) -I - (required unless I is true) arrayref of body text lines +=item content-type -I - (optional, but required if I is true) arrayref of MIME::Entity->build PARAMHASH refs or MIME::Entity objects. These will be passed as arguments to MIME::Entity->attach(). +(optional) MIME type for the body -I - (optional) when set true, send_email will ignore the I option and simply construct a message with the given I. In this case, +=item body + +(required unless I is true) arrayref of body text lines + +=item mimeparts + +(optional, but required if I is true) arrayref of MIME::Entity->build PARAMHASH refs or MIME::Entity objects. These will be passed as arguments to MIME::Entity->attach(). + +=item nobody + +(optional) when set true, send_email will ignore the I option and simply construct a message with the given I. In this case, I, if specified, overrides the default "multipart/mixed" for the outermost MIME container. -I - (optional) when using nobody, optional top-level MIME +=item content-encoding + +(optional) when using nobody, optional top-level MIME encoding which, if specified, overrides the default "7bit". -I - (optional) type parameter for multipart/related messages +=item type + +(optional) type parameter for multipart/related messages + +=back =cut @@ -500,12 +646,7 @@ sub generate_ps { my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc; chdir($dir); - my $sfile = shell_quote $file; - - system("pslatex $sfile.tex >/dev/null 2>&1") == 0 - or die "pslatex $file.tex failed; see $file.log for details?\n"; - system("pslatex $sfile.tex >/dev/null 2>&1") == 0 - or die "pslatex $file.tex failed; see $file.log for details?\n"; + _pslatex($file); system('dvips', '-q', '-t', 'letter', "$file.dvi", '-o', "$file.ps" ) == 0 or die "dvips failed"; @@ -537,14 +678,87 @@ sub generate_ps { } +=item generate_pdf FILENAME + +Returns an PDF rendition of the LaTex file, as a scalar. FILENAME does not +contain the .tex suffix and is unlinked by this function. + +=cut + +use String::ShellQuote; + +sub generate_pdf { + my $file = shift; + + my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc; + chdir($dir); + + #system('pdflatex', "$file.tex"); + #system('pdflatex', "$file.tex"); + #! LaTeX Error: Unknown graphics extension: .eps. + + _pslatex($file); + + my $sfile = shell_quote $file; + + #system('dvipdf', "$file.dvi", "$file.pdf" ); + system( + "dvips -q -t letter -f $sfile.dvi ". + "| gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$sfile.pdf ". + " -c save pop -" + ) == 0 + or die "dvips | gs failed: $!"; + + open(PDF, "<$file.pdf") + or die "can't open $file.pdf: $! (error in LaTeX template?)\n"; + + unlink("$file.dvi", "$file.log", "$file.aux", "$file.pdf", "$file.tex"); + + my $pdf = ''; + while () { + $pdf .= $_; + } + + close PDF; + + return $pdf; + +} + +sub _pslatex { + my $file = shift; + + #my $sfile = shell_quote $file; + + my @cmd = ( + 'latex', + '-interaction=batchmode', + '\AtBeginDocument{\RequirePackage{pslatex}}', + '\def\PSLATEXTMP{\futurelet\PSLATEXTMP\PSLATEXTMPB}', + '\def\PSLATEXTMPB{\ifx\PSLATEXTMP\nonstopmode\else\input\fi}', + '\PSLATEXTMP', + "$file.tex" + ); + + my $timeout = 30; #? should be more than enough + + for ( 1, 2 ) { + + local($SIG{CHLD}) = sub {}; + #run( \@cmd, '>'=>'/dev/null', '2>'=>'/dev/null', timeout($timeout) ) + run( \@cmd, timeout($timeout) ) + or die "pslatex $file.tex failed; see $file.log for details?\n"; + + } + +} + =item print ARRAYREF Sends the lines in ARRAYREF to the printer. =cut -use IPC::Run3; - sub do_print { my $data = shift;