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;
 
  51   $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
 
  59 #   1. A typeglob for the file handle. The output will be written
 
  60 #      to this file handle.
 
  63 #   1 on success and undef or 0 if there was an error. In the latter case
 
  64 #   the calling function can retrieve the error message via $obj->get_error()
 
  69   print(OUT "Hallo!\n");
 
  75   return $self->{"error"};
 
  82 sub _get_loop_variable {
 
  85   my $get_array = shift;
 
  88   my $form      = $self->{form};
 
  91   if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
 
  92     $value = $form->{TEMPLATE_ARRAYS}->{$var};
 
  94     $value = $form->{$var};
 
  97   for (my $i = 0; $i < scalar(@indices); $i++) {
 
  98     last unless (ref($value) eq "ARRAY");
 
  99     $value = $value->[$indices[$i]];
 
 105 sub substitute_vars {
 
 106   my ($self, $text, @indices) = @_;
 
 108   my $form = $self->{"form"};
 
 110   while ($text =~ /$self->{substitute_vars_re}/) {
 
 111     my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
 
 112     my ($var, @options)     = split(/\s+/, $1);
 
 114     my $value               = $self->_get_loop_variable($var, 0, @indices);
 
 115     $value                  = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
 
 117     substr($text, $tag_pos, $tag_len, $value);
 
 129 package LaTeXTemplate;
 
 133 @ISA = qw(SimpleTemplate);
 
 138   my $self = $type->SUPER::new(@_);
 
 144   my ($self, $variable) = @_;
 
 145   my $form = $self->{"form"};
 
 147   $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
 
 149   # Allow some HTML markup to be converted into the output format's
 
 150   # corresponding markup code, e.g. bold or italic.
 
 151   my %markup_replace = ('b' => 'textbf',
 
 155   foreach my $key (keys(%markup_replace)) {
 
 156     my $new = $markup_replace{$key};
 
 157     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
 160   $variable =~ s/[\x00-\x1f]//g;
 
 166   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 168   my ($form, $new_contents) = ($self->{"form"}, "");
 
 170   my $ary = $self->_get_loop_variable($var, 1, @indices);
 
 173   my $current_page = 1;
 
 174   my ($current_line, $corrent_row) = (0, 1);
 
 176   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 177     $form->{"__first__"} = $i == 0;
 
 178     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 179     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 180     $form->{"__counter__"} = $i + 1;
 
 182     if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
 
 183         $self->{"chars_per_line"}) {
 
 185         int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
 
 188       $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
 
 189       my $_description = $form->{"description"}->[$i];
 
 190       while ($_description =~ /\\newline/) {
 
 192         $_description =~ s/\\newline//;
 
 196       if ($current_page == 1) {
 
 197         $lpp = $self->{"lines_on_first_page"};
 
 199         $lpp = $self->{"lines_on_second_page"};
 
 202       # Yes we need a manual page break -- or the user has forced one
 
 203       if ((($current_line + $lines) > $lpp) || ($form->{"description"}->[$i] =~ /<pagebreak>/) || ($form->{"longdescription"}->[$i] =~ /<pagebreak>/)) {
 
 204         my $pb = $self->{"pagebreak_block"};
 
 206         # replace the special variables <%sumcarriedforward%>
 
 209         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 210         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
 
 211         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
 
 213         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 214         return undef unless (defined($new_text));
 
 215         $new_contents .= $new_text;
 
 220       $current_line += $lines;
 
 222     if ($i < scalar(@{$form->{"linetotal"}})) {
 
 223       $sum += $form->parse_amount($self->{"myconfig"},
 
 224                                   $form->{"linetotal"}->[$i]);
 
 227     $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 229     my $new_text = $self->parse_block($text, (@indices, $i));
 
 230     return undef unless (defined($new_text));
 
 231     $new_contents .= $start_tag . $new_text . $end_tag;
 
 233   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 235   return $new_contents;
 
 239   my ($self, $text, $pos, $var, $not) = @_;
 
 241   my $tag_start_len = length $self->{tag_start};
 
 244   $pos = 0 unless ($pos);
 
 246   while ($pos < length($text)) {
 
 249     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
 251     my $keyword_pos = $pos - 1 + $tag_start_len;
 
 253     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
 
 256     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 259             "$self->{tag_start}else$self->{tag_end} outside of "
 
 260           . "$self->{tag_start}if$self->{tag_end} / "
 
 261           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 265       my $block = substr($text, 0, $pos - 1);
 
 266       substr($text, 0, $pos - 1) = "";
 
 267       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 268       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 270       return ($block, $text);
 
 272     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 275         my $block = substr($text, 0, $pos - 1);
 
 276         substr($text, 0, $pos - 1) = "";
 
 277         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 279         return ($block, $text);
 
 288   $main::lxdebug->enter_sub();
 
 290   my ($self, $contents, @indices) = @_;
 
 292   my $new_contents = "";
 
 294   while ($contents ne "") {
 
 295     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 296     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 298     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 299       $new_contents .= $self->substitute_vars($contents, @indices);
 
 303     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 304       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 305       substr($contents, 0, $pos_foreach) = "";
 
 307       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 308         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 309         $main::lxdebug->leave_sub();
 
 315       substr($contents, 0, length($&)) = "";
 
 318       ($block, $contents) = $self->find_end($contents);
 
 320         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 321         $main::lxdebug->leave_sub();
 
 325       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 326       if (!defined($new_text)) {
 
 327         $main::lxdebug->leave_sub();
 
 330       $new_contents .= $new_text;
 
 333       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 334       substr($contents, 0, $pos_if) = "";
 
 336       if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
 
 337         $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
 
 338         $main::lxdebug->leave_sub();
 
 342       my ($not, $var) = ($1, $2);
 
 344       substr($contents, 0, length($&)) = "";
 
 346       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 348         $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
 
 349         $main::lxdebug->leave_sub();
 
 353       my $value = $self->{"form"}->{$var};
 
 354       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 355         last unless (ref($value) eq "ARRAY");
 
 356         $value = $value->[$indices[$i]];
 
 359       if (($not && !$value) || (!$not && $value)) {
 
 360         my $new_text = $self->parse_block($block, @indices);
 
 361         if (!defined($new_text)) {
 
 362           $main::lxdebug->leave_sub();
 
 365         $new_contents .= $new_text;
 
 370   $main::lxdebug->leave_sub();
 
 372   return $new_contents;
 
 375 sub parse_first_line {
 
 377   my $line = shift || "";
 
 379   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 381       $self->{error} = "The tag start and end markers must not be equal.";
 
 385     $self->set_tag_style($1, $2);
 
 391 sub _parse_config_option {
 
 398   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 400   if ($key eq 'tag-style') {
 
 401     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 405 sub _parse_config_lines {
 
 409   my ($comment_start, $comment_end) = ("", "");
 
 411   if (ref $self eq 'LaTeXTemplate') {
 
 412     $comment_start = '\s*%';
 
 413   } elsif (ref $self eq 'HTMLTemplate') {
 
 414     $comment_start = '\s*<!--';
 
 415     $comment_end   = '>\s*';
 
 417     $comment_start = '\s*\#';
 
 420   my $num_lines = scalar @{ $lines };
 
 423   while ($i < $num_lines) {
 
 424     my $line = $lines->[$i];
 
 426     if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
 
 431     $self->_parse_config_option($1);
 
 432     splice @{ $lines }, $i, 1;
 
 437 sub _force_mandatory_packages {
 
 441   my (%used_packages, $document_start_line);
 
 443   foreach my $i (0 .. scalar @{ $lines } - 1) {
 
 444     if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
 
 445       $used_packages{$1} = 1;
 
 447     } elsif ($lines->[$i] =~ m/\\begin{document}/) {
 
 448       $document_start_line = $i;
 
 454   $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
 
 456   if (!$used_packages{textcomp}) {
 
 457     splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
 
 458     $document_start_line++;
 
 465   my $form = $self->{"form"};
 
 467   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 468     $self->{"error"} = "$!";
 
 474   $self->_parse_config_lines(\@lines);
 
 475   $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
 
 477   my $contents = join("", @lines);
 
 479   # detect pagebreak block and its parameters
 
 480   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) {
 
 481     $self->{"chars_per_line"} = $1;
 
 482     $self->{"lines_on_first_page"} = $2;
 
 483     $self->{"lines_on_second_page"} = $3;
 
 484     $self->{"pagebreak_block"} = $4;
 
 486     substr($contents, length($`), length($&)) = "";
 
 489   $self->{"forced_pagebreaks"} = [];
 
 491   my $new_contents = $self->parse_block($contents);
 
 492   if (!defined($new_contents)) {
 
 493     $main::lxdebug->leave_sub();
 
 497   print(OUT $new_contents);
 
 499   if ($form->{"format"} =~ /postscript/i) {
 
 500     return $self->convert_to_postscript();
 
 501   } elsif ($form->{"format"} =~ /pdf/i) {
 
 502     return $self->convert_to_pdf();
 
 508 sub convert_to_postscript {
 
 510   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 512   # Convert the tex file to postscript
 
 514   if (!chdir("$userspath")) {
 
 515     $self->{"error"} = "chdir : $!";
 
 520   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 522   for (my $run = 1; $run <= 2; $run++) {
 
 523     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 524            "> $form->{tmpfile}.err");
 
 526       $self->{"error"} = $form->cleanup();
 
 532   $form->{tmpfile} =~ s/tex$/dvi/;
 
 534   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 536     $self->{"error"} = "dvips : $!";
 
 540   $form->{tmpfile} =~ s/dvi$/ps/;
 
 549   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 551   # Convert the tex file to PDF
 
 553   if (!chdir("$userspath")) {
 
 554     $self->{"error"} = "chdir : $!";
 
 559   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 561   for (my $run = 1; $run <= 2; $run++) {
 
 562     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 563            "> $form->{tmpfile}.err");
 
 565       $self->{"error"} = $form->cleanup();
 
 571   $form->{tmpfile} =~ s/tex$/pdf/;
 
 576 sub get_mime_type() {
 
 579   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 580     return "application/postscript";
 
 582     return "application/pdf";
 
 595 package HTMLTemplate;
 
 599 @ISA = qw(LaTeXTemplate);
 
 604   return $type->SUPER::new(@_);
 
 608   my ($self, $variable) = @_;
 
 609   my $form = $self->{"form"};
 
 611   $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
 
 613   # Allow some HTML markup to be converted into the output format's
 
 614   # corresponding markup code, e.g. bold or italic.
 
 615   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 617   foreach my $key (@markup_replace) {
 
 618     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 624 sub get_mime_type() {
 
 627   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 628     return "application/postscript";
 
 629   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 630     return "application/pdf";
 
 639   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 641   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 648 sub convert_to_postscript {
 
 650   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 652   # Convert the HTML file to postscript
 
 654   if (!chdir("$userspath")) {
 
 655     $self->{"error"} = "chdir : $!";
 
 660   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 661   my $psfile = $form->{"tmpfile"};
 
 662   $psfile =~ s/.html/.ps/;
 
 663   if ($psfile eq $form->{"tmpfile"}) {
 
 667   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 669     $self->{"error"} = $form->cleanup();
 
 674   $form->{"tmpfile"} = $psfile;
 
 683   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 685   # Convert the HTML file to PDF
 
 687   if (!chdir("$userspath")) {
 
 688     $self->{"error"} = "chdir : $!";
 
 693   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 694   my $pdffile = $form->{"tmpfile"};
 
 695   $pdffile =~ s/.html/.pdf/;
 
 696   if ($pdffile eq $form->{"tmpfile"}) {
 
 700   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 702     $self->{"error"} = $form->cleanup();
 
 707   $form->{"tmpfile"} = $pdffile;
 
 716 #### PlainTextTemplate
 
 719 package PlainTextTemplate;
 
 723 @ISA = qw(LaTeXTemplate);
 
 728   return $type->SUPER::new(@_);
 
 732   my ($self, $variable) = @_;
 
 747 #### OpenDocumentTemplate
 
 750 package OpenDocumentTemplate;
 
 758 # use File::Temp qw(:mktemp);
 
 761 @ISA = qw(SimpleTemplate);
 
 766   $self = $type->SUPER::new(@_);
 
 768   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 769     eval("use ${module};");
 
 771       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 772                              "loaded. Support for OpenDocument templates " .
 
 773                              "does not work without it. Please install your " .
 
 774                              "distribution's package or get the module from " .
 
 775                              "CPAN ( http://www.cpan.org ).");
 
 779   $self->{"rnd"}   = int(rand(1000000));
 
 780   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 782   $self->set_tag_style('<%', '%>');
 
 788   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 790   my ($form, $new_contents) = ($self->{"form"}, "");
 
 792   my $ary = $self->_get_loop_variable($var, 1, @indices);
 
 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