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     $form->{"cumulatelinetotal"}[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
 
 219     my $new_text = $self->parse_block($text, (@indices, $i));
 
 220     return undef unless (defined($new_text));
 
 221     $new_contents .= $start_tag . $new_text . $end_tag;
 
 223   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 225   return $new_contents;
 
 229   my ($self, $text, $pos, $var, $not) = @_;
 
 232   $pos = 0 unless ($pos);
 
 234   while ($pos < length($text)) {
 
 237     next if (substr($text, $pos - 1, 2) ne '<%');
 
 239     if ((substr($text, $pos + 1, 2) eq 'if') || (substr($text, $pos + 1, 3) eq 'for')) {
 
 242     } elsif ((substr($text, $pos + 1, 4) eq 'else') && (1 == $depth)) {
 
 244         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 248       my $block = substr($text, 0, $pos - 1);
 
 249       substr($text, 0, $pos - 1) = "";
 
 250       $text =~ s!^<\%[^\%]+\%>!!;
 
 251       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 253       return ($block, $text);
 
 255     } elsif (substr($text, $pos + 1, 3) eq 'end') {
 
 258         my $block = substr($text, 0, $pos - 1);
 
 259         substr($text, 0, $pos - 1) = "";
 
 260         $text =~ s!^<\%[^\%]+\%>!!;
 
 262         return ($block, $text);
 
 271   $main::lxdebug->enter_sub();
 
 273   my ($self, $contents, @indices) = @_;
 
 275   my $new_contents = "";
 
 277   while ($contents ne "") {
 
 278     my $pos_if = index($contents, '<%if');
 
 279     my $pos_foreach = index($contents, '<%foreach');
 
 281     if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 282       $new_contents .= $self->substitute_vars($contents, @indices);
 
 286     if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 287       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 288       substr($contents, 0, $pos_foreach) = "";
 
 290       if ($contents !~ m|^<\%foreach (.*?)\%>|) {
 
 291         $self->{"error"} = "Malformed <\%foreach\%>.";
 
 292         $main::lxdebug->leave_sub();
 
 298       substr($contents, 0, length($&)) = "";
 
 301       ($block, $contents) = $self->find_end($contents);
 
 303         $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 304         $main::lxdebug->leave_sub();
 
 308       my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 309       if (!defined($new_text)) {
 
 310         $main::lxdebug->leave_sub();
 
 313       $new_contents .= $new_text;
 
 316       $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 317       substr($contents, 0, $pos_if) = "";
 
 319       if ($contents !~ m|^<\%if\s*(not)?\s+(.*?)\%>|) {
 
 320         $self->{"error"} = "Malformed <\%if\%>.";
 
 321         $main::lxdebug->leave_sub();
 
 325       my ($not, $var) = ($1, $2);
 
 327       substr($contents, 0, length($&)) = "";
 
 329       ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 331         $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 332         $main::lxdebug->leave_sub();
 
 336       my $value = $self->{"form"}->{$var};
 
 337       for (my $i = 0; $i < scalar(@indices); $i++) {
 
 338         last unless (ref($value) eq "ARRAY");
 
 339         $value = $value->[$indices[$i]];
 
 342       if (($not && !$value) || (!$not && $value)) {
 
 343         my $new_text = $self->parse_block($block, @indices);
 
 344         if (!defined($new_text)) {
 
 345           $main::lxdebug->leave_sub();
 
 348         $new_contents .= $new_text;
 
 353   $main::lxdebug->leave_sub();
 
 355   return $new_contents;
 
 361   my $form = $self->{"form"};
 
 363   if (!open(IN, "$form->{templates}/$form->{IN}")) {
 
 364     $self->{"error"} = "$!";
 
 370   my $contents = join("", @_);
 
 372   # detect pagebreak block and its parameters
 
 373   if ($contents =~ /<%pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*%>(.*?)<%end(\s*pagebreak)?%>/s) {
 
 374     $self->{"chars_per_line"} = $1;
 
 375     $self->{"lines_on_first_page"} = $2;
 
 376     $self->{"lines_on_second_page"} = $3;
 
 377     $self->{"pagebreak_block"} = $4;
 
 379     substr($contents, length($`), length($&)) = "";
 
 382   $self->{"forced_pagebreaks"} = [];
 
 384   my $new_contents = $self->parse_block($contents);
 
 385   if (!defined($new_contents)) {
 
 386     $main::lxdebug->leave_sub();
 
 390   print(OUT $new_contents);
 
 392   if ($form->{"format"} =~ /postscript/i) {
 
 393     return $self->convert_to_postscript();
 
 394   } elsif ($form->{"format"} =~ /pdf/i) {
 
 395     return $self->convert_to_pdf();
 
 401 sub convert_to_postscript {
 
 403   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 405   # Convert the tex file to postscript
 
 407   if (!chdir("$userspath")) {
 
 408     $self->{"error"} = "chdir : $!";
 
 413   $form->{tmpfile} =~ s/$userspath\///g;
 
 415   for (my $run = 1; $run <= 2; $run++) {
 
 416     system("latex --interaction=nonstopmode $form->{tmpfile} " .
 
 417            "> $form->{tmpfile}.err");
 
 419       $self->{"error"} = $form->cleanup();
 
 425   $form->{tmpfile} =~ s/tex$/dvi/;
 
 427   system("dvips $form->{tmpfile} -o -q > /dev/null");
 
 429     $self->{"error"} = "dvips : $!";
 
 433   $form->{tmpfile} =~ s/dvi$/ps/;
 
 442   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 444   # Convert the tex file to PDF
 
 446   if (!chdir("$userspath")) {
 
 447     $self->{"error"} = "chdir : $!";
 
 452   $form->{tmpfile} =~ s/$userspath\///g;
 
 454   for (my $run = 1; $run <= 2; $run++) {
 
 455     system("pdflatex --interaction=nonstopmode $form->{tmpfile} " .
 
 456            "> $form->{tmpfile}.err");
 
 458       $self->{"error"} = $form->cleanup();
 
 464   $form->{tmpfile} =~ s/tex$/pdf/;
 
 469 sub get_mime_type() {
 
 472   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 473     return "application/postscript";
 
 475     return "application/pdf";
 
 488 package HTMLTemplate;
 
 492 @ISA = qw(LaTeXTemplate);
 
 497   return $type->SUPER::new(@_);
 
 501   my ($self, $variable) = @_;
 
 502   my $form = $self->{"form"};
 
 505     ('order' => ['<', '>', quotemeta("\n")],
 
 508      quotemeta("\n") => '<br>',
 
 511   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
 513   # Allow some HTML markup to be converted into the output format's
 
 514   # corresponding markup code, e.g. bold or italic.
 
 515   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
 517   foreach my $key (@markup_replace) {
 
 518     $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
 
 524 sub get_mime_type() {
 
 527   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 528     return "application/postscript";
 
 529   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 530     return "application/pdf";
 
 539   if ($self->{"form"}->{"format"} =~ /postscript/i) {
 
 541   } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
 
 548 sub convert_to_postscript {
 
 550   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 552   # Convert the HTML file to postscript
 
 554   if (!chdir("$userspath")) {
 
 555     $self->{"error"} = "chdir : $!";
 
 560   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 561   my $psfile = $form->{"tmpfile"};
 
 562   $psfile =~ s/.html/.ps/;
 
 563   if ($psfile eq $form->{"tmpfile"}) {
 
 567   system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
 
 569     $self->{"error"} = $form->cleanup();
 
 574   $form->{"tmpfile"} = $psfile;
 
 583   my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
 
 585   # Convert the HTML file to PDF
 
 587   if (!chdir("$userspath")) {
 
 588     $self->{"error"} = "chdir : $!";
 
 593   $form->{"tmpfile"} =~ s/$userspath\///g;
 
 594   my $pdffile = $form->{"tmpfile"};
 
 595   $pdffile =~ s/.html/.pdf/;
 
 596   if ($pdffile eq $form->{"tmpfile"}) {
 
 600   system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
 
 602     $self->{"error"} = $form->cleanup();
 
 607   $form->{"tmpfile"} = $pdffile;
 
 616 #### OpenDocumentTemplate
 
 619 package OpenDocumentTemplate;
 
 627 # use File::Temp qw(:mktemp);
 
 630 @ISA = qw(SimpleTemplate);
 
 635   $self = $type->SUPER::new(@_);
 
 637   foreach my $module (qw(Archive::Zip Text::Iconv)) {
 
 638     eval("use ${module};");
 
 640       $self->{"form"}->error("The Perl module '${module}' could not be " .
 
 641                              "loaded. Support for OpenDocument templates " .
 
 642                              "does not work without it. Please install your " .
 
 643                              "distribution's package or get the module from " .
 
 644                              "CPAN ( http://www.cpan.org ).");
 
 648   $self->{"rnd"} = int(rand(1000000));
 
 649   $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
 
 654 sub substitute_vars {
 
 655   my ($self, $text, @indices) = @_;
 
 657   my $form = $self->{"form"};
 
 659   while ($text =~ /\<\%(.*?)\%\>/) {
 
 660     my $value = $form->{$1};
 
 662     for (my $i = 0; $i < scalar(@indices); $i++) {
 
 663       last unless (ref($value) eq "ARRAY");
 
 664       $value = $value->[$indices[$i]];
 
 666     substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
 
 673   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
 675   my ($form, $new_contents) = ($self->{"form"}, "");
 
 677   my $ary = $form->{$var};
 
 678   for (my $i = 0; $i < scalar(@indices); $i++) {
 
 679     last unless (ref($ary) eq "ARRAY");
 
 680     $ary = $ary->[$indices[$i]];
 
 683   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
 684     $form->{"__first__"} = $i == 0;
 
 685     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
 686     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
 687     $form->{"__counter__"} = $i + 1;
 
 688     my $new_text = $self->parse_block($text, (@indices, $i));
 
 689     return undef unless (defined($new_text));
 
 690     $new_contents .= $start_tag . $new_text . $end_tag;
 
 692   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
 694   return $new_contents;
 
 698   my ($self, $text, $pos, $var, $not) = @_;
 
 701   $pos = 0 unless ($pos);
 
 703   while ($pos < length($text)) {
 
 706     next if (substr($text, $pos - 1, 5) ne '<%');
 
 708     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
 711     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
 713         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
 717       my $block = substr($text, 0, $pos - 1);
 
 718       substr($text, 0, $pos - 1) = "";
 
 719       $text =~ s!^\<\%[^\%]+\%\>!!;
 
 720       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
 722       return ($block, $text);
 
 724     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
 727         my $block = substr($text, 0, $pos - 1);
 
 728         substr($text, 0, $pos - 1) = "";
 
 729         $text =~ s!^\<\%[^\%]+\%\>!!;
 
 731         return ($block, $text);
 
 740   $main::lxdebug->enter_sub();
 
 742   my ($self, $contents, @indices) = @_;
 
 744   my $new_contents = "";
 
 746   while ($contents ne "") {
 
 747     if (substr($contents, 0, 1) eq "<") {
 
 748       $contents =~ m|^<[^>]+>|;
 
 750       substr($contents, 0, length($&)) = "";
 
 752       if ($tag =~ m|<table:table-row|) {
 
 753         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 756         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 758         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 761           substr($table_row, length($`), length($&)) = "";
 
 763           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 765             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 766             $main::lxdebug->leave_sub();
 
 770           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 771           if (!defined($new_text)) {
 
 772             $main::lxdebug->leave_sub();
 
 775           $new_contents .= $new_text;
 
 778           my $new_text = $self->parse_block($table_row, @indices);
 
 779           if (!defined($new_text)) {
 
 780             $main::lxdebug->leave_sub();
 
 783           $new_contents .= $tag . $new_text . $end_tag;
 
 787         $new_contents .= $tag;
 
 791       $contents =~ /^[^<]+/;
 
 794       my $pos_if = index($text, '<%if');
 
 795       my $pos_foreach = index($text, '<%foreach');
 
 797       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 798         substr($contents, 0, length($text)) = "";
 
 799         $new_contents .= $self->substitute_vars($text, @indices);
 
 803       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 804         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 805         substr($contents, 0, $pos_foreach) = "";
 
 807         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 808           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 809           $main::lxdebug->leave_sub();
 
 815         substr($contents, 0, length($&)) = "";
 
 818         ($block, $contents) = $self->find_end($contents);
 
 820           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 821           $main::lxdebug->leave_sub();
 
 825         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 826         if (!defined($new_text)) {
 
 827           $main::lxdebug->leave_sub();
 
 830         $new_contents .= $new_text;
 
 833         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
 
 834         substr($contents, 0, $pos_if) = "";
 
 836         if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
 
 837           $self->{"error"} = "Malformed <\%if\%>.";
 
 838           $main::lxdebug->leave_sub();
 
 842         my ($not, $var) = ($1, $2);
 
 844         substr($contents, 0, length($&)) = "";
 
 846         ($block, $contents) = $self->find_end($contents, 0, $var, $not);
 
 848           $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
 
 849           $main::lxdebug->leave_sub();
 
 853         my $value = $self->{"form"}->{$var};
 
 854         for (my $i = 0; $i < scalar(@indices); $i++) {
 
 855           last unless (ref($value) eq "ARRAY");
 
 856           $value = $value->[$indices[$i]];
 
 859         if (($not && !$value) || (!$not && $value)) {
 
 860           my $new_text = $self->parse_block($block, @indices);
 
 861           if (!defined($new_text)) {
 
 862             $main::lxdebug->leave_sub();
 
 865           $new_contents .= $new_text;
 
 871   $main::lxdebug->leave_sub();
 
 873   return $new_contents;
 
 877   $main::lxdebug->enter_sub();
 
 881   my $form = $self->{"form"};
 
 886   if ($form->{"IN"} =~ m|^/|) {
 
 887     $file_name = $form->{"IN"};
 
 889     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
 892   my $zip = Archive::Zip->new();
 
 893   if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
 
 894     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
 895     $main::lxdebug->leave_sub();
 
 899   my $contents = $zip->contents("content.xml");
 
 901     $self->{"error"} = "File is not a OpenDocument file.";
 
 902     $main::lxdebug->leave_sub();
 
 906   my $rnd = $self->{"rnd"};
 
 907   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
 908 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
 910 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
 911 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
 913 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
 914 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
 916 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
 917 <style:text-properties style:text-line-through-style="solid"/>
 
 919 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
 920 <style:text-properties style:text-position="super 58%"/>
 
 922 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
 923 <style:text-properties style:text-position="sub 58%"/>
 
 927   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
 928   $contents =~ s|[\n\r]||gm;
 
 930   my $new_contents = $self->parse_block($contents);
 
 931   if (!defined($new_contents)) {
 
 932     $main::lxdebug->leave_sub();
 
 936 #   $new_contents =~ s|>|>\n|g;
 
 938   $zip->contents("content.xml", $new_contents);
 
 940   my $styles = $zip->contents("styles.xml");
 
 942     my $new_styles = $self->parse_block($styles);
 
 943     if (!defined($new_contents)) {
 
 944       $main::lxdebug->leave_sub();
 
 947     $zip->contents("styles.xml", $new_styles);
 
 950   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
 953   if ($form->{"format"} =~ /pdf/) {
 
 954     $res = $self->convert_to_pdf();
 
 957   $main::lxdebug->leave_sub();
 
 961 sub is_xvfb_running {
 
 962   $main::lxdebug->enter_sub();
 
 967   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
 970   $main::lxdebug->message(LXDebug::DEBUG2, "    Looking for $dfname\n");
 
 971   if ((-f $dfname) && open(IN, $dfname)) {
 
 976     my $xauthority = <IN>;
 
 980     $main::lxdebug->message(LXDebug::DEBUG2, "      found with $pid and $display\n");
 
 982     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
 983       $main::lxdebug->message(LXDebug::DEBUG2, "  no/wrong process #1\n");
 
 984       unlink($dfname, $xauthority);
 
 985       $main::lxdebug->leave_sub();
 
 990     if ($line !~ /xvfb/i) {
 
 991       $main::lxdebug->message(LXDebug::DEBUG2, "      no/wrong process #2\n");
 
 992       unlink($dfname, $xauthority);
 
 993       $main::lxdebug->leave_sub();
 
 997     $ENV{"XAUTHORITY"} = $xauthority;
 
 998     $ENV{"DISPLAY"} = $display;
 
1000     $main::lxdebug->message(LXDebug::DEBUG2, "      not found\n");
 
1003   $main::lxdebug->leave_sub();
 
1009   $main::lxdebug->enter_sub();
 
1013   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
 
1015   my $display = $self->is_xvfb_running();
 
1018     $main::lxdebug->leave_sub();
 
1023   while ( -f "/tmp/.X${display}-lock") {
 
1026   $display = ":${display}";
 
1027   $main::lxdebug->message(LXDebug::DEBUG2, "  display $display\n");
 
1029   my $mcookie = `mcookie`;
 
1030   die("Installation error: mcookie not found.") if ($? != 0);
 
1033   $main::lxdebug->message(LXDebug::DEBUG2, "  mcookie $mcookie\n");
 
1035   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
1036   $ENV{"XAUTHORITY"} = $xauthority;
 
1038   $main::lxdebug->message(LXDebug::DEBUG2, "  xauthority $xauthority\n");
 
1040   system("xauth add \"${display}\" . \"${mcookie}\"");
 
1042     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
1043     $main::lxdebug->leave_sub();
 
1047   $main::lxdebug->message(LXDebug::DEBUG2, "  about to fork()\n");
 
1051     $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1052     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
1055   $main::lxdebug->message(LXDebug::DEBUG2, "  parent dont sleeping\n");
 
1058   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
1059   if (!open(OUT, ">$dfname")) {
 
1060     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
1061     unlink($xauthority);
 
1063     $main::lxdebug->leave_sub();
 
1066   print(OUT "$pid\n$display\n$xauthority\n");
 
1069   $main::lxdebug->message(LXDebug::DEBUG2, "  parent re-testing\n");
 
1071   if (!$self->is_xvfb_running()) {
 
1072     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
1073     unlink($xauthority, $dfname);
 
1075     $main::lxdebug->leave_sub();
 
1079   $main::lxdebug->message(LXDebug::DEBUG2, "  spawn OK\n");
 
1081   $main::lxdebug->leave_sub();
 
1086 sub is_openoffice_running {
 
1087   $main::lxdebug->enter_sub();
 
1089   system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
 
1090          "> /dev/null 2> /dev/null");
 
1092   $main::lxdebug->message(LXDebug::DEBUG2, "  is_openoffice_running(): $?\n");
 
1094   $main::lxdebug->leave_sub();
 
1099 sub spawn_openoffice {
 
1100   $main::lxdebug->enter_sub();
 
1104   $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
 
1106   my ($try, $spawned_oo, $res);
 
1109   for ($try = 0; $try < 15; $try++) {
 
1110     if ($self->is_openoffice_running()) {
 
1118         $main::lxdebug->message(LXDebug::DEBUG2, "  Child daemonizing\n");
 
1120         open(STDIN, '/dev/null');
 
1121         open(STDOUT, '>/dev/null');
 
1122         my $new_pid = fork();
 
1124         my $ssres = setsid();
 
1125         $main::lxdebug->message(LXDebug::DEBUG2, "  Child execing\n");
 
1126         my @cmdline = ($main::openofficeorg_writer_bin,
 
1127                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1129                        "-accept=socket,host=localhost,port=" .
 
1130                        $main::openofficeorg_daemon_port . ";urp;");
 
1134       $main::lxdebug->message(LXDebug::DEBUG2, "  Parent after fork\n");
 
1139     sleep($try >= 5 ? 2 : 1);
 
1143     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
1144       "OpenOffice could not be started.";
 
1147   $main::lxdebug->leave_sub();
 
1152 sub convert_to_pdf {
 
1153   $main::lxdebug->enter_sub();
 
1157   my $form = $self->{"form"};
 
1159   my $filename = $form->{"tmpfile"};
 
1160   $filename =~ s/.odt$//;
 
1161   if (substr($filename, 0, 1) ne "/") {
 
1162     $filename = getcwd() . "/${filename}";
 
1165   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
1166     $ENV{'HOME'} = $self->{"userspath"};
 
1168     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
1171   if (!$self->spawn_xvfb()) {
 
1172     $main::lxdebug->leave_sub();
 
1177   if (!$main::openofficeorg_daemon) {
 
1178     @cmdline = ($main::openofficeorg_writer_bin,
 
1179                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
1181                 "file:${filename}.odt",
 
1182                 "macro://" . (split('/', $filename))[-1] .
 
1183                 "/Standard.Conversion.ConvertSelfToPDF()");
 
1185     if (!$self->spawn_openoffice()) {
 
1186       $main::lxdebug->leave_sub();
 
1190     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
1191                 $main::openofficeorg_daemon_port,
 
1199     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
1201     unlink($filename . ".odt");
 
1203     $main::lxdebug->leave_sub();
 
1208   unlink($filename . ".odt", $filename . ".pdf");
 
1209   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
1212   $main::lxdebug->leave_sub();
 
1217   my ($self, $variable) = @_;
 
1218   my $form = $self->{"form"};
 
1219   my $iconv = $self->{"iconv"};
 
1222     ('order' => ['&', '<', '>', '"', "'",
 
1224                  quotemeta("\n"), quotemeta("\r")],
 
1230      '\x80'          => chr(0xa4), # Euro
 
1231      quotemeta("\n") => '<text:line-break/>',
 
1232      quotemeta("\r") => '',
 
1235   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1237   # Allow some HTML markup to be converted into the output format's
 
1238   # corresponding markup code, e.g. bold or italic.
 
1239   my $rnd = $self->{"rnd"};
 
1240   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
1241                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
1243   foreach my $key (keys(%markup_replace)) {
 
1244     my $value = $markup_replace{$key};
 
1245     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi;
 
1246     $variable =~ s|\</${key}\>|</text:span>|gi;
 
1249   return $iconv->convert($variable);
 
1252 sub get_mime_type() {
 
1253   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
1254     return "application/pdf";
 
1256     return "application/vnd.oasis.opendocument.text";
 
1260 sub uses_temp_file {
 
1265 ##########################################################
 
1269 ##########################################################
 
1271 package XMLTemplate; 
 
1275 @ISA = qw(HTMLTemplate);
 
1278   #evtl auskommentieren
 
1281   return $type->SUPER::new(@_);
 
1285   my ($self, $variable) = @_;
 
1286   my $form = $self->{"form"};
 
1289     ('order' => ['<', '>', quotemeta("\n")],
 
1292      quotemeta("\n") => '<br>',
 
1295   map({ $variable =~ s/$_/$replace{$_}/g; } @{ $replace{"order"} });
 
1297   # Allow no markup to be converted into the output format
 
1298   my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
 
1300   foreach my $key (@markup_replace) {
 
1301     $variable =~ s/\<(\/?)${key}\>//g;
 
1307 sub get_mime_type() {
 
1310   if ($self->{"form"}->{"format"} =~ /xml/i) {
 
1311     return "application/xml ";
 
1317 sub uses_temp_file {
 
1319   # no tempfile needet for XML Output