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($self->{"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($self->{"myconfig"},
 
 214                                   $form->{"linetotal"}->[$i]);
 
 217     my $new_text = $self->parse_block($text, (@indices, $i));
 
 218     return undef unless (defined($new_text));
 
 219     $new_contents .= $start_tag . $new_text . $end_tag;
 
 221   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 223   return $new_contents;
 
 227   my ($self, $text, $pos, $var, $not) = @_;
 
 230   $pos = 0 unless ($pos);
 
 232   while ($pos < length($text)) {
 
 235     next if (substr($text, $pos - 1, 2) ne '<%');
 
 237     if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
 
 240     } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
 
 242         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 246       my $block = substr($text, 0, $pos - 1);
 
 247       substr($text, 0, $pos - 1) = "";
 
 248       $text =~ s!^<\%[^\%]+\%>!!;
 
 249       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 251       return ($block, $text);
 
 253     } elsif (substr($text, $pos + 1, 3) eq 'end') {
 
 256         my $block = substr($text, 0, $pos - 1);
 
 257         substr($text, 0, $pos - 1) = "";
 
 258         $text =~ s!^<\%[^\%]+\%>!!;
 
 260         return ($block, $text);
 
 269   $main::lxdebug->enter_sub();
 
 271   my ($self, $contents, @indices) = @_;
 
 273   my $new_contents = "";
 
 275   while ($contents ne "") {
 
 276     my $pos_if = index($contents, '<%if');
 
 277     my $pos_foreach = index($contents, '<%foreach');
 
 279     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 280       $new_contents .= $self->substitute_vars($contents, @indices);
 
 284     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 285       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 286       substr($contents, 0, $pos_foreach) = "";
 
 288       if ($contents !~ m|^<\%foreach (.*?)\%>|) {
 
 289         $self->{"error"} = "Malformed <\%foreach\%>.";
 
 290         $main::lxdebug->leave_sub();
 
 296       substr($contents, 0, length($&)) = "";
 
 299       ($block, $contents) = $self->find_end($contents);
 
 301         $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 302         $main::lxdebug->leave_sub();
 
 306       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 307       if (!defined($new_text)) {
 
 308         $main::lxdebug->leave_sub();
 
 311       $new_contents .= $new_text;
 
 314       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 315       substr($contents, 0, $pos_if) = "";
 
 317       if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
 
 318         $self->{"error"} = "Malformed <\%if\%>.";
 
 319         $main::lxdebug->leave_sub();
 
 323       my ($not, $var) = ($1, $2);
 
 325       substr($contents, 0, length($&)) = "";
 
 327       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 329         $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 330         $main::lxdebug->leave_sub();
 
 334       my $value = $self->{"form"}->{$var};
 
 335       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 336         last unless (ref($value) eq "ARRAY");
 
 337         $value = $value->[$indices[$i]];
 
 340       if (($not && !$value) || (!$not && $value)) {
 
 341         my $new_text = $self->parse_block($block, @indices);
 
 342         if (!defined($new_text)) {
 
 343           $main::lxdebug->leave_sub();
 
 346         $new_contents .= $new_text;
 
 351   $main::lxdebug->leave_sub();
 
 353   return $new_contents;
 
 359   my $form = $self->{"form"};
 
 361   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 362     $self->{"error"} = "$!";
 
 368   my $contents = join("", @_);
 
 370   # detect pagebreak block and its parameters
 
 371   if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
 
 372     $self->{"chars_per_line"} = $1;
 
 373     $self->{"lines_on_first_page"} = $2;
 
 374     $self->{"lines_on_second_page"} = $3;
 
 375     $self->{"pagebreak_block"} = $4;
 
 377     substr($contents, length($`), length($&)) = "";
 
 380   $self->{"forced_pagebreaks"} = [];
 
 382   my $new_contents = $self->parse_block($contents);
 
 383   if (!defined($new_contents)) {
 
 384     $main::lxdebug->leave_sub();
 
 388   print(OUT $new_contents);
 
 390   if ($form->{"format"} =~ /postscript/i) {
 
 391     return $self->convert_to_postscript();
 
 392   } elsif ($form->{"format"} =~ /pdf/i) {
 
 393     return $self->convert_to_pdf();
 
 399 sub convert_to_postscript {
 
 401   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 403   # Convert the tex file to postscript
 
 405   if (!chdir("$userspath")) {
 
 406     $self->{"error"} = "chdir : $!";
 
 411   $form->{tmpfile} =~ s/$userspath\///g;
 
 413   for (my $run = 1; $run <= 2; $run++) {
 
 414     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 415            "> $form->{tmpfile}.err");
 
 417       $self->{"error"} = $form->cleanup();
 
 423   $form->{tmpfile} =~ s/tex$/dvi/;
 
 425   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 427     $self->{"error"} = "dvips : $!";
 
 431   $form->{tmpfile} =~ s/dvi$/ps/;
 
 440   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 442   # Convert the tex file to PDF
 
 444   if (!chdir("$userspath")) {
 
 445     $self->{"error"} = "chdir : $!";
 
 450   $form->{tmpfile} =~ s/$userspath\///g;
 
 452   for (my $run = 1; $run <= 2; $run++) {
 
 453     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 454            "> $form->{tmpfile}.err");
 
 456       $self->{"error"} = $form->cleanup();
 
 462   $form->{tmpfile} =~ s/tex$/pdf/;
 
 467 sub get_mime_type() {
 
 470   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 471     return "application/postscript";
 
 473     return "application/pdf";
 
 486 package HTMLTemplate;
 
 490 @ISA = qw(LaTeXTemplate);
 
 495   return $type->SUPER::new(@_);
 
 499   my ($self, $variable) = @_;
 
 500   my $form = $self->{"form"};
 
 503     ('order' => ['<', '>', quotemeta("\n")],
 
 506      quotemeta("\n") => '<br>',
 
 509   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 511   # Allow some HTML markup to be converted into the output format's
 
 512   # corresponding markup code, e.g. bold or italic.
 
 513   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 515   foreach my $key (@markup_replace) {
 
 516     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 522 sub get_mime_type() {
 
 525   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 526     return "application/postscript";
 
 527   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 528     return "application/pdf";
 
 537   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 539   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 546 sub convert_to_postscript {
 
 548   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 550   # Convert the HTML file to postscript
 
 552   if (!chdir("$userspath")) {
 
 553     $self->{"error"} = "chdir : $!";
 
 558   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 559   my $psfile = $form->{"tmpfile"};
 
 560   $psfile =~ s/.html/.ps/;
 
 561   if ($psfile eq $form->{"tmpfile"}) {
 
 565   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 567     $self->{"error"} = $form->cleanup();
 
 572   $form->{"tmpfile"} = $psfile;
 
 581   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 583   # Convert the HTML file to PDF
 
 585   if (!chdir("$userspath")) {
 
 586     $self->{"error"} = "chdir : $!";
 
 591   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 592   my $pdffile = $form->{"tmpfile"};
 
 593   $pdffile =~ s/.html/.pdf/;
 
 594   if ($pdffile eq $form->{"tmpfile"}) {
 
 598   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 600     $self->{"error"} = $form->cleanup();
 
 605   $form->{"tmpfile"} = $pdffile;
 
 614 #### OpenDocumentTemplate
 
 617 package OpenDocumentTemplate;
 
 625 # use File::Temp qw(:mktemp);
 
 628 @ISA = qw(SimpleTemplate);
 
 633   $self = $type->SUPER::new(@_);
 
 635   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 636     eval("use ${module};");
 
 638       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 639                              "loaded. Support for OpenDocument templates " .
 
 640                              "does not work without it. Please install your " .
 
 641                              "distribution's package or get the module from " .
 
 642                              "CPAN ( http://www.cpan.org ).");
 
 646   $self->{"rnd"} = int(rand(1000000));
 
 647   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 652 sub substitute_vars {
 
 653   my ($self, $text, @indices) = @_;
 
 655   my $form = $self->{"form"};
 
 657   while ($text =~ /\<\%(.*?)\%\>/) {
 
 658     my $value = $form->{$1};
 
 660     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 661       last unless (ref($value) eq "ARRAY");
 
 662       $value = $value->[$indices[$i]];
 
 664     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 671   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 673   my ($form, $new_contents) = ($self->{"form"}, "");
 
 675   my $ary = $form->{$var};
 
 676   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 677     last unless (ref($ary) eq "ARRAY");
 
 678     $ary = $ary->[$indices[$i]];
 
 681   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 682     $form->{"__first__"} = $i == 0;
 
 683     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 684     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 685     $form->{"__counter__"} = $i + 1;
 
 686     my $new_text = $self->parse_block($text, (@indices, $i));
 
 687     return undef unless (defined($new_text));
 
 688     $new_contents .= $start_tag . $new_text . $end_tag;
 
 690   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 692   return $new_contents;
 
 696   my ($self, $text, $pos, $var, $not) = @_;
 
 699   $pos = 0 unless ($pos);
 
 701   while ($pos < length($text)) {
 
 704     next if (substr($text, $pos - 1, 5) ne '<%');
 
 706     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 709     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 711         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 715       my $block = substr($text, 0, $pos - 1);
 
 716       substr($text, 0, $pos - 1) = "";
 
 717       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 718       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 720       return ($block, $text);
 
 722     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 725         my $block = substr($text, 0, $pos - 1);
 
 726         substr($text, 0, $pos - 1) = "";
 
 727         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 729         return ($block, $text);
 
 738   $main::lxdebug->enter_sub();
 
 740   my ($self, $contents, @indices) = @_;
 
 742   my $new_contents = "";
 
 744   while ($contents ne "") {
 
 745     if (substr($contents, 0, 1) eq "<") {
 
 746       $contents =~ m|^<[^>]+>|;
 
 748       substr($contents, 0, length($&)) = "";
 
 750       if ($tag =~ m|<table:table-row|) {
 
 751         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 754         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 756         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 759           substr($table_row, length($`), length($&)) = "";
 
 761           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 763             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 764             $main::lxdebug->leave_sub();
 
 768           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 769           if (!defined($new_text)) {
 
 770             $main::lxdebug->leave_sub();
 
 773           $new_contents .= $new_text;
 
 776           my $new_text = $self->parse_block($table_row, @indices);
 
 777           if (!defined($new_text)) {
 
 778             $main::lxdebug->leave_sub();
 
 781           $new_contents .= $tag . $new_text . $end_tag;
 
 785         $new_contents .= $tag;
 
 789       $contents =~ /^[^<]+/;
 
 792       my $pos_if = index($text, '<%if');
 
 793       my $pos_foreach = index($text, '<%foreach');
 
 795       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 796         substr($contents, 0, length($text)) = "";
 
 797         $new_contents .= $self->substitute_vars($text, @indices);
 
 801       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 802         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 803         substr($contents, 0, $pos_foreach) = "";
 
 805         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 806           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 807           $main::lxdebug->leave_sub();
 
 813         substr($contents, 0, length($&)) = "";
 
 816         ($block, $contents) = $self->find_end($contents);
 
 818           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 819           $main::lxdebug->leave_sub();
 
 823         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 824         if (!defined($new_text)) {
 
 825           $main::lxdebug->leave_sub();
 
 828         $new_contents .= $new_text;
 
 831         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 832         substr($contents, 0, $pos_if) = "";
 
 834         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 835           $self->{"error"} = "Malformed <\%if\%>.";
 
 836           $main::lxdebug->leave_sub();
 
 840         my ($not, $var) = ($1, $2);
 
 842         substr($contents, 0, length($&)) = "";
 
 844         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 846           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 847           $main::lxdebug->leave_sub();
 
 851         my $value = $self->{"form"}->{$var};
 
 852         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 853           last unless (ref($value) eq "ARRAY");
 
 854           $value = $value->[$indices[$i]];
 
 857         if (($not && !$value) || (!$not && $value)) {
 
 858           my $new_text = $self->parse_block($block, @indices);
 
 859           if (!defined($new_text)) {
 
 860             $main::lxdebug->leave_sub();
 
 863           $new_contents .= $new_text;
 
 869   $main::lxdebug->leave_sub();
 
 871   return $new_contents;
 
 875   $main::lxdebug->enter_sub();
 
 879   my $form = $self->{"form"};
 
 884   if ($form->{"IN"} =~ m|^/|) {
 
 885     $file_name = $form->{"IN"};
 
 887     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
 890   my $zip = Archive::Zip->new();
 
 891   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
 892     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
 893     $main::lxdebug->leave_sub();
 
 897   my $contents = $zip->contents("content.xml");
 
 899     $self->{"error"} = "File is not a OpenDocument file.";
 
 900     $main::lxdebug->leave_sub();
 
 904   my $rnd = $self->{"rnd"};
 
 905   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
 906 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
 908 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
 909 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
 911 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
 912 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
 914 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
 915 <style:text-properties style:text-line-through-style="solid"/>
 
 917 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
 918 <style:text-properties style:text-position="super 58%"/>
 
 920 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
 921 <style:text-properties style:text-position="sub 58%"/>
 
 925   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
 926   $contents =~ s|[\n\r]||gm;
 
 928   my $new_contents = $self->parse_block($contents);
 
 929   if (!defined($new_contents)) {
 
 930     $main::lxdebug->leave_sub();
 
 934 #   $new_contents =~ s|>|>\n|g;
 
 936   $zip->contents("content.xml", $new_contents);
 
 938   my $styles = $zip->contents("styles.xml");
 
 940     my $new_styles = $self->parse_block($styles);
 
 941     if (!defined($new_contents)) {
 
 942       $main::lxdebug->leave_sub();
 
 945     $zip->contents("styles.xml", $new_styles);
 
 948   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
 951   if ($form->{"format"} =~ /pdf/) {
 
 952     $res = $self->convert_to_pdf();
 
 955   $main::lxdebug->leave_sub();
 
 959 sub is_xvfb_running {
 
 960   $main::lxdebug->enter_sub();
 
 965   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
 968   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
 969   if ((-f $dfname) && open(IN, $dfname)) {
 
 974     my $xauthority = <IN>;
 
 978     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
 980     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
 981       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
 982       unlink($dfname, $xauthority);
 
 983       $main::lxdebug->leave_sub();
 
 988     if ($line !~ /xvfb/i) {
 
 989       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
 990       unlink($dfname, $xauthority);
 
 991       $main::lxdebug->leave_sub();
 
 995     $ENV{"XAUTHORITY"} = $xauthority;
 
 996     $ENV{"DISPLAY"} = $display;
 
 998     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1001   $main::lxdebug->leave_sub();
 
1007   $main::lxdebug->enter_sub();
 
1011   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1013   my $display = $self->is_xvfb_running();
 
1016     $main::lxdebug->leave_sub();
 
1021   while ( -f "/tmp/.X${display}-lock") {
 
1024   $display = ":${display}";
 
1025   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1027   my $mcookie = `mcookie`;
 
1028   die("Installation error: mcookie not found.") if ($? != 0);
 
1031   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1033   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1034   $ENV{"XAUTHORITY"} = $xauthority;
 
1036   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1038   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1040     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1041     $main::lxdebug->leave_sub();
 
1045   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1049     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1050     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1053   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1056   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1057   if (!open(OUT, ">$dfname")) {
 
1058     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1059     unlink($xauthority);
 
1061     $main::lxdebug->leave_sub();
 
1064   print(OUT "$pid\n$display\n$xauthority\n");
 
1067   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1069   if (!$self->is_xvfb_running()) {
 
1070     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1071     unlink($xauthority, $dfname);
 
1073     $main::lxdebug->leave_sub();
 
1077   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1079   $main::lxdebug->leave_sub();
 
1084 sub is_openoffice_running {
 
1085   $main::lxdebug->enter_sub();
 
1087   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1088          "> /dev/null 2> /dev/null");
 
1090   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1092   $main::lxdebug->leave_sub();
 
1097 sub spawn_openoffice {
 
1098   $main::lxdebug->enter_sub();
 
1102   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1104   my ($try, $spawned_oo, $res);
 
1107   for ($try = 0; $try < 15; $try++) {
 
1108     if ($self->is_openoffice_running()) {
 
1116         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1118         open(STDIN, '/dev/null');
 
1119         open(STDOUT, '>/dev/null');
 
1120         my $new_pid = fork();
 
1122         my $ssres = setsid();
 
1123         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1124         my @cmdline = ($main::openofficeorg_writer_bin,
 
1125                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1127                        "-accept=socket,host=localhost,port=" .
 
1128                        $main::openofficeorg_daemon_port . ";urp;");
 
1132       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1137     sleep($try >= 5 ? 2 : 1);
 
1141     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1142       "OpenOffice could not be started.";
 
1145   $main::lxdebug->leave_sub();
 
1150 sub convert_to_pdf {
 
1151   $main::lxdebug->enter_sub();
 
1155   my $form = $self->{"form"};
 
1157   my $filename = $form->{"tmpfile"};
 
1158   $filename =~ s/.odt$//;
 
1159   if (substr($filename, 0, 1) ne "/") {
 
1160     $filename = getcwd() . "/${filename}";
 
1163   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1164     $ENV{'HOME'} = $self->{"userspath"};
 
1166     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1169   if (!$self->spawn_xvfb()) {
 
1170     $main::lxdebug->leave_sub();
 
1175   if (!$main::openofficeorg_daemon) {
 
1176     @cmdline = ($main::openofficeorg_writer_bin,
 
1177                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1179                 "file:${filename}.odt",
 
1180                 "macro://" . (split('/', $filename))[-1] .
 
1181                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1183     if (!$self->spawn_openoffice()) {
 
1184       $main::lxdebug->leave_sub();
 
1188     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1189                 $main::openofficeorg_daemon_port,
 
1197     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1199     unlink($filename . ".odt");
 
1201     $main::lxdebug->leave_sub();
 
1206   unlink($filename . ".odt", $filename . ".pdf");
 
1207   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1210   $main::lxdebug->leave_sub();
 
1215   my ($self, $variable) = @_;
 
1216   my $form = $self->{"form"};
 
1217   my $iconv = $self->{"iconv"};
 
1220     ('order' => ['&', '<', '>', '"', "'",
 
1222                  quotemeta("\n"), quotemeta("\r")],
 
1228      '\x80'          => chr(0xa4), # Euro
 
1229      quotemeta("\n") => '<text:line-break/>',
 
1230      quotemeta("\r") => '',
 
1233   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1235   # Allow some HTML markup to be converted into the output format's
 
1236   # corresponding markup code, e.g. bold or italic.
 
1237   my $rnd = $self->{"rnd"};
 
1238   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1239                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1241   foreach my $key (keys(%markup_replace)) {
 
1242     my $value = $markup_replace{$key};
 
1243     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
 
1244     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1247   return $iconv->convert($variable);
 
1250 sub get_mime_type() {
 
1251   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1252     return "application/pdf";
 
1254     return "application/vnd.oasis.opendocument.text";
 
1258 sub uses_temp_file {