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"};
 
 103     ('order' => [quotemeta("\\"),
 
 105                  '&', quotemeta("\n"),
 
 106                  '"', '\$', '%', '_', '#', quotemeta('^'),
 
 107                  '{', '}',  '<', '>', '£', "\r", '±', '\xe1',
 
 111      quotemeta("\\") => '\\textbackslash ',
 
 126      '\xe1'          => '$\bullet$',
 
 127      quotemeta('^')  => '\^\\',
 
 128      quotemeta("\n") => '\newline ',
 
 133   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 135   # Allow some HTML markup to be converted into the output format's
 
 136   # corresponding markup code, e.g. bold or italic.
 
 137   my %markup_replace = ('b' => 'textbf',
 
 141   foreach my $key (keys(%markup_replace)) {
 
 142     my $new = $markup_replace{$key};
 
 143     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
 146   $variable =~ s/[\x00-\x1f]//g;
 
 151 sub substitute_vars {
 
 152   my ($self, $text, @indices) = @_;
 
 154   my $form = $self->{"form"};
 
 156   while ($text =~ /$self->{tag_start_qm}(.+?)$self->{tag_end_qm}/) {
 
 157     my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
 
 158     my ($var, @options) = split(/\s+/, $1);
 
 159     my $value = $form->{$var};
 
 161     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 162       last unless (ref($value) eq "ARRAY");
 
 163       $value = $value->[$indices[$i]];
 
 165     $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
 
 166     substr($text, $tag_pos, $tag_len) = $value;
 
 173   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 175   my ($form, $new_contents) = ($self->{"form"}, "");
 
 177   my $ary = $form->{$var};
 
 178   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 179     last unless (ref($ary) eq "ARRAY");
 
 180     $ary = $ary->[$indices[$i]];
 
 184   my $current_page = 1;
 
 185   my ($current_line, $corrent_row) = (0, 1);
 
 187   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 188     $form->{"__first__"} = $i == 0;
 
 189     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 190     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 191     $form->{"__counter__"} = $i + 1;
 
 193     if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
 
 194         $self->{"chars_per_line"}) {
 
 196         int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
 
 199       $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
 
 200       my $_description = $form->{"description"}->[$i];
 
 201       while ($_description =~ /\\newline/) {
 
 203         $_description =~ s/\\newline//;
 
 207       if ($current_page == 1) {
 
 208         $lpp = $self->{"lines_on_first_page"};
 
 210         $lpp = $self->{"lines_on_second_page"};
 
 213       # Yes we need a manual page break -- or the user has forced one
 
 214       if ((($current_line + $lines) > $lpp) ||
 
 215           ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
 
 216         my $pb = $self->{"pagebreak_block"};
 
 218         # replace the special variables <%sumcarriedforward%>
 
 221         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 222         $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
 
 223         $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
 
 225         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 226         return undef unless (defined($new_text));
 
 227         $new_contents .= $new_text;
 
 232       $current_line += $lines;
 
 234     if ($i < scalar(@{$form->{"linetotal"}})) {
 
 235       $sum += $form->parse_amount($self->{"myconfig"},
 
 236                                   $form->{"linetotal"}->[$i]);
 
 239     $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 241     my $new_text = $self->parse_block($text, (@indices, $i));
 
 242     return undef unless (defined($new_text));
 
 243     $new_contents .= $start_tag . $new_text . $end_tag;
 
 245   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 247   return $new_contents;
 
 251   my ($self, $text, $pos, $var, $not) = @_;
 
 253   my $tag_start_len = length $self->{tag_start};
 
 256   $pos = 0 unless ($pos);
 
 258   while ($pos < length($text)) {
 
 261     next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
 
 263     my $keyword_pos = $pos - 1 + $tag_start_len;
 
 265     if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
 
 268     } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
 
 271             "$self->{tag_start}else$self->{tag_end} outside of "
 
 272           . "$self->{tag_start}if$self->{tag_end} / "
 
 273           . "$self->{tag_start}ifnot$self->{tag_end}.";
 
 277       my $block = substr($text, 0, $pos - 1);
 
 278       substr($text, 0, $pos - 1) = "";
 
 279       $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 280       $text =  $self->{tag_start} . 'if' . ($not ?  " " : "not ") . $var . $self->{tag_end} . $text;
 
 282       return ($block, $text);
 
 284     } elsif (substr($text, $keyword_pos, 3) eq 'end') {
 
 287         my $block = substr($text, 0, $pos - 1);
 
 288         substr($text, 0, $pos - 1) = "";
 
 289         $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
 
 291         return ($block, $text);
 
 300   $main::lxdebug->enter_sub();
 
 302   my ($self, $contents, @indices) = @_;
 
 304   my $new_contents = "";
 
 306   while ($contents ne "") {
 
 307     my $pos_if      = index($contents, $self->{tag_start} . 'if');
 
 308     my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
 
 310     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 311       $new_contents .= $self->substitute_vars($contents, @indices);
 
 315     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 316       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 317       substr($contents, 0, $pos_foreach) = "";
 
 319       if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
 
 320         $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
 
 321         $main::lxdebug->leave_sub();
 
 327       substr($contents, 0, length($&)) = "";
 
 330       ($block, $contents) = $self->find_end($contents);
 
 332         $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
 
 333         $main::lxdebug->leave_sub();
 
 337       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 338       if (!defined($new_text)) {
 
 339         $main::lxdebug->leave_sub();
 
 342       $new_contents .= $new_text;
 
 345       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 346       substr($contents, 0, $pos_if) = "";
 
 348       if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
 
 349         $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
 
 350         $main::lxdebug->leave_sub();
 
 354       my ($not, $var) = ($1, $2);
 
 356       substr($contents, 0, length($&)) = "";
 
 358       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 360         $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
 
 361         $main::lxdebug->leave_sub();
 
 365       my $value = $self->{"form"}->{$var};
 
 366       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 367         last unless (ref($value) eq "ARRAY");
 
 368         $value = $value->[$indices[$i]];
 
 371       if (($not && !$value) || (!$not && $value)) {
 
 372         my $new_text = $self->parse_block($block, @indices);
 
 373         if (!defined($new_text)) {
 
 374           $main::lxdebug->leave_sub();
 
 377         $new_contents .= $new_text;
 
 382   $main::lxdebug->leave_sub();
 
 384   return $new_contents;
 
 387 sub parse_first_line {
 
 389   my $line = shift || "";
 
 391   if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
 
 393       $self->{error} = "The tag start and end markers must not be equal.";
 
 397     $self->set_tag_style($1, $2);
 
 403 sub _parse_config_option {
 
 410   my ($key, $value) = split m/\s*=\s*/, $line, 2;
 
 412   if ($key eq 'tag-style') {
 
 413     $self->set_tag_style(split(m/\s+/, $value, 2));
 
 417 sub _parse_config_lines {
 
 421   my ($comment_start, $comment_end) = ("", "");
 
 423   if (ref $self eq 'LaTeXTemplate') {
 
 424     $comment_start = '\s*%';
 
 425   } elsif (ref $self eq 'HTMLTemplate') {
 
 426     $comment_start = '\s*<!--';
 
 427     $comment_end   = '>\s*';
 
 429     $comment_start = '\s*\#';
 
 432   my $num_lines = scalar @{ $lines };
 
 435   while ($i < $num_lines) {
 
 436     my $line = $lines->[$i];
 
 438     if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
 
 443     $self->_parse_config_option($1);
 
 444     splice @{ $lines }, $i, 1;
 
 452   my $form = $self->{"form"};
 
 454   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 455     $self->{"error"} = "$!";
 
 461   $self->_parse_config_lines(\@lines);
 
 463   my $contents = join("", @lines);
 
 465   # detect pagebreak block and its parameters
 
 466   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) {
 
 467     $self->{"chars_per_line"} = $1;
 
 468     $self->{"lines_on_first_page"} = $2;
 
 469     $self->{"lines_on_second_page"} = $3;
 
 470     $self->{"pagebreak_block"} = $4;
 
 472     substr($contents, length($`), length($&)) = "";
 
 475   $self->{"forced_pagebreaks"} = [];
 
 477   my $new_contents = $self->parse_block($contents);
 
 478   if (!defined($new_contents)) {
 
 479     $main::lxdebug->leave_sub();
 
 483   print(OUT $new_contents);
 
 485   if ($form->{"format"} =~ /postscript/i) {
 
 486     return $self->convert_to_postscript();
 
 487   } elsif ($form->{"format"} =~ /pdf/i) {
 
 488     return $self->convert_to_pdf();
 
 494 sub convert_to_postscript {
 
 496   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 498   # Convert the tex file to postscript
 
 500   if (!chdir("$userspath")) {
 
 501     $self->{"error"} = "chdir : $!";
 
 506   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 508   for (my $run = 1; $run <= 2; $run++) {
 
 509     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 510            "> $form->{tmpfile}.err");
 
 512       $self->{"error"} = $form->cleanup();
 
 518   $form->{tmpfile} =~ s/tex$/dvi/;
 
 520   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 522     $self->{"error"} = "dvips : $!";
 
 526   $form->{tmpfile} =~ s/dvi$/ps/;
 
 535   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 537   # Convert the tex file to PDF
 
 539   if (!chdir("$userspath")) {
 
 540     $self->{"error"} = "chdir : $!";
 
 545   $form->{tmpfile} =~ s/\Q$userspath\E\///g;
 
 547   for (my $run = 1; $run <= 2; $run++) {
 
 548     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 549            "> $form->{tmpfile}.err");
 
 551       $self->{"error"} = $form->cleanup();
 
 557   $form->{tmpfile} =~ s/tex$/pdf/;
 
 562 sub get_mime_type() {
 
 565   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 566     return "application/postscript";
 
 568     return "application/pdf";
 
 581 package HTMLTemplate;
 
 585 @ISA = qw(LaTeXTemplate);
 
 590   return $type->SUPER::new(@_);
 
 594   my ($self, $variable) = @_;
 
 595   my $form = $self->{"form"};
 
 598     ('order' => ['<', '>', quotemeta("\n")],
 
 601      quotemeta("\n") => '<br>',
 
 604   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 606   # Allow some HTML markup to be converted into the output format's
 
 607   # corresponding markup code, e.g. bold or italic.
 
 608   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 610   foreach my $key (@markup_replace) {
 
 611     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 617 sub get_mime_type() {
 
 620   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 621     return "application/postscript";
 
 622   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 623     return "application/pdf";
 
 632   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 634   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 641 sub convert_to_postscript {
 
 643   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 645   # Convert the HTML file to postscript
 
 647   if (!chdir("$userspath")) {
 
 648     $self->{"error"} = "chdir : $!";
 
 653   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 654   my $psfile = $form->{"tmpfile"};
 
 655   $psfile =~ s/.html/.ps/;
 
 656   if ($psfile eq $form->{"tmpfile"}) {
 
 660   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 662     $self->{"error"} = $form->cleanup();
 
 667   $form->{"tmpfile"} = $psfile;
 
 676   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 678   # Convert the HTML file to PDF
 
 680   if (!chdir("$userspath")) {
 
 681     $self->{"error"} = "chdir : $!";
 
 686   $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
 
 687   my $pdffile = $form->{"tmpfile"};
 
 688   $pdffile =~ s/.html/.pdf/;
 
 689   if ($pdffile eq $form->{"tmpfile"}) {
 
 693   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 695     $self->{"error"} = $form->cleanup();
 
 700   $form->{"tmpfile"} = $pdffile;
 
 709 #### PlainTextTemplate
 
 712 package PlainTextTemplate;
 
 716 @ISA = qw(LaTeXTemplate);
 
 721   return $type->SUPER::new(@_);
 
 725   my ($self, $variable) = @_;
 
 740 #### OpenDocumentTemplate
 
 743 package OpenDocumentTemplate;
 
 751 # use File::Temp qw(:mktemp);
 
 754 @ISA = qw(SimpleTemplate);
 
 759   $self = $type->SUPER::new(@_);
 
 761   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 762     eval("use ${module};");
 
 764       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 765                              "loaded. Support for OpenDocument templates " .
 
 766                              "does not work without it. Please install your " .
 
 767                              "distribution's package or get the module from " .
 
 768                              "CPAN ( http://www.cpan.org ).");
 
 772   $self->{"rnd"} = int(rand(1000000));
 
 773   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 778 sub substitute_vars {
 
 779   my ($self, $text, @indices) = @_;
 
 781   my $form = $self->{"form"};
 
 783   while ($text =~ /\<\%(.*?)\%\>/) {
 
 784     my $value = $form->{$1};
 
 786     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 787       last unless (ref($value) eq "ARRAY");
 
 788       $value = $value->[$indices[$i]];
 
 790     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 797   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 799   my ($form, $new_contents) = ($self->{"form"}, "");
 
 801   my $ary = $form->{$var};
 
 802   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 803     last unless (ref($ary) eq "ARRAY");
 
 804     $ary = $ary->[$indices[$i]];
 
 807   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 808     $form->{"__first__"} = $i == 0;
 
 809     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 810     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 811     $form->{"__counter__"} = $i + 1;
 
 812     my $new_text = $self->parse_block($text, (@indices, $i));
 
 813     return undef unless (defined($new_text));
 
 814     $new_contents .= $start_tag . $new_text . $end_tag;
 
 816   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 818   return $new_contents;
 
 822   my ($self, $text, $pos, $var, $not) = @_;
 
 825   $pos = 0 unless ($pos);
 
 827   while ($pos < length($text)) {
 
 830     next if (substr($text, $pos - 1, 5) ne '<%');
 
 832     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 835     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 837         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 841       my $block = substr($text, 0, $pos - 1);
 
 842       substr($text, 0, $pos - 1) = "";
 
 843       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 844       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 846       return ($block, $text);
 
 848     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 851         my $block = substr($text, 0, $pos - 1);
 
 852         substr($text, 0, $pos - 1) = "";
 
 853         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 855         return ($block, $text);
 
 864   $main::lxdebug->enter_sub();
 
 866   my ($self, $contents, @indices) = @_;
 
 868   my $new_contents = "";
 
 870   while ($contents ne "") {
 
 871     if (substr($contents, 0, 1) eq "<") {
 
 872       $contents =~ m|^<[^>]+>|;
 
 874       substr($contents, 0, length($&)) = "";
 
 876       if ($tag =~ m|<table:table-row|) {
 
 877         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 880         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 882         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 885           substr($table_row, length($`), length($&)) = "";
 
 887           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 889             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 890             $main::lxdebug->leave_sub();
 
 894           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 895           if (!defined($new_text)) {
 
 896             $main::lxdebug->leave_sub();
 
 899           $new_contents .= $new_text;
 
 902           my $new_text = $self->parse_block($table_row, @indices);
 
 903           if (!defined($new_text)) {
 
 904             $main::lxdebug->leave_sub();
 
 907           $new_contents .= $tag . $new_text . $end_tag;
 
 911         $new_contents .= $tag;
 
 915       $contents =~ /^[^<]+/;
 
 918       my $pos_if = index($text, '<%if');
 
 919       my $pos_foreach = index($text, '<%foreach');
 
 921       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 922         substr($contents, 0, length($text)) = "";
 
 923         $new_contents .= $self->substitute_vars($text, @indices);
 
 927       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 928         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 929         substr($contents, 0, $pos_foreach) = "";
 
 931         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 932           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 933           $main::lxdebug->leave_sub();
 
 939         substr($contents, 0, length($&)) = "";
 
 942         ($block, $contents) = $self->find_end($contents);
 
 944           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 945           $main::lxdebug->leave_sub();
 
 949         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 950         if (!defined($new_text)) {
 
 951           $main::lxdebug->leave_sub();
 
 954         $new_contents .= $new_text;
 
 957         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 958         substr($contents, 0, $pos_if) = "";
 
 960         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 961           $self->{"error"} = "Malformed <\%if\%>.";
 
 962           $main::lxdebug->leave_sub();
 
 966         my ($not, $var) = ($1, $2);
 
 968         substr($contents, 0, length($&)) = "";
 
 970         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 972           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 973           $main::lxdebug->leave_sub();
 
 977         my $value = $self->{"form"}->{$var};
 
 978         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 979           last unless (ref($value) eq "ARRAY");
 
 980           $value = $value->[$indices[$i]];
 
 983         if (($not && !$value) || (!$not && $value)) {
 
 984           my $new_text = $self->parse_block($block, @indices);
 
 985           if (!defined($new_text)) {
 
 986             $main::lxdebug->leave_sub();
 
 989           $new_contents .= $new_text;
 
 995   $main::lxdebug->leave_sub();
 
 997   return $new_contents;
 
1001   $main::lxdebug->enter_sub();
 
1005   my $form = $self->{"form"};
 
1010   if ($form->{"IN"} =~ m|^/|) {
 
1011     $file_name = $form->{"IN"};
 
1013     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
1016   my $zip = Archive::Zip->new();
 
1017   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
1018     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
1019     $main::lxdebug->leave_sub();
 
1023   my $contents = $zip->contents("content.xml");
 
1025     $self->{"error"} = "File is not a OpenDocument file.";
 
1026     $main::lxdebug->leave_sub();
 
1030   my $rnd = $self->{"rnd"};
 
1031   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
1032 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
1034 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
1035 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
1037 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
1038 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
1040 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
1041 <style:text-properties style:text-line-through-style="solid"/>
 
1043 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
1044 <style:text-properties style:text-position="super 58%"/>
 
1046 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
1047 <style:text-properties style:text-position="sub 58%"/>
 
1051   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
1052   $contents =~ s|[\n\r]||gm;
 
1054   my $new_contents = $self->parse_block($contents);
 
1055   if (!defined($new_contents)) {
 
1056     $main::lxdebug->leave_sub();
 
1060 #   $new_contents =~ s|>|>\n|g;
 
1062   $zip->contents("content.xml", $new_contents);
 
1064   my $styles = $zip->contents("styles.xml");
 
1066     my $new_styles = $self->parse_block($styles);
 
1067     if (!defined($new_contents)) {
 
1068       $main::lxdebug->leave_sub();
 
1071     $zip->contents("styles.xml", $new_styles);
 
1074   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
1077   if ($form->{"format"} =~ /pdf/) {
 
1078     $res = $self->convert_to_pdf();
 
1081   $main::lxdebug->leave_sub();
 
1085 sub is_xvfb_running {
 
1086   $main::lxdebug->enter_sub();
 
1091   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1094   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
1095   if ((-f $dfname) && open(IN, $dfname)) {
 
1100     my $xauthority = <IN>;
 
1104     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
1106     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
1107       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
1108       unlink($dfname, $xauthority);
 
1109       $main::lxdebug->leave_sub();
 
1114     if ($line !~ /xvfb/i) {
 
1115       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
1116       unlink($dfname, $xauthority);
 
1117       $main::lxdebug->leave_sub();
 
1121     $ENV{"XAUTHORITY"} = $xauthority;
 
1122     $ENV{"DISPLAY"} = $display;
 
1124     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1127   $main::lxdebug->leave_sub();
 
1133   $main::lxdebug->enter_sub();
 
1137   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1139   my $display = $self->is_xvfb_running();
 
1142     $main::lxdebug->leave_sub();
 
1147   while ( -f "/tmp/.X${display}-lock") {
 
1150   $display = ":${display}";
 
1151   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1153   my $mcookie = `mcookie`;
 
1154   die("Installation error: mcookie not found.") if ($? != 0);
 
1157   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1159   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1160   $ENV{"XAUTHORITY"} = $xauthority;
 
1162   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1164   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1166     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1167     $main::lxdebug->leave_sub();
 
1171   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1175     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1176     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1179   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1182   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1183   if (!open(OUT, ">$dfname")) {
 
1184     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1185     unlink($xauthority);
 
1187     $main::lxdebug->leave_sub();
 
1190   print(OUT "$pid\n$display\n$xauthority\n");
 
1193   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1195   if (!$self->is_xvfb_running()) {
 
1196     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1197     unlink($xauthority, $dfname);
 
1199     $main::lxdebug->leave_sub();
 
1203   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1205   $main::lxdebug->leave_sub();
 
1210 sub is_openoffice_running {
 
1211   $main::lxdebug->enter_sub();
 
1213   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1214          "> /dev/null 2> /dev/null");
 
1216   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1218   $main::lxdebug->leave_sub();
 
1223 sub spawn_openoffice {
 
1224   $main::lxdebug->enter_sub();
 
1228   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1230   my ($try, $spawned_oo, $res);
 
1233   for ($try = 0; $try < 15; $try++) {
 
1234     if ($self->is_openoffice_running()) {
 
1242         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1244         open(STDIN, '/dev/null');
 
1245         open(STDOUT, '>/dev/null');
 
1246         my $new_pid = fork();
 
1248         my $ssres = setsid();
 
1249         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1250         my @cmdline = ($main::openofficeorg_writer_bin,
 
1251                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1253                        "-accept=socket,host=localhost,port=" .
 
1254                        $main::openofficeorg_daemon_port . ";urp;");
 
1258       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1263     sleep($try >= 5 ? 2 : 1);
 
1267     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1268       "OpenOffice could not be started.";
 
1271   $main::lxdebug->leave_sub();
 
1276 sub convert_to_pdf {
 
1277   $main::lxdebug->enter_sub();
 
1281   my $form = $self->{"form"};
 
1283   my $filename = $form->{"tmpfile"};
 
1284   $filename =~ s/.odt$//;
 
1285   if (substr($filename, 0, 1) ne "/") {
 
1286     $filename = getcwd() . "/${filename}";
 
1289   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1290     $ENV{'HOME'} = $self->{"userspath"};
 
1292     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1295   if (!$self->spawn_xvfb()) {
 
1296     $main::lxdebug->leave_sub();
 
1301   if (!$main::openofficeorg_daemon) {
 
1302     @cmdline = ($main::openofficeorg_writer_bin,
 
1303                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1305                 "file:${filename}.odt",
 
1306                 "macro://" . (split('/', $filename))[-1] .
 
1307                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1309     if (!$self->spawn_openoffice()) {
 
1310       $main::lxdebug->leave_sub();
 
1314     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1315                 $main::openofficeorg_daemon_port,
 
1323     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1325     unlink($filename . ".odt");
 
1327     $main::lxdebug->leave_sub();
 
1332   unlink($filename . ".odt", $filename . ".pdf");
 
1333   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1336   $main::lxdebug->leave_sub();
 
1341   my ($self, $variable) = @_;
 
1342   my $form = $self->{"form"};
 
1343   my $iconv = $self->{"iconv"};
 
1346     ('order' => ['&', '<', '>', '"', "'",
 
1348                  quotemeta("\n"), quotemeta("\r")],
 
1354      '\x80'          => chr(0xa4), # Euro
 
1355      quotemeta("\n") => '<text:line-break/>',
 
1356      quotemeta("\r") => '',
 
1359   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1361   # Allow some HTML markup to be converted into the output format's
 
1362   # corresponding markup code, e.g. bold or italic.
 
1363   my $rnd = $self->{"rnd"};
 
1364   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1365                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1367   foreach my $key (keys(%markup_replace)) {
 
1368     my $value = $markup_replace{$key};
 
1369     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
 
1370     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1373   return $iconv->convert($variable);
 
1376 sub get_mime_type() {
 
1377   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1378     return "application/pdf";
 
1380     return "application/vnd.oasis.opendocument.text";
 
1384 sub uses_temp_file {
 
1389 ##########################################################
 
1393 ##########################################################
 
1395 package XMLTemplate; 
 
1399 @ISA = qw(HTMLTemplate);
 
1402   #evtl auskommentieren
 
1405   return $type->SUPER::new(@_);
 
1409   my ($self, $variable) = @_;
 
1410   my $form = $self->{"form"};
 
1413     ('order' => ['<', '>', quotemeta("\n")],
 
1416      quotemeta("\n") => '<br>',
 
1419   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1421   # Allow no markup to be converted into the output format
 
1422   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
1424   foreach my $key (@markup_replace) {
 
1425     $variable =~ s/\<(\/?)${key}\>//g;
 
1431 sub get_mime_type() {
 
1434   if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
 
1435     return "application/xml ";
 
1436   } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
 
1437     return "application/x-taxbird";
 
1443 sub uses_temp_file {
 
1444   # tempfile needet for XML Output