1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
9 package SimpleTemplate;
12 # 1. The template's file name
13 # 2. A reference to the Form object
14 # 3. A reference to the myconfig hash
17 # A new template object
31 $self->{source} = shift;
32 $self->{form} = shift;
33 $self->{myconfig} = shift;
34 $self->{userspath} = shift;
36 $self->{error} = undef;
38 $self->set_tag_style('<%', '%>');
43 my $tag_start = shift;
46 $self->{tag_start} = $tag_start;
47 $self->{tag_end} = $tag_end;
48 $self->{tag_start_qm} = quotemeta $tag_start;
49 $self->{tag_end_qm} = quotemeta $tag_end;
51 $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
59 # 1. A typeglob for the file handle. The output will be written
60 # to this file handle.
63 # 1 on success and undef or 0 if there was an error. In the latter case
64 # the calling function can retrieve the error message via $obj->get_error()
69 print(OUT "Hallo!\n");
75 return $self->{"error"};
82 sub _get_loop_variable {
85 my $get_array = shift;
88 my $form = $self->{form};
91 if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
92 $value = $form->{TEMPLATE_ARRAYS}->{$var};
94 $value = $form->{$var};
97 for (my $i = 0; $i < scalar(@indices); $i++) {
98 last unless (ref($value) eq "ARRAY");
99 $value = $value->[$indices[$i]];
105 sub substitute_vars {
106 my ($self, $text, @indices) = @_;
108 my $form = $self->{"form"};
110 while ($text =~ /$self->{substitute_vars_re}/) {
111 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
112 my ($var, @options) = split(/\s+/, $1);
114 my $value = $self->_get_loop_variable($var, 0, @indices);
115 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
117 substr($text, $tag_pos, $tag_len, $value);
129 package LaTeXTemplate;
133 @ISA = qw(SimpleTemplate);
138 my $self = $type->SUPER::new(@_);
144 my ($self, $variable) = @_;
145 my $form = $self->{"form"};
147 $variable = $main::locale->quote_special_chars('Template/LaTeX', $variable);
149 # Allow some HTML markup to be converted into the output format's
150 # corresponding markup code, e.g. bold or italic.
151 my %markup_replace = ('b' => 'textbf',
155 foreach my $key (keys(%markup_replace)) {
156 my $new = $markup_replace{$key};
157 $variable =~ s/\$\<\$${key}\$\>\$(.*?)\$<\$\/${key}\$>\$/\\${new}\{$1\}/gi;
160 $variable =~ s/[\x00-\x1f]//g;
166 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
168 my ($form, $new_contents) = ($self->{"form"}, "");
170 my $ary = $self->_get_loop_variable($var, 1, @indices);
173 my $current_page = 1;
174 my ($current_line, $corrent_row) = (0, 1);
175 my $description_array = $self->_get_loop_variable("description", 1);
176 my $longdescription_array = $self->_get_loop_variable("longdescription", 1);
177 my $linetotal_array = $self->_get_loop_variable("linetotal", 1);
179 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal} = [];
181 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
182 $form->{"__first__"} = $i == 1;
183 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
184 $form->{"__odd__"} = (($i + 1) % 2) == 1;
185 $form->{"__counter__"} = $i + 1;
187 if (scalar @{$description_array} == scalar @{$ary} && $self->{"chars_per_line"} != 0) {
188 my $lines = int(length($description_array->[$i]) / $self->{"chars_per_line"});
191 $description_array->[$i] =~ s/(\\newline\s?)*$//;
192 my $_description = $description_array->[$i];
193 while ($_description =~ /\\newline/) {
195 $_description =~ s/\\newline//;
199 if ($current_page == 1) {
200 $lpp = $self->{"lines_on_first_page"};
202 $lpp = $self->{"lines_on_second_page"};
205 # Yes we need a manual page break -- or the user has forced one
206 if ((($current_line + $lines) > $lpp) || ($description_array->[$i] =~ /<pagebreak>/) || ($longdescription_array->[$i] =~ /<pagebreak>/)) {
207 my $pb = $self->{"pagebreak_block"};
209 # replace the special variables <%sumcarriedforward%>
212 my $psum = $form->format_amount($self->{"myconfig"}, $sum, 2);
213 $pb =~ s/$self->{tag_start_qm}sumcarriedforward$self->{tag_end_qm}/$psum/g;
214 $pb =~ s/$self->{tag_start_qm}lastpage$self->{tag_end_qm}/$current_page/g;
216 my $new_text = $self->parse_block($pb, (@indices, $i));
217 return undef unless (defined($new_text));
218 $new_contents .= $new_text;
223 $current_line += $lines;
226 if ($i < scalar(@{$linetotal_array})) {
227 $sum += $form->parse_amount($self->{"myconfig"}, $linetotal_array->[$i]);
230 $form->{TEMPLATE_ARRAYS}->{cumulatelinetotal}->[$i] = $form->format_amount($self->{"myconfig"}, $sum, 2);
232 my $new_text = $self->parse_block($text, (@indices, $i));
233 return undef unless (defined($new_text));
234 $new_contents .= $start_tag . $new_text . $end_tag;
236 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
238 return $new_contents;
242 my ($self, $text, $pos, $var, $not) = @_;
244 my $tag_start_len = length $self->{tag_start};
247 $pos = 0 unless ($pos);
249 while ($pos < length($text)) {
252 next if (substr($text, $pos - 1, length($self->{tag_start})) ne $self->{tag_start});
254 my $keyword_pos = $pos - 1 + $tag_start_len;
256 if ((substr($text, $keyword_pos, 2) eq 'if') || (substr($text, $keyword_pos, 3) eq 'for')) {
259 } elsif ((substr($text, $keyword_pos, 4) eq 'else') && (1 == $depth)) {
262 "$self->{tag_start}else$self->{tag_end} outside of "
263 . "$self->{tag_start}if$self->{tag_end} / "
264 . "$self->{tag_start}ifnot$self->{tag_end}.";
268 my $block = substr($text, 0, $pos - 1);
269 substr($text, 0, $pos - 1) = "";
270 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
271 $text = $self->{tag_start} . 'if' . ($not ? " " : "not ") . $var . $self->{tag_end} . $text;
273 return ($block, $text);
275 } elsif (substr($text, $keyword_pos, 3) eq 'end') {
278 my $block = substr($text, 0, $pos - 1);
279 substr($text, 0, $pos - 1) = "";
280 $text =~ s!^$self->{tag_start_qm}.+?$self->{tag_end_qm}!!;
282 return ($block, $text);
291 $main::lxdebug->enter_sub();
293 my ($self, $contents, @indices) = @_;
295 my $new_contents = "";
297 while ($contents ne "") {
298 my $pos_if = index($contents, $self->{tag_start} . 'if');
299 my $pos_foreach = index($contents, $self->{tag_start} . 'foreach');
301 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
302 $new_contents .= $self->substitute_vars($contents, @indices);
306 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
307 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
308 substr($contents, 0, $pos_foreach) = "";
310 if ($contents !~ m|^$self->{tag_start_qm}foreach (.+?)$self->{tag_end_qm}|) {
311 $self->{"error"} = "Malformed $self->{tag_start}foreach$self->{tag_end}.";
312 $main::lxdebug->leave_sub();
318 substr($contents, 0, length($&)) = "";
321 ($block, $contents) = $self->find_end($contents);
323 $self->{"error"} = "Unclosed $self->{tag_start}foreach$self->{tag_end}." unless ($self->{"error"});
324 $main::lxdebug->leave_sub();
328 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
329 if (!defined($new_text)) {
330 $main::lxdebug->leave_sub();
333 $new_contents .= $new_text;
336 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
337 substr($contents, 0, $pos_if) = "";
339 # if ($contents !~ m|^$self->{tag_start_qm}if\s*(not)?\s+(.*?)$self->{tag_end_qm}|) {
340 if ($contents !~ m/^$self->{tag_start_qm}
342 (not|\!)? # $1 -- Eventuelle Negierung
344 ([^\s]+) # $2 -- Name der zu überprüfenden Variablen
345 ( # $3 -- Beginn des optionalen Vergleiches
347 ([!=])= # $4 -- Art des Vergleiches speichern
349 ( # $5 -- Gequoteter String oder Bareword
350 "(.*)(?<!\\)" # $6 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne; Escapete Anführungs als Teil des Strings belassen
352 ([^\s]+) # $7 -- Bareword -- als Index für $form benutzen
358 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
359 $main::lxdebug->leave_sub();
365 my $operator = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
366 my $quoted = $5; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
367 my $bareword = $7; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
369 $not = !$not if ($operator && $operator eq '!');
371 substr($contents, 0, length($&)) = "";
373 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
375 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
376 $main::lxdebug->leave_sub();
380 my $value = $self->_get_loop_variable($var, 0, @indices);
384 my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted;
385 $hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to));
388 $hit = ($not && ! $value) || (!$not && $value);
392 my $new_text = $self->parse_block($block, @indices);
393 if (!defined($new_text)) {
394 $main::lxdebug->leave_sub();
397 $new_contents .= $new_text;
402 $main::lxdebug->leave_sub();
404 return $new_contents;
407 sub parse_first_line {
409 my $line = shift || "";
411 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
413 $self->{error} = "The tag start and end markers must not be equal.";
417 $self->set_tag_style($1, $2);
423 sub _parse_config_option {
430 my ($key, $value) = split m/\s*=\s*/, $line, 2;
432 if ($key eq 'tag-style') {
433 $self->set_tag_style(split(m/\s+/, $value, 2));
437 sub _parse_config_lines {
441 my ($comment_start, $comment_end) = ("", "");
443 if (ref $self eq 'LaTeXTemplate') {
444 $comment_start = '\s*%';
445 } elsif (ref $self eq 'HTMLTemplate') {
446 $comment_start = '\s*<!--';
447 $comment_end = '>\s*';
449 $comment_start = '\s*\#';
452 my $num_lines = scalar @{ $lines };
455 while ($i < $num_lines) {
456 my $line = $lines->[$i];
458 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
463 $self->_parse_config_option($1);
464 splice @{ $lines }, $i, 1;
469 sub _force_mandatory_packages {
473 my (%used_packages, $document_start_line);
475 foreach my $i (0 .. scalar @{ $lines } - 1) {
476 if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
477 $used_packages{$1} = 1;
479 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
480 $document_start_line = $i;
486 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
488 if (!$used_packages{textcomp}) {
489 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
490 $document_start_line++;
497 my $form = $self->{"form"};
499 if (!open(IN, "$form->{templates}/$form->{IN}")) {
500 $self->{"error"} = "$!";
506 $self->_parse_config_lines(\@lines);
507 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
509 my $contents = join("", @lines);
511 # detect pagebreak block and its parameters
512 if ($contents =~ /$self->{tag_start_qm}pagebreak\s+(\d+)\s+(\d+)\s+(\d+)\s*$self->{tag_end_qm}(.*?)$self->{tag_start_qm}end(\s*pagebreak)?$self->{tag_end_qm}/s) {
513 $self->{"chars_per_line"} = $1;
514 $self->{"lines_on_first_page"} = $2;
515 $self->{"lines_on_second_page"} = $3;
516 $self->{"pagebreak_block"} = $4;
518 substr($contents, length($`), length($&)) = "";
521 $self->{"forced_pagebreaks"} = [];
523 my $new_contents = $self->parse_block($contents);
524 if (!defined($new_contents)) {
525 $main::lxdebug->leave_sub();
529 print(OUT $new_contents);
531 if ($form->{"format"} =~ /postscript/i) {
532 return $self->convert_to_postscript();
533 } elsif ($form->{"format"} =~ /pdf/i) {
534 return $self->convert_to_pdf();
540 sub convert_to_postscript {
542 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
544 # Convert the tex file to postscript
546 if (!chdir("$userspath")) {
547 $self->{"error"} = "chdir : $!";
552 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
554 my $latex = $self->_get_latex_path();
556 for (my $run = 1; $run <= 2; $run++) {
557 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
558 "> $form->{tmpfile}.err");
560 $self->{"error"} = $form->cleanup();
566 $form->{tmpfile} =~ s/tex$/dvi/;
568 system("dvips $form->{tmpfile} -o -q > /dev/null");
570 $self->{"error"} = "dvips : $!";
574 $form->{tmpfile} =~ s/dvi$/ps/;
583 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
585 # Convert the tex file to PDF
587 if (!chdir("$userspath")) {
588 $self->{"error"} = "chdir : $!";
593 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
595 my $latex = $self->_get_latex_path();
597 for (my $run = 1; $run <= 2; $run++) {
598 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
599 "> $form->{tmpfile}.err");
601 $self->{"error"} = $form->cleanup();
607 $form->{tmpfile} =~ s/tex$/pdf/;
612 sub _get_latex_path {
613 return $main::latex_bin || 'pdflatex';
616 sub get_mime_type() {
619 if ($self->{"form"}->{"format"} =~ /postscript/i) {
620 return "application/postscript";
622 return "application/pdf";
635 package HTMLTemplate;
639 @ISA = qw(LaTeXTemplate);
644 return $type->SUPER::new(@_);
648 my ($self, $variable) = @_;
649 my $form = $self->{"form"};
651 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
653 # Allow some HTML markup to be converted into the output format's
654 # corresponding markup code, e.g. bold or italic.
655 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
657 foreach my $key (@markup_replace) {
658 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
664 sub get_mime_type() {
667 if ($self->{"form"}->{"format"} =~ /postscript/i) {
668 return "application/postscript";
669 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
670 return "application/pdf";
679 if ($self->{"form"}->{"format"} =~ /postscript/i) {
681 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
688 sub convert_to_postscript {
690 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
692 # Convert the HTML file to postscript
694 if (!chdir("$userspath")) {
695 $self->{"error"} = "chdir : $!";
700 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
701 my $psfile = $form->{"tmpfile"};
702 $psfile =~ s/.html/.ps/;
703 if ($psfile eq $form->{"tmpfile"}) {
707 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
709 $self->{"error"} = $form->cleanup();
714 $form->{"tmpfile"} = $psfile;
723 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
725 # Convert the HTML file to PDF
727 if (!chdir("$userspath")) {
728 $self->{"error"} = "chdir : $!";
733 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
734 my $pdffile = $form->{"tmpfile"};
735 $pdffile =~ s/.html/.pdf/;
736 if ($pdffile eq $form->{"tmpfile"}) {
740 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
742 $self->{"error"} = $form->cleanup();
747 $form->{"tmpfile"} = $pdffile;
756 #### PlainTextTemplate
759 package PlainTextTemplate;
763 @ISA = qw(LaTeXTemplate);
768 return $type->SUPER::new(@_);
772 my ($self, $variable) = @_;
787 #### OpenDocumentTemplate
790 package OpenDocumentTemplate;
798 # use File::Temp qw(:mktemp);
801 @ISA = qw(SimpleTemplate);
806 $self = $type->SUPER::new(@_);
808 foreach my $module (qw(Archive::Zip Text::Iconv)) {
809 eval("use ${module};");
811 $self->{"form"}->error("The Perl module '${module}' could not be " .
812 "loaded. Support for OpenDocument templates " .
813 "does not work without it. Please install your " .
814 "distribution's package or get the module from " .
815 "CPAN ( http://www.cpan.org ).");
819 $self->{"rnd"} = int(rand(1000000));
820 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
822 $self->set_tag_style('<%', '%>');
828 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
830 my ($form, $new_contents) = ($self->{"form"}, "");
832 my $ary = $self->_get_loop_variable($var, 1, @indices);
834 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
835 $form->{"__first__"} = $i == 0;
836 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
837 $form->{"__odd__"} = (($i + 1) % 2) == 1;
838 $form->{"__counter__"} = $i + 1;
839 my $new_text = $self->parse_block($text, (@indices, $i));
840 return undef unless (defined($new_text));
841 $new_contents .= $start_tag . $new_text . $end_tag;
843 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
845 return $new_contents;
849 my ($self, $text, $pos, $var, $not) = @_;
852 $pos = 0 unless ($pos);
854 while ($pos < length($text)) {
857 next if (substr($text, $pos - 1, 5) ne '<%');
859 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
862 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
864 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
868 my $block = substr($text, 0, $pos - 1);
869 substr($text, 0, $pos - 1) = "";
870 $text =~ s!^\<\%[^\%]+\%\>!!;
871 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
873 return ($block, $text);
875 } elsif (substr($text, $pos + 4, 3) eq 'end') {
878 my $block = substr($text, 0, $pos - 1);
879 substr($text, 0, $pos - 1) = "";
880 $text =~ s!^\<\%[^\%]+\%\>!!;
882 return ($block, $text);
891 $main::lxdebug->enter_sub();
893 my ($self, $contents, @indices) = @_;
895 my $new_contents = "";
897 while ($contents ne "") {
898 if (substr($contents, 0, 1) eq "<") {
899 $contents =~ m|^<[^>]+>|;
901 substr($contents, 0, length($&)) = "";
903 if ($tag =~ m|<table:table-row|) {
904 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
907 substr($contents, 0, length($1) + length($end_tag)) = "";
909 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
912 substr($table_row, length($`), length($&)) = "";
914 my ($t1, $t2) = $self->find_end($table_row, length($`));
916 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
917 $main::lxdebug->leave_sub();
921 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
922 if (!defined($new_text)) {
923 $main::lxdebug->leave_sub();
926 $new_contents .= $new_text;
929 my $new_text = $self->parse_block($table_row, @indices);
930 if (!defined($new_text)) {
931 $main::lxdebug->leave_sub();
934 $new_contents .= $tag . $new_text . $end_tag;
938 $new_contents .= $tag;
942 $contents =~ /^[^<]+/;
945 my $pos_if = index($text, '<%if');
946 my $pos_foreach = index($text, '<%foreach');
948 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
949 substr($contents, 0, length($text)) = "";
950 $new_contents .= $self->substitute_vars($text, @indices);
954 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
955 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
956 substr($contents, 0, $pos_foreach) = "";
958 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
959 $self->{"error"} = "Malformed <\%foreach\%>.";
960 $main::lxdebug->leave_sub();
966 substr($contents, 0, length($&)) = "";
969 ($block, $contents) = $self->find_end($contents);
971 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
972 $main::lxdebug->leave_sub();
976 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
977 if (!defined($new_text)) {
978 $main::lxdebug->leave_sub();
981 $new_contents .= $new_text;
984 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
985 substr($contents, 0, $pos_if) = "";
987 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
988 $self->{"error"} = "Malformed <\%if\%>.";
989 $main::lxdebug->leave_sub();
993 my ($not, $var) = ($1, $2);
995 substr($contents, 0, length($&)) = "";
997 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
999 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
1000 $main::lxdebug->leave_sub();
1004 my $value = $self->{"form"}->{$var};
1005 for (my $i = 0; $i < scalar(@indices); $i++) {
1006 last unless (ref($value) eq "ARRAY");
1007 $value = $value->[$indices[$i]];
1010 if (($not && !$value) || (!$not && $value)) {
1011 my $new_text = $self->parse_block($block, @indices);
1012 if (!defined($new_text)) {
1013 $main::lxdebug->leave_sub();
1016 $new_contents .= $new_text;
1022 $main::lxdebug->leave_sub();
1024 return $new_contents;
1028 $main::lxdebug->enter_sub();
1032 my $form = $self->{"form"};
1037 if ($form->{"IN"} =~ m|^/|) {
1038 $file_name = $form->{"IN"};
1040 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1043 my $zip = Archive::Zip->new();
1044 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
1045 $self->{"error"} = "File not found/is not a OpenDocument file.";
1046 $main::lxdebug->leave_sub();
1050 my $contents = $zip->contents("content.xml");
1052 $self->{"error"} = "File is not a OpenDocument file.";
1053 $main::lxdebug->leave_sub();
1057 my $rnd = $self->{"rnd"};
1058 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1059 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1061 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1062 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1064 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1065 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1067 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1068 <style:text-properties style:text-line-through-style="solid"/>
1070 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1071 <style:text-properties style:text-position="super 58%"/>
1073 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1074 <style:text-properties style:text-position="sub 58%"/>
1078 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1079 $contents =~ s|[\n\r]||gm;
1081 my $new_contents = $self->parse_block($contents);
1082 if (!defined($new_contents)) {
1083 $main::lxdebug->leave_sub();
1087 # $new_contents =~ s|>|>\n|g;
1089 $zip->contents("content.xml", $new_contents);
1091 my $styles = $zip->contents("styles.xml");
1093 my $new_styles = $self->parse_block($styles);
1094 if (!defined($new_contents)) {
1095 $main::lxdebug->leave_sub();
1098 $zip->contents("styles.xml", $new_styles);
1101 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1104 if ($form->{"format"} =~ /pdf/) {
1105 $res = $self->convert_to_pdf();
1108 $main::lxdebug->leave_sub();
1112 sub is_xvfb_running {
1113 $main::lxdebug->enter_sub();
1118 my $dfname = $self->{"userspath"} . "/xvfb_display";
1121 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1122 if ((-f $dfname) && open(IN, $dfname)) {
1127 my $xauthority = <IN>;
1131 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1133 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1134 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1135 unlink($dfname, $xauthority);
1136 $main::lxdebug->leave_sub();
1141 if ($line !~ /xvfb/i) {
1142 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1143 unlink($dfname, $xauthority);
1144 $main::lxdebug->leave_sub();
1148 $ENV{"XAUTHORITY"} = $xauthority;
1149 $ENV{"DISPLAY"} = $display;
1151 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1154 $main::lxdebug->leave_sub();
1160 $main::lxdebug->enter_sub();
1164 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1166 my $display = $self->is_xvfb_running();
1169 $main::lxdebug->leave_sub();
1174 while ( -f "/tmp/.X${display}-lock") {
1177 $display = ":${display}";
1178 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1180 my $mcookie = `mcookie`;
1181 die("Installation error: mcookie not found.") if ($? != 0);
1184 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1186 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1187 $ENV{"XAUTHORITY"} = $xauthority;
1189 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1191 system("xauth add \"${display}\" . \"${mcookie}\"");
1193 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1194 $main::lxdebug->leave_sub();
1198 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1202 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1203 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1206 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1209 my $dfname = $self->{"userspath"} . "/xvfb_display";
1210 if (!open(OUT, ">$dfname")) {
1211 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1212 unlink($xauthority);
1214 $main::lxdebug->leave_sub();
1217 print(OUT "$pid\n$display\n$xauthority\n");
1220 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1222 if (!$self->is_xvfb_running()) {
1223 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1224 unlink($xauthority, $dfname);
1226 $main::lxdebug->leave_sub();
1230 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1232 $main::lxdebug->leave_sub();
1237 sub is_openoffice_running {
1238 $main::lxdebug->enter_sub();
1240 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1241 "> /dev/null 2> /dev/null");
1243 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1245 $main::lxdebug->leave_sub();
1250 sub spawn_openoffice {
1251 $main::lxdebug->enter_sub();
1255 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1257 my ($try, $spawned_oo, $res);
1260 for ($try = 0; $try < 15; $try++) {
1261 if ($self->is_openoffice_running()) {
1269 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1271 open(STDIN, '/dev/null');
1272 open(STDOUT, '>/dev/null');
1273 my $new_pid = fork();
1275 my $ssres = setsid();
1276 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1277 my @cmdline = ($main::openofficeorg_writer_bin,
1278 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1280 "-accept=socket,host=localhost,port=" .
1281 $main::openofficeorg_daemon_port . ";urp;");
1285 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1290 sleep($try >= 5 ? 2 : 1);
1294 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1295 "OpenOffice could not be started.";
1298 $main::lxdebug->leave_sub();
1303 sub convert_to_pdf {
1304 $main::lxdebug->enter_sub();
1308 my $form = $self->{"form"};
1310 my $filename = $form->{"tmpfile"};
1311 $filename =~ s/.odt$//;
1312 if (substr($filename, 0, 1) ne "/") {
1313 $filename = getcwd() . "/${filename}";
1316 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1317 $ENV{'HOME'} = $self->{"userspath"};
1319 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1322 if (!$self->spawn_xvfb()) {
1323 $main::lxdebug->leave_sub();
1328 if (!$main::openofficeorg_daemon) {
1329 @cmdline = ($main::openofficeorg_writer_bin,
1330 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1332 "file:${filename}.odt",
1333 "macro://" . (split('/', $filename))[-1] .
1334 "/Standard.Conversion.ConvertSelfToPDF()");
1336 if (!$self->spawn_openoffice()) {
1337 $main::lxdebug->leave_sub();
1341 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1342 $main::openofficeorg_daemon_port,
1350 $form->{"tmpfile"} =~ s/odt$/pdf/;
1352 unlink($filename . ".odt");
1354 $main::lxdebug->leave_sub();
1359 unlink($filename . ".odt", $filename . ".pdf");
1360 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1363 $main::lxdebug->leave_sub();
1368 my ($self, $variable) = @_;
1369 my $form = $self->{"form"};
1370 my $iconv = $self->{"iconv"};
1372 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1374 # Allow some HTML markup to be converted into the output format's
1375 # corresponding markup code, e.g. bold or italic.
1376 my $rnd = $self->{"rnd"};
1377 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1378 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1380 foreach my $key (keys(%markup_replace)) {
1381 my $value = $markup_replace{$key};
1382 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1383 $variable =~ s|\</${key}\>|</text:span>|gi;
1386 return $iconv->convert($variable);
1389 sub get_mime_type() {
1390 if ($self->{"form"}->{"format"} =~ /pdf/) {
1391 return "application/pdf";
1393 return "application/vnd.oasis.opendocument.text";
1397 sub uses_temp_file {
1402 ##########################################################
1406 ##########################################################
1408 package XMLTemplate;
1412 @ISA = qw(HTMLTemplate);
1415 #evtl auskommentieren
1418 return $type->SUPER::new(@_);
1422 my ($self, $variable) = @_;
1423 my $form = $self->{"form"};
1425 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1427 # Allow no markup to be converted into the output format
1428 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1430 foreach my $key (@markup_replace) {
1431 $variable =~ s/\<(\/?)${key}\>//g;
1437 sub get_mime_type() {
1440 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1441 return "application/xml ";
1442 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1443 return "application/x-taxbird";
1449 sub uses_temp_file {
1450 # tempfile needet for XML Output