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) || ($form->{"description"}->[$i] =~ /<pagebreak>/) || ($form->{"longdescription"}->[$i] =~ /<pagebreak>/)) {
 
 184         my $pb = $self->{"pagebreak_block"};
 
 186         # replace the special variables <%sumcarriedforward%>
 
 189         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 190         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
 
 191         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
 
 193         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 194         return undef unless (defined($new_text));
 
 195         $new_contents .= $new_text;
 
 200       $current_line += $lines;
 
 202     if ($i < scalar(@{$form->{"linetotal"}})) {
 
 203       $sum += $form->parse_amount($self->{"myconfig"},
 
 204                                   $form->{"linetotal"}->[$i]);
 
 207     $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 209     my $new_text = $self->parse_block($text, (@indices, $i));
 
 210     return undef unless (defined($new_text));
 
 211     $new_contents .= $start_tag . $new_text . $end_tag;
 
 213   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 215   return $new_contents;
 
 219   my ($self, $text, $pos, $var, $not) = @_;
 
 221   my $tag_start_len = length $self->{tag_start};
 
 224   $pos = 0 unless ($pos);
 
 226   while ($pos < length($text)) {
 
 229     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
 231     my $keyword_pos = $pos - 1 + $tag_start_len;
 
 233     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
 
 236     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 239             "$self->{tag_start}else$self->{tag_end} outside of "
 
 240           . "$self->{tag_start}if$self->{tag_end} / "
 
 241           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 245       my $block = substr($text, 0, $pos - 1);
 
 246       substr($text, 0, $pos - 1) = "";
 
 247       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 248       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 250       return ($block, $text);
 
 252     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 255         my $block = substr($text, 0, $pos - 1);
 
 256         substr($text, 0, $pos - 1) = "";
 
 257         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 259         return ($block, $text);
 
 268   $main::lxdebug->enter_sub();
 
 270   my ($self, $contents, @indices) = @_;
 
 272   my $new_contents = "";
 
 274   while ($contents ne "") {
 
 275     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 276     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 278     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 279       $new_contents .= $self->substitute_vars($contents, @indices);
 
 283     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 284       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 285       substr($contents, 0, $pos_foreach) = "";
 
 287       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 288         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 289         $main::lxdebug->leave_sub();
 
 295       substr($contents, 0, length($&)) = "";
 
 298       ($block, $contents) = $self->find_end($contents);
 
 300         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 301         $main::lxdebug->leave_sub();
 
 305       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 306       if (!defined($new_text)) {
 
 307         $main::lxdebug->leave_sub();
 
 310       $new_contents .= $new_text;
 
 313       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 314       substr($contents, 0, $pos_if) = "";
 
 316       if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
 
 317         $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
 
 318         $main::lxdebug->leave_sub();
 
 322       my ($not, $var) = ($1, $2);
 
 324       substr($contents, 0, length($&)) = "";
 
 326       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 328         $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
 
 329         $main::lxdebug->leave_sub();
 
 333       my $value = $self->{"form"}->{$var};
 
 334       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 335         last unless (ref($value) eq "ARRAY");
 
 336         $value = $value->[$indices[$i]];
 
 339       if (($not && !$value) || (!$not && $value)) {
 
 340         my $new_text = $self->parse_block($block, @indices);
 
 341         if (!defined($new_text)) {
 
 342           $main::lxdebug->leave_sub();
 
 345         $new_contents .= $new_text;
 
 350   $main::lxdebug->leave_sub();
 
 352   return $new_contents;
 
 355 sub parse_first_line {
 
 357   my $line = shift || "";
 
 359   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 361       $self->{error} = "The tag start and end markers must not be equal.";
 
 365     $self->set_tag_style($1, $2);
 
 371 sub _parse_config_option {
 
 378   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 380   if ($key eq 'tag-style') {
 
 381     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 385 sub _parse_config_lines {
 
 389   my ($comment_start, $comment_end) = ("", "");
 
 391   if (ref $self eq 'LaTeXTemplate') {
 
 392     $comment_start = '\s*%';
 
 393   } elsif (ref $self eq 'HTMLTemplate') {
 
 394     $comment_start = '\s*<!--';
 
 395     $comment_end   = '>\s*';
 
 397     $comment_start = '\s*\#';
 
 400   my $num_lines = scalar @{ $lines };
 
 403   while ($i < $num_lines) {
 
 404     my $line = $lines->[$i];
 
 406     if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
 
 411     $self->_parse_config_option($1);
 
 412     splice @{ $lines }, $i, 1;
 
 417 sub _force_mandatory_packages {
 
 421   my (%used_packages, $document_start_line);
 
 423   foreach my $i (0 .. scalar @{ $lines } - 1) {
 
 424     if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
 
 425       $used_packages{$1} = 1;
 
 427     } elsif ($lines->[$i] =~ m/\\begin{document}/) {
 
 428       $document_start_line = $i;
 
 434   $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
 
 436   if (!$used_packages{textcomp}) {
 
 437     splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
 
 438     $document_start_line++;
 
 445   my $form = $self->{"form"};
 
 447   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 448     $self->{"error"} = "$!";
 
 454   $self->_parse_config_lines(\@lines);
 
 455   $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
 
 457   my $contents = join("", @lines);
 
 459   # detect pagebreak block and its parameters
 
 460   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) {
 
 461     $self->{"chars_per_line"} = $1;
 
 462     $self->{"lines_on_first_page"} = $2;
 
 463     $self->{"lines_on_second_page"} = $3;
 
 464     $self->{"pagebreak_block"} = $4;
 
 466     substr($contents, length($`), length($&)) = "";
 
 469   $self->{"forced_pagebreaks"} = [];
 
 471   my $new_contents = $self->parse_block($contents);
 
 472   if (!defined($new_contents)) {
 
 473     $main::lxdebug->leave_sub();
 
 477   print(OUT $new_contents);
 
 479   if ($form->{"format"} =~ /postscript/i) {
 
 480     return $self->convert_to_postscript();
 
 481   } elsif ($form->{"format"} =~ /pdf/i) {
 
 482     return $self->convert_to_pdf();
 
 488 sub convert_to_postscript {
 
 490   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 492   # Convert the tex file to postscript
 
 494   if (!chdir("$userspath")) {
 
 495     $self->{"error"} = "chdir : $!";
 
 500   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 502   for (my $run = 1; $run <= 2; $run++) {
 
 503     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 504            "> $form->{tmpfile}.err");
 
 506       $self->{"error"} = $form->cleanup();
 
 512   $form->{tmpfile} =~ s/tex$/dvi/;
 
 514   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 516     $self->{"error"} = "dvips : $!";
 
 520   $form->{tmpfile} =~ s/dvi$/ps/;
 
 529   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 531   # Convert the tex file to PDF
 
 533   if (!chdir("$userspath")) {
 
 534     $self->{"error"} = "chdir : $!";
 
 539   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 541   for (my $run = 1; $run <= 2; $run++) {
 
 542     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 543            "> $form->{tmpfile}.err");
 
 545       $self->{"error"} = $form->cleanup();
 
 551   $form->{tmpfile} =~ s/tex$/pdf/;
 
 556 sub get_mime_type() {
 
 559   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 560     return "application/postscript";
 
 562     return "application/pdf";
 
 575 package HTMLTemplate;
 
 579 @ISA = qw(LaTeXTemplate);
 
 584   return $type->SUPER::new(@_);
 
 588   my ($self, $variable) = @_;
 
 589   my $form = $self->{"form"};
 
 591   $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
 
 593   # Allow some HTML markup to be converted into the output format's
 
 594   # corresponding markup code, e.g. bold or italic.
 
 595   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 597   foreach my $key (@markup_replace) {
 
 598     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 604 sub get_mime_type() {
 
 607   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 608     return "application/postscript";
 
 609   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 610     return "application/pdf";
 
 619   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 621   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 628 sub convert_to_postscript {
 
 630   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 632   # Convert the HTML file to postscript
 
 634   if (!chdir("$userspath")) {
 
 635     $self->{"error"} = "chdir : $!";
 
 640   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 641   my $psfile = $form->{"tmpfile"};
 
 642   $psfile =~ s/.html/.ps/;
 
 643   if ($psfile eq $form->{"tmpfile"}) {
 
 647   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 649     $self->{"error"} = $form->cleanup();
 
 654   $form->{"tmpfile"} = $psfile;
 
 663   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 665   # Convert the HTML file to PDF
 
 667   if (!chdir("$userspath")) {
 
 668     $self->{"error"} = "chdir : $!";
 
 673   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 674   my $pdffile = $form->{"tmpfile"};
 
 675   $pdffile =~ s/.html/.pdf/;
 
 676   if ($pdffile eq $form->{"tmpfile"}) {
 
 680   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 682     $self->{"error"} = $form->cleanup();
 
 687   $form->{"tmpfile"} = $pdffile;
 
 696 #### PlainTextTemplate
 
 699 package PlainTextTemplate;
 
 703 @ISA = qw(LaTeXTemplate);
 
 708   return $type->SUPER::new(@_);
 
 712   my ($self, $variable) = @_;
 
 727 #### OpenDocumentTemplate
 
 730 package OpenDocumentTemplate;
 
 738 # use File::Temp qw(:mktemp);
 
 741 @ISA = qw(SimpleTemplate);
 
 746   $self = $type->SUPER::new(@_);
 
 748   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 749     eval("use ${module};");
 
 751       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 752                              "loaded. Support for OpenDocument templates " .
 
 753                              "does not work without it. Please install your " .
 
 754                              "distribution's package or get the module from " .
 
 755                              "CPAN ( http://www.cpan.org ).");
 
 759   $self->{"rnd"} = int(rand(1000000));
 
 760   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 765 sub substitute_vars {
 
 766   my ($self, $text, @indices) = @_;
 
 768   my $form = $self->{"form"};
 
 770   while ($text =~ /\<\%(.*?)\%\>/) {
 
 771     my $value = $form->{$1};
 
 773     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 774       last unless (ref($value) eq "ARRAY");
 
 775       $value = $value->[$indices[$i]];
 
 777     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 784   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 786   my ($form, $new_contents) = ($self->{"form"}, "");
 
 788   my $ary = $form->{$var};
 
 789   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 790     last unless (ref($ary) eq "ARRAY");
 
 791     $ary = $ary->[$indices[$i]];
 
 794   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 795     $form->{"__first__"} = $i == 0;
 
 796     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 797     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 798     $form->{"__counter__"} = $i + 1;
 
 799     my $new_text = $self->parse_block($text, (@indices, $i));
 
 800     return undef unless (defined($new_text));
 
 801     $new_contents .= $start_tag . $new_text . $end_tag;
 
 803   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 805   return $new_contents;
 
 809   my ($self, $text, $pos, $var, $not) = @_;
 
 812   $pos = 0 unless ($pos);
 
 814   while ($pos < length($text)) {
 
 817     next if (substr($text, $pos - 1, 5) ne '<%');
 
 819     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 822     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 824         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 828       my $block = substr($text, 0, $pos - 1);
 
 829       substr($text, 0, $pos - 1) = "";
 
 830       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 831       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 833       return ($block, $text);
 
 835     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 838         my $block = substr($text, 0, $pos - 1);
 
 839         substr($text, 0, $pos - 1) = "";
 
 840         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 842         return ($block, $text);
 
 851   $main::lxdebug->enter_sub();
 
 853   my ($self, $contents, @indices) = @_;
 
 855   my $new_contents = "";
 
 857   while ($contents ne "") {
 
 858     if (substr($contents, 0, 1) eq "<") {
 
 859       $contents =~ m|^<[^>]+>|;
 
 861       substr($contents, 0, length($&)) = "";
 
 863       if ($tag =~ m|<table:table-row|) {
 
 864         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 867         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 869         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 872           substr($table_row, length($`), length($&)) = "";
 
 874           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 876             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 877             $main::lxdebug->leave_sub();
 
 881           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 882           if (!defined($new_text)) {
 
 883             $main::lxdebug->leave_sub();
 
 886           $new_contents .= $new_text;
 
 889           my $new_text = $self->parse_block($table_row, @indices);
 
 890           if (!defined($new_text)) {
 
 891             $main::lxdebug->leave_sub();
 
 894           $new_contents .= $tag . $new_text . $end_tag;
 
 898         $new_contents .= $tag;
 
 902       $contents =~ /^[^<]+/;
 
 905       my $pos_if = index($text, '<%if');
 
 906       my $pos_foreach = index($text, '<%foreach');
 
 908       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 909         substr($contents, 0, length($text)) = "";
 
 910         $new_contents .= $self->substitute_vars($text, @indices);
 
 914       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 915         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 916         substr($contents, 0, $pos_foreach) = "";
 
 918         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 919           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 920           $main::lxdebug->leave_sub();
 
 926         substr($contents, 0, length($&)) = "";
 
 929         ($block, $contents) = $self->find_end($contents);
 
 931           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 932           $main::lxdebug->leave_sub();
 
 936         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 937         if (!defined($new_text)) {
 
 938           $main::lxdebug->leave_sub();
 
 941         $new_contents .= $new_text;
 
 944         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 945         substr($contents, 0, $pos_if) = "";
 
 947         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 948           $self->{"error"} = "Malformed <\%if\%>.";
 
 949           $main::lxdebug->leave_sub();
 
 953         my ($not, $var) = ($1, $2);
 
 955         substr($contents, 0, length($&)) = "";
 
 957         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 959           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 960           $main::lxdebug->leave_sub();
 
 964         my $value = $self->{"form"}->{$var};
 
 965         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 966           last unless (ref($value) eq "ARRAY");
 
 967           $value = $value->[$indices[$i]];
 
 970         if (($not && !$value) || (!$not && $value)) {
 
 971           my $new_text = $self->parse_block($block, @indices);
 
 972           if (!defined($new_text)) {
 
 973             $main::lxdebug->leave_sub();
 
 976           $new_contents .= $new_text;
 
 982   $main::lxdebug->leave_sub();
 
 984   return $new_contents;
 
 988   $main::lxdebug->enter_sub();
 
 992   my $form = $self->{"form"};
 
 997   if ($form->{"IN"} =~ m|^/|) {
 
 998     $file_name = $form->{"IN"};
 
1000     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
1003   my $zip = Archive::Zip->new();
 
1004   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
1005     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
1006     $main::lxdebug->leave_sub();
 
1010   my $contents = $zip->contents("content.xml");
 
1012     $self->{"error"} = "File is not a OpenDocument file.";
 
1013     $main::lxdebug->leave_sub();
 
1017   my $rnd = $self->{"rnd"};
 
1018   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
1019 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
1021 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
1022 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
1024 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
1025 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
1027 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
1028 <style:text-properties style:text-line-through-style="solid"/>
 
1030 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
1031 <style:text-properties style:text-position="super 58%"/>
 
1033 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
1034 <style:text-properties style:text-position="sub 58%"/>
 
1038   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
1039   $contents =~ s|[\n\r]||gm;
 
1041   my $new_contents = $self->parse_block($contents);
 
1042   if (!defined($new_contents)) {
 
1043     $main::lxdebug->leave_sub();
 
1047 #   $new_contents =~ s|>|>\n|g;
 
1049   $zip->contents("content.xml", $new_contents);
 
1051   my $styles = $zip->contents("styles.xml");
 
1053     my $new_styles = $self->parse_block($styles);
 
1054     if (!defined($new_contents)) {
 
1055       $main::lxdebug->leave_sub();
 
1058     $zip->contents("styles.xml", $new_styles);
 
1061   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
1064   if ($form->{"format"} =~ /pdf/) {
 
1065     $res = $self->convert_to_pdf();
 
1068   $main::lxdebug->leave_sub();
 
1072 sub is_xvfb_running {
 
1073   $main::lxdebug->enter_sub();
 
1078   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1081   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
1082   if ((-f $dfname) && open(IN, $dfname)) {
 
1087     my $xauthority = <IN>;
 
1091     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
1093     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
1094       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
1095       unlink($dfname, $xauthority);
 
1096       $main::lxdebug->leave_sub();
 
1101     if ($line !~ /xvfb/i) {
 
1102       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
1103       unlink($dfname, $xauthority);
 
1104       $main::lxdebug->leave_sub();
 
1108     $ENV{"XAUTHORITY"} = $xauthority;
 
1109     $ENV{"DISPLAY"} = $display;
 
1111     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1114   $main::lxdebug->leave_sub();
 
1120   $main::lxdebug->enter_sub();
 
1124   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1126   my $display = $self->is_xvfb_running();
 
1129     $main::lxdebug->leave_sub();
 
1134   while ( -f "/tmp/.X${display}-lock") {
 
1137   $display = ":${display}";
 
1138   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1140   my $mcookie = `mcookie`;
 
1141   die("Installation error: mcookie not found.") if ($? != 0);
 
1144   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1146   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1147   $ENV{"XAUTHORITY"} = $xauthority;
 
1149   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1151   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1153     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1154     $main::lxdebug->leave_sub();
 
1158   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1162     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1163     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1166   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1169   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1170   if (!open(OUT, ">$dfname")) {
 
1171     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1172     unlink($xauthority);
 
1174     $main::lxdebug->leave_sub();
 
1177   print(OUT "$pid\n$display\n$xauthority\n");
 
1180   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1182   if (!$self->is_xvfb_running()) {
 
1183     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1184     unlink($xauthority, $dfname);
 
1186     $main::lxdebug->leave_sub();
 
1190   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1192   $main::lxdebug->leave_sub();
 
1197 sub is_openoffice_running {
 
1198   $main::lxdebug->enter_sub();
 
1200   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1201          "> /dev/null 2> /dev/null");
 
1203   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1205   $main::lxdebug->leave_sub();
 
1210 sub spawn_openoffice {
 
1211   $main::lxdebug->enter_sub();
 
1215   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1217   my ($try, $spawned_oo, $res);
 
1220   for ($try = 0; $try < 15; $try++) {
 
1221     if ($self->is_openoffice_running()) {
 
1229         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1231         open(STDIN, '/dev/null');
 
1232         open(STDOUT, '>/dev/null');
 
1233         my $new_pid = fork();
 
1235         my $ssres = setsid();
 
1236         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1237         my @cmdline = ($main::openofficeorg_writer_bin,
 
1238                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1240                        "-accept=socket,host=localhost,port=" .
 
1241                        $main::openofficeorg_daemon_port . ";urp;");
 
1245       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1250     sleep($try >= 5 ? 2 : 1);
 
1254     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1255       "OpenOffice could not be started.";
 
1258   $main::lxdebug->leave_sub();
 
1263 sub convert_to_pdf {
 
1264   $main::lxdebug->enter_sub();
 
1268   my $form = $self->{"form"};
 
1270   my $filename = $form->{"tmpfile"};
 
1271   $filename =~ s/.odt$//;
 
1272   if (substr($filename, 0, 1) ne "/") {
 
1273     $filename = getcwd() . "/${filename}";
 
1276   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1277     $ENV{'HOME'} = $self->{"userspath"};
 
1279     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1282   if (!$self->spawn_xvfb()) {
 
1283     $main::lxdebug->leave_sub();
 
1288   if (!$main::openofficeorg_daemon) {
 
1289     @cmdline = ($main::openofficeorg_writer_bin,
 
1290                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1292                 "file:${filename}.odt",
 
1293                 "macro://" . (split('/', $filename))[-1] .
 
1294                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1296     if (!$self->spawn_openoffice()) {
 
1297       $main::lxdebug->leave_sub();
 
1301     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1302                 $main::openofficeorg_daemon_port,
 
1310     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1312     unlink($filename . ".odt");
 
1314     $main::lxdebug->leave_sub();
 
1319   unlink($filename . ".odt", $filename . ".pdf");
 
1320   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1323   $main::lxdebug->leave_sub();
 
1328   my ($self, $variable) = @_;
 
1329   my $form = $self->{"form"};
 
1330   my $iconv = $self->{"iconv"};
 
1332   $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
 
1334   # Allow some HTML markup to be converted into the output format's
 
1335   # corresponding markup code, e.g. bold or italic.
 
1336   my $rnd = $self->{"rnd"};
 
1337   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1338                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1340   foreach my $key (keys(%markup_replace)) {
 
1341     my $value = $markup_replace{$key};
 
1342     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
 
1343     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1346   return $iconv->convert($variable);
 
1349 sub get_mime_type() {
 
1350   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1351     return "application/pdf";
 
1353     return "application/vnd.oasis.opendocument.text";
 
1357 sub uses_temp_file {
 
1362 ##########################################################
 
1366 ##########################################################
 
1368 package XMLTemplate; 
 
1372 @ISA = qw(HTMLTemplate);
 
1375   #evtl auskommentieren
 
1378   return $type->SUPER::new(@_);
 
1382   my ($self, $variable) = @_;
 
1383   my $form = $self->{"form"};
 
1385   $variable = $main::locale->quote_special_chars('Template/XML', $variable);
 
1387   # Allow no markup to be converted into the output format
 
1388   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
1390   foreach my $key (@markup_replace) {
 
1391     $variable =~ s/\<(\/?)${key}\>//g;
 
1397 sub get_mime_type() {
 
1400   if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
 
1401     return "application/xml ";
 
1402   } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
 
1403     return "application/x-taxbird";
 
1409 sub uses_temp_file {
 
1410   # tempfile needet for XML Output