1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
10 # NOTE: strict checks are package global. don't check this file
11 # with perl -sc, it will only capture SimpleTemplate
15 package SimpleTemplate;
20 # 1. The template's file name
21 # 2. A reference to the Form object
22 # 3. A reference to the myconfig hash
25 # A new template object
39 $self->{source} = shift;
40 $self->{form} = shift;
41 $self->{myconfig} = shift;
42 $self->{userspath} = shift;
44 $self->{error} = undef;
45 $self->{quot_re} = '"';
47 $self->set_tag_style('<%', '%>');
52 my $tag_start = shift;
55 $self->{tag_start} = $tag_start;
56 $self->{tag_end} = $tag_end;
57 $self->{tag_start_qm} = quotemeta $tag_start;
58 $self->{tag_end_qm} = quotemeta $tag_end;
60 $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
68 # 1. A typeglob for the file handle. The output will be written
69 # to this file handle.
72 # 1 on success and undef or 0 if there was an error. In the latter case
73 # the calling function can retrieve the error message via $obj->get_error()
78 print(OUT "Hallo!\n");
84 return $self->{"error"};
91 sub _get_loop_variable {
94 my $get_array = shift;
97 my $form = $self->{form};
100 if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
101 $value = $form->{TEMPLATE_ARRAYS}->{$var};
103 $value = $form->{$var};
106 for (my $i = 0; $i < scalar(@indices); $i++) {
107 last unless (ref($value) eq "ARRAY");
108 $value = $value->[$indices[$i]];
114 sub substitute_vars {
115 my ($self, $text, @indices) = @_;
117 my $form = $self->{"form"};
119 while ($text =~ /$self->{substitute_vars_re}/) {
120 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
121 my ($var, @options) = split(/\s+/, $1);
123 my $value = $self->_get_loop_variable($var, 0, @indices);
124 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
126 substr($text, $tag_pos, $tag_len, $value);
132 sub _parse_block_if {
133 $main::lxdebug->enter_sub();
136 my $contents = shift;
137 my $new_contents = shift;
141 $$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices);
142 substr($$contents, 0, $pos_if) = "";
144 if ($$contents !~ m/^$self->{tag_start_qm}if
146 (not\b|\!)? # $1 -- Eventuelle Negierung
148 (\b.+?\b) # $2 -- Name der zu überprüfenden Variablen
149 ( # $3 -- Beginn des optionalen Vergleiches
151 ([!=]) # $4 -- Negierung des Vergleiches speichern
152 ([=~]) # $5 -- Art des Vergleiches speichern
154 ( # $6 -- Gequoteter String oder Bareword
156 (.*?)(?<!\\) # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
159 (\b.+?\b) # $8 -- Bareword -- als Index für $form benutzen
165 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
166 $main::lxdebug->leave_sub();
172 my $operator_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
173 my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
174 my $quoted_word = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
175 my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
177 $not = !$not if ($operator_neg && $operator_neg eq '!');
179 substr($$contents, 0, length($&)) = "";
182 ($block, $$contents) = $self->find_end($$contents, 0, $var, $not);
184 $self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"});
185 $main::lxdebug->leave_sub();
189 my $value = $self->_get_loop_variable($var, 0, @indices);
192 if ($operator_type) {
193 my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
194 if ($operator_type eq '=') {
195 $hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to));
197 $hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
201 $hit = ($not && ! $value) || (!$not && $value);
205 my $new_text = $self->parse_block($block, @indices);
206 if (!defined($new_text)) {
207 $main::lxdebug->leave_sub();
210 $$new_contents .= $new_text;
213 $main::lxdebug->leave_sub();
224 package LaTeXTemplate;
228 @ISA = qw(SimpleTemplate);
235 my $self = $type->SUPER::new(@_);
241 my ($self, $variable) = @_;
242 my $form = $self->{"form"};
244 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
246 # Allow some HTML markup to be converted into the output format's
247 # corresponding markup code, e.g. bold or italic.
248 my %markup_replace = ('b' => 'textbf',
252 foreach my $key (keys(%markup_replace)) {
253 my $new = $markup_replace{$key};
254 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
257 $variable =~ s/[\x00-\x1f]//g;
263 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
265 my ($form, $new_contents) = ($self->{"form"}, "");
267 my $ary = $self->_get_loop_variable($var, 1, @indices);
270 my $current_page = 1;
271 my ($current_line, $corrent_row) = (0, 1);
272 my $description_array = $self->_get_loop_variable("description", 1);
273 my $longdescription_array = $self->_get_loop_variable("longdescription", 1);
274 my $linetotal_array = $self->_get_loop_variable("linetotal", 1);
276 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
278 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
279 $form->{"__first__"} = $i == 1;
280 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
281 $form->{"__odd__"} = (($i + 1) % 2) == 1;
282 $form->{"__counter__"} = $i + 1;
284 if (scalar @{$description_array} == scalar @{$ary} && $self->{"chars_per_line"} != 0) {
285 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
288 $description_array->[$i] =~ s/(\\newline\s?)*$//;
289 my $_description = $description_array->[$i];
290 while ($_description =~ /\\newline/) {
292 $_description =~ s/\\newline//;
296 if ($current_page == 1) {
297 $lpp = $self->{"lines_on_first_page"};
299 $lpp = $self->{"lines_on_second_page"};
302 # Yes we need a manual page break -- or the user has forced one
303 if ((($current_line + $lines) > $lpp) || ($description_array->[$i] =~ /<pagebreak>/) || ($longdescription_array->[$i] =~ /<pagebreak>/)) {
304 my $pb = $self->{"pagebreak_block"};
306 # replace the special variables <%sumcarriedforward%>
309 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
310 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
311 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
313 my $new_text = $self->parse_block($pb, (@indices, $i));
314 return undef unless (defined($new_text));
315 $new_contents .= $new_text;
320 $current_line += $lines;
323 if ($i < scalar(@{$linetotal_array})) {
324 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
327 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
329 my $new_text = $self->parse_block($text, (@indices, $i));
330 return undef unless (defined($new_text));
331 $new_contents .= $start_tag . $new_text . $end_tag;
333 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
335 return $new_contents;
339 my ($self, $text, $pos, $var, $not) = @_;
341 my $tag_start_len = length $self->{tag_start};
344 $pos = 0 unless ($pos);
346 while ($pos < length($text)) {
349 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
351 my $keyword_pos = $pos - 1 + $tag_start_len;
353 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
356 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
359 "$self->{tag_start}else$self->{tag_end} outside of "
360 . "$self->{tag_start}if$self->{tag_end} / "
361 . "$self->{tag_start}ifnot$self->{tag_end}.";
365 my $block = substr($text, 0, $pos - 1);
366 substr($text, 0, $pos - 1) = "";
367 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
368 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
370 return ($block, $text);
372 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
375 my $block = substr($text, 0, $pos - 1);
376 substr($text, 0, $pos - 1) = "";
377 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
379 return ($block, $text);
388 $main::lxdebug->enter_sub();
390 my ($self, $contents, @indices) = @_;
392 my $new_contents = "";
394 while ($contents ne "") {
395 my $pos_if = index($contents, $self->{tag_start} . 'if');
396 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
398 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
399 $new_contents .= $self->substitute_vars($contents, @indices);
403 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
404 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
405 substr($contents, 0, $pos_foreach) = "";
407 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
408 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
409 $main::lxdebug->leave_sub();
415 substr($contents, 0, length($&)) = "";
418 ($block, $contents) = $self->find_end($contents);
420 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
421 $main::lxdebug->leave_sub();
425 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
426 if (!defined($new_text)) {
427 $main::lxdebug->leave_sub();
430 $new_contents .= $new_text;
433 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
434 $main::lxdebug->leave_sub();
440 $main::lxdebug->leave_sub();
442 return $new_contents;
445 sub parse_first_line {
447 my $line = shift || "";
449 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
451 $self->{error} = "The tag start and end markers must not be equal.";
455 $self->set_tag_style($1, $2);
461 sub _parse_config_option {
468 my ($key, $value) = split m/\s*=\s*/, $line, 2;
470 if ($key eq 'tag-style') {
471 $self->set_tag_style(split(m/\s+/, $value, 2));
475 sub _parse_config_lines {
479 my ($comment_start, $comment_end) = ("", "");
481 if (ref $self eq 'LaTeXTemplate') {
482 $comment_start = '\s*%';
483 } elsif (ref $self eq 'HTMLTemplate') {
484 $comment_start = '\s*<!--';
485 $comment_end = '>\s*';
487 $comment_start = '\s*\#';
490 my $num_lines = scalar @{ $lines };
493 while ($i < $num_lines) {
494 my $line = $lines->[$i];
496 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
501 $self->_parse_config_option($1);
502 splice @{ $lines }, $i, 1;
507 sub _force_mandatory_packages {
511 my (%used_packages, $document_start_line);
513 foreach my $i (0 .. scalar @{ $lines } - 1) {
514 if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
515 $used_packages{$1} = 1;
517 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
518 $document_start_line = $i;
524 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
526 if (!$used_packages{textcomp}) {
527 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
528 $document_start_line++;
535 my $form = $self->{"form"};
537 if (!open(IN, "$form->{templates}/$form->{IN}")) {
538 $self->{"error"} = "$!";
544 $self->_parse_config_lines(\@lines);
545 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
547 my $contents = join("", @lines);
549 # detect pagebreak block and its parameters
550 if ($contents =~ /$self->{tag_start_qm}pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*$self->{tag_end_qm}(.*?)$self->{tag_start_qm}end(\s*pagebreak)?$self->{tag_end_qm}/s) {
551 $self->{"chars_per_line"} = $1;
552 $self->{"lines_on_first_page"} = $2;
553 $self->{"lines_on_second_page"} = $3;
554 $self->{"pagebreak_block"} = $4;
556 substr($contents, length($`), length($&)) = "";
559 $self->{"forced_pagebreaks"} = [];
561 my $new_contents = $self->parse_block($contents);
562 if (!defined($new_contents)) {
563 $main::lxdebug->leave_sub();
567 print(OUT $new_contents);
569 if ($form->{"format"} =~ /postscript/i) {
570 return $self->convert_to_postscript();
571 } elsif ($form->{"format"} =~ /pdf/i) {
572 return $self->convert_to_pdf();
578 sub convert_to_postscript {
580 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
582 # Convert the tex file to postscript
584 if (!chdir("$userspath")) {
585 $self->{"error"} = "chdir : $!";
590 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
592 my $latex = $self->_get_latex_path();
594 for (my $run = 1; $run <= 2; $run++) {
595 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
596 "> $form->{tmpfile}.err");
598 $self->{"error"} = $form->cleanup();
604 $form->{tmpfile} =~ s/tex$/dvi/;
606 system("dvips $form->{tmpfile} -o -q > /dev/null");
608 $self->{"error"} = "dvips : $!";
612 $form->{tmpfile} =~ s/dvi$/ps/;
621 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
623 # Convert the tex file to PDF
625 if (!chdir("$userspath")) {
626 $self->{"error"} = "chdir : $!";
631 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
633 my $latex = $self->_get_latex_path();
635 for (my $run = 1; $run <= 2; $run++) {
636 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
637 "> $form->{tmpfile}.err");
639 $self->{"error"} = $form->cleanup();
645 $form->{tmpfile} =~ s/tex$/pdf/;
650 sub _get_latex_path {
651 return $main::latex_bin || 'pdflatex';
654 sub get_mime_type() {
657 if ($self->{"form"}->{"format"} =~ /postscript/i) {
658 return "application/postscript";
660 return "application/pdf";
673 package HTMLTemplate;
677 @ISA = qw(LaTeXTemplate);
684 return $type->SUPER::new(@_);
688 my ($self, $variable) = @_;
689 my $form = $self->{"form"};
691 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
693 # Allow some HTML markup to be converted into the output format's
694 # corresponding markup code, e.g. bold or italic.
695 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
697 foreach my $key (@markup_replace) {
698 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
704 sub get_mime_type() {
707 if ($self->{"form"}->{"format"} =~ /postscript/i) {
708 return "application/postscript";
709 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
710 return "application/pdf";
719 if ($self->{"form"}->{"format"} =~ /postscript/i) {
721 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
728 sub convert_to_postscript {
730 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
732 # Convert the HTML file to postscript
734 if (!chdir("$userspath")) {
735 $self->{"error"} = "chdir : $!";
740 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
741 my $psfile = $form->{"tmpfile"};
742 $psfile =~ s/.html/.ps/;
743 if ($psfile eq $form->{"tmpfile"}) {
747 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
749 $self->{"error"} = $form->cleanup();
754 $form->{"tmpfile"} = $psfile;
763 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
765 # Convert the HTML file to PDF
767 if (!chdir("$userspath")) {
768 $self->{"error"} = "chdir : $!";
773 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
774 my $pdffile = $form->{"tmpfile"};
775 $pdffile =~ s/.html/.pdf/;
776 if ($pdffile eq $form->{"tmpfile"}) {
780 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
782 $self->{"error"} = $form->cleanup();
787 $form->{"tmpfile"} = $pdffile;
796 #### PlainTextTemplate
799 package PlainTextTemplate;
803 @ISA = qw(LaTeXTemplate);
810 return $type->SUPER::new(@_);
814 my ($self, $variable) = @_;
829 #### OpenDocumentTemplate
832 package OpenDocumentTemplate;
840 # use File::Temp qw(:mktemp);
843 @ISA = qw(SimpleTemplate);
850 my $self = $type->SUPER::new(@_);
852 foreach my $module (qw(Archive::Zip Text::Iconv)) {
853 eval("use ${module};");
855 $self->{"form"}->error("The Perl module '${module}' could not be " .
856 "loaded. Support for OpenDocument templates " .
857 "does not work without it. Please install your " .
858 "distribution's package or get the module from " .
859 "CPAN ( http://www.cpan.org ).");
863 $self->{"rnd"} = int(rand(1000000));
864 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
866 $self->set_tag_style('<%', '%>');
867 $self->{quot_re} = '"';
873 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
875 my ($form, $new_contents) = ($self->{"form"}, "");
877 my $ary = $self->_get_loop_variable($var, 1, @indices);
879 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
880 $form->{"__first__"} = $i == 0;
881 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
882 $form->{"__odd__"} = (($i + 1) % 2) == 1;
883 $form->{"__counter__"} = $i + 1;
884 my $new_text = $self->parse_block($text, (@indices, $i));
885 return undef unless (defined($new_text));
886 $new_contents .= $start_tag . $new_text . $end_tag;
888 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
890 return $new_contents;
894 my ($self, $text, $pos, $var, $not) = @_;
897 $pos = 0 unless ($pos);
899 while ($pos < length($text)) {
902 next if (substr($text, $pos - 1, 5) ne '<%');
904 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
907 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
909 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
913 my $block = substr($text, 0, $pos - 1);
914 substr($text, 0, $pos - 1) = "";
915 $text =~ s!^\<\%[^\%]+\%\>!!;
916 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
918 return ($block, $text);
920 } elsif (substr($text, $pos + 4, 3) eq 'end') {
923 my $block = substr($text, 0, $pos - 1);
924 substr($text, 0, $pos - 1) = "";
925 $text =~ s!^\<\%[^\%]+\%\>!!;
927 return ($block, $text);
936 $main::lxdebug->enter_sub();
938 my ($self, $contents, @indices) = @_;
940 my $new_contents = "";
942 while ($contents ne "") {
943 if (substr($contents, 0, 1) eq "<") {
944 $contents =~ m|^<[^>]+>|;
946 substr($contents, 0, length($&)) = "";
948 if ($tag =~ m|<table:table-row|) {
949 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
952 substr($contents, 0, length($1) + length($end_tag)) = "";
954 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
957 substr($table_row, length($`), length($&)) = "";
959 my ($t1, $t2) = $self->find_end($table_row, length($`));
961 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
962 $main::lxdebug->leave_sub();
966 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
967 if (!defined($new_text)) {
968 $main::lxdebug->leave_sub();
971 $new_contents .= $new_text;
974 my $new_text = $self->parse_block($table_row, @indices);
975 if (!defined($new_text)) {
976 $main::lxdebug->leave_sub();
979 $new_contents .= $tag . $new_text . $end_tag;
983 $new_contents .= $tag;
987 $contents =~ /^[^<]+/;
990 my $pos_if = index($text, '<%if');
991 my $pos_foreach = index($text, '<%foreach');
993 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
994 substr($contents, 0, length($text)) = "";
995 $new_contents .= $self->substitute_vars($text, @indices);
999 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
1000 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
1001 substr($contents, 0, $pos_foreach) = "";
1003 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
1004 $self->{"error"} = "Malformed <\%foreach\%>.";
1005 $main::lxdebug->leave_sub();
1011 substr($contents, 0, length($&)) = "";
1014 ($block, $contents) = $self->find_end($contents);
1016 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
1017 $main::lxdebug->leave_sub();
1021 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
1022 if (!defined($new_text)) {
1023 $main::lxdebug->leave_sub();
1026 $new_contents .= $new_text;
1029 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
1030 $main::lxdebug->leave_sub();
1037 $main::lxdebug->leave_sub();
1039 return $new_contents;
1043 $main::lxdebug->enter_sub();
1047 my $form = $self->{"form"};
1052 if ($form->{"IN"} =~ m|^/|) {
1053 $file_name = $form->{"IN"};
1055 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1058 my $zip = Archive::Zip->new();
1059 if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
1060 $self->{"error"} = "File not found/is not a OpenDocument file.";
1061 $main::lxdebug->leave_sub();
1065 my $contents = $zip->contents("content.xml");
1067 $self->{"error"} = "File is not a OpenDocument file.";
1068 $main::lxdebug->leave_sub();
1072 my $rnd = $self->{"rnd"};
1073 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1074 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1076 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1077 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1079 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1080 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1082 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1083 <style:text-properties style:text-line-through-style="solid"/>
1085 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1086 <style:text-properties style:text-position="super 58%"/>
1088 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1089 <style:text-properties style:text-position="sub 58%"/>
1093 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1094 $contents =~ s|[\n\r]||gm;
1096 my $new_contents = $self->parse_block($contents);
1097 if (!defined($new_contents)) {
1098 $main::lxdebug->leave_sub();
1102 # $new_contents =~ s|>|>\n|g;
1104 $zip->contents("content.xml", $new_contents);
1106 my $styles = $zip->contents("styles.xml");
1108 my $new_styles = $self->parse_block($styles);
1109 if (!defined($new_contents)) {
1110 $main::lxdebug->leave_sub();
1113 $zip->contents("styles.xml", $new_styles);
1116 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1119 if ($form->{"format"} =~ /pdf/) {
1120 $res = $self->convert_to_pdf();
1123 $main::lxdebug->leave_sub();
1127 sub is_xvfb_running {
1128 $main::lxdebug->enter_sub();
1133 my $dfname = $self->{"userspath"} . "/xvfb_display";
1136 $main::lxdebug->message(LXDebug->DEBUG2(), " Looking for $dfname\n");
1137 if ((-f $dfname) && open(IN, $dfname)) {
1142 my $xauthority = <IN>;
1146 $main::lxdebug->message(LXDebug->DEBUG2(), " found with $pid and $display\n");
1148 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1149 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #1\n");
1150 unlink($dfname, $xauthority);
1151 $main::lxdebug->leave_sub();
1156 if ($line !~ /xvfb/i) {
1157 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #2\n");
1158 unlink($dfname, $xauthority);
1159 $main::lxdebug->leave_sub();
1163 $ENV{"XAUTHORITY"} = $xauthority;
1164 $ENV{"DISPLAY"} = $display;
1166 $main::lxdebug->message(LXDebug->DEBUG2(), " not found\n");
1169 $main::lxdebug->leave_sub();
1175 $main::lxdebug->enter_sub();
1179 $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
1181 my $display = $self->is_xvfb_running();
1184 $main::lxdebug->leave_sub();
1189 while ( -f "/tmp/.X${display}-lock") {
1192 $display = ":${display}";
1193 $main::lxdebug->message(LXDebug->DEBUG2(), " display $display\n");
1195 my $mcookie = `mcookie`;
1196 die("Installation error: mcookie not found.") if ($? != 0);
1199 $main::lxdebug->message(LXDebug->DEBUG2(), " mcookie $mcookie\n");
1201 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1202 $ENV{"XAUTHORITY"} = $xauthority;
1204 $main::lxdebug->message(LXDebug->DEBUG2(), " xauthority $xauthority\n");
1206 system("xauth add \"${display}\" . \"${mcookie}\"");
1208 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1209 $main::lxdebug->leave_sub();
1213 $main::lxdebug->message(LXDebug->DEBUG2(), " about to fork()\n");
1217 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
1218 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1221 $main::lxdebug->message(LXDebug->DEBUG2(), " parent dont sleeping\n");
1224 my $dfname = $self->{"userspath"} . "/xvfb_display";
1225 if (!open(OUT, ">$dfname")) {
1226 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1227 unlink($xauthority);
1229 $main::lxdebug->leave_sub();
1232 print(OUT "$pid\n$display\n$xauthority\n");
1235 $main::lxdebug->message(LXDebug->DEBUG2(), " parent re-testing\n");
1237 if (!$self->is_xvfb_running()) {
1238 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1239 unlink($xauthority, $dfname);
1241 $main::lxdebug->leave_sub();
1245 $main::lxdebug->message(LXDebug->DEBUG2(), " spawn OK\n");
1247 $main::lxdebug->leave_sub();
1252 sub is_openoffice_running {
1253 $main::lxdebug->enter_sub();
1255 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1256 "> /dev/null 2> /dev/null");
1258 $main::lxdebug->message(LXDebug->DEBUG2(), " is_openoffice_running(): $?\n");
1260 $main::lxdebug->leave_sub();
1265 sub spawn_openoffice {
1266 $main::lxdebug->enter_sub();
1270 $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
1272 my ($try, $spawned_oo, $res);
1275 for ($try = 0; $try < 15; $try++) {
1276 if ($self->is_openoffice_running()) {
1284 $main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
1286 open(STDIN, '/dev/null');
1287 open(STDOUT, '>/dev/null');
1288 my $new_pid = fork();
1290 my $ssres = setsid();
1291 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
1292 my @cmdline = ($main::openofficeorg_writer_bin,
1293 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1295 "-accept=socket,host=localhost,port=" .
1296 $main::openofficeorg_daemon_port . ";urp;");
1300 $main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
1305 sleep($try >= 5 ? 2 : 1);
1309 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1310 "OpenOffice could not be started.";
1313 $main::lxdebug->leave_sub();
1318 sub convert_to_pdf {
1319 $main::lxdebug->enter_sub();
1323 my $form = $self->{"form"};
1325 my $filename = $form->{"tmpfile"};
1326 $filename =~ s/.odt$//;
1327 if (substr($filename, 0, 1) ne "/") {
1328 $filename = getcwd() . "/${filename}";
1331 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1332 $ENV{'HOME'} = $self->{"userspath"};
1334 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1337 if (!$self->spawn_xvfb()) {
1338 $main::lxdebug->leave_sub();
1343 if (!$main::openofficeorg_daemon) {
1344 @cmdline = ($main::openofficeorg_writer_bin,
1345 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1347 "file:${filename}.odt",
1348 "macro://" . (split('/', $filename))[-1] .
1349 "/Standard.Conversion.ConvertSelfToPDF()");
1351 if (!$self->spawn_openoffice()) {
1352 $main::lxdebug->leave_sub();
1356 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1357 $main::openofficeorg_daemon_port,
1365 $form->{"tmpfile"} =~ s/odt$/pdf/;
1367 unlink($filename . ".odt");
1369 $main::lxdebug->leave_sub();
1374 unlink($filename . ".odt", $filename . ".pdf");
1375 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1378 $main::lxdebug->leave_sub();
1383 my ($self, $variable) = @_;
1384 my $form = $self->{"form"};
1385 my $iconv = $self->{"iconv"};
1387 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1389 # Allow some HTML markup to be converted into the output format's
1390 # corresponding markup code, e.g. bold or italic.
1391 my $rnd = $self->{"rnd"};
1392 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1393 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1395 foreach my $key (keys(%markup_replace)) {
1396 my $value = $markup_replace{$key};
1397 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1398 $variable =~ s|\</${key}\>|</text:span>|gi;
1401 return $iconv->convert($variable);
1404 sub get_mime_type() {
1407 if ($self->{"form"}->{"format"} =~ /pdf/) {
1408 return "application/pdf";
1410 return "application/vnd.oasis.opendocument.text";
1414 sub uses_temp_file {
1419 ##########################################################
1423 ##########################################################
1425 package XMLTemplate;
1429 @ISA = qw(HTMLTemplate);
1434 #evtl auskommentieren
1437 return $type->SUPER::new(@_);
1441 my ($self, $variable) = @_;
1442 my $form = $self->{"form"};
1444 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1446 # Allow no markup to be converted into the output format
1447 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1449 foreach my $key (@markup_replace) {
1450 $variable =~ s/\<(\/?)${key}\>//g;
1456 sub get_mime_type() {
1459 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1460 return "application/xml ";
1461 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1462 return "application/x-taxbird";
1468 sub uses_temp_file {
1469 # tempfile needet for XML Output