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", '±', '\xe1',
96 quotemeta("\\") => '\\textbackslash ',
111 '\xe1' => '$\bullet$',
112 quotemeta('^') => '\^\\',
113 quotemeta("\n") => '\newline '
116 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
118 # Allow some HTML markup to be converted into the output format's
119 # corresponding markup code, e.g. bold or italic.
120 my %markup_replace = ('b' => 'textbf',
124 foreach my $key (keys(%markup_replace)) {
125 my $new = $markup_replace{$key};
126 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
129 $variable =~ s/[\x00-\x1f]//g;
134 sub substitute_vars {
135 my ($self, $text, @indices) = @_;
137 my $form = $self->{"form"};
139 while ($text =~ /<\%(.*?)\%>/) {
140 my ($var, @options) = split(/\s+/, $1);
141 my $value = $form->{$var};
143 for (my $i = 0; $i < scalar(@indices); $i++) {
144 last unless (ref($value) eq "ARRAY");
145 $value = $value->[$indices[$i]];
147 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
148 substr($text, $-[0], $+[0] - $-[0]) = $value;
155 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
157 my ($form, $new_contents) = ($self->{"form"}, "");
159 my $ary = $form->{$var};
160 for (my $i = 0; $i < scalar(@indices); $i++) {
161 last unless (ref($ary) eq "ARRAY");
162 $ary = $ary->[$indices[$i]];
166 my $current_page = 1;
167 my ($current_line, $corrent_row) = (0, 1);
169 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
170 $form->{"__first__"} = $i == 0;
171 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
172 $form->{"__odd__"} = (($i + 1) % 2) == 1;
173 $form->{"__counter__"} = $i + 1;
175 if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
176 $self->{"chars_per_line"}) {
178 int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
181 $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
182 my $_description = $form->{"description"}->[$i];
183 while ($_description =~ /\\newline/) {
185 $_description =~ s/\\newline//;
189 if ($current_page == 1) {
190 $lpp = $self->{"lines_on_first_page"};
192 $lpp = $self->{"lines_on_second_page"};
195 # Yes we need a manual page break -- or the user has forced one
196 if ((($current_line + $lines) > $lpp) ||
197 ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
198 my $pb = $self->{"pagebreak_block"};
200 # replace the special variables <%sumcarriedforward%>
203 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
204 $pb =~ s/<%sumcarriedforward%>/$psum/g;
205 $pb =~ s/<%lastpage%>/$current_page/g;
207 my $new_text = $self->parse_block($pb, (@indices, $i));
208 return undef unless (defined($new_text));
209 $new_contents .= $new_text;
214 $current_line += $lines;
216 if ($i < scalar(@{$form->{"linetotal"}})) {
217 $sum += $form->parse_amount($self->{"myconfig"},
218 $form->{"linetotal"}->[$i]);
221 $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
223 my $new_text = $self->parse_block($text, (@indices, $i));
224 return undef unless (defined($new_text));
225 $new_contents .= $start_tag . $new_text . $end_tag;
227 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
229 return $new_contents;
233 my ($self, $text, $pos, $var, $not) = @_;
236 $pos = 0 unless ($pos);
238 while ($pos < length($text)) {
241 next if (substr($text, $pos - 1, 2) ne '<%');
243 if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
246 } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
248 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
252 my $block = substr($text, 0, $pos - 1);
253 substr($text, 0, $pos - 1) = "";
254 $text =~ s!^<\%[^\%]+\%>!!;
255 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
257 return ($block, $text);
259 } elsif (substr($text, $pos + 1, 3) eq 'end') {
262 my $block = substr($text, 0, $pos - 1);
263 substr($text, 0, $pos - 1) = "";
264 $text =~ s!^<\%[^\%]+\%>!!;
266 return ($block, $text);
275 $main::lxdebug->enter_sub();
277 my ($self, $contents, @indices) = @_;
279 my $new_contents = "";
281 while ($contents ne "") {
282 my $pos_if = index($contents, '<%if');
283 my $pos_foreach = index($contents, '<%foreach');
285 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
286 $new_contents .= $self->substitute_vars($contents, @indices);
290 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
291 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
292 substr($contents, 0, $pos_foreach) = "";
294 if ($contents !~ m|^<\%foreach (.*?)\%>|) {
295 $self->{"error"} = "Malformed <\%foreach\%>.";
296 $main::lxdebug->leave_sub();
302 substr($contents, 0, length($&)) = "";
305 ($block, $contents) = $self->find_end($contents);
307 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
308 $main::lxdebug->leave_sub();
312 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
313 if (!defined($new_text)) {
314 $main::lxdebug->leave_sub();
317 $new_contents .= $new_text;
320 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
321 substr($contents, 0, $pos_if) = "";
323 if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
324 $self->{"error"} = "Malformed <\%if\%>.";
325 $main::lxdebug->leave_sub();
329 my ($not, $var) = ($1, $2);
331 substr($contents, 0, length($&)) = "";
333 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
335 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
336 $main::lxdebug->leave_sub();
340 my $value = $self->{"form"}->{$var};
341 for (my $i = 0; $i < scalar(@indices); $i++) {
342 last unless (ref($value) eq "ARRAY");
343 $value = $value->[$indices[$i]];
346 if (($not && !$value) || (!$not && $value)) {
347 my $new_text = $self->parse_block($block, @indices);
348 if (!defined($new_text)) {
349 $main::lxdebug->leave_sub();
352 $new_contents .= $new_text;
357 $main::lxdebug->leave_sub();
359 return $new_contents;
365 my $form = $self->{"form"};
367 if (!open(IN, "$form->{templates}/$form->{IN}")) {
368 $self->{"error"} = "$!";
374 my $contents = join("", @_);
376 # detect pagebreak block and its parameters
377 if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
378 $self->{"chars_per_line"} = $1;
379 $self->{"lines_on_first_page"} = $2;
380 $self->{"lines_on_second_page"} = $3;
381 $self->{"pagebreak_block"} = $4;
383 substr($contents, length($`), length($&)) = "";
386 $self->{"forced_pagebreaks"} = [];
388 my $new_contents = $self->parse_block($contents);
389 if (!defined($new_contents)) {
390 $main::lxdebug->leave_sub();
394 print(OUT $new_contents);
396 if ($form->{"format"} =~ /postscript/i) {
397 return $self->convert_to_postscript();
398 } elsif ($form->{"format"} =~ /pdf/i) {
399 return $self->convert_to_pdf();
405 sub convert_to_postscript {
407 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
409 # Convert the tex file to postscript
411 if (!chdir("$userspath")) {
412 $self->{"error"} = "chdir : $!";
417 $form->{tmpfile} =~ s/$userspath\///g;
419 for (my $run = 1; $run <= 2; $run++) {
420 system("latex --interaction=nonstopmode $form->{tmpfile} " .
421 "> $form->{tmpfile}.err");
423 $self->{"error"} = $form->cleanup();
429 $form->{tmpfile} =~ s/tex$/dvi/;
431 system("dvips $form->{tmpfile} -o -q > /dev/null");
433 $self->{"error"} = "dvips : $!";
437 $form->{tmpfile} =~ s/dvi$/ps/;
446 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
448 # Convert the tex file to PDF
450 if (!chdir("$userspath")) {
451 $self->{"error"} = "chdir : $!";
456 $form->{tmpfile} =~ s/$userspath\///g;
458 for (my $run = 1; $run <= 2; $run++) {
459 system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
460 "> $form->{tmpfile}.err");
462 $self->{"error"} = $form->cleanup();
468 $form->{tmpfile} =~ s/tex$/pdf/;
473 sub get_mime_type() {
476 if ($self->{"form"}->{"format"} =~ /postscript/i) {
477 return "application/postscript";
479 return "application/pdf";
492 package HTMLTemplate;
496 @ISA = qw(LaTeXTemplate);
501 return $type->SUPER::new(@_);
505 my ($self, $variable) = @_;
506 my $form = $self->{"form"};
509 ('order' => ['<', '>', quotemeta("\n")],
512 quotemeta("\n") => '<br>',
515 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
517 # Allow some HTML markup to be converted into the output format's
518 # corresponding markup code, e.g. bold or italic.
519 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
521 foreach my $key (@markup_replace) {
522 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
528 sub get_mime_type() {
531 if ($self->{"form"}->{"format"} =~ /postscript/i) {
532 return "application/postscript";
533 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
534 return "application/pdf";
543 if ($self->{"form"}->{"format"} =~ /postscript/i) {
545 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
552 sub convert_to_postscript {
554 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
556 # Convert the HTML file to postscript
558 if (!chdir("$userspath")) {
559 $self->{"error"} = "chdir : $!";
564 $form->{"tmpfile"} =~ s/$userspath\///g;
565 my $psfile = $form->{"tmpfile"};
566 $psfile =~ s/.html/.ps/;
567 if ($psfile eq $form->{"tmpfile"}) {
571 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
573 $self->{"error"} = $form->cleanup();
578 $form->{"tmpfile"} = $psfile;
587 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
589 # Convert the HTML file to PDF
591 if (!chdir("$userspath")) {
592 $self->{"error"} = "chdir : $!";
597 $form->{"tmpfile"} =~ s/$userspath\///g;
598 my $pdffile = $form->{"tmpfile"};
599 $pdffile =~ s/.html/.pdf/;
600 if ($pdffile eq $form->{"tmpfile"}) {
604 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
606 $self->{"error"} = $form->cleanup();
611 $form->{"tmpfile"} = $pdffile;
620 #### OpenDocumentTemplate
623 package OpenDocumentTemplate;
631 # use File::Temp qw(:mktemp);
634 @ISA = qw(SimpleTemplate);
639 $self = $type->SUPER::new(@_);
641 foreach my $module (qw(Archive::Zip Text::Iconv)) {
642 eval("use ${module};");
644 $self->{"form"}->error("The Perl module '${module}' could not be " .
645 "loaded. Support for OpenDocument templates " .
646 "does not work without it. Please install your " .
647 "distribution's package or get the module from " .
648 "CPAN ( http://www.cpan.org ).");
652 $self->{"rnd"} = int(rand(1000000));
653 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
658 sub substitute_vars {
659 my ($self, $text, @indices) = @_;
661 my $form = $self->{"form"};
663 while ($text =~ /\<\%(.*?)\%\>/) {
664 my $value = $form->{$1};
666 for (my $i = 0; $i < scalar(@indices); $i++) {
667 last unless (ref($value) eq "ARRAY");
668 $value = $value->[$indices[$i]];
670 substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
677 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
679 my ($form, $new_contents) = ($self->{"form"}, "");
681 my $ary = $form->{$var};
682 for (my $i = 0; $i < scalar(@indices); $i++) {
683 last unless (ref($ary) eq "ARRAY");
684 $ary = $ary->[$indices[$i]];
687 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
688 $form->{"__first__"} = $i == 0;
689 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
690 $form->{"__odd__"} = (($i + 1) % 2) == 1;
691 $form->{"__counter__"} = $i + 1;
692 my $new_text = $self->parse_block($text, (@indices, $i));
693 return undef unless (defined($new_text));
694 $new_contents .= $start_tag . $new_text . $end_tag;
696 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
698 return $new_contents;
702 my ($self, $text, $pos, $var, $not) = @_;
705 $pos = 0 unless ($pos);
707 while ($pos < length($text)) {
710 next if (substr($text, $pos - 1, 5) ne '<%');
712 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
715 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
717 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
721 my $block = substr($text, 0, $pos - 1);
722 substr($text, 0, $pos - 1) = "";
723 $text =~ s!^\<\%[^\%]+\%\>!!;
724 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
726 return ($block, $text);
728 } elsif (substr($text, $pos + 4, 3) eq 'end') {
731 my $block = substr($text, 0, $pos - 1);
732 substr($text, 0, $pos - 1) = "";
733 $text =~ s!^\<\%[^\%]+\%\>!!;
735 return ($block, $text);
744 $main::lxdebug->enter_sub();
746 my ($self, $contents, @indices) = @_;
748 my $new_contents = "";
750 while ($contents ne "") {
751 if (substr($contents, 0, 1) eq "<") {
752 $contents =~ m|^<[^>]+>|;
754 substr($contents, 0, length($&)) = "";
756 if ($tag =~ m|<table:table-row|) {
757 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
760 substr($contents, 0, length($1) + length($end_tag)) = "";
762 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
765 substr($table_row, length($`), length($&)) = "";
767 my ($t1, $t2) = $self->find_end($table_row, length($`));
769 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
770 $main::lxdebug->leave_sub();
774 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
775 if (!defined($new_text)) {
776 $main::lxdebug->leave_sub();
779 $new_contents .= $new_text;
782 my $new_text = $self->parse_block($table_row, @indices);
783 if (!defined($new_text)) {
784 $main::lxdebug->leave_sub();
787 $new_contents .= $tag . $new_text . $end_tag;
791 $new_contents .= $tag;
795 $contents =~ /^[^<]+/;
798 my $pos_if = index($text, '<%if');
799 my $pos_foreach = index($text, '<%foreach');
801 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
802 substr($contents, 0, length($text)) = "";
803 $new_contents .= $self->substitute_vars($text, @indices);
807 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
808 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
809 substr($contents, 0, $pos_foreach) = "";
811 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
812 $self->{"error"} = "Malformed <\%foreach\%>.";
813 $main::lxdebug->leave_sub();
819 substr($contents, 0, length($&)) = "";
822 ($block, $contents) = $self->find_end($contents);
824 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
825 $main::lxdebug->leave_sub();
829 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
830 if (!defined($new_text)) {
831 $main::lxdebug->leave_sub();
834 $new_contents .= $new_text;
837 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
838 substr($contents, 0, $pos_if) = "";
840 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
841 $self->{"error"} = "Malformed <\%if\%>.";
842 $main::lxdebug->leave_sub();
846 my ($not, $var) = ($1, $2);
848 substr($contents, 0, length($&)) = "";
850 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
852 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
853 $main::lxdebug->leave_sub();
857 my $value = $self->{"form"}->{$var};
858 for (my $i = 0; $i < scalar(@indices); $i++) {
859 last unless (ref($value) eq "ARRAY");
860 $value = $value->[$indices[$i]];
863 if (($not && !$value) || (!$not && $value)) {
864 my $new_text = $self->parse_block($block, @indices);
865 if (!defined($new_text)) {
866 $main::lxdebug->leave_sub();
869 $new_contents .= $new_text;
875 $main::lxdebug->leave_sub();
877 return $new_contents;
881 $main::lxdebug->enter_sub();
885 my $form = $self->{"form"};
890 if ($form->{"IN"} =~ m|^/|) {
891 $file_name = $form->{"IN"};
893 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
896 my $zip = Archive::Zip->new();
897 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
898 $self->{"error"} = "File not found/is not a OpenDocument file.";
899 $main::lxdebug->leave_sub();
903 my $contents = $zip->contents("content.xml");
905 $self->{"error"} = "File is not a OpenDocument file.";
906 $main::lxdebug->leave_sub();
910 my $rnd = $self->{"rnd"};
911 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
912 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
914 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
915 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
917 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
918 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
920 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
921 <style:text-properties style:text-line-through-style="solid"/>
923 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
924 <style:text-properties style:text-position="super 58%"/>
926 <style:style style:name="TLXO${rnd}SUB" style:family="text">
927 <style:text-properties style:text-position="sub 58%"/>
931 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
932 $contents =~ s|[\n\r]||gm;
934 my $new_contents = $self->parse_block($contents);
935 if (!defined($new_contents)) {
936 $main::lxdebug->leave_sub();
940 # $new_contents =~ s|>|>\n|g;
942 $zip->contents("content.xml", $new_contents);
944 my $styles = $zip->contents("styles.xml");
946 my $new_styles = $self->parse_block($styles);
947 if (!defined($new_contents)) {
948 $main::lxdebug->leave_sub();
951 $zip->contents("styles.xml", $new_styles);
954 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
957 if ($form->{"format"} =~ /pdf/) {
958 $res = $self->convert_to_pdf();
961 $main::lxdebug->leave_sub();
965 sub is_xvfb_running {
966 $main::lxdebug->enter_sub();
971 my $dfname = $self->{"userspath"} . "/xvfb_display";
974 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
975 if ((-f $dfname) && open(IN, $dfname)) {
980 my $xauthority = <IN>;
984 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
986 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
987 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
988 unlink($dfname, $xauthority);
989 $main::lxdebug->leave_sub();
994 if ($line !~ /xvfb/i) {
995 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
996 unlink($dfname, $xauthority);
997 $main::lxdebug->leave_sub();
1001 $ENV{"XAUTHORITY"} = $xauthority;
1002 $ENV{"DISPLAY"} = $display;
1004 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1007 $main::lxdebug->leave_sub();
1013 $main::lxdebug->enter_sub();
1017 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1019 my $display = $self->is_xvfb_running();
1022 $main::lxdebug->leave_sub();
1027 while ( -f "/tmp/.X${display}-lock") {
1030 $display = ":${display}";
1031 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1033 my $mcookie = `mcookie`;
1034 die("Installation error: mcookie not found.") if ($? != 0);
1037 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1039 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1040 $ENV{"XAUTHORITY"} = $xauthority;
1042 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1044 system("xauth add \"${display}\" . \"${mcookie}\"");
1046 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1047 $main::lxdebug->leave_sub();
1051 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1055 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1056 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1059 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1062 my $dfname = $self->{"userspath"} . "/xvfb_display";
1063 if (!open(OUT, ">$dfname")) {
1064 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1065 unlink($xauthority);
1067 $main::lxdebug->leave_sub();
1070 print(OUT "$pid\n$display\n$xauthority\n");
1073 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1075 if (!$self->is_xvfb_running()) {
1076 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1077 unlink($xauthority, $dfname);
1079 $main::lxdebug->leave_sub();
1083 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1085 $main::lxdebug->leave_sub();
1090 sub is_openoffice_running {
1091 $main::lxdebug->enter_sub();
1093 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1094 "> /dev/null 2> /dev/null");
1096 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1098 $main::lxdebug->leave_sub();
1103 sub spawn_openoffice {
1104 $main::lxdebug->enter_sub();
1108 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1110 my ($try, $spawned_oo, $res);
1113 for ($try = 0; $try < 15; $try++) {
1114 if ($self->is_openoffice_running()) {
1122 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1124 open(STDIN, '/dev/null');
1125 open(STDOUT, '>/dev/null');
1126 my $new_pid = fork();
1128 my $ssres = setsid();
1129 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1130 my @cmdline = ($main::openofficeorg_writer_bin,
1131 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1133 "-accept=socket,host=localhost,port=" .
1134 $main::openofficeorg_daemon_port . ";urp;");
1138 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1143 sleep($try >= 5 ? 2 : 1);
1147 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1148 "OpenOffice could not be started.";
1151 $main::lxdebug->leave_sub();
1156 sub convert_to_pdf {
1157 $main::lxdebug->enter_sub();
1161 my $form = $self->{"form"};
1163 my $filename = $form->{"tmpfile"};
1164 $filename =~ s/.odt$//;
1165 if (substr($filename, 0, 1) ne "/") {
1166 $filename = getcwd() . "/${filename}";
1169 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1170 $ENV{'HOME'} = $self->{"userspath"};
1172 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1175 if (!$self->spawn_xvfb()) {
1176 $main::lxdebug->leave_sub();
1181 if (!$main::openofficeorg_daemon) {
1182 @cmdline = ($main::openofficeorg_writer_bin,
1183 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1185 "file:${filename}.odt",
1186 "macro://" . (split('/', $filename))[-1] .
1187 "/Standard.Conversion.ConvertSelfToPDF()");
1189 if (!$self->spawn_openoffice()) {
1190 $main::lxdebug->leave_sub();
1194 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1195 $main::openofficeorg_daemon_port,
1203 $form->{"tmpfile"} =~ s/odt$/pdf/;
1205 unlink($filename . ".odt");
1207 $main::lxdebug->leave_sub();
1212 unlink($filename . ".odt", $filename . ".pdf");
1213 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1216 $main::lxdebug->leave_sub();
1221 my ($self, $variable) = @_;
1222 my $form = $self->{"form"};
1223 my $iconv = $self->{"iconv"};
1226 ('order' => ['&', '<', '>', '"', "'",
1228 quotemeta("\n"), quotemeta("\r")],
1234 '\x80' => chr(0xa4), # Euro
1235 quotemeta("\n") => '<text:line-break/>',
1236 quotemeta("\r") => '',
1239 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1241 # Allow some HTML markup to be converted into the output format's
1242 # corresponding markup code, e.g. bold or italic.
1243 my $rnd = $self->{"rnd"};
1244 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1245 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1247 foreach my $key (keys(%markup_replace)) {
1248 my $value = $markup_replace{$key};
1249 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
1250 $variable =~ s|\</${key}\>|</text:span>|gi;
1253 return $iconv->convert($variable);
1256 sub get_mime_type() {
1257 if ($self->{"form"}->{"format"} =~ /pdf/) {
1258 return "application/pdf";
1260 return "application/vnd.oasis.opendocument.text";
1264 sub uses_temp_file {
1269 ##########################################################
1273 ##########################################################
1275 package XMLTemplate;
1279 @ISA = qw(HTMLTemplate);
1282 #evtl auskommentieren
1285 return $type->SUPER::new(@_);
1289 my ($self, $variable) = @_;
1290 my $form = $self->{"form"};
1293 ('order' => ['<', '>', quotemeta("\n")],
1296 quotemeta("\n") => '<br>',
1299 map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
1301 # Allow no markup to be converted into the output format
1302 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1304 foreach my $key (@markup_replace) {
1305 $variable =~ s/\<(\/?)${key}\>//g;
1311 sub get_mime_type() {
1314 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1315 return "application/xml ";
1316 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1317 return "application/x-taxbird";
1323 sub uses_temp_file {
1324 # tempfile needet for XML Output