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;
 
  44 #   1. A typeglob for the file handle. The output will be written
 
  45 #      to this file handle.
 
  48 #   1 on success and undef or 0 if there was an error. In the latter case
 
  49 #   the calling function can retrieve the error message via $obj->get_error()
 
  54   print(OUT "Hallo!\n");
 
  60   return $self->{"error"};
 
  73 package LaTeXTemplate;
 
  77 @ISA = qw(SimpleTemplate);
 
  82   return $type->SUPER::new(@_);
 
  86   my ($self, $variable) = @_;
 
  87   my $form = $self->{"form"};
 
  90     ('order' => [quotemeta("\\"),
 
  93                  '"', '\$', '%', '_', '#', quotemeta('^'),
 
  94                  '{', '}',  '<', '>', '£', "\r", '±', '\xe1',
 
  96      quotemeta("\\") => '\\textbackslash ',
 
 111      '\xe1'          => '$\bullet$',
 
 112      quotemeta('^')  => '\^\\',
 
 113      quotemeta("\n") => '\newline '
 
 116   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 118   # Allow some HTML markup to be converted into the output format's
 
 119   # corresponding markup code, e.g. bold or italic.
 
 120   my %markup_replace = ('b' => 'textbf',
 
 124   foreach my $key (keys(%markup_replace)) {
 
 125     my $new = $markup_replace{$key};
 
 126     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
 129   $variable =~ s/[\x00-\x1f]//g;
 
 134 sub substitute_vars {
 
 135   my ($self, $text, @indices) = @_;
 
 137   my $form = $self->{"form"};
 
 139   while ($text =~ /<\%(.*?)\%>/) {
 
 140     my ($var, @options) = split(/\s+/, $1);
 
 141     my $value = $form->{$var};
 
 143     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 144       last unless (ref($value) eq "ARRAY");
 
 145       $value = $value->[$indices[$i]];
 
 147     $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
 
 148     substr($text, $-[0], $+[0] - $-[0]) = $value;
 
 155   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 157   my ($form, $new_contents) = ($self->{"form"}, "");
 
 159   my $ary = $form->{$var};
 
 160   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 161     last unless (ref($ary) eq "ARRAY");
 
 162     $ary = $ary->[$indices[$i]];
 
 166   my $current_page = 1;
 
 167   my ($current_line, $corrent_row) = (0, 1);
 
 169   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 170     $form->{"__first__"} = $i == 0;
 
 171     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 172     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 173     $form->{"__counter__"} = $i + 1;
 
 175     if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
 
 176         $self->{"chars_per_line"}) {
 
 178         int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
 
 181       $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
 
 182       my $_description = $form->{"description"}->[$i];
 
 183       while ($_description =~ /\\newline/) {
 
 185         $_description =~ s/\\newline//;
 
 189       if ($current_page == 1) {
 
 190         $lpp = $self->{"lines_on_first_page"};
 
 192         $lpp = $self->{"lines_on_second_page"};
 
 195       # Yes we need a manual page break -- or the user has forced one
 
 196       if ((($current_line + $lines) > $lpp) ||
 
 197           ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
 
 198         my $pb = $self->{"pagebreak_block"};
 
 200         # replace the special variables <%sumcarriedforward%>
 
 203         my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 204         $pb =~ s/<%sumcarriedforward%>/$psum/g;
 
 205         $pb =~ s/<%lastpage%>/$current_page/g;
 
 207         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 208         return undef unless (defined($new_text));
 
 209         $new_contents .= $new_text;
 
 214       $current_line += $lines;
 
 216     if ($i < scalar(@{$form->{"linetotal"}})) {
 
 217       $sum += $form->parse_amount($self->{"myconfig"},
 
 218                                   $form->{"linetotal"}->[$i]);
 
 221     $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 223     my $new_text = $self->parse_block($text, (@indices, $i));
 
 224     return undef unless (defined($new_text));
 
 225     $new_contents .= $start_tag . $new_text . $end_tag;
 
 227   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 229   return $new_contents;
 
 233   my ($self, $text, $pos, $var, $not) = @_;
 
 236   $pos = 0 unless ($pos);
 
 238   while ($pos < length($text)) {
 
 241     next if (substr($text, $pos - 1, 2) ne '<%');
 
 243     if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
 
 246     } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
 
 248         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 252       my $block = substr($text, 0, $pos - 1);
 
 253       substr($text, 0, $pos - 1) = "";
 
 254       $text =~ s!^<\%[^\%]+\%>!!;
 
 255       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 257       return ($block, $text);
 
 259     } elsif (substr($text, $pos + 1, 3) eq 'end') {
 
 262         my $block = substr($text, 0, $pos - 1);
 
 263         substr($text, 0, $pos - 1) = "";
 
 264         $text =~ s!^<\%[^\%]+\%>!!;
 
 266         return ($block, $text);
 
 275   $main::lxdebug->enter_sub();
 
 277   my ($self, $contents, @indices) = @_;
 
 279   my $new_contents = "";
 
 281   while ($contents ne "") {
 
 282     my $pos_if = index($contents, '<%if');
 
 283     my $pos_foreach = index($contents, '<%foreach');
 
 285     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 286       $new_contents .= $self->substitute_vars($contents, @indices);
 
 290     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 291       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 292       substr($contents, 0, $pos_foreach) = "";
 
 294       if ($contents !~ m|^<\%foreach (.*?)\%>|) {
 
 295         $self->{"error"} = "Malformed <\%foreach\%>.";
 
 296         $main::lxdebug->leave_sub();
 
 302       substr($contents, 0, length($&)) = "";
 
 305       ($block, $contents) = $self->find_end($contents);
 
 307         $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 308         $main::lxdebug->leave_sub();
 
 312       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 313       if (!defined($new_text)) {
 
 314         $main::lxdebug->leave_sub();
 
 317       $new_contents .= $new_text;
 
 320       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 321       substr($contents, 0, $pos_if) = "";
 
 323       if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
 
 324         $self->{"error"} = "Malformed <\%if\%>.";
 
 325         $main::lxdebug->leave_sub();
 
 329       my ($not, $var) = ($1, $2);
 
 331       substr($contents, 0, length($&)) = "";
 
 333       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 335         $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 336         $main::lxdebug->leave_sub();
 
 340       my $value = $self->{"form"}->{$var};
 
 341       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 342         last unless (ref($value) eq "ARRAY");
 
 343         $value = $value->[$indices[$i]];
 
 346       if (($not && !$value) || (!$not && $value)) {
 
 347         my $new_text = $self->parse_block($block, @indices);
 
 348         if (!defined($new_text)) {
 
 349           $main::lxdebug->leave_sub();
 
 352         $new_contents .= $new_text;
 
 357   $main::lxdebug->leave_sub();
 
 359   return $new_contents;
 
 365   my $form = $self->{"form"};
 
 367   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 368     $self->{"error"} = "$!";
 
 374   my $contents = join("", @_);
 
 376   # detect pagebreak block and its parameters
 
 377   if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
 
 378     $self->{"chars_per_line"} = $1;
 
 379     $self->{"lines_on_first_page"} = $2;
 
 380     $self->{"lines_on_second_page"} = $3;
 
 381     $self->{"pagebreak_block"} = $4;
 
 383     substr($contents, length($`), length($&)) = "";
 
 386   $self->{"forced_pagebreaks"} = [];
 
 388   my $new_contents = $self->parse_block($contents);
 
 389   if (!defined($new_contents)) {
 
 390     $main::lxdebug->leave_sub();
 
 394   print(OUT $new_contents);
 
 396   if ($form->{"format"} =~ /postscript/i) {
 
 397     return $self->convert_to_postscript();
 
 398   } elsif ($form->{"format"} =~ /pdf/i) {
 
 399     return $self->convert_to_pdf();
 
 405 sub convert_to_postscript {
 
 407   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 409   # Convert the tex file to postscript
 
 411   if (!chdir("$userspath")) {
 
 412     $self->{"error"} = "chdir : $!";
 
 417   $form->{tmpfile} =~ s/$userspath\///g;
 
 419   for (my $run = 1; $run <= 2; $run++) {
 
 420     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 421            "> $form->{tmpfile}.err");
 
 423       $self->{"error"} = $form->cleanup();
 
 429   $form->{tmpfile} =~ s/tex$/dvi/;
 
 431   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 433     $self->{"error"} = "dvips : $!";
 
 437   $form->{tmpfile} =~ s/dvi$/ps/;
 
 446   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 448   # Convert the tex file to PDF
 
 450   if (!chdir("$userspath")) {
 
 451     $self->{"error"} = "chdir : $!";
 
 456   $form->{tmpfile} =~ s/$userspath\///g;
 
 458   for (my $run = 1; $run <= 2; $run++) {
 
 459     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 460            "> $form->{tmpfile}.err");
 
 462       $self->{"error"} = $form->cleanup();
 
 468   $form->{tmpfile} =~ s/tex$/pdf/;
 
 473 sub get_mime_type() {
 
 476   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 477     return "application/postscript";
 
 479     return "application/pdf";
 
 492 package HTMLTemplate;
 
 496 @ISA = qw(LaTeXTemplate);
 
 501   return $type->SUPER::new(@_);
 
 505   my ($self, $variable) = @_;
 
 506   my $form = $self->{"form"};
 
 509     ('order' => ['<', '>', quotemeta("\n")],
 
 512      quotemeta("\n") => '<br>',
 
 515   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 517   # Allow some HTML markup to be converted into the output format's
 
 518   # corresponding markup code, e.g. bold or italic.
 
 519   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 521   foreach my $key (@markup_replace) {
 
 522     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 528 sub get_mime_type() {
 
 531   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 532     return "application/postscript";
 
 533   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 534     return "application/pdf";
 
 543   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 545   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 552 sub convert_to_postscript {
 
 554   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 556   # Convert the HTML file to postscript
 
 558   if (!chdir("$userspath")) {
 
 559     $self->{"error"} = "chdir : $!";
 
 564   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 565   my $psfile = $form->{"tmpfile"};
 
 566   $psfile =~ s/.html/.ps/;
 
 567   if ($psfile eq $form->{"tmpfile"}) {
 
 571   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 573     $self->{"error"} = $form->cleanup();
 
 578   $form->{"tmpfile"} = $psfile;
 
 587   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 589   # Convert the HTML file to PDF
 
 591   if (!chdir("$userspath")) {
 
 592     $self->{"error"} = "chdir : $!";
 
 597   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 598   my $pdffile = $form->{"tmpfile"};
 
 599   $pdffile =~ s/.html/.pdf/;
 
 600   if ($pdffile eq $form->{"tmpfile"}) {
 
 604   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 606     $self->{"error"} = $form->cleanup();
 
 611   $form->{"tmpfile"} = $pdffile;
 
 620 #### PlainTextTemplate
 
 623 package PlainTextTemplate;
 
 627 @ISA = qw(LaTeXTemplate);
 
 632   return $type->SUPER::new(@_);
 
 636   my ($self, $variable) = @_;
 
 651 #### OpenDocumentTemplate
 
 654 package OpenDocumentTemplate;
 
 662 # use File::Temp qw(:mktemp);
 
 665 @ISA = qw(SimpleTemplate);
 
 670   $self = $type->SUPER::new(@_);
 
 672   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 673     eval("use ${module};");
 
 675       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 676                              "loaded. Support for OpenDocument templates " .
 
 677                              "does not work without it. Please install your " .
 
 678                              "distribution's package or get the module from " .
 
 679                              "CPAN ( http://www.cpan.org ).");
 
 683   $self->{"rnd"} = int(rand(1000000));
 
 684   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 689 sub substitute_vars {
 
 690   my ($self, $text, @indices) = @_;
 
 692   my $form = $self->{"form"};
 
 694   while ($text =~ /\<\%(.*?)\%\>/) {
 
 695     my $value = $form->{$1};
 
 697     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 698       last unless (ref($value) eq "ARRAY");
 
 699       $value = $value->[$indices[$i]];
 
 701     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 708   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 710   my ($form, $new_contents) = ($self->{"form"}, "");
 
 712   my $ary = $form->{$var};
 
 713   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 714     last unless (ref($ary) eq "ARRAY");
 
 715     $ary = $ary->[$indices[$i]];
 
 718   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 719     $form->{"__first__"} = $i == 0;
 
 720     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 721     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 722     $form->{"__counter__"} = $i + 1;
 
 723     my $new_text = $self->parse_block($text, (@indices, $i));
 
 724     return undef unless (defined($new_text));
 
 725     $new_contents .= $start_tag . $new_text . $end_tag;
 
 727   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 729   return $new_contents;
 
 733   my ($self, $text, $pos, $var, $not) = @_;
 
 736   $pos = 0 unless ($pos);
 
 738   while ($pos < length($text)) {
 
 741     next if (substr($text, $pos - 1, 5) ne '<%');
 
 743     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 746     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 748         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 752       my $block = substr($text, 0, $pos - 1);
 
 753       substr($text, 0, $pos - 1) = "";
 
 754       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 755       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 757       return ($block, $text);
 
 759     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 762         my $block = substr($text, 0, $pos - 1);
 
 763         substr($text, 0, $pos - 1) = "";
 
 764         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 766         return ($block, $text);
 
 775   $main::lxdebug->enter_sub();
 
 777   my ($self, $contents, @indices) = @_;
 
 779   my $new_contents = "";
 
 781   while ($contents ne "") {
 
 782     if (substr($contents, 0, 1) eq "<") {
 
 783       $contents =~ m|^<[^>]+>|;
 
 785       substr($contents, 0, length($&)) = "";
 
 787       if ($tag =~ m|<table:table-row|) {
 
 788         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 791         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 793         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 796           substr($table_row, length($`), length($&)) = "";
 
 798           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 800             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 801             $main::lxdebug->leave_sub();
 
 805           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 806           if (!defined($new_text)) {
 
 807             $main::lxdebug->leave_sub();
 
 810           $new_contents .= $new_text;
 
 813           my $new_text = $self->parse_block($table_row, @indices);
 
 814           if (!defined($new_text)) {
 
 815             $main::lxdebug->leave_sub();
 
 818           $new_contents .= $tag . $new_text . $end_tag;
 
 822         $new_contents .= $tag;
 
 826       $contents =~ /^[^<]+/;
 
 829       my $pos_if = index($text, '<%if');
 
 830       my $pos_foreach = index($text, '<%foreach');
 
 832       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 833         substr($contents, 0, length($text)) = "";
 
 834         $new_contents .= $self->substitute_vars($text, @indices);
 
 838       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 839         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 840         substr($contents, 0, $pos_foreach) = "";
 
 842         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 843           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 844           $main::lxdebug->leave_sub();
 
 850         substr($contents, 0, length($&)) = "";
 
 853         ($block, $contents) = $self->find_end($contents);
 
 855           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 856           $main::lxdebug->leave_sub();
 
 860         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 861         if (!defined($new_text)) {
 
 862           $main::lxdebug->leave_sub();
 
 865         $new_contents .= $new_text;
 
 868         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 869         substr($contents, 0, $pos_if) = "";
 
 871         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 872           $self->{"error"} = "Malformed <\%if\%>.";
 
 873           $main::lxdebug->leave_sub();
 
 877         my ($not, $var) = ($1, $2);
 
 879         substr($contents, 0, length($&)) = "";
 
 881         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 883           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 884           $main::lxdebug->leave_sub();
 
 888         my $value = $self->{"form"}->{$var};
 
 889         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 890           last unless (ref($value) eq "ARRAY");
 
 891           $value = $value->[$indices[$i]];
 
 894         if (($not && !$value) || (!$not && $value)) {
 
 895           my $new_text = $self->parse_block($block, @indices);
 
 896           if (!defined($new_text)) {
 
 897             $main::lxdebug->leave_sub();
 
 900           $new_contents .= $new_text;
 
 906   $main::lxdebug->leave_sub();
 
 908   return $new_contents;
 
 912   $main::lxdebug->enter_sub();
 
 916   my $form = $self->{"form"};
 
 921   if ($form->{"IN"} =~ m|^/|) {
 
 922     $file_name = $form->{"IN"};
 
 924     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
 927   my $zip = Archive::Zip->new();
 
 928   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
 929     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
 930     $main::lxdebug->leave_sub();
 
 934   my $contents = $zip->contents("content.xml");
 
 936     $self->{"error"} = "File is not a OpenDocument file.";
 
 937     $main::lxdebug->leave_sub();
 
 941   my $rnd = $self->{"rnd"};
 
 942   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
 943 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
 945 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
 946 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
 948 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
 949 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
 951 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
 952 <style:text-properties style:text-line-through-style="solid"/>
 
 954 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
 955 <style:text-properties style:text-position="super 58%"/>
 
 957 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
 958 <style:text-properties style:text-position="sub 58%"/>
 
 962   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
 963   $contents =~ s|[\n\r]||gm;
 
 965   my $new_contents = $self->parse_block($contents);
 
 966   if (!defined($new_contents)) {
 
 967     $main::lxdebug->leave_sub();
 
 971 #   $new_contents =~ s|>|>\n|g;
 
 973   $zip->contents("content.xml", $new_contents);
 
 975   my $styles = $zip->contents("styles.xml");
 
 977     my $new_styles = $self->parse_block($styles);
 
 978     if (!defined($new_contents)) {
 
 979       $main::lxdebug->leave_sub();
 
 982     $zip->contents("styles.xml", $new_styles);
 
 985   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
 988   if ($form->{"format"} =~ /pdf/) {
 
 989     $res = $self->convert_to_pdf();
 
 992   $main::lxdebug->leave_sub();
 
 996 sub is_xvfb_running {
 
 997   $main::lxdebug->enter_sub();
 
1002   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1005   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
1006   if ((-f $dfname) && open(IN, $dfname)) {
 
1011     my $xauthority = <IN>;
 
1015     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
1017     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
1018       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
1019       unlink($dfname, $xauthority);
 
1020       $main::lxdebug->leave_sub();
 
1025     if ($line !~ /xvfb/i) {
 
1026       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
1027       unlink($dfname, $xauthority);
 
1028       $main::lxdebug->leave_sub();
 
1032     $ENV{"XAUTHORITY"} = $xauthority;
 
1033     $ENV{"DISPLAY"} = $display;
 
1035     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1038   $main::lxdebug->leave_sub();
 
1044   $main::lxdebug->enter_sub();
 
1048   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1050   my $display = $self->is_xvfb_running();
 
1053     $main::lxdebug->leave_sub();
 
1058   while ( -f "/tmp/.X${display}-lock") {
 
1061   $display = ":${display}";
 
1062   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1064   my $mcookie = `mcookie`;
 
1065   die("Installation error: mcookie not found.") if ($? != 0);
 
1068   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1070   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1071   $ENV{"XAUTHORITY"} = $xauthority;
 
1073   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1075   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1077     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1078     $main::lxdebug->leave_sub();
 
1082   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1086     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1087     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1090   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1093   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1094   if (!open(OUT, ">$dfname")) {
 
1095     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1096     unlink($xauthority);
 
1098     $main::lxdebug->leave_sub();
 
1101   print(OUT "$pid\n$display\n$xauthority\n");
 
1104   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1106   if (!$self->is_xvfb_running()) {
 
1107     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1108     unlink($xauthority, $dfname);
 
1110     $main::lxdebug->leave_sub();
 
1114   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1116   $main::lxdebug->leave_sub();
 
1121 sub is_openoffice_running {
 
1122   $main::lxdebug->enter_sub();
 
1124   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1125          "> /dev/null 2> /dev/null");
 
1127   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1129   $main::lxdebug->leave_sub();
 
1134 sub spawn_openoffice {
 
1135   $main::lxdebug->enter_sub();
 
1139   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1141   my ($try, $spawned_oo, $res);
 
1144   for ($try = 0; $try < 15; $try++) {
 
1145     if ($self->is_openoffice_running()) {
 
1153         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1155         open(STDIN, '/dev/null');
 
1156         open(STDOUT, '>/dev/null');
 
1157         my $new_pid = fork();
 
1159         my $ssres = setsid();
 
1160         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1161         my @cmdline = ($main::openofficeorg_writer_bin,
 
1162                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1164                        "-accept=socket,host=localhost,port=" .
 
1165                        $main::openofficeorg_daemon_port . ";urp;");
 
1169       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1174     sleep($try >= 5 ? 2 : 1);
 
1178     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1179       "OpenOffice could not be started.";
 
1182   $main::lxdebug->leave_sub();
 
1187 sub convert_to_pdf {
 
1188   $main::lxdebug->enter_sub();
 
1192   my $form = $self->{"form"};
 
1194   my $filename = $form->{"tmpfile"};
 
1195   $filename =~ s/.odt$//;
 
1196   if (substr($filename, 0, 1) ne "/") {
 
1197     $filename = getcwd() . "/${filename}";
 
1200   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1201     $ENV{'HOME'} = $self->{"userspath"};
 
1203     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1206   if (!$self->spawn_xvfb()) {
 
1207     $main::lxdebug->leave_sub();
 
1212   if (!$main::openofficeorg_daemon) {
 
1213     @cmdline = ($main::openofficeorg_writer_bin,
 
1214                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1216                 "file:${filename}.odt",
 
1217                 "macro://" . (split('/', $filename))[-1] .
 
1218                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1220     if (!$self->spawn_openoffice()) {
 
1221       $main::lxdebug->leave_sub();
 
1225     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1226                 $main::openofficeorg_daemon_port,
 
1234     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1236     unlink($filename . ".odt");
 
1238     $main::lxdebug->leave_sub();
 
1243   unlink($filename . ".odt", $filename . ".pdf");
 
1244   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1247   $main::lxdebug->leave_sub();
 
1252   my ($self, $variable) = @_;
 
1253   my $form = $self->{"form"};
 
1254   my $iconv = $self->{"iconv"};
 
1257     ('order' => ['&', '<', '>', '"', "'",
 
1259                  quotemeta("\n"), quotemeta("\r")],
 
1265      '\x80'          => chr(0xa4), # Euro
 
1266      quotemeta("\n") => '<text:line-break/>',
 
1267      quotemeta("\r") => '',
 
1270   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1272   # Allow some HTML markup to be converted into the output format's
 
1273   # corresponding markup code, e.g. bold or italic.
 
1274   my $rnd = $self->{"rnd"};
 
1275   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1276                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1278   foreach my $key (keys(%markup_replace)) {
 
1279     my $value = $markup_replace{$key};
 
1280     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
 
1281     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1284   return $iconv->convert($variable);
 
1287 sub get_mime_type() {
 
1288   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1289     return "application/pdf";
 
1291     return "application/vnd.oasis.opendocument.text";
 
1295 sub uses_temp_file {
 
1300 ##########################################################
 
1304 ##########################################################
 
1306 package XMLTemplate; 
 
1310 @ISA = qw(HTMLTemplate);
 
1313   #evtl auskommentieren
 
1316   return $type->SUPER::new(@_);
 
1320   my ($self, $variable) = @_;
 
1321   my $form = $self->{"form"};
 
1324     ('order' => ['<', '>', quotemeta("\n")],
 
1327      quotemeta("\n") => '<br>',
 
1330   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1332   # Allow no markup to be converted into the output format
 
1333   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
1335   foreach my $key (@markup_replace) {
 
1336     $variable =~ s/\<(\/?)${key}\>//g;
 
1342 sub get_mime_type() {
 
1345   if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
 
1346     return "application/xml ";
 
1347   } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
 
1348     return "application/x-taxbird";
 
1354 sub uses_temp_file {
 
1355   # tempfile needet for XML Output