1 package SL::Template::OpenDocument;
3 use SL::Template::Simple;
14 # use File::Temp qw(:mktemp);
17 @ISA = qw(SL::Template::Simple);
24 my $self = $type->SUPER::new(@_);
26 $self->{"rnd"} = int(rand(1000000));
27 $self->{"iconv"} = SL::Iconv->new($main::dbcharset, "UTF-8");
29 $self->set_tag_style('<%', '%>');
30 $self->{quot_re} = '"';
36 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
38 my ($form, $new_contents) = ($self->{"form"}, "");
40 my $ary = $self->_get_loop_variable($var, 1, @indices);
42 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
43 $form->{"__first__"} = $i == 0;
44 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
45 $form->{"__odd__"} = (($i + 1) % 2) == 1;
46 $form->{"__counter__"} = $i + 1;
47 my $new_text = $self->parse_block($text, (@indices, $i));
48 return undef unless (defined($new_text));
49 $new_contents .= $start_tag . $new_text . $end_tag;
51 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
57 my ($self, $text, $pos, $var, $not) = @_;
60 $pos = 0 unless ($pos);
62 while ($pos < length($text)) {
65 next if (substr($text, $pos - 1, 5) ne '<%');
67 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
70 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
72 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
76 my $block = substr($text, 0, $pos - 1);
77 substr($text, 0, $pos - 1) = "";
78 $text =~ s!^\<\%[^\%]+\%\>!!;
79 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
81 return ($block, $text);
83 } elsif (substr($text, $pos + 4, 3) eq 'end') {
86 my $block = substr($text, 0, $pos - 1);
87 substr($text, 0, $pos - 1) = "";
88 $text =~ s!^\<\%[^\%]+\%\>!!;
90 return ($block, $text);
99 $main::lxdebug->enter_sub();
101 my ($self, $contents, @indices) = @_;
103 my $new_contents = "";
105 while ($contents ne "") {
106 if (substr($contents, 0, 1) eq "<") {
107 $contents =~ m|^<[^>]+>|;
109 substr($contents, 0, length($&)) = "";
111 if ($tag =~ m|<table:table-row|) {
112 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
115 substr($contents, 0, length($1) + length($end_tag)) = "";
117 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
120 substr($table_row, length($`), length($&)) = "";
122 my ($t1, $t2) = $self->find_end($table_row, length($`));
124 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
125 $main::lxdebug->leave_sub();
129 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
130 if (!defined($new_text)) {
131 $main::lxdebug->leave_sub();
134 $new_contents .= $new_text;
137 my $new_text = $self->parse_block($table_row, @indices);
138 if (!defined($new_text)) {
139 $main::lxdebug->leave_sub();
142 $new_contents .= $tag . $new_text . $end_tag;
146 $new_contents .= $tag;
150 $contents =~ /^[^<]+/;
153 my $pos_if = index($text, '<%if');
154 my $pos_foreach = index($text, '<%foreach');
156 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
157 substr($contents, 0, length($text)) = "";
158 $new_contents .= $self->substitute_vars($text, @indices);
162 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
163 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
164 substr($contents, 0, $pos_foreach) = "";
166 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
167 $self->{"error"} = "Malformed <\%foreach\%>.";
168 $main::lxdebug->leave_sub();
174 substr($contents, 0, length($&)) = "";
177 ($block, $contents) = $self->find_end($contents);
179 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
180 $main::lxdebug->leave_sub();
184 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
185 if (!defined($new_text)) {
186 $main::lxdebug->leave_sub();
189 $new_contents .= $new_text;
192 if (!$self->_parse_block_if(\$contents, \$new_contents, $pos_if, @indices)) {
193 $main::lxdebug->leave_sub();
200 $main::lxdebug->leave_sub();
202 return $new_contents;
206 $main::lxdebug->enter_sub();
209 my $form = $self->{"form"};
214 if ($form->{"IN"} =~ m|^/|) {
215 $file_name = $form->{"IN"};
217 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
220 my $zip = Archive::Zip->new();
221 if (Archive::Zip->AZ_OK != $zip->read($file_name)) {
222 $self->{"error"} = "File not found/is not a OpenDocument file.";
223 $main::lxdebug->leave_sub();
227 my $contents = $zip->contents("content.xml");
229 $self->{"error"} = "File is not a OpenDocument file.";
230 $main::lxdebug->leave_sub();
234 my $rnd = $self->{"rnd"};
235 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
236 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
238 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
239 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
241 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
242 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
244 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
245 <style:text-properties style:text-line-through-style="solid"/>
247 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
248 <style:text-properties style:text-position="super 58%"/>
250 <style:style style:name="TLXO${rnd}SUB" style:family="text">
251 <style:text-properties style:text-position="sub 58%"/>
255 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
256 $contents =~ s|[\n\r]||gm;
258 my $new_contents = $self->parse_block($contents);
259 if (!defined($new_contents)) {
260 $main::lxdebug->leave_sub();
264 # $new_contents =~ s|>|>\n|g;
266 $zip->contents("content.xml", $new_contents);
268 my $styles = $zip->contents("styles.xml");
270 my $new_styles = $self->parse_block($styles);
271 if (!defined($new_contents)) {
272 $main::lxdebug->leave_sub();
275 $zip->contents("styles.xml", $new_styles);
278 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
281 if ($form->{"format"} =~ /pdf/) {
282 $res = $self->convert_to_pdf();
285 $main::lxdebug->leave_sub();
289 sub is_xvfb_running {
290 $main::lxdebug->enter_sub();
295 my $dfname = $self->{"userspath"} . "/xvfb_display";
298 $main::lxdebug->message(LXDebug->DEBUG2(), " Looking for $dfname\n");
299 if ((-f $dfname) && open(IN, $dfname)) {
304 my $xauthority = <IN>;
308 $main::lxdebug->message(LXDebug->DEBUG2(), " found with $pid and $display\n");
310 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
311 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #1\n");
312 unlink($dfname, $xauthority);
313 $main::lxdebug->leave_sub();
318 if ($line !~ /xvfb/i) {
319 $main::lxdebug->message(LXDebug->DEBUG2(), " no/wrong process #2\n");
320 unlink($dfname, $xauthority);
321 $main::lxdebug->leave_sub();
325 $ENV{"XAUTHORITY"} = $xauthority;
326 $ENV{"DISPLAY"} = $display;
328 $main::lxdebug->message(LXDebug->DEBUG2(), " not found\n");
331 $main::lxdebug->leave_sub();
337 $main::lxdebug->enter_sub();
341 $main::lxdebug->message(LXDebug->DEBUG2, "spawn_xvfb()\n");
343 my $display = $self->is_xvfb_running();
346 $main::lxdebug->leave_sub();
351 while ( -f "/tmp/.X${display}-lock") {
354 $display = ":${display}";
355 $main::lxdebug->message(LXDebug->DEBUG2(), " display $display\n");
357 my $mcookie = `mcookie`;
358 die("Installation error: mcookie not found.") if ($? != 0);
361 $main::lxdebug->message(LXDebug->DEBUG2(), " mcookie $mcookie\n");
363 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
364 $ENV{"XAUTHORITY"} = $xauthority;
366 $main::lxdebug->message(LXDebug->DEBUG2(), " xauthority $xauthority\n");
368 system("xauth add \"${display}\" . \"${mcookie}\"");
370 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
371 $main::lxdebug->leave_sub();
375 $main::lxdebug->message(LXDebug->DEBUG2(), " about to fork()\n");
379 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
380 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
383 $main::lxdebug->message(LXDebug->DEBUG2(), " parent dont sleeping\n");
386 my $dfname = $self->{"userspath"} . "/xvfb_display";
387 if (!open(OUT, ">$dfname")) {
388 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
391 $main::lxdebug->leave_sub();
394 print(OUT "$pid\n$display\n$xauthority\n");
397 $main::lxdebug->message(LXDebug->DEBUG2(), " parent re-testing\n");
399 if (!$self->is_xvfb_running()) {
400 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
401 unlink($xauthority, $dfname);
403 $main::lxdebug->leave_sub();
407 $main::lxdebug->message(LXDebug->DEBUG2(), " spawn OK\n");
409 $main::lxdebug->leave_sub();
414 sub is_openoffice_running {
415 $main::lxdebug->enter_sub();
417 my $output = `./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port 2> /dev/null`;
420 my $res = ($? == 0) || $output;
421 $main::lxdebug->message(LXDebug->DEBUG2(), " is_openoffice_running(): res $res\n");
423 $main::lxdebug->leave_sub();
428 sub spawn_openoffice {
429 $main::lxdebug->enter_sub();
433 $main::lxdebug->message(LXDebug->DEBUG2(), "spawn_openoffice()\n");
435 my ($try, $spawned_oo, $res);
438 for ($try = 0; $try < 15; $try++) {
439 if ($self->is_openoffice_running()) {
447 $main::lxdebug->message(LXDebug->DEBUG2(), " Child daemonizing\n");
449 open(STDIN, '/dev/null');
450 open(STDOUT, '>/dev/null');
451 my $new_pid = fork();
453 my $ssres = setsid();
454 $main::lxdebug->message(LXDebug->DEBUG2(), " Child execing\n");
455 my @cmdline = ($main::openofficeorg_writer_bin,
456 "-minimized", "-norestore", "-nologo", "-nolockcheck",
458 "-accept=socket,host=localhost,port=" .
459 $main::openofficeorg_daemon_port . ";urp;");
463 $main::lxdebug->message(LXDebug->DEBUG2(), " Parent after fork\n");
468 sleep($try >= 5 ? 2 : 1);
472 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
473 "OpenOffice could not be started.";
476 $main::lxdebug->leave_sub();
482 $main::lxdebug->enter_sub();
486 my $form = $self->{"form"};
488 my $filename = $form->{"tmpfile"};
489 $filename =~ s/.odt$//;
490 if (substr($filename, 0, 1) ne "/") {
491 $filename = getcwd() . "/${filename}";
494 if (substr($self->{"userspath"}, 0, 1) eq "/") {
495 $ENV{'HOME'} = $self->{"userspath"};
497 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
500 if (!$self->spawn_xvfb()) {
501 $main::lxdebug->leave_sub();
506 if (!$main::openofficeorg_daemon) {
507 @cmdline = ($main::openofficeorg_writer_bin,
508 "-minimized", "-norestore", "-nologo", "-nolockcheck",
510 "file:${filename}.odt",
511 "macro://" . (split('/', $filename))[-1] .
512 "/Standard.Conversion.ConvertSelfToPDF()");
514 if (!$self->spawn_openoffice()) {
515 $main::lxdebug->leave_sub();
519 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
520 $main::openofficeorg_daemon_port,
527 if ((0 == $?) || (-f "${filename}.pdf" && -s "${filename}.pdf")) {
528 $form->{"tmpfile"} =~ s/odt$/pdf/;
530 unlink($filename . ".odt");
532 $main::lxdebug->leave_sub();
537 unlink($filename . ".odt", $filename . ".pdf");
538 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
541 $main::lxdebug->leave_sub();
546 my ($self, $variable) = @_;
547 my $form = $self->{"form"};
548 my $iconv = $self->{"iconv"};
550 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
552 # Allow some HTML markup to be converted into the output format's
553 # corresponding markup code, e.g. bold or italic.
554 my $rnd = $self->{"rnd"};
555 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
556 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
558 foreach my $key (keys(%markup_replace)) {
559 my $value = $markup_replace{$key};
560 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
561 $variable =~ s|\</${key}\>|</text:span>|gi;
564 return $iconv->convert($variable);
567 sub get_mime_type() {
570 if ($self->{"form"}->{"format"} =~ /pdf/) {
571 return "application/pdf";
573 return "application/vnd.oasis.opendocument.text";