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"
 
  96      quotemeta("\\") => '\\textbackslash ',
 
 110      quotemeta('^')  => '\^\\',
 
 111      quotemeta("\n") => '\newline '
 
 114   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 116   # Allow some HTML markup to be converted into the output format's
 
 117   # corresponding markup code, e.g. bold or italic.
 
 118   my %markup_replace = ('b' => 'textbf',
 
 122   foreach my $key (keys(%markup_replace)) {
 
 123     my $new = $markup_replace{$key};
 
 124     $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
 
 130 sub substitute_vars {
 
 131   my ($self, $text, @indices) = @_;
 
 133   my $form = $self->{"form"};
 
 135   while ($text =~ /<\%(.*?)\%>/) {
 
 136     my ($var, @options) = split(/\s+/, $1);
 
 137     my $value = $form->{$var};
 
 139     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 140       last unless (ref($value) eq "ARRAY");
 
 141       $value = $value->[$indices[$i]];
 
 143     $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
 
 144     substr($text, $-[0], $+[0] - $-[0]) = $value;
 
 151   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 153   my ($form, $new_contents) = ($self->{"form"}, "");
 
 155   my $ary = $form->{$var};
 
 156   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 157     last unless (ref($ary) eq "ARRAY");
 
 158     $ary = $ary->[$indices[$i]];
 
 162   my $current_page = 1;
 
 163   my ($current_line, $corrent_row) = (0, 1);
 
 165   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 166     $form->{"__first__"} = $i == 0;
 
 167     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 168     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 169     $form->{"__counter__"} = $i + 1;
 
 171     if ((scalar(@{$form->{"description"}}) == scalar(@{$ary})) &&
 
 172         $self->{"chars_per_line"}) {
 
 174         int(length($form->{"description"}->[$i]) / $self->{"chars_per_line"});
 
 177       $form->{"description"}->[$i] =~ s/(\\newline\s?)*$//;
 
 178       my $_description = $form->{"description"}->[$i];
 
 179       while ($_description =~ /\\newline/) {
 
 181         $_description =~ s/\\newline//;
 
 185       if ($current_page == 1) {
 
 186         $lpp = $self->{"lines_on_first_page"};
 
 188         $lpp = $self->{"lines_on_second_page"};
 
 191       # Yes we need a manual page break -- or the user has forced one
 
 192       if ((($current_line + $lines) > $lpp) ||
 
 193           ($form->{"description"}->[$i] =~ /<pagebreak>/)) {
 
 194         my $pb = $self->{"pagebreak_block"};
 
 196         # replace the special variables <%sumcarriedforward%>
 
 199         my $psum = $form->format_amount($myconfig, $sum, 2);
 
 200         $pb =~ s/<%sumcarriedforward%>/$psum/g;
 
 201         $pb =~ s/<%lastpage%>/$current_page/g;
 
 203         my $new_text = $self->parse_block($pb, (@indices, $i));
 
 204         return undef unless (defined($new_text));
 
 205         $new_contents .= $new_text;
 
 210       $current_line += $lines;
 
 212     if ($i < scalar(@{$form->{"linetotal"}})) {
 
 213       $sum += $form->parse_amount($myconfig, $form->{"linetotal"}->[$i]);
 
 216     my $new_text = $self->parse_block($text, (@indices, $i));
 
 217     return undef unless (defined($new_text));
 
 218     $new_contents .= $start_tag . $new_text . $end_tag;
 
 220   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 222   return $new_contents;
 
 226   my ($self, $text, $pos, $var, $not) = @_;
 
 229   $pos = 0 unless ($pos);
 
 231   while ($pos < length($text)) {
 
 234     next if (substr($text, $pos - 1, 2) ne '<%');
 
 236     if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
 
 239     } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
 
 241         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 245       my $block = substr($text, 0, $pos - 1);
 
 246       substr($text, 0, $pos - 1) = "";
 
 247       $text =~ s!^<\%[^\%]+\%>!!;
 
 248       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 250       return ($block, $text);
 
 252     } elsif (substr($text, $pos + 1, 3) eq 'end') {
 
 255         my $block = substr($text, 0, $pos - 1);
 
 256         substr($text, 0, $pos - 1) = "";
 
 257         $text =~ s!^<\%[^\%]+\%>!!;
 
 259         return ($block, $text);
 
 268   $main::lxdebug->enter_sub();
 
 270   my ($self, $contents, @indices) = @_;
 
 272   my $new_contents = "";
 
 274   while ($contents ne "") {
 
 275     my $pos_if = index($contents, '<%if');
 
 276     my $pos_foreach = index($contents, '<%foreach');
 
 278     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 279       $new_contents .= $self->substitute_vars($contents, @indices);
 
 283     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 284       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 285       substr($contents, 0, $pos_foreach) = "";
 
 287       if ($contents !~ m|^<\%foreach (.*?)\%>|) {
 
 288         $self->{"error"} = "Malformed <\%foreach\%>.";
 
 289         $main::lxdebug->leave_sub();
 
 295       substr($contents, 0, length($&)) = "";
 
 298       ($block, $contents) = $self->find_end($contents);
 
 300         $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 301         $main::lxdebug->leave_sub();
 
 305       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 306       if (!defined($new_text)) {
 
 307         $main::lxdebug->leave_sub();
 
 310       $new_contents .= $new_text;
 
 313       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 314       substr($contents, 0, $pos_if) = "";
 
 316       if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
 
 317         $self->{"error"} = "Malformed <\%if\%>.";
 
 318         $main::lxdebug->leave_sub();
 
 322       my ($not, $var) = ($1, $2);
 
 324       substr($contents, 0, length($&)) = "";
 
 326       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 328         $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 329         $main::lxdebug->leave_sub();
 
 333       my $value = $self->{"form"}->{$var};
 
 334       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 335         last unless (ref($value) eq "ARRAY");
 
 336         $value = $value->[$indices[$i]];
 
 339       if (($not && !$value) || (!$not && $value)) {
 
 340         my $new_text = $self->parse_block($block, @indices);
 
 341         if (!defined($new_text)) {
 
 342           $main::lxdebug->leave_sub();
 
 345         $new_contents .= $new_text;
 
 350   $main::lxdebug->leave_sub();
 
 352   return $new_contents;
 
 358   my $form = $self->{"form"};
 
 360   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 361     $self->{"error"} = "$!";
 
 367   my $contents = join("", @_);
 
 369   # detect pagebreak block and its parameters
 
 370   if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
 
 371     $self->{"chars_per_line"} = $1;
 
 372     $self->{"lines_on_first_page"} = $2;
 
 373     $self->{"lines_on_second_page"} = $3;
 
 374     $self->{"pagebreak_block"} = $4;
 
 376     substr($contents, length($`), length($&)) = "";
 
 379   $self->{"forced_pagebreaks"} = [];
 
 381   my $new_contents = $self->parse_block($contents);
 
 382   if (!defined($new_contents)) {
 
 383     $main::lxdebug->leave_sub();
 
 387   print(OUT $new_contents);
 
 389   if ($form->{"format"} =~ /postscript/i) {
 
 390     return $self->convert_to_postscript();
 
 391   } elsif ($form->{"format"} =~ /pdf/i) {
 
 392     return $self->convert_to_pdf();
 
 398 sub convert_to_postscript {
 
 400   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 402   # Convert the tex file to postscript
 
 404   if (!chdir("$userspath")) {
 
 405     $self->{"error"} = "chdir : $!";
 
 410   $form->{tmpfile} =~ s/$userspath\///g;
 
 412   for (my $run = 1; $run <= 2; $run++) {
 
 413     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 414            "> $form->{tmpfile}.err");
 
 416       $self->{"error"} = $form->cleanup();
 
 422   $form->{tmpfile} =~ s/tex$/dvi/;
 
 424   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 426     $self->{"error"} = "dvips : $!";
 
 430   $form->{tmpfile} =~ s/dvi$/ps/;
 
 439   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 441   # Convert the tex file to PDF
 
 443   if (!chdir("$userspath")) {
 
 444     $self->{"error"} = "chdir : $!";
 
 449   $form->{tmpfile} =~ s/$userspath\///g;
 
 451   for (my $run = 1; $run <= 2; $run++) {
 
 452     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 453            "> $form->{tmpfile}.err");
 
 455       $self->{"error"} = $form->cleanup();
 
 461   $form->{tmpfile} =~ s/tex$/pdf/;
 
 466 sub get_mime_type() {
 
 469   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 470     return "application/postscript";
 
 472     return "application/pdf";
 
 485 package HTMLTemplate;
 
 489 @ISA = qw(LaTeXTemplate);
 
 494   return $type->SUPER::new(@_);
 
 498   my ($self, $variable) = @_;
 
 499   my $form = $self->{"form"};
 
 502     ('order' => ['<', '>', quotemeta("\n")],
 
 505      quotemeta("\n") => '<br>',
 
 508   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 510   # Allow some HTML markup to be converted into the output format's
 
 511   # corresponding markup code, e.g. bold or italic.
 
 512   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 514   foreach my $key (@markup_replace) {
 
 515     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 521 sub get_mime_type() {
 
 524   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 525     return "application/postscript";
 
 526   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 527     return "application/pdf";
 
 536   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 538   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 545 sub convert_to_postscript {
 
 547   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 549   # Convert the HTML file to postscript
 
 551   if (!chdir("$userspath")) {
 
 552     $self->{"error"} = "chdir : $!";
 
 557   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 558   my $psfile = $form->{"tmpfile"};
 
 559   $psfile =~ s/.html/.ps/;
 
 560   if ($psfile eq $form->{"tmpfile"}) {
 
 564   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 566     $self->{"error"} = $form->cleanup();
 
 571   $form->{"tmpfile"} = $psfile;
 
 580   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 582   # Convert the HTML file to PDF
 
 584   if (!chdir("$userspath")) {
 
 585     $self->{"error"} = "chdir : $!";
 
 590   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 591   my $pdffile = $form->{"tmpfile"};
 
 592   $pdffile =~ s/.html/.pdf/;
 
 593   if ($pdffile eq $form->{"tmpfile"}) {
 
 597   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 599     $self->{"error"} = $form->cleanup();
 
 604   $form->{"tmpfile"} = $pdffile;
 
 616 package OpenDocumentTemplate;
 
 624 # use File::Temp qw(:mktemp);
 
 627 @ISA = qw(SimpleTemplate);
 
 632   $self = $type->SUPER::new(@_);
 
 634   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 635     eval("use ${module};");
 
 637       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 638                              "loaded. Support for OpenDocument templates " .
 
 639                              "does not work without it. Please install your " .
 
 640                              "distribution's package or get the module from " .
 
 641                              "CPAN ( http://www.cpan.org ).");
 
 645   $self->{"rnd"} = int(rand(1000000));
 
 646   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 651 sub substitute_vars {
 
 652   my ($self, $text, @indices) = @_;
 
 654   my $form = $self->{"form"};
 
 656   while ($text =~ /\<\%(.*?)\%\>/) {
 
 657     my $value = $form->{$1};
 
 659     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 660       last unless (ref($value) eq "ARRAY");
 
 661       $value = $value->[$indices[$i]];
 
 663     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 670   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 672   my ($form, $new_contents) = ($self->{"form"}, "");
 
 674   my $ary = $form->{$var};
 
 675   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 676     last unless (ref($ary) eq "ARRAY");
 
 677     $ary = $ary->[$indices[$i]];
 
 680   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 681     $form->{"__first__"} = $i == 0;
 
 682     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 683     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 684     $form->{"__counter__"} = $i + 1;
 
 685     my $new_text = $self->parse_block($text, (@indices, $i));
 
 686     return undef unless (defined($new_text));
 
 687     $new_contents .= $start_tag . $new_text . $end_tag;
 
 689   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 691   return $new_contents;
 
 695   my ($self, $text, $pos, $var, $not) = @_;
 
 698   $pos = 0 unless ($pos);
 
 700   while ($pos < length($text)) {
 
 703     next if (substr($text, $pos - 1, 5) ne '<%');
 
 705     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 708     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 710         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 714       my $block = substr($text, 0, $pos - 1);
 
 715       substr($text, 0, $pos - 1) = "";
 
 716       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 717       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 719       return ($block, $text);
 
 721     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 724         my $block = substr($text, 0, $pos - 1);
 
 725         substr($text, 0, $pos - 1) = "";
 
 726         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 728         return ($block, $text);
 
 737   $main::lxdebug->enter_sub();
 
 739   my ($self, $contents, @indices) = @_;
 
 741   my $new_contents = "";
 
 743   while ($contents ne "") {
 
 744     if (substr($contents, 0, 1) eq "<") {
 
 745       $contents =~ m|^<[^>]+>|;
 
 747       substr($contents, 0, length($&)) = "";
 
 749       if ($tag =~ m|<table:table-row|) {
 
 750         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 753         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 755         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 758           substr($table_row, length($`), length($&)) = "";
 
 760           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 762             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 763             $main::lxdebug->leave_sub();
 
 767           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 768           if (!defined($new_text)) {
 
 769             $main::lxdebug->leave_sub();
 
 772           $new_contents .= $new_text;
 
 775           my $new_text = $self->parse_block($table_row, @indices);
 
 776           if (!defined($new_text)) {
 
 777             $main::lxdebug->leave_sub();
 
 780           $new_contents .= $tag . $new_text . $end_tag;
 
 784         $new_contents .= $tag;
 
 788       $contents =~ /^[^<]+/;
 
 791       my $pos_if = index($text, '<%if');
 
 792       my $pos_foreach = index($text, '<%foreach');
 
 794       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 795         substr($contents, 0, length($text)) = "";
 
 796         $new_contents .= $self->substitute_vars($text, @indices);
 
 800       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 801         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 802         substr($contents, 0, $pos_foreach) = "";
 
 804         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 805           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 806           $main::lxdebug->leave_sub();
 
 812         substr($contents, 0, length($&)) = "";
 
 815         ($block, $contents) = $self->find_end($contents);
 
 817           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 818           $main::lxdebug->leave_sub();
 
 822         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 823         if (!defined($new_text)) {
 
 824           $main::lxdebug->leave_sub();
 
 827         $new_contents .= $new_text;
 
 830         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 831         substr($contents, 0, $pos_if) = "";
 
 833         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 834           $self->{"error"} = "Malformed <\%if\%>.";
 
 835           $main::lxdebug->leave_sub();
 
 839         my ($not, $var) = ($1, $2);
 
 841         substr($contents, 0, length($&)) = "";
 
 843         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 845           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 846           $main::lxdebug->leave_sub();
 
 850         my $value = $self->{"form"}->{$var};
 
 851         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 852           last unless (ref($value) eq "ARRAY");
 
 853           $value = $value->[$indices[$i]];
 
 856         if (($not && !$value) || (!$not && $value)) {
 
 857           my $new_text = $self->parse_block($block, @indices);
 
 858           if (!defined($new_text)) {
 
 859             $main::lxdebug->leave_sub();
 
 862           $new_contents .= $new_text;
 
 868   $main::lxdebug->leave_sub();
 
 870   return $new_contents;
 
 874   $main::lxdebug->enter_sub();
 
 878   my $form = $self->{"form"};
 
 883   if ($form->{"IN"} =~ m|^/|) {
 
 884     $file_name = $form->{"IN"};
 
 886     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
 889   my $zip = Archive::Zip->new();
 
 890   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
 891     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
 892     $main::lxdebug->leave_sub();
 
 896   my $contents = $zip->contents("content.xml");
 
 898     $self->{"error"} = "File is not a OpenDocument file.";
 
 899     $main::lxdebug->leave_sub();
 
 903   my $rnd = $self->{"rnd"};
 
 904   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
 905 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
 907 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
 908 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
 910 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
 911 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
 913 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
 914 <style:text-properties style:text-line-through-style="solid"/>
 
 916 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
 917 <style:text-properties style:text-position="super 58%"/>
 
 919 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
 920 <style:text-properties style:text-position="sub 58%"/>
 
 924   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
 925   $contents =~ s|[\n\r]||gm;
 
 927   my $new_contents = $self->parse_block($contents);
 
 928   if (!defined($new_contents)) {
 
 929     $main::lxdebug->leave_sub();
 
 933 #   $new_contents =~ s|>|>\n|g;
 
 935   $zip->contents("content.xml", $new_contents);
 
 937   my $styles = $zip->contents("styles.xml");
 
 939     my $new_styles = $self->parse_block($styles);
 
 940     if (!defined($new_contents)) {
 
 941       $main::lxdebug->leave_sub();
 
 944     $zip->contents("styles.xml", $new_styles);
 
 947   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
 950   if ($form->{"format"} =~ /pdf/) {
 
 951     $res = $self->convert_to_pdf();
 
 954   $main::lxdebug->leave_sub();
 
 958 sub is_xvfb_running {
 
 959   $main::lxdebug->enter_sub();
 
 964   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
 967   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
 968   if ((-f $dfname) && open(IN, $dfname)) {
 
 973     my $xauthority = <IN>;
 
 977     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
 979     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
 980       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
 981       unlink($dfname, $xauthority);
 
 982       $main::lxdebug->leave_sub();
 
 987     if ($line !~ /xvfb/i) {
 
 988       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
 989       unlink($dfname, $xauthority);
 
 990       $main::lxdebug->leave_sub();
 
 994     $ENV{"XAUTHORITY"} = $xauthority;
 
 995     $ENV{"DISPLAY"} = $display;
 
 997     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1000   $main::lxdebug->leave_sub();
 
1006   $main::lxdebug->enter_sub();
 
1010   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1012   my $display = $self->is_xvfb_running();
 
1015     $main::lxdebug->leave_sub();
 
1020   while ( -f "/tmp/.X${display}-lock") {
 
1023   $display = ":${display}";
 
1024   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1026   my $mcookie = `mcookie`;
 
1027   die("Installation error: mcookie not found.") if ($? != 0);
 
1030   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1032   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1033   $ENV{"XAUTHORITY"} = $xauthority;
 
1035   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1037   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1039     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1040     $main::lxdebug->leave_sub();
 
1044   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1048     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1049     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1052   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1055   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1056   if (!open(OUT, ">$dfname")) {
 
1057     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1058     unlink($xauthority);
 
1060     $main::lxdebug->leave_sub();
 
1063   print(OUT "$pid\n$display\n$xauthority\n");
 
1066   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1068   if (!$self->is_xvfb_running()) {
 
1069     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1070     unlink($xauthority, $dfname);
 
1072     $main::lxdebug->leave_sub();
 
1076   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1078   $main::lxdebug->leave_sub();
 
1083 sub is_openoffice_running {
 
1084   $main::lxdebug->enter_sub();
 
1086   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1087          "> /dev/null 2> /dev/null");
 
1089   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1091   $main::lxdebug->leave_sub();
 
1096 sub spawn_openoffice {
 
1097   $main::lxdebug->enter_sub();
 
1101   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1103   my ($try, $spawned_oo, $res);
 
1106   for ($try = 0; $try < 15; $try++) {
 
1107     if ($self->is_openoffice_running()) {
 
1115         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1117         open(STDIN, '/dev/null');
 
1118         open(STDOUT, '>/dev/null');
 
1119         my $new_pid = fork();
 
1121         my $ssres = setsid();
 
1122         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1123         my @cmdline = ($main::openofficeorg_writer_bin,
 
1124                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1126                        "-accept=socket,host=localhost,port=" .
 
1127                        $main::openofficeorg_daemon_port . ";urp;");
 
1131       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1136     sleep($try >= 5 ? 2 : 1);
 
1140     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1141       "OpenOffice could not be started.";
 
1144   $main::lxdebug->leave_sub();
 
1149 sub convert_to_pdf {
 
1150   $main::lxdebug->enter_sub();
 
1154   my $form = $self->{"form"};
 
1156   my $filename = $form->{"tmpfile"};
 
1157   $filename =~ s/.odt$//;
 
1158   if (substr($filename, 0, 1) ne "/") {
 
1159     $filename = getcwd() . "/${filename}";
 
1162   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1163     $ENV{'HOME'} = $self->{"userspath"};
 
1165     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1168   if (!$self->spawn_xvfb()) {
 
1169     $main::lxdebug->leave_sub();
 
1174   if (!$main::openofficeorg_daemon) {
 
1175     @cmdline = ($main::openofficeorg_writer_bin,
 
1176                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1178                 "file:${filename}.odt",
 
1179                 "macro://" . (split('/', $filename))[-1] .
 
1180                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1182     if (!$self->spawn_openoffice()) {
 
1183       $main::lxdebug->leave_sub();
 
1187     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1188                 $main::openofficeorg_daemon_port,
 
1196     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1198     unlink($filename . ".odt");
 
1200     $main::lxdebug->leave_sub();
 
1205   unlink($filename . ".odt", $filename . ".pdf");
 
1206   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1209   $main::lxdebug->leave_sub();
 
1214   my ($self, $variable) = @_;
 
1215   my $form = $self->{"form"};
 
1216   my $iconv = $self->{"iconv"};
 
1219     ('order' => ['&', '<', '>', '"', "'",
 
1221                  quotemeta("\n"), quotemeta("\r")],
 
1227      '\x80'          => chr(0xa4), # Euro
 
1228      quotemeta("\n") => '<text:line-break/>',
 
1229      quotemeta("\r") => '',
 
1232   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1234   # Allow some HTML markup to be converted into the output format's
 
1235   # corresponding markup code, e.g. bold or italic.
 
1236   my $rnd = $self->{"rnd"};
 
1237   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1238                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1240   foreach my $key (keys(%markup_replace)) {
 
1241     my $value = $markup_replace{$key};
 
1242     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
 
1243     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1246   return $iconv->convert($variable);
 
1249 sub get_mime_type() {
 
1250   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1251     return "application/pdf";
 
1253     return "application/vnd.oasis.opendocument.text";
 
1257 sub uses_temp_file {