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[^>]*>)|;
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 = $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;
262 my $new_contents = $self->parse_block($contents);
263 if (!defined($new_contents)) {
264 $main::lxdebug->leave_sub();
268 # $new_contents =~ s|>|>\n|g;
270 $zip->contents("content.xml", $new_contents);
272 my $styles = $zip->contents("styles.xml");
274 my $new_styles = $self->parse_block($styles);
275 if (!defined($new_contents)) {
276 $main::lxdebug->leave_sub();
279 $zip->contents("styles.xml", $new_styles);
282 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
285 if ($form->{"format"} =~ /pdf/) {
286 $res = $self->convert_to_pdf();
289 $main::lxdebug->leave_sub();
293 sub is_xvfb_running {
294 $main::lxdebug->enter_sub();
299 my $dfname = $self->{"userspath"} . "/xvfb_display";
302 $main::lxdebug->message(LXDebug->DEBUG2(), " Looking for $dfname\n");
303 if ((-f $dfname) && open(IN, $dfname)) {
308 my $xauthority = <IN>;
312 $main::lxdebug->message(LXDebug->DEBUG2(), " found with $pid and $display\n");
314 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
315 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #1\n");
316 unlink($dfname, $xauthority);
317 $main::lxdebug->leave_sub();
322 if ($line !~ /xvfb/i) {
323 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #2\n");
324 unlink($dfname, $xauthority);
325 $main::lxdebug->leave_sub();
329 $ENV{"XAUTHORITY"} = $xauthority;
330 $ENV{"DISPLAY"} = $display;
332 $main::lxdebug->message(LXDebug->DEBUG2(), " not found\n");
335 $main::lxdebug->leave_sub();
341 $main::lxdebug->enter_sub();
345 $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
347 my $display = $self->is_xvfb_running();
350 $main::lxdebug->leave_sub();
355 while ( -f "/tmp/.X${display}-lock") {
358 $display = ":${display}";
359 $main::lxdebug->message(LXDebug->DEBUG2(), " display $display\n");
361 my $mcookie = `mcookie`;
362 die("Installation error: mcookie not found.") if ($? != 0);
365 $main::lxdebug->message(LXDebug->DEBUG2(), " mcookie $mcookie\n");
367 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
368 $ENV{"XAUTHORITY"} = $xauthority;
370 $main::lxdebug->message(LXDebug->DEBUG2(), " xauthority $xauthority\n");
372 system("xauth add \"${display}\" . \"${mcookie}\"");
374 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
375 $main::lxdebug->leave_sub();
379 $main::lxdebug->message(LXDebug->DEBUG2(), " about to fork()\n");
383 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
384 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
387 $main::lxdebug->message(LXDebug->DEBUG2(), " parent dont sleeping\n");
390 my $dfname = $self->{"userspath"} . "/xvfb_display";
391 if (!open(OUT, ">$dfname")) {
392 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
395 $main::lxdebug->leave_sub();
398 print(OUT "$pid\n$display\n$xauthority\n");
401 $main::lxdebug->message(LXDebug->DEBUG2(), " parent re-testing\n");
403 if (!$self->is_xvfb_running()) {
404 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
405 unlink($xauthority, $dfname);
407 $main::lxdebug->leave_sub();
411 $main::lxdebug->message(LXDebug->DEBUG2(), " spawn OK\n");
413 $main::lxdebug->leave_sub();
418 sub is_openoffice_running {
419 $main::lxdebug->enter_sub();
421 my $output = `./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port 2> /dev/null`;
424 my $res = ($? == 0) || $output;
425 $main::lxdebug->message(LXDebug->DEBUG2(), " is_openoffice_running(): res $res\n");
427 $main::lxdebug->leave_sub();
432 sub spawn_openoffice {
433 $main::lxdebug->enter_sub();
437 $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
439 my ($try, $spawned_oo, $res);
442 for ($try = 0; $try < 15; $try++) {
443 if ($self->is_openoffice_running()) {
451 $main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
453 open(STDIN, '/dev/null');
454 open(STDOUT, '>/dev/null');
455 my $new_pid = fork();
457 my $ssres = setsid();
458 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
459 my @cmdline = ($main::openofficeorg_writer_bin,
460 "-minimized", "-norestore", "-nologo", "-nolockcheck",
462 "-accept=socket,host=localhost,port=" .
463 $main::openofficeorg_daemon_port . ";urp;");
467 $main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
472 sleep($try >= 5 ? 2 : 1);
476 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
477 "OpenOffice could not be started.";
480 $main::lxdebug->leave_sub();
486 $main::lxdebug->enter_sub();
490 my $form = $self->{"form"};
492 my $filename = $form->{"tmpfile"};
493 $filename =~ s/.odt$//;
494 if (substr($filename, 0, 1) ne "/") {
495 $filename = getcwd() . "/${filename}";
498 if (substr($self->{"userspath"}, 0, 1) eq "/") {
499 $ENV{'HOME'} = $self->{"userspath"};
501 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
504 if (!$self->spawn_xvfb()) {
505 $main::lxdebug->leave_sub();
510 if (!$main::openofficeorg_daemon) {
511 @cmdline = ($main::openofficeorg_writer_bin,
512 "-minimized", "-norestore", "-nologo", "-nolockcheck",
514 "file:${filename}.odt",
515 "macro://" . (split('/', $filename))[-1] .
516 "/Standard.Conversion.ConvertSelfToPDF()");
518 if (!$self->spawn_openoffice()) {
519 $main::lxdebug->leave_sub();
523 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
524 $main::openofficeorg_daemon_port,
531 if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
532 $form->{"tmpfile"} =~ s/odt$/pdf/;
534 unlink($filename . ".odt");
536 $main::lxdebug->leave_sub();
541 unlink($filename . ".odt", $filename . ".pdf");
542 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
545 $main::lxdebug->leave_sub();
550 my ($self, $variable) = @_;
551 my $form = $self->{"form"};
552 my $iconv = $self->{"iconv"};
554 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
556 # Allow some HTML markup to be converted into the output format's
557 # corresponding markup code, e.g. bold or italic.
558 my $rnd = $self->{"rnd"};
559 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
560 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
562 foreach my $key (keys(%markup_replace)) {
563 my $value = $markup_replace{$key};
564 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
565 $variable =~ s|\</${key}\>|</text:span>|gi;
568 return $iconv->convert($variable);
571 sub get_mime_type() {
574 if ($self->{"form"}->{"format"} =~ /pdf/) {
575 return "application/pdf";
577 return "application/vnd.oasis.opendocument.text";