1 package SL::Template::OpenDocument;
 
   3 use parent qw(SL::Template::Simple);
 
  13 # use File::Temp qw(:mktemp);
 
  21   my $self = $type->SUPER::new(@_);
 
  23   $self->{"rnd"}   = int(rand(1000000));
 
  24   $self->{"iconv"} = SL::Iconv->new($main::dbcharset, "UTF-8");
 
  26   $self->set_tag_style('<%', '%>');
 
  27   $self->{quot_re} = '"';
 
  33   my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
 
  35   my ($form, $new_contents) = ($self->{"form"}, "");
 
  37   my $ary = $self->_get_loop_variable($var, 1, @indices);
 
  39   for (my $i = 0; $i < scalar(@{$ary}); $i++) {
 
  40     $form->{"__first__"} = $i == 0;
 
  41     $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
 
  42     $form->{"__odd__"} = (($i + 1) % 2) == 1;
 
  43     $form->{"__counter__"} = $i + 1;
 
  44     my $new_text = $self->parse_block($text, (@indices, $i));
 
  45     return undef unless (defined($new_text));
 
  46     $new_contents .= $start_tag . $new_text . $end_tag;
 
  48   map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
 
  54   my ($self, $text, $pos, $var, $not) = @_;
 
  57   $pos = 0 unless ($pos);
 
  59   while ($pos < length($text)) {
 
  62     next if (substr($text, $pos - 1, 5) ne '<%');
 
  64     if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
 
  67     } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
 
  69         $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
 
  73       my $block = substr($text, 0, $pos - 1);
 
  74       substr($text, 0, $pos - 1) = "";
 
  75       $text =~ s!^\<\%[^\%]+\%\>!!;
 
  76       $text = '<%if' . ($not ?  " " : "not ") . $var . '%>' . $text;
 
  78       return ($block, $text);
 
  80     } elsif (substr($text, $pos + 4, 3) eq 'end') {
 
  83         my $block = substr($text, 0, $pos - 1);
 
  84         substr($text, 0, $pos - 1) = "";
 
  85         $text =~ s!^\<\%[^\%]+\%\>!!;
 
  87         return ($block, $text);
 
  96   $main::lxdebug->enter_sub();
 
  98   my ($self, $contents, @indices) = @_;
 
 100   my $new_contents = "";
 
 102   while ($contents ne "") {
 
 103     if (substr($contents, 0, 1) eq "<") {
 
 104       $contents =~ m|^<[^>]+>|;
 
 106       substr($contents, 0, length($&)) = "";
 
 108       if ($tag =~ m|<table:table-row|) {
 
 109         $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
 
 112         substr($contents, 0, length($1) + length($end_tag)) = "";
 
 114         if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
 
 117           substr($table_row, length($`), length($&)) = "";
 
 119           my ($t1, $t2) = $self->find_end($table_row, length($`));
 
 121             $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
 
 122             $main::lxdebug->leave_sub();
 
 126           my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
 
 127           if (!defined($new_text)) {
 
 128             $main::lxdebug->leave_sub();
 
 131           $new_contents .= $new_text;
 
 134           my $new_text = $self->parse_block($table_row, @indices);
 
 135           if (!defined($new_text)) {
 
 136             $main::lxdebug->leave_sub();
 
 139           $new_contents .= $tag . $new_text . $end_tag;
 
 143         $new_contents .= $tag;
 
 147       $contents =~ /^[^<]+/;
 
 150       my $pos_if = index($text, '<%if');
 
 151       my $pos_foreach = index($text, '<%foreach');
 
 153       if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
 
 154         substr($contents, 0, length($text)) = "";
 
 155         $new_contents .= $self->substitute_vars($text, @indices);
 
 159       if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
 
 160         $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
 
 161         substr($contents, 0, $pos_foreach) = "";
 
 163         if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
 
 164           $self->{"error"} = "Malformed <\%foreach\%>.";
 
 165           $main::lxdebug->leave_sub();
 
 171         substr($contents, 0, length($&)) = "";
 
 174         ($block, $contents) = $self->find_end($contents);
 
 176           $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
 
 177           $main::lxdebug->leave_sub();
 
 181         my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
 
 182         if (!defined($new_text)) {
 
 183           $main::lxdebug->leave_sub();
 
 186         $new_contents .= $new_text;
 
 189         if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
 
 190           $main::lxdebug->leave_sub();
 
 197   $main::lxdebug->leave_sub();
 
 199   return $new_contents;
 
 203   $main::lxdebug->enter_sub();
 
 206   my $form = $self->{"form"};
 
 211   if ($form->{"IN"} =~ m|^/|) {
 
 212     $file_name = $form->{"IN"};
 
 214     $file_name = $form->{"templates"} . "/" . $form->{"IN"};
 
 217   my $zip = Archive::Zip->new();
 
 218   if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
 
 219     $self->{"error"} = "File not found/is not a OpenDocument file.";
 
 220     $main::lxdebug->leave_sub();
 
 224   my $contents = $zip->contents("content.xml");
 
 226     $self->{"error"} = "File is not a OpenDocument file.";
 
 227     $main::lxdebug->leave_sub();
 
 231   my $rnd = $self->{"rnd"};
 
 232   my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
 
 233 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
 
 235 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
 
 236 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
 
 238 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
 
 239 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
 
 241 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
 
 242 <style:text-properties style:text-line-through-style="solid"/>
 
 244 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
 
 245 <style:text-properties style:text-position="super 58%"/>
 
 247 <style:style style:name="TLXO${rnd}SUB" style:family="text">
 
 248 <style:text-properties style:text-position="sub 58%"/>
 
 252   $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
 
 253   $contents =~ s|[\n\r]||gm;
 
 255   my $new_contents = $self->parse_block($contents);
 
 256   if (!defined($new_contents)) {
 
 257     $main::lxdebug->leave_sub();
 
 261 #   $new_contents =~ s|>|>\n|g;
 
 263   $zip->contents("content.xml", $new_contents);
 
 265   my $styles = $zip->contents("styles.xml");
 
 267     my $new_styles = $self->parse_block($styles);
 
 268     if (!defined($new_contents)) {
 
 269       $main::lxdebug->leave_sub();
 
 272     $zip->contents("styles.xml", $new_styles);
 
 275   $zip->writeToFileNamed($form->{"tmpfile"}, 1);
 
 278   if ($form->{"format"} =~ /pdf/) {
 
 279     $res = $self->convert_to_pdf();
 
 282   $main::lxdebug->leave_sub();
 
 286 sub is_xvfb_running {
 
 287   $main::lxdebug->enter_sub();
 
 292   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
 295   $main::lxdebug->message(LXDebug->DEBUG2(), "    Looking for $dfname\n");
 
 296   if ((-f $dfname) && open(IN, $dfname)) {
 
 301     my $xauthority = <IN>;
 
 305     $main::lxdebug->message(LXDebug->DEBUG2(), "      found with $pid and $display\n");
 
 307     if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
 
 308       $main::lxdebug->message(LXDebug->DEBUG2(), "  no/wrong process #1\n");
 
 309       unlink($dfname, $xauthority);
 
 310       $main::lxdebug->leave_sub();
 
 315     if ($line !~ /xvfb/i) {
 
 316       $main::lxdebug->message(LXDebug->DEBUG2(), "      no/wrong process #2\n");
 
 317       unlink($dfname, $xauthority);
 
 318       $main::lxdebug->leave_sub();
 
 322     $ENV{"XAUTHORITY"} = $xauthority;
 
 323     $ENV{"DISPLAY"} = $display;
 
 325     $main::lxdebug->message(LXDebug->DEBUG2(), "      not found\n");
 
 328   $main::lxdebug->leave_sub();
 
 334   $main::lxdebug->enter_sub();
 
 338   $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
 
 340   my $display = $self->is_xvfb_running();
 
 343     $main::lxdebug->leave_sub();
 
 348   while ( -f "/tmp/.X${display}-lock") {
 
 351   $display = ":${display}";
 
 352   $main::lxdebug->message(LXDebug->DEBUG2(), "  display $display\n");
 
 354   my $mcookie = `mcookie`;
 
 355   die("Installation error: mcookie not found.") if ($? != 0);
 
 358   $main::lxdebug->message(LXDebug->DEBUG2(), "  mcookie $mcookie\n");
 
 360   my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
 
 361   $ENV{"XAUTHORITY"} = $xauthority;
 
 363   $main::lxdebug->message(LXDebug->DEBUG2(), "  xauthority $xauthority\n");
 
 365   system("xauth add \"${display}\" . \"${mcookie}\"");
 
 367     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
 
 368     $main::lxdebug->leave_sub();
 
 372   $main::lxdebug->message(LXDebug->DEBUG2(), "  about to fork()\n");
 
 376     $main::lxdebug->message(LXDebug->DEBUG2(), "  Child execing\n");
 
 377     exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
 
 380   $main::lxdebug->message(LXDebug->DEBUG2(), "  parent dont sleeping\n");
 
 383   my $dfname = $self->{"userspath"} . "/xvfb_display";
 
 384   if (!open(OUT, ">$dfname")) {
 
 385     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
 
 388     $main::lxdebug->leave_sub();
 
 391   print(OUT "$pid\n$display\n$xauthority\n");
 
 394   $main::lxdebug->message(LXDebug->DEBUG2(), "  parent re-testing\n");
 
 396   if (!$self->is_xvfb_running()) {
 
 397     $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
 
 398     unlink($xauthority, $dfname);
 
 400     $main::lxdebug->leave_sub();
 
 404   $main::lxdebug->message(LXDebug->DEBUG2(), "  spawn OK\n");
 
 406   $main::lxdebug->leave_sub();
 
 411 sub is_openoffice_running {
 
 412   $main::lxdebug->enter_sub();
 
 414   my $output = `./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port 2> /dev/null`;
 
 417   my $res = ($? == 0) || $output;
 
 418   $main::lxdebug->message(LXDebug->DEBUG2(), "  is_openoffice_running(): res $res\n");
 
 420   $main::lxdebug->leave_sub();
 
 425 sub spawn_openoffice {
 
 426   $main::lxdebug->enter_sub();
 
 430   $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
 
 432   my ($try, $spawned_oo, $res);
 
 435   for ($try = 0; $try < 15; $try++) {
 
 436     if ($self->is_openoffice_running()) {
 
 444         $main::lxdebug->message(LXDebug->DEBUG2(), "  Child daemonizing\n");
 
 446         open(STDIN, '/dev/null');
 
 447         open(STDOUT, '>/dev/null');
 
 448         my $new_pid = fork();
 
 450         my $ssres = setsid();
 
 451         $main::lxdebug->message(LXDebug->DEBUG2(), "  Child execing\n");
 
 452         my @cmdline = ($main::openofficeorg_writer_bin,
 
 453                        "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
 455                        "-accept=socket,host=localhost,port=" .
 
 456                        $main::openofficeorg_daemon_port . ";urp;");
 
 460       $main::lxdebug->message(LXDebug->DEBUG2(), "  Parent after fork\n");
 
 465     sleep($try >= 5 ? 2 : 1);
 
 469     $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
 
 470       "OpenOffice could not be started.";
 
 473   $main::lxdebug->leave_sub();
 
 479   $main::lxdebug->enter_sub();
 
 483   my $form = $self->{"form"};
 
 485   my $filename = $form->{"tmpfile"};
 
 486   $filename =~ s/.odt$//;
 
 487   if (substr($filename, 0, 1) ne "/") {
 
 488     $filename = getcwd() . "/${filename}";
 
 491   if (substr($self->{"userspath"}, 0, 1) eq "/") {
 
 492     $ENV{'HOME'} = $self->{"userspath"};
 
 494     $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
 
 497   if (!$self->spawn_xvfb()) {
 
 498     $main::lxdebug->leave_sub();
 
 503   if (!$main::openofficeorg_daemon) {
 
 504     @cmdline = ($main::openofficeorg_writer_bin,
 
 505                 "-minimized", "-norestore", "-nologo", "-nolockcheck",
 
 507                 "file:${filename}.odt",
 
 508                 "macro://" . (split('/', $filename))[-1] .
 
 509                 "/Standard.Conversion.ConvertSelfToPDF()");
 
 511     if (!$self->spawn_openoffice()) {
 
 512       $main::lxdebug->leave_sub();
 
 516     @cmdline = ("./scripts/oo-uno-convert-pdf.py",
 
 517                 $main::openofficeorg_daemon_port,
 
 524   if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
 
 525     $form->{"tmpfile"} =~ s/odt$/pdf/;
 
 527     unlink($filename . ".odt");
 
 529     $main::lxdebug->leave_sub();
 
 534   unlink($filename . ".odt", $filename . ".pdf");
 
 535   $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
 
 538   $main::lxdebug->leave_sub();
 
 543   my ($self, $variable) = @_;
 
 544   my $form = $self->{"form"};
 
 545   my $iconv = $self->{"iconv"};
 
 547   $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
 
 549   # Allow some HTML markup to be converted into the output format's
 
 550   # corresponding markup code, e.g. bold or italic.
 
 551   my $rnd = $self->{"rnd"};
 
 552   my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
 
 553                         "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
 
 555   foreach my $key (keys(%markup_replace)) {
 
 556     my $value = $markup_replace{$key};
 
 557     $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
 
 558     $variable =~ s|\</${key}\>|</text:span>|gi;
 
 561   return $iconv->convert($variable);
 
 564 sub get_mime_type() {
 
 567   if ($self->{"form"}->{"format"} =~ /pdf/) {
 
 568     return "application/pdf";
 
 570     return "application/vnd.oasis.opendocument.text";