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));
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[^>]*>)|;
113 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
116 $contents =~ m|\<\%foreachrow\s+.*?\%\>|;
117 substr($contents, length($`), length($&)) = "";
119 ($table_row, $contents) = $self->find_end($contents, length($`));
121 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
122 $main::lxdebug->leave_sub();
126 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
130 substr $contents, 0, length($&), '';
132 my $new_text = $self->parse_foreach($var, $table_row, $tag, $end_tag, @indices);
133 if (!defined($new_text)) {
134 $main::lxdebug->leave_sub();
137 $new_contents .= $new_text;
140 substr($contents, 0, length($table_row) + length($end_tag)) = "";
141 my $new_text = $self->parse_block($table_row, @indices);
142 if (!defined($new_text)) {
143 $main::lxdebug->leave_sub();
146 $new_contents .= $tag . $new_text . $end_tag;
150 $new_contents .= $tag;
154 $contents =~ /^[^<]+/;
157 my $pos_if = index($text, '<%if');
158 my $pos_foreach = index($text, '<%foreach');
160 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
161 substr($contents, 0, length($text)) = "";
162 $new_contents .= $self->substitute_vars($text, @indices);
166 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
167 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
168 substr($contents, 0, $pos_foreach) = "";
170 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
171 $self->{"error"} = "Malformed <\%foreach\%>.";
172 $main::lxdebug->leave_sub();
178 substr($contents, 0, length($&)) = "";
181 ($block, $contents) = $self->find_end($contents);
183 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
184 $main::lxdebug->leave_sub();
188 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
189 if (!defined($new_text)) {
190 $main::lxdebug->leave_sub();
193 $new_contents .= $new_text;
196 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
197 $main::lxdebug->leave_sub();
204 $main::lxdebug->leave_sub();
206 return $new_contents;
210 $main::lxdebug->enter_sub();
213 my $form = $self->{"form"};
218 if ($form->{"IN"} =~ m|^/|) {
219 $file_name = $form->{"IN"};
221 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
224 my $zip = Archive::Zip->new();
225 if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
226 $self->{"error"} = "File not found/is not a OpenDocument file.";
227 $main::lxdebug->leave_sub();
231 my $contents = Encode::decode('utf-8-strict', $zip->contents("content.xml"));
233 $self->{"error"} = "File is not a OpenDocument file.";
234 $main::lxdebug->leave_sub();
238 my $rnd = $self->{"rnd"};
239 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
240 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
242 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
243 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
245 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
246 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
248 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
249 <style:text-properties style:text-line-through-style="solid"/>
251 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
252 <style:text-properties style:text-position="super 58%"/>
254 <style:style style:name="TLXO${rnd}SUB" style:family="text">
255 <style:text-properties style:text-position="sub 58%"/>
259 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
260 $contents =~ s|[\n\r]||gm;
263 if ($self->{use_template_toolkit}) {
264 my $additional_params = $::form;
266 $::form->init_template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
268 $new_contents = $self->parse_block($contents);
270 if (!defined($new_contents)) {
271 $main::lxdebug->leave_sub();
275 # $new_contents =~ s|>|>\n|g;
277 $zip->contents("content.xml", Encode::encode('utf-8-strict', $new_contents));
279 my $styles = Encode::decode('utf-8-strict', $zip->contents("styles.xml"));
281 my $new_styles = $self->parse_block($styles);
282 if (!defined($new_contents)) {
283 $main::lxdebug->leave_sub();
286 $zip->contents("styles.xml", Encode::encode('utf-8-strict', $new_styles));
289 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
292 if ($form->{"format"} =~ /pdf/) {
293 $res = $self->convert_to_pdf();
296 $main::lxdebug->leave_sub();
300 sub is_xvfb_running {
301 $main::lxdebug->enter_sub();
306 my $dfname = $self->{"userspath"} . "/xvfb_display";
309 $main::lxdebug->message(LXDebug->DEBUG2(), " Looking for $dfname\n");
310 if ((-f $dfname) && open(IN, $dfname)) {
315 my $xauthority = <IN>;
319 $main::lxdebug->message(LXDebug->DEBUG2(), " found with $pid and $display\n");
321 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
322 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #1\n");
323 unlink($dfname, $xauthority);
324 $main::lxdebug->leave_sub();
329 if ($line !~ /xvfb/i) {
330 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #2\n");
331 unlink($dfname, $xauthority);
332 $main::lxdebug->leave_sub();
336 $ENV{"XAUTHORITY"} = $xauthority;
337 $ENV{"DISPLAY"} = $display;
339 $main::lxdebug->message(LXDebug->DEBUG2(), " not found\n");
342 $main::lxdebug->leave_sub();
348 $main::lxdebug->enter_sub();
352 $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
354 my $display = $self->is_xvfb_running();
357 $main::lxdebug->leave_sub();
362 while ( -f "/tmp/.X${display}-lock") {
365 $display = ":${display}";
366 $main::lxdebug->message(LXDebug->DEBUG2(), " display $display\n");
368 my $mcookie = `mcookie`;
369 die("Installation error: mcookie not found.") if ($? != 0);
372 $main::lxdebug->message(LXDebug->DEBUG2(), " mcookie $mcookie\n");
374 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
375 $ENV{"XAUTHORITY"} = $xauthority;
377 $main::lxdebug->message(LXDebug->DEBUG2(), " xauthority $xauthority\n");
379 system("xauth add \"${display}\" . \"${mcookie}\"");
381 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
382 $main::lxdebug->leave_sub();
386 $main::lxdebug->message(LXDebug->DEBUG2(), " about to fork()\n");
390 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
391 exec($::lx_office_conf{applications}->{xvfb}, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
394 $main::lxdebug->message(LXDebug->DEBUG2(), " parent dont sleeping\n");
397 my $dfname = $self->{"userspath"} . "/xvfb_display";
398 if (!open(OUT, ">", $dfname)) {
399 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
402 $main::lxdebug->leave_sub();
405 print(OUT "$pid\n$display\n$xauthority\n");
408 $main::lxdebug->message(LXDebug->DEBUG2(), " parent re-testing\n");
410 if (!$self->is_xvfb_running()) {
411 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
412 unlink($xauthority, $dfname);
414 $main::lxdebug->leave_sub();
418 $main::lxdebug->message(LXDebug->DEBUG2(), " spawn OK\n");
420 $main::lxdebug->leave_sub();
425 sub _run_python_uno {
426 my ($self, @args) = @_;
428 local $ENV{PYTHONPATH};
429 $ENV{PYTHONPATH} = $::lx_office_conf{environment}->{python_uno_path} . ':' . $ENV{PYTHONPATH} if $::lx_office_conf{environment}->{python_uno_path};
430 my $cmd = $::lx_office_conf{applications}->{python_uno} . ' ' . join(' ', @args);
434 sub is_openoffice_running {
437 $main::lxdebug->enter_sub();
439 my $output = $self->_run_python_uno('./scripts/oo-uno-test-conn.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, ' 2> /dev/null');
442 my $res = ($? == 0) || $output;
443 $main::lxdebug->message(LXDebug->DEBUG2(), " is_openoffice_running(): res $res\n");
445 $main::lxdebug->leave_sub();
450 sub spawn_openoffice {
451 $main::lxdebug->enter_sub();
455 $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
457 my ($try, $spawned_oo, $res);
460 for ($try = 0; $try < 15; $try++) {
461 if ($self->is_openoffice_running()) {
466 if ($::dispatcher->interface_type eq 'FastCGI') {
467 $::dispatcher->{request}->Detach;
473 $main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
475 if ($::dispatcher->interface_type eq 'FastCGI') {
476 $::dispatcher->{request}->Finish;
477 $::dispatcher->{request}->LastCall;
480 open(STDIN, '/dev/null');
481 open(STDOUT, '>/dev/null');
482 my $new_pid = fork();
484 my $ssres = setsid();
485 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
486 my @cmdline = ($::lx_office_conf{applications}->{openofficeorg_writer},
487 "-minimized", "-norestore", "-nologo", "-nolockcheck",
489 "-accept=socket,host=localhost,port=" .
490 $::lx_office_conf{print_templates}->{openofficeorg_daemon_port} . ";urp;");
494 if ($::dispatcher->interface_type eq 'FastCGI') {
495 $::dispatcher->{request}->Attach;
499 $main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
504 sleep($try >= 5 ? 2 : 1);
508 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
509 "OpenOffice could not be started.";
512 $main::lxdebug->leave_sub();
518 $main::lxdebug->enter_sub();
522 my $form = $self->{"form"};
524 my $filename = $form->{"tmpfile"};
525 $filename =~ s/.odt$//;
526 if (substr($filename, 0, 1) ne "/") {
527 $filename = getcwd() . "/${filename}";
530 if (substr($self->{"userspath"}, 0, 1) eq "/") {
531 $ENV{'HOME'} = $self->{"userspath"};
533 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
536 if (!$self->spawn_xvfb()) {
537 $main::lxdebug->leave_sub();
541 if (!$::lx_office_conf{print_templates}->{openofficeorg_daemon}) {
542 system($::lx_office_conf{applications}->{openofficeorg_writer},
543 "-minimized", "-norestore", "-nologo", "-nolockcheck", "-headless",
544 "file:${filename}.odt",
545 "macro://" . (split('/', $filename))[-1] . "/Standard.Conversion.ConvertSelfToPDF()");
547 if (!$self->spawn_openoffice()) {
548 $main::lxdebug->leave_sub();
552 $self->_run_python_uno('./scripts/oo-uno-convert-pdf.py', $::lx_office_conf{print_templates}->{openofficeorg_daemon_port}, "${filename}.odt");
556 if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
557 $form->{"tmpfile"} =~ s/odt$/pdf/;
559 unlink($filename . ".odt");
561 $main::lxdebug->leave_sub();
566 unlink($filename . ".odt", $filename . ".pdf");
567 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
570 $main::lxdebug->leave_sub();
575 my ($self, $variable) = @_;
576 my $form = $self->{"form"};
578 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
580 # Allow some HTML markup to be converted into the output format's
581 # corresponding markup code, e.g. bold or italic.
582 my $rnd = $self->{"rnd"};
583 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
584 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
586 foreach my $key (keys(%markup_replace)) {
587 my $value = $markup_replace{$key};
588 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
589 $variable =~ s|\</${key}\>|</text:span>|gi;
595 sub get_mime_type() {
598 if ($self->{"form"}->{"format"} =~ /pdf/) {
599 return "application/pdf";
601 return "application/vnd.oasis.opendocument.text";