1 #====================================================================
 
   4 # Based on SQL-Ledger Version 2.1.9
 
   5 # Web http://www.lx-office.org
 
   7 #====================================================================
 
  10 #    NOTE: strict checks are package global. don't check this file
 
  11 #            with perl -sc, it will only capture SimpleTemplate
 
  15 package SimpleTemplate;
 
  20 #   1. The template's file name
 
  21 #   2. A reference to the Form object
 
  22 #   3. A reference to the myconfig hash
 
  25 #   A new template object
 
  39   $self->{source}    = shift;
 
  40   $self->{form}      = shift;
 
  41   $self->{myconfig}  = shift;
 
  42   $self->{userspath} = shift;
 
  44   $self->{error}     = undef;
 
  45   $self->{quot_re}   = '"';
 
  47   $self->set_tag_style('<%', '%>');
 
  52   my $tag_start               = shift;
 
  55   $self->{tag_start}          = $tag_start;
 
  56   $self->{tag_end}            = $tag_end;
 
  57   $self->{tag_start_qm}       = quotemeta $tag_start;
 
  58   $self->{tag_end_qm}         = quotemeta $tag_end;
 
  60   $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
 
  68 #   1. A typeglob for the file handle. The output will be written
 
  69 #      to this file handle.
 
  72 #   1 on success and undef or 0 if there was an error. In the latter case
 
  73 #   the calling function can retrieve the error message via $obj->get_error()
 
  78   print(OUT "Hallo!\n");
 
  84   return $self->{"error"};
 
  91 sub _get_loop_variable {
 
  94   my $get_array = shift;
 
  97   my $form      = $self->{form};
 
 100   if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
 
 101     $value = $form->{TEMPLATE_ARRAYS}->{$var};
 
 103     $value = $form->{$var};
 
 106   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 107     last unless (ref($value) eq "ARRAY");
 
 108     $value = $value->[$indices[$i]];
 
 114 sub substitute_vars {
 
 115   my ($self, $text, @indices) = @_;
 
 117   my $form = $self->{"form"};
 
 119   while ($text =~ /$self->{substitute_vars_re}/) {
 
 120     my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
 
 121     my ($var, @options)     = split(/\s+/, $1);
 
 123     my $value               = $self->_get_loop_variable($var, 0, @indices);
 
 124     $value                  = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
 
 126     substr($text, $tag_pos, $tag_len, $value);
 
 132 sub _parse_block_if {
 
 133   $main::lxdebug->enter_sub();
 
 136   my $contents     = shift;
 
 137   my $new_contents = shift;
 
 141   $$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices);
 
 142   substr($$contents, 0, $pos_if) = "";
 
 144   if ($$contents !~ m/^$self->{tag_start_qm}if
 
 146                      (not\b|\!)?           # $1 -- Eventuelle Negierung
 
 148                      (\b.+?\b)             # $2 -- Name der zu überprüfenden Variablen
 
 149                      (                     # $3 -- Beginn des optionalen Vergleiches
 
 151                        ([!=])              # $4 -- Negierung des Vergleiches speichern
 
 152                        ([=~])              # $5 -- Art des Vergleiches speichern
 
 154                        (                   # $6 -- Gequoteter String oder Bareword
 
 156                          (.*?)(?<!\\)      # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
 
 159                          (\b.+?\b)         # $8 -- Bareword -- als Index für $form benutzen
 
 165     $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
 
 166     $main::lxdebug->leave_sub();
 
 172   my $operator_neg  = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
 
 173   my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
 
 174   my $quoted_word   = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
 
 175   my $bareword      = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
 
 177   $not = !$not if ($operator_neg && $operator_neg eq '!');
 
 179   substr($$contents, 0, length($&)) = "";
 
 182   ($block, $$contents) = $self->find_end($$contents, 0, $var, $not);
 
 184     $self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"});
 
 185     $main::lxdebug->leave_sub();
 
 189   my $value = $self->_get_loop_variable($var, 0, @indices);
 
 192   if ($operator_type) {
 
 193     my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
 
 194     if ($operator_type eq '=') {
 
 195       $hit         = ($not && !($value eq $compare_to))     || (!$not && ($value eq $compare_to));
 
 197       $hit         = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
 
 201     $hit           = ($not && ! $value)                     || (!$not &&  $value);
 
 205     my $new_text = $self->parse_block($block, @indices);
 
 206     if (!defined($new_text)) {
 
 207       $main::lxdebug->leave_sub();
 
 210     $$new_contents .= $new_text;
 
 213   $main::lxdebug->leave_sub();
 
 224 package LaTeXTemplate;
 
 228 @ISA = qw(SimpleTemplate);
 
 235   my $self = $type->SUPER::new(@_);
 
 241   my ($self, $variable) = @_;
 
 242   my $form = $self->{"form"};
 
 244   $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
 
 246   # Allow some HTML markup to be converted into the output format's
 
 247   # corresponding markup code, e.g. bold or italic.
 
 248   my %markup_replace = ('b' => 'textbf',
 
 252   foreach my $key (keys(%markup_replace)) {
 
 253     my $new = $markup_replace{$key};
 
 254     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
 257   $variable =~ s/[\x00-\x1f]//g;
 
 263   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 265   my ($form, $new_contents) = ($self->{"form"}, "");
 
 267   my $ary = $self->_get_loop_variable($var, 1, @indices);
 
 270   my $current_page                 = 1;
 
 271   my ($current_line, $corrent_row) = (0, 1);
 
 272   my $description_array            = $self->_get_loop_variable("description",     1);
 
 273   my $longdescription_array        = $self->_get_loop_variable("longdescription", 1);
 
 274   my $linetotal_array              = $self->_get_loop_variable("linetotal",       1);
 
 276   $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
 
 278   # forech block hasn't given us an array. ignore
 
 279   return $new_contents unless ref $ary eq 'ARRAY';
 
 281   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 283     $form->{"__first__"}   = $i == 1;
 
 284     $form->{"__last__"}    = ($i + 1) == scalar(@{$ary});
 
 285     $form->{"__odd__"}     = (($i + 1) % 2) == 1;
 
 286     $form->{"__counter__"} = $i + 1;
 
 288     if (   ref $description_array       eq 'ARRAY'
 
 289         && scalar @{$description_array} == scalar @{$ary}
 
 290         && $self->{"chars_per_line"}    != 0)
 
 292       my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
 
 295       $description_array->[$i] =~ s/(\\newline\s?)*$//;
 
 296       $lines++ while ($description_array->[$i] =~ m/\\newline/g);
 
 299       if ($current_page == 1) {
 
 300         $lpp = $self->{"lines_on_first_page"};
 
 302         $lpp = $self->{"lines_on_second_page"};
 
 305       # Yes we need a manual page break -- or the user has forced one
 
 306       if (   (($current_line + $lines) > $lpp)
 
 307           || ($description_array->[$i]     =~ /<pagebreak>/)
 
 308           || (   ref $longdescription_array eq 'ARRAY'
 
 309               && $longdescription_array->[$i] =~ /<pagebreak>/)) {
 
 310         my $pb = $self->{"pagebreak_block"};
 
 312         # replace the special variables <%sumcarriedforward%>
 
 315         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 316         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
 
 317         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
 
 319         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 320         return undef unless (defined($new_text));
 
 321         $new_contents .= $new_text;
 
 326       $current_line += $lines;
 
 329     if (   ref $linetotal_array eq 'ARRAY'
 
 330         && $i < scalar(@{$linetotal_array})) {
 
 331       $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
 
 334     $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 336     my $new_text = $self->parse_block($text, (@indices, $i));
 
 337     return undef unless (defined($new_text));
 
 338     $new_contents .= $start_tag . $new_text . $end_tag;
 
 340   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 342   return $new_contents;
 
 346   my ($self, $text, $pos, $var, $not) = @_;
 
 348   my $tag_start_len = length $self->{tag_start};
 
 351   $pos = 0 unless ($pos);
 
 353   while ($pos < length($text)) {
 
 356     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
 358     my $keyword_pos = $pos - 1 + $tag_start_len;
 
 360     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
 
 363     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 366             "$self->{tag_start}else$self->{tag_end} outside of "
 
 367           . "$self->{tag_start}if$self->{tag_end} / "
 
 368           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 372       my $block = substr($text, 0, $pos - 1);
 
 373       substr($text, 0, $pos - 1) = "";
 
 374       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 375       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 377       return ($block, $text);
 
 379     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 382         my $block = substr($text, 0, $pos - 1);
 
 383         substr($text, 0, $pos - 1) = "";
 
 384         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 386         return ($block, $text);
 
 395   $main::lxdebug->enter_sub();
 
 397   my ($self, $contents, @indices) = @_;
 
 399   my $new_contents = "";
 
 401   while ($contents ne "") {
 
 402     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 403     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 405     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 406       $new_contents .= $self->substitute_vars($contents, @indices);
 
 410     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 411       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 412       substr($contents, 0, $pos_foreach) = "";
 
 414       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 415         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 416         $main::lxdebug->leave_sub();
 
 422       substr($contents, 0, length($&)) = "";
 
 425       ($block, $contents) = $self->find_end($contents);
 
 427         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 428         $main::lxdebug->leave_sub();
 
 432       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 433       if (!defined($new_text)) {
 
 434         $main::lxdebug->leave_sub();
 
 437       $new_contents .= $new_text;
 
 440       if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
 
 441         $main::lxdebug->leave_sub();
 
 447   $main::lxdebug->leave_sub();
 
 449   return $new_contents;
 
 452 sub parse_first_line {
 
 454   my $line = shift || "";
 
 456   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 458       $self->{error} = "The tag start and end markers must not be equal.";
 
 462     $self->set_tag_style($1, $2);
 
 468 sub _parse_config_option {
 
 475   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 477   if ($key eq 'tag-style') {
 
 478     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 482 sub _parse_config_lines {
 
 486   my ($comment_start, $comment_end) = ("", "");
 
 488   if (ref $self eq 'LaTeXTemplate') {
 
 489     $comment_start = '\s*%';
 
 490   } elsif (ref $self eq 'HTMLTemplate') {
 
 491     $comment_start = '\s*<!--';
 
 492     $comment_end   = '>\s*';
 
 494     $comment_start = '\s*\#';
 
 497   my $num_lines = scalar @{ $lines };
 
 500   while ($i < $num_lines) {
 
 501     my $line = $lines->[$i];
 
 503     if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
 
 508     $self->_parse_config_option($1);
 
 509     splice @{ $lines }, $i, 1;
 
 514 sub _force_mandatory_packages {
 
 518   my (%used_packages, $document_start_line);
 
 520   foreach my $i (0 .. scalar @{ $lines } - 1) {
 
 521     if ($lines->[$i] =~ m/\\usepackage[^\{]*{(.*?)}/) {
 
 522       $used_packages{$1} = 1;
 
 524     } elsif ($lines->[$i] =~ m/\\begin{document}/) {
 
 525       $document_start_line = $i;
 
 531   $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
 
 533   if (!$used_packages{textcomp}) {
 
 534     splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
 
 535     $document_start_line++;
 
 542   my $form = $self->{"form"};
 
 544   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 545     $self->{"error"} = "$!";
 
 551   $self->_parse_config_lines(\@lines);
 
 552   $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
 
 554   my $contents = join("", @lines);
 
 556   # detect pagebreak block and its parameters
 
 557   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) {
 
 558     $self->{"chars_per_line"} = $1;
 
 559     $self->{"lines_on_first_page"} = $2;
 
 560     $self->{"lines_on_second_page"} = $3;
 
 561     $self->{"pagebreak_block"} = $4;
 
 563     substr($contents, length($`), length($&)) = "";
 
 566   $self->{"forced_pagebreaks"} = [];
 
 568   my $new_contents = $self->parse_block($contents);
 
 569   if (!defined($new_contents)) {
 
 570     $main::lxdebug->leave_sub();
 
 574   print(OUT $new_contents);
 
 576   if ($form->{"format"} =~ /postscript/i) {
 
 577     return $self->convert_to_postscript();
 
 578   } elsif ($form->{"format"} =~ /pdf/i) {
 
 579     return $self->convert_to_pdf();
 
 585 sub convert_to_postscript {
 
 587   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 589   # Convert the tex file to postscript
 
 591   if (!chdir("$userspath")) {
 
 592     $self->{"error"} = "chdir : $!";
 
 597   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 599   my $latex = $self->_get_latex_path();
 
 601   for (my $run = 1; $run <= 2; $run++) {
 
 602     system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
 
 603            "> $form->{tmpfile}.err");
 
 605       $self->{"error"} = $form->cleanup();
 
 611   $form->{tmpfile} =~ s/tex$/dvi/;
 
 613   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 615     $self->{"error"} = "dvips : $!";
 
 619   $form->{tmpfile} =~ s/dvi$/ps/;
 
 628   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 630   # Convert the tex file to PDF
 
 632   if (!chdir("$userspath")) {
 
 633     $self->{"error"} = "chdir : $!";
 
 638   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 640   my $latex = $self->_get_latex_path();
 
 642   for (my $run = 1; $run <= 2; $run++) {
 
 643     system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
 
 644            "> $form->{tmpfile}.err");
 
 646       $self->{"error"} = $form->cleanup();
 
 652   $form->{tmpfile} =~ s/tex$/pdf/;
 
 657 sub _get_latex_path {
 
 658   return $main::latex_bin || 'pdflatex';
 
 661 sub get_mime_type() {
 
 664   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 665     return "application/postscript";
 
 667     return "application/pdf";
 
 680 package HTMLTemplate;
 
 684 @ISA = qw(LaTeXTemplate);
 
 691   return $type->SUPER::new(@_);
 
 695   my ($self, $variable) = @_;
 
 696   my $form = $self->{"form"};
 
 698   $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
 
 700   # Allow some HTML markup to be converted into the output format's
 
 701   # corresponding markup code, e.g. bold or italic.
 
 702   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 704   foreach my $key (@markup_replace) {
 
 705     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 711 sub get_mime_type() {
 
 714   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 715     return "application/postscript";
 
 716   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 717     return "application/pdf";
 
 726   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 728   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 735 sub convert_to_postscript {
 
 737   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 739   # Convert the HTML file to postscript
 
 741   if (!chdir("$userspath")) {
 
 742     $self->{"error"} = "chdir : $!";
 
 747   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 748   my $psfile = $form->{"tmpfile"};
 
 749   $psfile =~ s/.html/.ps/;
 
 750   if ($psfile eq $form->{"tmpfile"}) {
 
 754   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 756     $self->{"error"} = $form->cleanup();
 
 761   $form->{"tmpfile"} = $psfile;
 
 770   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 772   # Convert the HTML file to PDF
 
 774   if (!chdir("$userspath")) {
 
 775     $self->{"error"} = "chdir : $!";
 
 780   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 781   my $pdffile = $form->{"tmpfile"};
 
 782   $pdffile =~ s/.html/.pdf/;
 
 783   if ($pdffile eq $form->{"tmpfile"}) {
 
 787   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 789     $self->{"error"} = $form->cleanup();
 
 794   $form->{"tmpfile"} = $pdffile;
 
 803 #### PlainTextTemplate
 
 806 package PlainTextTemplate;
 
 810 @ISA = qw(LaTeXTemplate);
 
 817   return $type->SUPER::new(@_);
 
 821   my ($self, $variable) = @_;
 
 836 #### OpenDocumentTemplate
 
 839 package OpenDocumentTemplate;
 
 850 # use File::Temp qw(:mktemp);
 
 853 @ISA = qw(SimpleTemplate);
 
 860   my $self = $type->SUPER::new(@_);
 
 862   $self->{"rnd"}   = int(rand(1000000));
 
 863   $self->{"iconv"} = SL::Iconv->new($main::dbcharset, "UTF-8");
 
 865   $self->set_tag_style('<%', '%>');
 
 866   $self->{quot_re} = '"';
 
 872   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 874   my ($form, $new_contents) = ($self->{"form"}, "");
 
 876   my $ary = $self->_get_loop_variable($var, 1, @indices);
 
 878   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 879     $form->{"__first__"} = $i == 0;
 
 880     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 881     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 882     $form->{"__counter__"} = $i + 1;
 
 883     my $new_text = $self->parse_block($text, (@indices, $i));
 
 884     return undef unless (defined($new_text));
 
 885     $new_contents .= $start_tag . $new_text . $end_tag;
 
 887   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 889   return $new_contents;
 
 893   my ($self, $text, $pos, $var, $not) = @_;
 
 896   $pos = 0 unless ($pos);
 
 898   while ($pos < length($text)) {
 
 901     next if (substr($text, $pos - 1, 5) ne '<%');
 
 903     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 906     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 908         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 912       my $block = substr($text, 0, $pos - 1);
 
 913       substr($text, 0, $pos - 1) = "";
 
 914       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 915       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 917       return ($block, $text);
 
 919     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 922         my $block = substr($text, 0, $pos - 1);
 
 923         substr($text, 0, $pos - 1) = "";
 
 924         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 926         return ($block, $text);
 
 935   $main::lxdebug->enter_sub();
 
 937   my ($self, $contents, @indices) = @_;
 
 939   my $new_contents = "";
 
 941   while ($contents ne "") {
 
 942     if (substr($contents, 0, 1) eq "<") {
 
 943       $contents =~ m|^<[^>]+>|;
 
 945       substr($contents, 0, length($&)) = "";
 
 947       if ($tag =~ m|<table:table-row|) {
 
 948         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 951         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 953         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 956           substr($table_row, length($`), length($&)) = "";
 
 958           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 960             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 961             $main::lxdebug->leave_sub();
 
 965           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 966           if (!defined($new_text)) {
 
 967             $main::lxdebug->leave_sub();
 
 970           $new_contents .= $new_text;
 
 973           my $new_text = $self->parse_block($table_row, @indices);
 
 974           if (!defined($new_text)) {
 
 975             $main::lxdebug->leave_sub();
 
 978           $new_contents .= $tag . $new_text . $end_tag;
 
 982         $new_contents .= $tag;
 
 986       $contents =~ /^[^<]+/;
 
 989       my $pos_if = index($text, '<%if');
 
 990       my $pos_foreach = index($text, '<%foreach');
 
 992       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 993         substr($contents, 0, length($text)) = "";
 
 994         $new_contents .= $self->substitute_vars($text, @indices);
 
 998       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 999         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
1000         substr($contents, 0, $pos_foreach) = "";
 
1002         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
1003           $self->{"error"} = "Malformed <\%foreach\%>.";
 
1004           $main::lxdebug->leave_sub();
 
1010         substr($contents, 0, length($&)) = "";
 
1013         ($block, $contents) = $self->find_end($contents);
 
1015           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
1016           $main::lxdebug->leave_sub();
 
1020         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
1021         if (!defined($new_text)) {
 
1022           $main::lxdebug->leave_sub();
 
1025         $new_contents .= $new_text;
 
1028         if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
 
1029           $main::lxdebug->leave_sub();
 
1036   $main::lxdebug->leave_sub();
 
1038   return $new_contents;
 
1042   $main::lxdebug->enter_sub();
 
1046   my $form = $self->{"form"};
 
1051   if ($form->{"IN"} =~ m|^/|) {
 
1052     $file_name = $form->{"IN"};
 
1054     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
1057   my $zip = Archive::Zip->new();
 
1058   if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
 
1059     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
1060     $main::lxdebug->leave_sub();
 
1064   my $contents = $zip->contents("content.xml");
 
1066     $self->{"error"} = "File is not a OpenDocument file.";
 
1067     $main::lxdebug->leave_sub();
 
1071   my $rnd = $self->{"rnd"};
 
1072   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
1073 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
1075 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
1076 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
1078 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
1079 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
1081 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
1082 <style:text-properties style:text-line-through-style="solid"/>
 
1084 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
1085 <style:text-properties style:text-position="super 58%"/>
 
1087 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
1088 <style:text-properties style:text-position="sub 58%"/>
 
1092   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
1093   $contents =~ s|[\n\r]||gm;
 
1095   my $new_contents = $self->parse_block($contents);
 
1096   if (!defined($new_contents)) {
 
1097     $main::lxdebug->leave_sub();
 
1101 #   $new_contents =~ s|>|>\n|g;
 
1103   $zip->contents("content.xml", $new_contents);
 
1105   my $styles = $zip->contents("styles.xml");
 
1107     my $new_styles = $self->parse_block($styles);
 
1108     if (!defined($new_contents)) {
 
1109       $main::lxdebug->leave_sub();
 
1112     $zip->contents("styles.xml", $new_styles);
 
1115   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
1118   if ($form->{"format"} =~ /pdf/) {
 
1119     $res = $self->convert_to_pdf();
 
1122   $main::lxdebug->leave_sub();
 
1126 sub is_xvfb_running {
 
1127   $main::lxdebug->enter_sub();
 
1132   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1135   $main::lxdebug->message(LXDebug->DEBUG2(), "    Looking for $dfname\n");
 
1136   if ((-f $dfname) && open(IN, $dfname)) {
 
1141     my $xauthority = <IN>;
 
1145     $main::lxdebug->message(LXDebug->DEBUG2(), "      found with $pid and $display\n");
 
1147     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
1148       $main::lxdebug->message(LXDebug->DEBUG2(), "  no/wrong process #1\n");
 
1149       unlink($dfname, $xauthority);
 
1150       $main::lxdebug->leave_sub();
 
1155     if ($line !~ /xvfb/i) {
 
1156       $main::lxdebug->message(LXDebug->DEBUG2(), "      no/wrong process #2\n");
 
1157       unlink($dfname, $xauthority);
 
1158       $main::lxdebug->leave_sub();
 
1162     $ENV{"XAUTHORITY"} = $xauthority;
 
1163     $ENV{"DISPLAY"} = $display;
 
1165     $main::lxdebug->message(LXDebug->DEBUG2(), "      not found\n");
 
1168   $main::lxdebug->leave_sub();
 
1174   $main::lxdebug->enter_sub();
 
1178   $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
 
1180   my $display = $self->is_xvfb_running();
 
1183     $main::lxdebug->leave_sub();
 
1188   while ( -f "/tmp/.X${display}-lock") {
 
1191   $display = ":${display}";
 
1192   $main::lxdebug->message(LXDebug->DEBUG2(), "  display $display\n");
 
1194   my $mcookie = `mcookie`;
 
1195   die("Installation error: mcookie not found.") if ($? != 0);
 
1198   $main::lxdebug->message(LXDebug->DEBUG2(), "  mcookie $mcookie\n");
 
1200   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1201   $ENV{"XAUTHORITY"} = $xauthority;
 
1203   $main::lxdebug->message(LXDebug->DEBUG2(), "  xauthority $xauthority\n");
 
1205   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1207     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1208     $main::lxdebug->leave_sub();
 
1212   $main::lxdebug->message(LXDebug->DEBUG2(), "  about to fork()\n");
 
1216     $main::lxdebug->message(LXDebug->DEBUG2(), "  Child execing\n");
 
1217     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1220   $main::lxdebug->message(LXDebug->DEBUG2(), "  parent dont sleeping\n");
 
1223   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1224   if (!open(OUT, ">$dfname")) {
 
1225     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1226     unlink($xauthority);
 
1228     $main::lxdebug->leave_sub();
 
1231   print(OUT "$pid\n$display\n$xauthority\n");
 
1234   $main::lxdebug->message(LXDebug->DEBUG2(), "  parent re-testing\n");
 
1236   if (!$self->is_xvfb_running()) {
 
1237     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1238     unlink($xauthority, $dfname);
 
1240     $main::lxdebug->leave_sub();
 
1244   $main::lxdebug->message(LXDebug->DEBUG2(), "  spawn OK\n");
 
1246   $main::lxdebug->leave_sub();
 
1251 sub is_openoffice_running {
 
1252   $main::lxdebug->enter_sub();
 
1254   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1255          "> /dev/null 2> /dev/null");
 
1257   $main::lxdebug->message(LXDebug->DEBUG2(), "  is_openoffice_running(): $?\n");
 
1259   $main::lxdebug->leave_sub();
 
1264 sub spawn_openoffice {
 
1265   $main::lxdebug->enter_sub();
 
1269   $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
 
1271   my ($try, $spawned_oo, $res);
 
1274   for ($try = 0; $try < 15; $try++) {
 
1275     if ($self->is_openoffice_running()) {
 
1283         $main::lxdebug->message(LXDebug->DEBUG2(), "  Child daemonizing\n");
 
1285         open(STDIN, '/dev/null');
 
1286         open(STDOUT, '>/dev/null');
 
1287         my $new_pid = fork();
 
1289         my $ssres = setsid();
 
1290         $main::lxdebug->message(LXDebug->DEBUG2(), "  Child execing\n");
 
1291         my @cmdline = ($main::openofficeorg_writer_bin,
 
1292                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1294                        "-accept=socket,host=localhost,port=" .
 
1295                        $main::openofficeorg_daemon_port . ";urp;");
 
1299       $main::lxdebug->message(LXDebug->DEBUG2(), "  Parent after fork\n");
 
1304     sleep($try >= 5 ? 2 : 1);
 
1308     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1309       "OpenOffice could not be started.";
 
1312   $main::lxdebug->leave_sub();
 
1317 sub convert_to_pdf {
 
1318   $main::lxdebug->enter_sub();
 
1322   my $form = $self->{"form"};
 
1324   my $filename = $form->{"tmpfile"};
 
1325   $filename =~ s/.odt$//;
 
1326   if (substr($filename, 0, 1) ne "/") {
 
1327     $filename = getcwd() . "/${filename}";
 
1330   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1331     $ENV{'HOME'} = $self->{"userspath"};
 
1333     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1336   if (!$self->spawn_xvfb()) {
 
1337     $main::lxdebug->leave_sub();
 
1342   if (!$main::openofficeorg_daemon) {
 
1343     @cmdline = ($main::openofficeorg_writer_bin,
 
1344                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1346                 "file:${filename}.odt",
 
1347                 "macro://" . (split('/', $filename))[-1] .
 
1348                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1350     if (!$self->spawn_openoffice()) {
 
1351       $main::lxdebug->leave_sub();
 
1355     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1356                 $main::openofficeorg_daemon_port,
 
1364     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1366     unlink($filename . ".odt");
 
1368     $main::lxdebug->leave_sub();
 
1373   unlink($filename . ".odt", $filename . ".pdf");
 
1374   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1377   $main::lxdebug->leave_sub();
 
1382   my ($self, $variable) = @_;
 
1383   my $form = $self->{"form"};
 
1384   my $iconv = $self->{"iconv"};
 
1386   $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
 
1388   # Allow some HTML markup to be converted into the output format's
 
1389   # corresponding markup code, e.g. bold or italic.
 
1390   my $rnd = $self->{"rnd"};
 
1391   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1392                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1394   foreach my $key (keys(%markup_replace)) {
 
1395     my $value = $markup_replace{$key};
 
1396     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
 
1397     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1400   return $iconv->convert($variable);
 
1403 sub get_mime_type() {
 
1406   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1407     return "application/pdf";
 
1409     return "application/vnd.oasis.opendocument.text";
 
1413 sub uses_temp_file {
 
1418 ##########################################################
 
1422 ##########################################################
 
1424 package XMLTemplate;
 
1428 @ISA = qw(HTMLTemplate);
 
1433   #evtl auskommentieren
 
1436   return $type->SUPER::new(@_);
 
1440   my ($self, $variable) = @_;
 
1441   my $form = $self->{"form"};
 
1443   $variable = $main::locale->quote_special_chars('Template/XML', $variable);
 
1445   # Allow no markup to be converted into the output format
 
1446   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
1448   foreach my $key (@markup_replace) {
 
1449     $variable =~ s/\<(\/?)${key}\>//g;
 
1455 sub get_mime_type() {
 
1458   if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
 
1459     return "application/xml ";
 
1460   } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
 
1461     return "application/x-taxbird";
 
1467 sub uses_temp_file {
 
1468   # tempfile needet for XML Output
 
1473 ##########################################################
 
1477 ##########################################################
 
1479 package ExcelTemplate;
 
1483 @ISA = qw(SimpleTemplate);
 
1488   my $self = $type->SUPER::new(@_);
 
1495   $self->{source}    = shift;
 
1496   $self->{form}      = shift;
 
1497   $self->{myconfig}  = shift;
 
1498   $self->{userspath} = shift;
 
1500   $self->{error}     = undef;
 
1502   $self->set_tag_style('<<', '>>');
 
1505 sub get_mime_type() {
 
1508   return "application/msexcel";
 
1511 sub uses_temp_file {
 
1516   $main::lxdebug->enter_sub();
 
1520   my $form   = $self->{"form"};
 
1522   open(IN, "$form->{templates}/$form->{IN}") or do { $self->{"error"} = "$!"; return 0; };
 
1526   my $contents = join("", @lines);
 
1529     $self->{tag_start} [<]* (\s?) [<>\s]* ([\w\s]+) [<>\s]* $self->{tag_end}
 
1531     $self->format_vars(align_right => $1 ne '', varstring => $2, length => length($&), indices =>  \@indices)
 
1534   if (!defined($contents)) {
 
1535     $main::lxdebug->leave_sub();
 
1539   print OUT $contents;
 
1541   $main::lxdebug->leave_sub();
 
1546   my ($self, %params) = @_;
 
1547   my $form            = $self->{"form"};
 
1548   my @indices         = @{ $params{indices} };
 
1549   my $align_right     = $params{align_right};
 
1550   my $varstring       = $params{varstring};
 
1551   my $length          = $params{length};
 
1553   $varstring =~ s/(\w+)/ $self->_get_loop_variable($1, 0, @indices) /eg;
 
1554   my $old_string=$varstring;
 
1555   my $new_string = sprintf "%*s", ($align_right ? 1 : -1 ) * $length, $varstring;
 
1556   if (!defined($new_string) || $new_string eq ''){
 
1557     $main::lxdebug->message(0, 'varstring' . $varstring . "old" . $old_string); 
 
1558     #  return substr $varstring, ($align_right ? (0, $length) : -$length);
 
1560   return substr $new_string, ($align_right ? (0, $length) : -$length);