1 package SL::Template::OpenDocument;
3 use parent qw(SL::Template::Simple);
14 # use File::Temp qw(:mktemp);
22 my $self = $type->SUPER::new(@_);
24 $self->{"rnd"} = int(rand(1000000));
25 $self->{"iconv"} = SL::Iconv->new($::lx_office_conf{system}->{dbcharset}, "UTF-8");
27 $self->set_tag_style('<%', '%>');
28 $self->{quot_re} = '"';
34 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
36 my ($form, $new_contents) = ($self->{"form"}, "");
38 my $ary = $self->_get_loop_variable($var, 1, @indices);
40 for (my $i = 0; $i < scalar(@{$ary || []}); $i++) {
41 $form->{"__first__"} = $i == 0;
42 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
43 $form->{"__odd__"} = (($i + 1) % 2) == 1;
44 $form->{"__counter__"} = $i + 1;
45 my $new_text = $self->parse_block($text, (@indices, $i));
46 return undef unless (defined($new_text));
47 $new_contents .= $start_tag . $new_text . $end_tag;
49 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
55 my ($self, $text, $pos, $var, $not) = @_;
58 $pos = 0 unless ($pos);
60 while ($pos < length($text)) {
63 next if (substr($text, $pos - 1, 5) ne '<%');
65 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
68 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
70 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
74 my $block = substr($text, 0, $pos - 1);
75 substr($text, 0, $pos - 1) = "";
76 $text =~ s!^\<\%[^\%]+\%\>!!;
77 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
79 return ($block, $text);
81 } elsif (substr($text, $pos + 4, 3) eq 'end') {
84 my $block = substr($text, 0, $pos - 1);
85 substr($text, 0, $pos - 1) = "";
86 $text =~ s!^\<\%[^\%]+\%\>!!;
88 return ($block, $text);
97 $main::lxdebug->enter_sub();
99 my ($self, $contents, @indices) = @_;
101 my $new_contents = "";
103 while ($contents ne "") {
104 if (substr($contents, 0, 1) eq "<") {
105 $contents =~ m|^<[^>]+>|;
107 substr($contents, 0, length($&)) = "";
109 if ($tag =~ m|<table:table-row|) {
110 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
114 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
117 $contents =~ m|\<\%foreachrow\s+.*?\%\>|;
118 substr($contents, length($`), length($&)) = "";
120 ($table_row, $contents) = $self->find_end($contents, length($`));
122 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
123 $main::lxdebug->leave_sub();
127 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
131 substr $contents, 0, length($&), '';
133 my $new_text = $self->parse_foreach($var, $table_row, $tag, $end_tag, @indices);
134 if (!defined($new_text)) {
135 $main::lxdebug->leave_sub();
138 $new_contents .= $new_text;
141 substr($contents, 0, length($table_row) + length($end_tag)) = "";
142 my $new_text = $self->parse_block($table_row, @indices);
143 if (!defined($new_text)) {
144 $main::lxdebug->leave_sub();
147 $new_contents .= $tag . $new_text . $end_tag;
151 $new_contents .= $tag;
155 $contents =~ /^[^<]+/;
158 my $pos_if = index($text, '<%if');
159 my $pos_foreach = index($text, '<%foreach');
161 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
162 substr($contents, 0, length($text)) = "";
163 $new_contents .= $self->substitute_vars($text, @indices);
167 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
168 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
169 substr($contents, 0, $pos_foreach) = "";
171 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
172 $self->{"error"} = "Malformed <\%foreach\%>.";
173 $main::lxdebug->leave_sub();
179 substr($contents, 0, length($&)) = "";
182 ($block, $contents) = $self->find_end($contents);
184 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
185 $main::lxdebug->leave_sub();
189 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
190 if (!defined($new_text)) {
191 $main::lxdebug->leave_sub();
194 $new_contents .= $new_text;
197 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
198 $main::lxdebug->leave_sub();
205 $main::lxdebug->leave_sub();
207 return $new_contents;
211 $main::lxdebug->enter_sub();
214 my $form = $self->{"form"};
219 if ($form->{"IN"} =~ m|^/|) {
220 $file_name = $form->{"IN"};
222 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
225 my $zip = Archive::Zip->new();
226 if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
227 $self->{"error"} = "File not found/is not a OpenDocument file.";
228 $main::lxdebug->leave_sub();
232 my $contents = Encode::decode('utf-8-strict', $zip->contents("content.xml"));
234 $self->{"error"} = "File is not a OpenDocument file.";
235 $main::lxdebug->leave_sub();
239 my $rnd = $self->{"rnd"};
240 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
241 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
243 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
244 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
246 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
247 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
249 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
250 <style:text-properties style:text-line-through-style="solid"/>
252 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
253 <style:text-properties style:text-position="super 58%"/>
255 <style:style style:name="TLXO${rnd}SUB" style:family="text">
256 <style:text-properties style:text-position="sub 58%"/>
260 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
261 $contents =~ s|[\n\r]||gm;
263 my $new_contents = $self->parse_block($contents);
264 if (!defined($new_contents)) {
265 $main::lxdebug->leave_sub();
269 # $new_contents =~ s|>|>\n|g;
271 $zip->contents("content.xml", Encode::encode('utf-8-strict', $new_contents));
273 my $styles = Encode::decode('utf-8-strict', $zip->contents("styles.xml"));
275 my $new_styles = $self->parse_block($styles);
276 if (!defined($new_contents)) {
277 $main::lxdebug->leave_sub();
280 $zip->contents("styles.xml", Encode::encode('utf-8-strict', $new_styles));
283 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
286 if ($form->{"format"} =~ /pdf/) {
287 $res = $self->convert_to_pdf();
290 $main::lxdebug->leave_sub();
294 sub is_xvfb_running {
295 $main::lxdebug->enter_sub();
300 my $dfname = $self->{"userspath"} . "/xvfb_display";
303 $main::lxdebug->message(LXDebug->DEBUG2(), " Looking for $dfname\n");
304 if ((-f $dfname) && open(IN, $dfname)) {
309 my $xauthority = <IN>;
313 $main::lxdebug->message(LXDebug->DEBUG2(), " found with $pid and $display\n");
315 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
316 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #1\n");
317 unlink($dfname, $xauthority);
318 $main::lxdebug->leave_sub();
323 if ($line !~ /xvfb/i) {
324 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #2\n");
325 unlink($dfname, $xauthority);
326 $main::lxdebug->leave_sub();
330 $ENV{"XAUTHORITY"} = $xauthority;
331 $ENV{"DISPLAY"} = $display;
333 $main::lxdebug->message(LXDebug->DEBUG2(), " not found\n");
336 $main::lxdebug->leave_sub();
342 $main::lxdebug->enter_sub();
346 $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
348 my $display = $self->is_xvfb_running();
351 $main::lxdebug->leave_sub();
356 while ( -f "/tmp/.X${display}-lock") {
359 $display = ":${display}";
360 $main::lxdebug->message(LXDebug->DEBUG2(), " display $display\n");
362 my $mcookie = `mcookie`;
363 die("Installation error: mcookie not found.") if ($? != 0);
366 $main::lxdebug->message(LXDebug->DEBUG2(), " mcookie $mcookie\n");
368 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
369 $ENV{"XAUTHORITY"} = $xauthority;
371 $main::lxdebug->message(LXDebug->DEBUG2(), " xauthority $xauthority\n");
373 system("xauth add \"${display}\" . \"${mcookie}\"");
375 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
376 $main::lxdebug->leave_sub();
380 $main::lxdebug->message(LXDebug->DEBUG2(), " about to fork()\n");
384 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
385 exec($::lx_office_conf{applications}->{xvfb}, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
388 $main::lxdebug->message(LXDebug->DEBUG2(), " parent dont sleeping\n");
391 my $dfname = $self->{"userspath"} . "/xvfb_display";
392 if (!open(OUT, ">", $dfname)) {
393 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
396 $main::lxdebug->leave_sub();
399 print(OUT "$pid\n$display\n$xauthority\n");
402 $main::lxdebug->message(LXDebug->DEBUG2(), " parent re-testing\n");
404 if (!$self->is_xvfb_running()) {
405 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
406 unlink($xauthority, $dfname);
408 $main::lxdebug->leave_sub();
412 $main::lxdebug->message(LXDebug->DEBUG2(), " spawn OK\n");
414 $main::lxdebug->leave_sub();
419 sub _run_python_uno {
420 my ($self, @args) = @_;
422 local $ENV{PYTHONPATH};
423 $ENV{PYTHONPATH} = $::lx_office_conf{environment}->{python_uno_path} . ':' . $ENV{PYTHONPATH} if $::lx_office_conf{environment}->{python_uno_path};
424 my $cmd = $::lx_office_conf{applications}->{python_uno} . ' ' . join(' ', @args);
428 sub is_openoffice_running {
431 $main::lxdebug->enter_sub();
433 my $output = $self->_run_python_uno('./scripts/oo-uno-test-conn.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, ' 2> /dev/null');
436 my $res = ($? == 0) || $output;
437 $main::lxdebug->message(LXDebug->DEBUG2(), " is_openoffice_running(): res $res\n");
439 $main::lxdebug->leave_sub();
444 sub spawn_openoffice {
445 $main::lxdebug->enter_sub();
449 $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
451 my ($try, $spawned_oo, $res);
454 for ($try = 0; $try < 15; $try++) {
455 if ($self->is_openoffice_running()) {
460 if ($::dispatcher->interface_type eq 'FastCGI') {
461 $::dispatcher->{request}->Detach;
467 $main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
469 if ($::dispatcher->interface_type eq 'FastCGI') {
470 $::dispatcher->{request}->Finish;
471 $::dispatcher->{request}->LastCall;
474 open(STDIN, '/dev/null');
475 open(STDOUT, '>/dev/null');
476 my $new_pid = fork();
478 my $ssres = setsid();
479 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
480 my @cmdline = ($::lx_office_conf{applications}->{openofficeorg_writer},
481 "-minimized", "-norestore", "-nologo", "-nolockcheck",
483 "-accept=socket,host=localhost,port=" .
484 $::lx_office_conf{print_templates}->{openofficeorg_daemon_port} . ";urp;");
488 if ($::dispatcher->interface_type eq 'FastCGI') {
489 $::dispatcher->{request}->Attach;
493 $main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
498 sleep($try >= 5 ? 2 : 1);
502 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
503 "OpenOffice could not be started.";
506 $main::lxdebug->leave_sub();
512 $main::lxdebug->enter_sub();
516 my $form = $self->{"form"};
518 my $filename = $form->{"tmpfile"};
519 $filename =~ s/.odt$//;
520 if (substr($filename, 0, 1) ne "/") {
521 $filename = getcwd() . "/${filename}";
524 if (substr($self->{"userspath"}, 0, 1) eq "/") {
525 $ENV{'HOME'} = $self->{"userspath"};
527 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
530 if (!$self->spawn_xvfb()) {
531 $main::lxdebug->leave_sub();
535 if (!$::lx_office_conf{print_templates}->{openofficeorg_daemon}) {
536 system($::lx_office_conf{applications}->{openofficeorg_writer},
537 "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
538 "file:${filename}.odt",
539 "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()");
541 if (!$self->spawn_openoffice()) {
542 $main::lxdebug->leave_sub();
546 $self->_run_python_uno('./scripts/oo-uno-convert-pdf.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, "${filename}.odt");
550 if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
551 $form->{"tmpfile"} =~ s/odt$/pdf/;
553 unlink($filename . ".odt");
555 $main::lxdebug->leave_sub();
560 unlink($filename . ".odt", $filename . ".pdf");
561 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
564 $main::lxdebug->leave_sub();
569 my ($self, $variable) = @_;
570 my $form = $self->{"form"};
571 my $iconv = $self->{"iconv"};
573 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
575 # Allow some HTML markup to be converted into the output format's
576 # corresponding markup code, e.g. bold or italic.
577 my $rnd = $self->{"rnd"};
578 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
579 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
581 foreach my $key (keys(%markup_replace)) {
582 my $value = $markup_replace{$key};
583 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
584 $variable =~ s|\</${key}\>|</text:span>|gi;
587 return $iconv->convert($variable);
590 sub get_mime_type() {
593 if ($self->{"form"}->{"format"} =~ /pdf/) {
594 return "application/pdf";
596 return "application/vnd.oasis.opendocument.text";