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;
 
  38   $self->set_tag_style('<%', '%>');
 
  43   my $tag_start         = shift;
 
  46   $self->{tag_start}    = $tag_start;
 
  47   $self->{tag_end}      = $tag_end;
 
  48   $self->{tag_start_qm} = quotemeta $tag_start;
 
  49   $self->{tag_end_qm}   = quotemeta $tag_end;
 
  57 #   1. A typeglob for the file handle. The output will be written
 
  58 #      to this file handle.
 
  61 #   1 on success and undef or 0 if there was an error. In the latter case
 
  62 #   the calling function can retrieve the error message via $obj->get_error()
 
  67   print(OUT "Hallo!\n");
 
  73   return $self->{"error"};
 
  86 package LaTeXTemplate;
 
  90 @ISA = qw(SimpleTemplate);
 
  95   return $type->SUPER::new(@_);
 
  99   my ($self, $variable) = @_;
 
 100   my $form = $self->{"form"};
 
 102   $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
 
 104   # Allow some HTML markup to be converted into the output format's
 
 105   # corresponding markup code, e.g. bold or italic.
 
 106   my %markup_replace = ('b' => 'textbf',
 
 110   foreach my $key (keys(%markup_replace)) {
 
 111     my $new = $markup_replace{$key};
 
 112     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
 115   $variable =~ s/[\x00-\x1f]//g;
 
 120 sub substitute_vars {
 
 121   my ($self, $text, @indices) = @_;
 
 123   my $form = $self->{"form"};
 
 125   while ($text =~ /$self->{tag_start_qm}(.+?)$self->{tag_end_qm}/) {
 
 126     my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
 
 127     my ($var, @options) = split(/\s+/, $1);
 
 128     my $value = $form->{$var};
 
 130     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 131       last unless (ref($value) eq "ARRAY");
 
 132       $value = $value->[$indices[$i]];
 
 134     $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
 
 135     substr($text, $tag_pos, $tag_len) = $value;
 
 142   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 144   my ($form, $new_contents) = ($self->{"form"}, "");
 
 146   my $ary = $form->{$var};
 
 147   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 148     last unless (ref($ary) eq "ARRAY");
 
 149     $ary = $ary->[$indices[$i]];
 
 153   my $current_page = 1;
 
 154   my ($current_line, $corrent_row) = (0, 1);
 
 156   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 157     $form->{"__first__"} = $i == 0;
 
 158     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 159     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 160     $form->{"__counter__"} = $i + 1;
 
 162     if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
 
 163         $self->{"chars_per_line"}) {
 
 165         int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
 
 168       $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
 
 169       my $_description = $form->{"description"}->[$i];
 
 170       while ($_description =~ /\\newline/) {
 
 172         $_description =~ s/\\newline//;
 
 176       if ($current_page == 1) {
 
 177         $lpp = $self->{"lines_on_first_page"};
 
 179         $lpp = $self->{"lines_on_second_page"};
 
 182       # Yes we need a manual page break -- or the user has forced one
 
 183       if ((($current_line + $lines) > $lpp) ||
 
 184           ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
 
 185         my $pb = $self->{"pagebreak_block"};
 
 187         # replace the special variables <%sumcarriedforward%>
 
 190         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 191         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
 
 192         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
 
 194         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 195         return undef unless (defined($new_text));
 
 196         $new_contents .= $new_text;
 
 201       $current_line += $lines;
 
 203     if ($i < scalar(@{$form->{"linetotal"}})) {
 
 204       $sum += $form->parse_amount($self->{"myconfig"},
 
 205                                   $form->{"linetotal"}->[$i]);
 
 208     $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 210     my $new_text = $self->parse_block($text, (@indices, $i));
 
 211     return undef unless (defined($new_text));
 
 212     $new_contents .= $start_tag . $new_text . $end_tag;
 
 214   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 216   return $new_contents;
 
 220   my ($self, $text, $pos, $var, $not) = @_;
 
 222   my $tag_start_len = length $self->{tag_start};
 
 225   $pos = 0 unless ($pos);
 
 227   while ($pos < length($text)) {
 
 230     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
 232     my $keyword_pos = $pos - 1 + $tag_start_len;
 
 234     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
 
 237     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 240             "$self->{tag_start}else$self->{tag_end} outside of "
 
 241           . "$self->{tag_start}if$self->{tag_end} / "
 
 242           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 246       my $block = substr($text, 0, $pos - 1);
 
 247       substr($text, 0, $pos - 1) = "";
 
 248       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 249       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 251       return ($block, $text);
 
 253     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 256         my $block = substr($text, 0, $pos - 1);
 
 257         substr($text, 0, $pos - 1) = "";
 
 258         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 260         return ($block, $text);
 
 269   $main::lxdebug->enter_sub();
 
 271   my ($self, $contents, @indices) = @_;
 
 273   my $new_contents = "";
 
 275   while ($contents ne "") {
 
 276     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 277     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 279     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 280       $new_contents .= $self->substitute_vars($contents, @indices);
 
 284     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 285       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 286       substr($contents, 0, $pos_foreach) = "";
 
 288       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 289         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 290         $main::lxdebug->leave_sub();
 
 296       substr($contents, 0, length($&)) = "";
 
 299       ($block, $contents) = $self->find_end($contents);
 
 301         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 302         $main::lxdebug->leave_sub();
 
 306       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 307       if (!defined($new_text)) {
 
 308         $main::lxdebug->leave_sub();
 
 311       $new_contents .= $new_text;
 
 314       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 315       substr($contents, 0, $pos_if) = "";
 
 317       if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
 
 318         $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
 
 319         $main::lxdebug->leave_sub();
 
 323       my ($not, $var) = ($1, $2);
 
 325       substr($contents, 0, length($&)) = "";
 
 327       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 329         $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
 
 330         $main::lxdebug->leave_sub();
 
 334       my $value = $self->{"form"}->{$var};
 
 335       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 336         last unless (ref($value) eq "ARRAY");
 
 337         $value = $value->[$indices[$i]];
 
 340       if (($not && !$value) || (!$not && $value)) {
 
 341         my $new_text = $self->parse_block($block, @indices);
 
 342         if (!defined($new_text)) {
 
 343           $main::lxdebug->leave_sub();
 
 346         $new_contents .= $new_text;
 
 351   $main::lxdebug->leave_sub();
 
 353   return $new_contents;
 
 356 sub parse_first_line {
 
 358   my $line = shift || "";
 
 360   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 362       $self->{error} = "The tag start and end markers must not be equal.";
 
 366     $self->set_tag_style($1, $2);
 
 372 sub _parse_config_option {
 
 379   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 381   if ($key eq 'tag-style') {
 
 382     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 386 sub _parse_config_lines {
 
 390   my ($comment_start, $comment_end) = ("", "");
 
 392   if (ref $self eq 'LaTeXTemplate') {
 
 393     $comment_start = '\s*%';
 
 394   } elsif (ref $self eq 'HTMLTemplate') {
 
 395     $comment_start = '\s*<!--';
 
 396     $comment_end   = '>\s*';
 
 398     $comment_start = '\s*\#';
 
 401   my $num_lines = scalar @{ $lines };
 
 404   while ($i < $num_lines) {
 
 405     my $line = $lines->[$i];
 
 407     if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
 
 412     $self->_parse_config_option($1);
 
 413     splice @{ $lines }, $i, 1;
 
 418 sub _force_mandatory_packages {
 
 422   my (%used_packages, $document_start_line);
 
 424   foreach my $i (0 .. scalar @{ $lines } - 1) {
 
 425     if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
 
 426       $used_packages{$1} = 1;
 
 428     } elsif ($lines->[$i] =~ m/\\begin{document}/) {
 
 429       $document_start_line = $i;
 
 435   $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
 
 437   if (!$used_packages{textcomp}) {
 
 438     splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
 
 439     $document_start_line++;
 
 446   my $form = $self->{"form"};
 
 448   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 449     $self->{"error"} = "$!";
 
 455   $self->_parse_config_lines(\@lines);
 
 456   $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
 
 458   my $contents = join("", @lines);
 
 460   # detect pagebreak block and its parameters
 
 461   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) {
 
 462     $self->{"chars_per_line"} = $1;
 
 463     $self->{"lines_on_first_page"} = $2;
 
 464     $self->{"lines_on_second_page"} = $3;
 
 465     $self->{"pagebreak_block"} = $4;
 
 467     substr($contents, length($`), length($&)) = "";
 
 470   $self->{"forced_pagebreaks"} = [];
 
 472   my $new_contents = $self->parse_block($contents);
 
 473   if (!defined($new_contents)) {
 
 474     $main::lxdebug->leave_sub();
 
 478   print(OUT $new_contents);
 
 480   if ($form->{"format"} =~ /postscript/i) {
 
 481     return $self->convert_to_postscript();
 
 482   } elsif ($form->{"format"} =~ /pdf/i) {
 
 483     return $self->convert_to_pdf();
 
 489 sub convert_to_postscript {
 
 491   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 493   # Convert the tex file to postscript
 
 495   if (!chdir("$userspath")) {
 
 496     $self->{"error"} = "chdir : $!";
 
 501   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 503   for (my $run = 1; $run <= 2; $run++) {
 
 504     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 505            "> $form->{tmpfile}.err");
 
 507       $self->{"error"} = $form->cleanup();
 
 513   $form->{tmpfile} =~ s/tex$/dvi/;
 
 515   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 517     $self->{"error"} = "dvips : $!";
 
 521   $form->{tmpfile} =~ s/dvi$/ps/;
 
 530   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 532   # Convert the tex file to PDF
 
 534   if (!chdir("$userspath")) {
 
 535     $self->{"error"} = "chdir : $!";
 
 540   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 542   for (my $run = 1; $run <= 2; $run++) {
 
 543     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 544            "> $form->{tmpfile}.err");
 
 546       $self->{"error"} = $form->cleanup();
 
 552   $form->{tmpfile} =~ s/tex$/pdf/;
 
 557 sub get_mime_type() {
 
 560   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 561     return "application/postscript";
 
 563     return "application/pdf";
 
 576 package HTMLTemplate;
 
 580 @ISA = qw(LaTeXTemplate);
 
 585   return $type->SUPER::new(@_);
 
 589   my ($self, $variable) = @_;
 
 590   my $form = $self->{"form"};
 
 592   $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
 
 594   # Allow some HTML markup to be converted into the output format's
 
 595   # corresponding markup code, e.g. bold or italic.
 
 596   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 598   foreach my $key (@markup_replace) {
 
 599     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 605 sub get_mime_type() {
 
 608   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 609     return "application/postscript";
 
 610   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 611     return "application/pdf";
 
 620   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 622   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 629 sub convert_to_postscript {
 
 631   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 633   # Convert the HTML file to postscript
 
 635   if (!chdir("$userspath")) {
 
 636     $self->{"error"} = "chdir : $!";
 
 641   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 642   my $psfile = $form->{"tmpfile"};
 
 643   $psfile =~ s/.html/.ps/;
 
 644   if ($psfile eq $form->{"tmpfile"}) {
 
 648   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 650     $self->{"error"} = $form->cleanup();
 
 655   $form->{"tmpfile"} = $psfile;
 
 664   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 666   # Convert the HTML file to PDF
 
 668   if (!chdir("$userspath")) {
 
 669     $self->{"error"} = "chdir : $!";
 
 674   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 675   my $pdffile = $form->{"tmpfile"};
 
 676   $pdffile =~ s/.html/.pdf/;
 
 677   if ($pdffile eq $form->{"tmpfile"}) {
 
 681   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 683     $self->{"error"} = $form->cleanup();
 
 688   $form->{"tmpfile"} = $pdffile;
 
 697 #### PlainTextTemplate
 
 700 package PlainTextTemplate;
 
 704 @ISA = qw(LaTeXTemplate);
 
 709   return $type->SUPER::new(@_);
 
 713   my ($self, $variable) = @_;
 
 728 #### OpenDocumentTemplate
 
 731 package OpenDocumentTemplate;
 
 739 # use File::Temp qw(:mktemp);
 
 742 @ISA = qw(SimpleTemplate);
 
 747   $self = $type->SUPER::new(@_);
 
 749   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 750     eval("use ${module};");
 
 752       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 753                              "loaded. Support for OpenDocument templates " .
 
 754                              "does not work without it. Please install your " .
 
 755                              "distribution's package or get the module from " .
 
 756                              "CPAN ( http://www.cpan.org ).");
 
 760   $self->{"rnd"} = int(rand(1000000));
 
 761   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 766 sub substitute_vars {
 
 767   my ($self, $text, @indices) = @_;
 
 769   my $form = $self->{"form"};
 
 771   while ($text =~ /\<\%(.*?)\%\>/) {
 
 772     my $value = $form->{$1};
 
 774     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 775       last unless (ref($value) eq "ARRAY");
 
 776       $value = $value->[$indices[$i]];
 
 778     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 785   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 787   my ($form, $new_contents) = ($self->{"form"}, "");
 
 789   my $ary = $form->{$var};
 
 790   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 791     last unless (ref($ary) eq "ARRAY");
 
 792     $ary = $ary->[$indices[$i]];
 
 795   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 796     $form->{"__first__"} = $i == 0;
 
 797     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 798     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 799     $form->{"__counter__"} = $i + 1;
 
 800     my $new_text = $self->parse_block($text, (@indices, $i));
 
 801     return undef unless (defined($new_text));
 
 802     $new_contents .= $start_tag . $new_text . $end_tag;
 
 804   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 806   return $new_contents;
 
 810   my ($self, $text, $pos, $var, $not) = @_;
 
 813   $pos = 0 unless ($pos);
 
 815   while ($pos < length($text)) {
 
 818     next if (substr($text, $pos - 1, 5) ne '<%');
 
 820     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 823     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 825         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 829       my $block = substr($text, 0, $pos - 1);
 
 830       substr($text, 0, $pos - 1) = "";
 
 831       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 832       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 834       return ($block, $text);
 
 836     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 839         my $block = substr($text, 0, $pos - 1);
 
 840         substr($text, 0, $pos - 1) = "";
 
 841         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 843         return ($block, $text);
 
 852   $main::lxdebug->enter_sub();
 
 854   my ($self, $contents, @indices) = @_;
 
 856   my $new_contents = "";
 
 858   while ($contents ne "") {
 
 859     if (substr($contents, 0, 1) eq "<") {
 
 860       $contents =~ m|^<[^>]+>|;
 
 862       substr($contents, 0, length($&)) = "";
 
 864       if ($tag =~ m|<table:table-row|) {
 
 865         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 868         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 870         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 873           substr($table_row, length($`), length($&)) = "";
 
 875           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 877             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 878             $main::lxdebug->leave_sub();
 
 882           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 883           if (!defined($new_text)) {
 
 884             $main::lxdebug->leave_sub();
 
 887           $new_contents .= $new_text;
 
 890           my $new_text = $self->parse_block($table_row, @indices);
 
 891           if (!defined($new_text)) {
 
 892             $main::lxdebug->leave_sub();
 
 895           $new_contents .= $tag . $new_text . $end_tag;
 
 899         $new_contents .= $tag;
 
 903       $contents =~ /^[^<]+/;
 
 906       my $pos_if = index($text, '<%if');
 
 907       my $pos_foreach = index($text, '<%foreach');
 
 909       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 910         substr($contents, 0, length($text)) = "";
 
 911         $new_contents .= $self->substitute_vars($text, @indices);
 
 915       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 916         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 917         substr($contents, 0, $pos_foreach) = "";
 
 919         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 920           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 921           $main::lxdebug->leave_sub();
 
 927         substr($contents, 0, length($&)) = "";
 
 930         ($block, $contents) = $self->find_end($contents);
 
 932           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 933           $main::lxdebug->leave_sub();
 
 937         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 938         if (!defined($new_text)) {
 
 939           $main::lxdebug->leave_sub();
 
 942         $new_contents .= $new_text;
 
 945         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 946         substr($contents, 0, $pos_if) = "";
 
 948         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 949           $self->{"error"} = "Malformed <\%if\%>.";
 
 950           $main::lxdebug->leave_sub();
 
 954         my ($not, $var) = ($1, $2);
 
 956         substr($contents, 0, length($&)) = "";
 
 958         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 960           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 961           $main::lxdebug->leave_sub();
 
 965         my $value = $self->{"form"}->{$var};
 
 966         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 967           last unless (ref($value) eq "ARRAY");
 
 968           $value = $value->[$indices[$i]];
 
 971         if (($not && !$value) || (!$not && $value)) {
 
 972           my $new_text = $self->parse_block($block, @indices);
 
 973           if (!defined($new_text)) {
 
 974             $main::lxdebug->leave_sub();
 
 977           $new_contents .= $new_text;
 
 983   $main::lxdebug->leave_sub();
 
 985   return $new_contents;
 
 989   $main::lxdebug->enter_sub();
 
 993   my $form = $self->{"form"};
 
 998   if ($form->{"IN"} =~ m|^/|) {
 
 999     $file_name = $form->{"IN"};
 
1001     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
1004   my $zip = Archive::Zip->new();
 
1005   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
1006     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
1007     $main::lxdebug->leave_sub();
 
1011   my $contents = $zip->contents("content.xml");
 
1013     $self->{"error"} = "File is not a OpenDocument file.";
 
1014     $main::lxdebug->leave_sub();
 
1018   my $rnd = $self->{"rnd"};
 
1019   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
1020 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
1022 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
1023 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
1025 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
1026 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
1028 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
1029 <style:text-properties style:text-line-through-style="solid"/>
 
1031 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
1032 <style:text-properties style:text-position="super 58%"/>
 
1034 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
1035 <style:text-properties style:text-position="sub 58%"/>
 
1039   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
1040   $contents =~ s|[\n\r]||gm;
 
1042   my $new_contents = $self->parse_block($contents);
 
1043   if (!defined($new_contents)) {
 
1044     $main::lxdebug->leave_sub();
 
1048 #   $new_contents =~ s|>|>\n|g;
 
1050   $zip->contents("content.xml", $new_contents);
 
1052   my $styles = $zip->contents("styles.xml");
 
1054     my $new_styles = $self->parse_block($styles);
 
1055     if (!defined($new_contents)) {
 
1056       $main::lxdebug->leave_sub();
 
1059     $zip->contents("styles.xml", $new_styles);
 
1062   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
1065   if ($form->{"format"} =~ /pdf/) {
 
1066     $res = $self->convert_to_pdf();
 
1069   $main::lxdebug->leave_sub();
 
1073 sub is_xvfb_running {
 
1074   $main::lxdebug->enter_sub();
 
1079   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1082   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
1083   if ((-f $dfname) && open(IN, $dfname)) {
 
1088     my $xauthority = <IN>;
 
1092     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
1094     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
1095       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
1096       unlink($dfname, $xauthority);
 
1097       $main::lxdebug->leave_sub();
 
1102     if ($line !~ /xvfb/i) {
 
1103       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
1104       unlink($dfname, $xauthority);
 
1105       $main::lxdebug->leave_sub();
 
1109     $ENV{"XAUTHORITY"} = $xauthority;
 
1110     $ENV{"DISPLAY"} = $display;
 
1112     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1115   $main::lxdebug->leave_sub();
 
1121   $main::lxdebug->enter_sub();
 
1125   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1127   my $display = $self->is_xvfb_running();
 
1130     $main::lxdebug->leave_sub();
 
1135   while ( -f "/tmp/.X${display}-lock") {
 
1138   $display = ":${display}";
 
1139   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1141   my $mcookie = `mcookie`;
 
1142   die("Installation error: mcookie not found.") if ($? != 0);
 
1145   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1147   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1148   $ENV{"XAUTHORITY"} = $xauthority;
 
1150   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1152   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1154     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1155     $main::lxdebug->leave_sub();
 
1159   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1163     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1164     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1167   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1170   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1171   if (!open(OUT, ">$dfname")) {
 
1172     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1173     unlink($xauthority);
 
1175     $main::lxdebug->leave_sub();
 
1178   print(OUT "$pid\n$display\n$xauthority\n");
 
1181   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1183   if (!$self->is_xvfb_running()) {
 
1184     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1185     unlink($xauthority, $dfname);
 
1187     $main::lxdebug->leave_sub();
 
1191   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1193   $main::lxdebug->leave_sub();
 
1198 sub is_openoffice_running {
 
1199   $main::lxdebug->enter_sub();
 
1201   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1202          "> /dev/null 2> /dev/null");
 
1204   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1206   $main::lxdebug->leave_sub();
 
1211 sub spawn_openoffice {
 
1212   $main::lxdebug->enter_sub();
 
1216   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1218   my ($try, $spawned_oo, $res);
 
1221   for ($try = 0; $try < 15; $try++) {
 
1222     if ($self->is_openoffice_running()) {
 
1230         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1232         open(STDIN, '/dev/null');
 
1233         open(STDOUT, '>/dev/null');
 
1234         my $new_pid = fork();
 
1236         my $ssres = setsid();
 
1237         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1238         my @cmdline = ($main::openofficeorg_writer_bin,
 
1239                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1241                        "-accept=socket,host=localhost,port=" .
 
1242                        $main::openofficeorg_daemon_port . ";urp;");
 
1246       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1251     sleep($try >= 5 ? 2 : 1);
 
1255     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1256       "OpenOffice could not be started.";
 
1259   $main::lxdebug->leave_sub();
 
1264 sub convert_to_pdf {
 
1265   $main::lxdebug->enter_sub();
 
1269   my $form = $self->{"form"};
 
1271   my $filename = $form->{"tmpfile"};
 
1272   $filename =~ s/.odt$//;
 
1273   if (substr($filename, 0, 1) ne "/") {
 
1274     $filename = getcwd() . "/${filename}";
 
1277   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1278     $ENV{'HOME'} = $self->{"userspath"};
 
1280     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1283   if (!$self->spawn_xvfb()) {
 
1284     $main::lxdebug->leave_sub();
 
1289   if (!$main::openofficeorg_daemon) {
 
1290     @cmdline = ($main::openofficeorg_writer_bin,
 
1291                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1293                 "file:${filename}.odt",
 
1294                 "macro://" . (split('/', $filename))[-1] .
 
1295                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1297     if (!$self->spawn_openoffice()) {
 
1298       $main::lxdebug->leave_sub();
 
1302     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1303                 $main::openofficeorg_daemon_port,
 
1311     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1313     unlink($filename . ".odt");
 
1315     $main::lxdebug->leave_sub();
 
1320   unlink($filename . ".odt", $filename . ".pdf");
 
1321   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1324   $main::lxdebug->leave_sub();
 
1329   my ($self, $variable) = @_;
 
1330   my $form = $self->{"form"};
 
1331   my $iconv = $self->{"iconv"};
 
1333   $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
 
1335   # Allow some HTML markup to be converted into the output format's
 
1336   # corresponding markup code, e.g. bold or italic.
 
1337   my $rnd = $self->{"rnd"};
 
1338   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1339                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1341   foreach my $key (keys(%markup_replace)) {
 
1342     my $value = $markup_replace{$key};
 
1343     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
 
1344     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1347   return $iconv->convert($variable);
 
1350 sub get_mime_type() {
 
1351   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1352     return "application/pdf";
 
1354     return "application/vnd.oasis.opendocument.text";
 
1358 sub uses_temp_file {
 
1363 ##########################################################
 
1367 ##########################################################
 
1369 package XMLTemplate; 
 
1373 @ISA = qw(HTMLTemplate);
 
1376   #evtl auskommentieren
 
1379   return $type->SUPER::new(@_);
 
1383   my ($self, $variable) = @_;
 
1384   my $form = $self->{"form"};
 
1386   $variable = $main::locale->quote_special_chars('Template/XML', $variable);
 
1388   # Allow no markup to be converted into the output format
 
1389   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
1391   foreach my $key (@markup_replace) {
 
1392     $variable =~ s/\<(\/?)${key}\>//g;
 
1398 sub get_mime_type() {
 
1401   if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
 
1402     return "application/xml ";
 
1403   } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
 
1404     return "application/x-taxbird";
 
1410 sub uses_temp_file {
 
1411   # tempfile needet for XML Output