1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
9 package SimpleTemplate;
12 # 1. The template's file name
13 # 2. A reference to the Form object
14 # 3. A reference to the myconfig hash
17 # A new template object
31 $self->{"source"} = shift;
32 $self->{"form"} = shift;
33 $self->{"myconfig"} = shift;
34 $self->{"userspath"} = shift;
36 $self->{"error"} = undef;
44 # 1. A typeglob for the file handle. The output will be written
45 # to this file handle.
48 # 1 on success and undef or 0 if there was an error. In the latter case
49 # the calling function can retrieve the error message via $obj->get_error()
54 print(OUT "Hallo!\n");
60 return $self->{"error"};
73 package LaTeXTemplate;
77 @ISA = qw(SimpleTemplate);
82 return $type->SUPER::new(@_);
86 my ($self, $variable) = @_;
87 my $form = $self->{"form"};
90 ('order' => [quotemeta("\\"),
93 '"', '\$', '%', '_', '#', quotemeta('^'),
94 '{', '}', '<', '>', '£', "\r", '±',
96 quotemeta("\\") => '\\textbackslash ',
111 quotemeta('^') => '\^\\',
112 quotemeta("\n") => '\newline '
115 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
117 # Allow some HTML markup to be converted into the output format's
118 # corresponding markup code, e.g. bold or italic.
119 my %markup_replace = ('b' => 'textbf',
123 foreach my $key (keys(%markup_replace)) {
124 my $new = $markup_replace{$key};
125 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
131 sub substitute_vars {
132 my ($self, $text, @indices) = @_;
134 my $form = $self->{"form"};
136 while ($text =~ /<\%(.*?)\%>/) {
137 my ($var, @options) = split(/\s+/, $1);
138 my $value = $form->{$var};
140 for (my $i = 0; $i < scalar(@indices); $i++) {
141 last unless (ref($value) eq "ARRAY");
142 $value = $value->[$indices[$i]];
144 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
145 substr($text, $-[0], $+[0] - $-[0]) = $value;
152 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
154 my ($form, $new_contents) = ($self->{"form"}, "");
156 my $ary = $form->{$var};
157 for (my $i = 0; $i < scalar(@indices); $i++) {
158 last unless (ref($ary) eq "ARRAY");
159 $ary = $ary->[$indices[$i]];
163 my $current_page = 1;
164 my ($current_line, $corrent_row) = (0, 1);
166 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
167 $form->{"__first__"} = $i == 0;
168 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
169 $form->{"__odd__"} = (($i + 1) % 2) == 1;
170 $form->{"__counter__"} = $i + 1;
172 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
173 $self->{"chars_per_line"}) {
175 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
178 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
179 my $_description = $form->{"description"}->[$i];
180 while ($_description =~ /\\newline/) {
182 $_description =~ s/\\newline//;
186 if ($current_page == 1) {
187 $lpp = $self->{"lines_on_first_page"};
189 $lpp = $self->{"lines_on_second_page"};
192 # Yes we need a manual page break -- or the user has forced one
193 if ((($current_line + $lines) > $lpp) ||
194 ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
195 my $pb = $self->{"pagebreak_block"};
197 # replace the special variables <%sumcarriedforward%>
200 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
201 $pb =~ s/<%sumcarriedforward%>/$psum/g;
202 $pb =~ s/<%lastpage%>/$current_page/g;
204 my $new_text = $self->parse_block($pb, (@indices, $i));
205 return undef unless (defined($new_text));
206 $new_contents .= $new_text;
211 $current_line += $lines;
213 if ($i < scalar(@{$form->{"linetotal"}})) {
214 $sum += $form->parse_amount($self->{"myconfig"},
215 $form->{"linetotal"}->[$i]);
218 $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
220 my $new_text = $self->parse_block($text, (@indices, $i));
221 return undef unless (defined($new_text));
222 $new_contents .= $start_tag . $new_text . $end_tag;
224 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
226 return $new_contents;
230 my ($self, $text, $pos, $var, $not) = @_;
233 $pos = 0 unless ($pos);
235 while ($pos < length($text)) {
238 next if (substr($text, $pos - 1, 2) ne '<%');
240 if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
243 } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
245 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
249 my $block = substr($text, 0, $pos - 1);
250 substr($text, 0, $pos - 1) = "";
251 $text =~ s!^<\%[^\%]+\%>!!;
252 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
254 return ($block, $text);
256 } elsif (substr($text, $pos + 1, 3) eq 'end') {
259 my $block = substr($text, 0, $pos - 1);
260 substr($text, 0, $pos - 1) = "";
261 $text =~ s!^<\%[^\%]+\%>!!;
263 return ($block, $text);
272 $main::lxdebug->enter_sub();
274 my ($self, $contents, @indices) = @_;
276 my $new_contents = "";
278 while ($contents ne "") {
279 my $pos_if = index($contents, '<%if');
280 my $pos_foreach = index($contents, '<%foreach');
282 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
283 $new_contents .= $self->substitute_vars($contents, @indices);
287 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
288 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
289 substr($contents, 0, $pos_foreach) = "";
291 if ($contents !~ m|^<\%foreach (.*?)\%>|) {
292 $self->{"error"} = "Malformed <\%foreach\%>.";
293 $main::lxdebug->leave_sub();
299 substr($contents, 0, length($&)) = "";
302 ($block, $contents) = $self->find_end($contents);
304 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
305 $main::lxdebug->leave_sub();
309 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
310 if (!defined($new_text)) {
311 $main::lxdebug->leave_sub();
314 $new_contents .= $new_text;
317 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
318 substr($contents, 0, $pos_if) = "";
320 if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
321 $self->{"error"} = "Malformed <\%if\%>.";
322 $main::lxdebug->leave_sub();
326 my ($not, $var) = ($1, $2);
328 substr($contents, 0, length($&)) = "";
330 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
332 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
333 $main::lxdebug->leave_sub();
337 my $value = $self->{"form"}->{$var};
338 for (my $i = 0; $i < scalar(@indices); $i++) {
339 last unless (ref($value) eq "ARRAY");
340 $value = $value->[$indices[$i]];
343 if (($not && !$value) || (!$not && $value)) {
344 my $new_text = $self->parse_block($block, @indices);
345 if (!defined($new_text)) {
346 $main::lxdebug->leave_sub();
349 $new_contents .= $new_text;
354 $main::lxdebug->leave_sub();
356 return $new_contents;
362 my $form = $self->{"form"};
364 if (!open(IN, "$form->{templates}/$form->{IN}")) {
365 $self->{"error"} = "$!";
371 my $contents = join("", @_);
373 # detect pagebreak block and its parameters
374 if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
375 $self->{"chars_per_line"} = $1;
376 $self->{"lines_on_first_page"} = $2;
377 $self->{"lines_on_second_page"} = $3;
378 $self->{"pagebreak_block"} = $4;
380 substr($contents, length($`), length($&)) = "";
383 $self->{"forced_pagebreaks"} = [];
385 my $new_contents = $self->parse_block($contents);
386 if (!defined($new_contents)) {
387 $main::lxdebug->leave_sub();
391 print(OUT $new_contents);
393 if ($form->{"format"} =~ /postscript/i) {
394 return $self->convert_to_postscript();
395 } elsif ($form->{"format"} =~ /pdf/i) {
396 return $self->convert_to_pdf();
402 sub convert_to_postscript {
404 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
406 # Convert the tex file to postscript
408 if (!chdir("$userspath")) {
409 $self->{"error"} = "chdir : $!";
414 $form->{tmpfile} =~ s/$userspath\///g;
416 for (my $run = 1; $run <= 2; $run++) {
417 system("latex --interaction=nonstopmode $form->{tmpfile} " .
418 "> $form->{tmpfile}.err");
420 $self->{"error"} = $form->cleanup();
426 $form->{tmpfile} =~ s/tex$/dvi/;
428 system("dvips $form->{tmpfile} -o -q > /dev/null");
430 $self->{"error"} = "dvips : $!";
434 $form->{tmpfile} =~ s/dvi$/ps/;
443 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
445 # Convert the tex file to PDF
447 if (!chdir("$userspath")) {
448 $self->{"error"} = "chdir : $!";
453 $form->{tmpfile} =~ s/$userspath\///g;
455 for (my $run = 1; $run <= 2; $run++) {
456 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
457 "> $form->{tmpfile}.err");
459 $self->{"error"} = $form->cleanup();
465 $form->{tmpfile} =~ s/tex$/pdf/;
470 sub get_mime_type() {
473 if ($self->{"form"}->{"format"} =~ /postscript/i) {
474 return "application/postscript";
476 return "application/pdf";
489 package HTMLTemplate;
493 @ISA = qw(LaTeXTemplate);
498 return $type->SUPER::new(@_);
502 my ($self, $variable) = @_;
503 my $form = $self->{"form"};
506 ('order' => ['<', '>', quotemeta("\n")],
509 quotemeta("\n") => '<br>',
512 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
514 # Allow some HTML markup to be converted into the output format's
515 # corresponding markup code, e.g. bold or italic.
516 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
518 foreach my $key (@markup_replace) {
519 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
525 sub get_mime_type() {
528 if ($self->{"form"}->{"format"} =~ /postscript/i) {
529 return "application/postscript";
530 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
531 return "application/pdf";
540 if ($self->{"form"}->{"format"} =~ /postscript/i) {
542 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
549 sub convert_to_postscript {
551 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
553 # Convert the HTML file to postscript
555 if (!chdir("$userspath")) {
556 $self->{"error"} = "chdir : $!";
561 $form->{"tmpfile"} =~ s/$userspath\///g;
562 my $psfile = $form->{"tmpfile"};
563 $psfile =~ s/.html/.ps/;
564 if ($psfile eq $form->{"tmpfile"}) {
568 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
570 $self->{"error"} = $form->cleanup();
575 $form->{"tmpfile"} = $psfile;
584 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
586 # Convert the HTML file to PDF
588 if (!chdir("$userspath")) {
589 $self->{"error"} = "chdir : $!";
594 $form->{"tmpfile"} =~ s/$userspath\///g;
595 my $pdffile = $form->{"tmpfile"};
596 $pdffile =~ s/.html/.pdf/;
597 if ($pdffile eq $form->{"tmpfile"}) {
601 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
603 $self->{"error"} = $form->cleanup();
608 $form->{"tmpfile"} = $pdffile;
617 #### OpenDocumentTemplate
620 package OpenDocumentTemplate;
628 # use File::Temp qw(:mktemp);
631 @ISA = qw(SimpleTemplate);
636 $self = $type->SUPER::new(@_);
638 foreach my $module (qw(Archive::Zip Text::Iconv)) {
639 eval("use ${module};");
641 $self->{"form"}->error("The Perl module '${module}' could not be " .
642 "loaded. Support for OpenDocument templates " .
643 "does not work without it. Please install your " .
644 "distribution's package or get the module from " .
645 "CPAN ( http://www.cpan.org ).");
649 $self->{"rnd"} = int(rand(1000000));
650 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
655 sub substitute_vars {
656 my ($self, $text, @indices) = @_;
658 my $form = $self->{"form"};
660 while ($text =~ /\<\%(.*?)\%\>/) {
661 my $value = $form->{$1};
663 for (my $i = 0; $i < scalar(@indices); $i++) {
664 last unless (ref($value) eq "ARRAY");
665 $value = $value->[$indices[$i]];
667 substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
674 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
676 my ($form, $new_contents) = ($self->{"form"}, "");
678 my $ary = $form->{$var};
679 for (my $i = 0; $i < scalar(@indices); $i++) {
680 last unless (ref($ary) eq "ARRAY");
681 $ary = $ary->[$indices[$i]];
684 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
685 $form->{"__first__"} = $i == 0;
686 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
687 $form->{"__odd__"} = (($i + 1) % 2) == 1;
688 $form->{"__counter__"} = $i + 1;
689 my $new_text = $self->parse_block($text, (@indices, $i));
690 return undef unless (defined($new_text));
691 $new_contents .= $start_tag . $new_text . $end_tag;
693 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
695 return $new_contents;
699 my ($self, $text, $pos, $var, $not) = @_;
702 $pos = 0 unless ($pos);
704 while ($pos < length($text)) {
707 next if (substr($text, $pos - 1, 5) ne '<%');
709 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
712 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
714 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
718 my $block = substr($text, 0, $pos - 1);
719 substr($text, 0, $pos - 1) = "";
720 $text =~ s!^\<\%[^\%]+\%\>!!;
721 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
723 return ($block, $text);
725 } elsif (substr($text, $pos + 4, 3) eq 'end') {
728 my $block = substr($text, 0, $pos - 1);
729 substr($text, 0, $pos - 1) = "";
730 $text =~ s!^\<\%[^\%]+\%\>!!;
732 return ($block, $text);
741 $main::lxdebug->enter_sub();
743 my ($self, $contents, @indices) = @_;
745 my $new_contents = "";
747 while ($contents ne "") {
748 if (substr($contents, 0, 1) eq "<") {
749 $contents =~ m|^<[^>]+>|;
751 substr($contents, 0, length($&)) = "";
753 if ($tag =~ m|<table:table-row|) {
754 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
757 substr($contents, 0, length($1) + length($end_tag)) = "";
759 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
762 substr($table_row, length($`), length($&)) = "";
764 my ($t1, $t2) = $self->find_end($table_row, length($`));
766 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
767 $main::lxdebug->leave_sub();
771 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
772 if (!defined($new_text)) {
773 $main::lxdebug->leave_sub();
776 $new_contents .= $new_text;
779 my $new_text = $self->parse_block($table_row, @indices);
780 if (!defined($new_text)) {
781 $main::lxdebug->leave_sub();
784 $new_contents .= $tag . $new_text . $end_tag;
788 $new_contents .= $tag;
792 $contents =~ /^[^<]+/;
795 my $pos_if = index($text, '<%if');
796 my $pos_foreach = index($text, '<%foreach');
798 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
799 substr($contents, 0, length($text)) = "";
800 $new_contents .= $self->substitute_vars($text, @indices);
804 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
805 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
806 substr($contents, 0, $pos_foreach) = "";
808 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
809 $self->{"error"} = "Malformed <\%foreach\%>.";
810 $main::lxdebug->leave_sub();
816 substr($contents, 0, length($&)) = "";
819 ($block, $contents) = $self->find_end($contents);
821 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
822 $main::lxdebug->leave_sub();
826 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
827 if (!defined($new_text)) {
828 $main::lxdebug->leave_sub();
831 $new_contents .= $new_text;
834 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
835 substr($contents, 0, $pos_if) = "";
837 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
838 $self->{"error"} = "Malformed <\%if\%>.";
839 $main::lxdebug->leave_sub();
843 my ($not, $var) = ($1, $2);
845 substr($contents, 0, length($&)) = "";
847 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
849 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
850 $main::lxdebug->leave_sub();
854 my $value = $self->{"form"}->{$var};
855 for (my $i = 0; $i < scalar(@indices); $i++) {
856 last unless (ref($value) eq "ARRAY");
857 $value = $value->[$indices[$i]];
860 if (($not && !$value) || (!$not && $value)) {
861 my $new_text = $self->parse_block($block, @indices);
862 if (!defined($new_text)) {
863 $main::lxdebug->leave_sub();
866 $new_contents .= $new_text;
872 $main::lxdebug->leave_sub();
874 return $new_contents;
878 $main::lxdebug->enter_sub();
882 my $form = $self->{"form"};
887 if ($form->{"IN"} =~ m|^/|) {
888 $file_name = $form->{"IN"};
890 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
893 my $zip = Archive::Zip->new();
894 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
895 $self->{"error"} = "File not found/is not a OpenDocument file.";
896 $main::lxdebug->leave_sub();
900 my $contents = $zip->contents("content.xml");
902 $self->{"error"} = "File is not a OpenDocument file.";
903 $main::lxdebug->leave_sub();
907 my $rnd = $self->{"rnd"};
908 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
909 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
911 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
912 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
914 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
915 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
917 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
918 <style:text-properties style:text-line-through-style="solid"/>
920 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
921 <style:text-properties style:text-position="super 58%"/>
923 <style:style style:name="TLXO${rnd}SUB" style:family="text">
924 <style:text-properties style:text-position="sub 58%"/>
928 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
929 $contents =~ s|[\n\r]||gm;
931 my $new_contents = $self->parse_block($contents);
932 if (!defined($new_contents)) {
933 $main::lxdebug->leave_sub();
937 # $new_contents =~ s|>|>\n|g;
939 $zip->contents("content.xml", $new_contents);
941 my $styles = $zip->contents("styles.xml");
943 my $new_styles = $self->parse_block($styles);
944 if (!defined($new_contents)) {
945 $main::lxdebug->leave_sub();
948 $zip->contents("styles.xml", $new_styles);
951 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
954 if ($form->{"format"} =~ /pdf/) {
955 $res = $self->convert_to_pdf();
958 $main::lxdebug->leave_sub();
962 sub is_xvfb_running {
963 $main::lxdebug->enter_sub();
968 my $dfname = $self->{"userspath"} . "/xvfb_display";
971 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
972 if ((-f $dfname) && open(IN, $dfname)) {
977 my $xauthority = <IN>;
981 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
983 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
984 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
985 unlink($dfname, $xauthority);
986 $main::lxdebug->leave_sub();
991 if ($line !~ /xvfb/i) {
992 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
993 unlink($dfname, $xauthority);
994 $main::lxdebug->leave_sub();
998 $ENV{"XAUTHORITY"} = $xauthority;
999 $ENV{"DISPLAY"} = $display;
1001 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1004 $main::lxdebug->leave_sub();
1010 $main::lxdebug->enter_sub();
1014 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1016 my $display = $self->is_xvfb_running();
1019 $main::lxdebug->leave_sub();
1024 while ( -f "/tmp/.X${display}-lock") {
1027 $display = ":${display}";
1028 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1030 my $mcookie = `mcookie`;
1031 die("Installation error: mcookie not found.") if ($? != 0);
1034 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1036 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1037 $ENV{"XAUTHORITY"} = $xauthority;
1039 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1041 system("xauth add \"${display}\" . \"${mcookie}\"");
1043 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1044 $main::lxdebug->leave_sub();
1048 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1052 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1053 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1056 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1059 my $dfname = $self->{"userspath"} . "/xvfb_display";
1060 if (!open(OUT, ">$dfname")) {
1061 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1062 unlink($xauthority);
1064 $main::lxdebug->leave_sub();
1067 print(OUT "$pid\n$display\n$xauthority\n");
1070 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1072 if (!$self->is_xvfb_running()) {
1073 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1074 unlink($xauthority, $dfname);
1076 $main::lxdebug->leave_sub();
1080 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1082 $main::lxdebug->leave_sub();
1087 sub is_openoffice_running {
1088 $main::lxdebug->enter_sub();
1090 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1091 "> /dev/null 2> /dev/null");
1093 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1095 $main::lxdebug->leave_sub();
1100 sub spawn_openoffice {
1101 $main::lxdebug->enter_sub();
1105 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1107 my ($try, $spawned_oo, $res);
1110 for ($try = 0; $try < 15; $try++) {
1111 if ($self->is_openoffice_running()) {
1119 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1121 open(STDIN, '/dev/null');
1122 open(STDOUT, '>/dev/null');
1123 my $new_pid = fork();
1125 my $ssres = setsid();
1126 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1127 my @cmdline = ($main::openofficeorg_writer_bin,
1128 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1130 "-accept=socket,host=localhost,port=" .
1131 $main::openofficeorg_daemon_port . ";urp;");
1135 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1140 sleep($try >= 5 ? 2 : 1);
1144 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1145 "OpenOffice could not be started.";
1148 $main::lxdebug->leave_sub();
1153 sub convert_to_pdf {
1154 $main::lxdebug->enter_sub();
1158 my $form = $self->{"form"};
1160 my $filename = $form->{"tmpfile"};
1161 $filename =~ s/.odt$//;
1162 if (substr($filename, 0, 1) ne "/") {
1163 $filename = getcwd() . "/${filename}";
1166 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1167 $ENV{'HOME'} = $self->{"userspath"};
1169 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1172 if (!$self->spawn_xvfb()) {
1173 $main::lxdebug->leave_sub();
1178 if (!$main::openofficeorg_daemon) {
1179 @cmdline = ($main::openofficeorg_writer_bin,
1180 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1182 "file:${filename}.odt",
1183 "macro://" . (split('/', $filename))[-1] .
1184 "/Standard.Conversion.ConvertSelfToPDF()");
1186 if (!$self->spawn_openoffice()) {
1187 $main::lxdebug->leave_sub();
1191 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1192 $main::openofficeorg_daemon_port,
1200 $form->{"tmpfile"} =~ s/odt$/pdf/;
1202 unlink($filename . ".odt");
1204 $main::lxdebug->leave_sub();
1209 unlink($filename . ".odt", $filename . ".pdf");
1210 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1213 $main::lxdebug->leave_sub();
1218 my ($self, $variable) = @_;
1219 my $form = $self->{"form"};
1220 my $iconv = $self->{"iconv"};
1223 ('order' => ['&', '<', '>', '"', "'",
1225 quotemeta("\n"), quotemeta("\r")],
1231 '\x80' => chr(0xa4), # Euro
1232 quotemeta("\n") => '<text:line-break/>',
1233 quotemeta("\r") => '',
1236 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1238 # Allow some HTML markup to be converted into the output format's
1239 # corresponding markup code, e.g. bold or italic.
1240 my $rnd = $self->{"rnd"};
1241 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1242 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1244 foreach my $key (keys(%markup_replace)) {
1245 my $value = $markup_replace{$key};
1246 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
1247 $variable =~ s|\</${key}\>|</text:span>|gi;
1250 return $iconv->convert($variable);
1253 sub get_mime_type() {
1254 if ($self->{"form"}->{"format"} =~ /pdf/) {
1255 return "application/pdf";
1257 return "application/vnd.oasis.opendocument.text";
1261 sub uses_temp_file {
1266 ##########################################################
1270 ##########################################################
1272 package XMLTemplate;
1276 @ISA = qw(HTMLTemplate);
1279 #evtl auskommentieren
1282 return $type->SUPER::new(@_);
1286 my ($self, $variable) = @_;
1287 my $form = $self->{"form"};
1290 ('order' => ['<', '>', quotemeta("\n")],
1293 quotemeta("\n") => '<br>',
1296 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1298 # Allow no markup to be converted into the output format
1299 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1301 foreach my $key (@markup_replace) {
1302 $variable =~ s/\<(\/?)${key}\>//g;
1308 sub get_mime_type() {
1311 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1312 return "application/xml ";
1313 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1314 return "application/x-taxbird";
1320 sub uses_temp_file {
1321 # tempfile needet for XML Output