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
341 (not\b|\!)? # $1 -- Eventuelle Negierung
343 (\b.+?\b) # $2 -- Name der zu überprüfenden Variablen
344 ( # $3 -- Beginn des optionalen Vergleiches
346 ([!=]) # $4 -- Negierung des Vergleiches speichern
347 ([=~]) # $5 -- Art des Vergleiches speichern
349 ( # $6 -- Gequoteter String oder Bareword
350 "(.*?)(?<!\\)" # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
352 (\b.+?\b) # $8 -- 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_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
366 my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
367 my $quoted_word = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
368 my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
370 $not = !$not if ($operator_neg && $operator_neg eq '!');
372 substr($contents, 0, length($&)) = "";
374 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
376 $self->{"error"} = "Unclosed $self->{tag_start}if${not}$self->{tag_end}." unless ($self->{"error"});
377 $main::lxdebug->leave_sub();
381 my $value = $self->_get_loop_variable($var, 0, @indices);
384 if ($operator_type) {
385 my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
386 if ($operator_type eq '=') {
387 $hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to));
389 $hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
393 $hit = ($not && ! $value) || (!$not && $value);
397 my $new_text = $self->parse_block($block, @indices);
398 if (!defined($new_text)) {
399 $main::lxdebug->leave_sub();
402 $new_contents .= $new_text;
407 $main::lxdebug->leave_sub();
409 return $new_contents;
412 sub parse_first_line {
414 my $line = shift || "";
416 if ($line =~ m/([^\s]+)set-tag-style([^\s]+)/) {
418 $self->{error} = "The tag start and end markers must not be equal.";
422 $self->set_tag_style($1, $2);
428 sub _parse_config_option {
435 my ($key, $value) = split m/\s*=\s*/, $line, 2;
437 if ($key eq 'tag-style') {
438 $self->set_tag_style(split(m/\s+/, $value, 2));
442 sub _parse_config_lines {
446 my ($comment_start, $comment_end) = ("", "");
448 if (ref $self eq 'LaTeXTemplate') {
449 $comment_start = '\s*%';
450 } elsif (ref $self eq 'HTMLTemplate') {
451 $comment_start = '\s*<!--';
452 $comment_end = '>\s*';
454 $comment_start = '\s*\#';
457 my $num_lines = scalar @{ $lines };
460 while ($i < $num_lines) {
461 my $line = $lines->[$i];
463 if ($line !~ m/^${comment_start}\s*config\s*:(.*)${comment_end}$/i) {
468 $self->_parse_config_option($1);
469 splice @{ $lines }, $i, 1;
474 sub _force_mandatory_packages {
478 my (%used_packages, $document_start_line);
480 foreach my $i (0 .. scalar @{ $lines } - 1) {
481 if ($lines->[$i] =~ m/\\usepackage[^{]*{(.*?)}/) {
482 $used_packages{$1} = 1;
484 } elsif ($lines->[$i] =~ m/\\begin{document}/) {
485 $document_start_line = $i;
491 $document_start_line = scalar @{ $lines } - 1 if (!defined $document_start_line);
493 if (!$used_packages{textcomp}) {
494 splice @{ $lines }, $document_start_line, 0, "\\usepackage{textcomp}\n";
495 $document_start_line++;
502 my $form = $self->{"form"};
504 if (!open(IN, "$form->{templates}/$form->{IN}")) {
505 $self->{"error"} = "$!";
511 $self->_parse_config_lines(\@lines);
512 $self->_force_mandatory_packages(\@lines) if (ref $self eq 'LaTeXTemplate');
514 my $contents = join("", @lines);
516 # detect pagebreak block and its parameters
517 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) {
518 $self->{"chars_per_line"} = $1;
519 $self->{"lines_on_first_page"} = $2;
520 $self->{"lines_on_second_page"} = $3;
521 $self->{"pagebreak_block"} = $4;
523 substr($contents, length($`), length($&)) = "";
526 $self->{"forced_pagebreaks"} = [];
528 my $new_contents = $self->parse_block($contents);
529 if (!defined($new_contents)) {
530 $main::lxdebug->leave_sub();
534 print(OUT $new_contents);
536 if ($form->{"format"} =~ /postscript/i) {
537 return $self->convert_to_postscript();
538 } elsif ($form->{"format"} =~ /pdf/i) {
539 return $self->convert_to_pdf();
545 sub convert_to_postscript {
547 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
549 # Convert the tex file to postscript
551 if (!chdir("$userspath")) {
552 $self->{"error"} = "chdir : $!";
557 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
559 my $latex = $self->_get_latex_path();
561 for (my $run = 1; $run <= 2; $run++) {
562 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
563 "> $form->{tmpfile}.err");
565 $self->{"error"} = $form->cleanup();
571 $form->{tmpfile} =~ s/tex$/dvi/;
573 system("dvips $form->{tmpfile} -o -q > /dev/null");
575 $self->{"error"} = "dvips : $!";
579 $form->{tmpfile} =~ s/dvi$/ps/;
588 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
590 # Convert the tex file to PDF
592 if (!chdir("$userspath")) {
593 $self->{"error"} = "chdir : $!";
598 $form->{tmpfile} =~ s/\Q$userspath\E\///g;
600 my $latex = $self->_get_latex_path();
602 for (my $run = 1; $run <= 2; $run++) {
603 system("${latex} --interaction=nonstopmode $form->{tmpfile} " .
604 "> $form->{tmpfile}.err");
606 $self->{"error"} = $form->cleanup();
612 $form->{tmpfile} =~ s/tex$/pdf/;
617 sub _get_latex_path {
618 return $main::latex_bin || 'pdflatex';
621 sub get_mime_type() {
624 if ($self->{"form"}->{"format"} =~ /postscript/i) {
625 return "application/postscript";
627 return "application/pdf";
640 package HTMLTemplate;
644 @ISA = qw(LaTeXTemplate);
649 return $type->SUPER::new(@_);
653 my ($self, $variable) = @_;
654 my $form = $self->{"form"};
656 $variable = $main::locale->quote_special_chars('Template/HTML', $variable);
658 # Allow some HTML markup to be converted into the output format's
659 # corresponding markup code, e.g. bold or italic.
660 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
662 foreach my $key (@markup_replace) {
663 $variable =~ s/\<(\/?)${key}\>/<$1${key}>/g;
669 sub get_mime_type() {
672 if ($self->{"form"}->{"format"} =~ /postscript/i) {
673 return "application/postscript";
674 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
675 return "application/pdf";
684 if ($self->{"form"}->{"format"} =~ /postscript/i) {
686 } elsif ($self->{"form"}->{"format"} =~ /pdf/i) {
693 sub convert_to_postscript {
695 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
697 # Convert the HTML file to postscript
699 if (!chdir("$userspath")) {
700 $self->{"error"} = "chdir : $!";
705 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
706 my $psfile = $form->{"tmpfile"};
707 $psfile =~ s/.html/.ps/;
708 if ($psfile eq $form->{"tmpfile"}) {
712 system("html2ps -f html2ps-config < $form->{tmpfile} > $psfile");
714 $self->{"error"} = $form->cleanup();
719 $form->{"tmpfile"} = $psfile;
728 my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
730 # Convert the HTML file to PDF
732 if (!chdir("$userspath")) {
733 $self->{"error"} = "chdir : $!";
738 $form->{"tmpfile"} =~ s/\Q$userspath\E\///g;
739 my $pdffile = $form->{"tmpfile"};
740 $pdffile =~ s/.html/.pdf/;
741 if ($pdffile eq $form->{"tmpfile"}) {
745 system("html2ps -f html2ps-config < $form->{tmpfile} | ps2pdf - $pdffile");
747 $self->{"error"} = $form->cleanup();
752 $form->{"tmpfile"} = $pdffile;
761 #### PlainTextTemplate
764 package PlainTextTemplate;
768 @ISA = qw(LaTeXTemplate);
773 return $type->SUPER::new(@_);
777 my ($self, $variable) = @_;
792 #### OpenDocumentTemplate
795 package OpenDocumentTemplate;
803 # use File::Temp qw(:mktemp);
806 @ISA = qw(SimpleTemplate);
811 $self = $type->SUPER::new(@_);
813 foreach my $module (qw(Archive::Zip Text::Iconv)) {
814 eval("use ${module};");
816 $self->{"form"}->error("The Perl module '${module}' could not be " .
817 "loaded. Support for OpenDocument templates " .
818 "does not work without it. Please install your " .
819 "distribution's package or get the module from " .
820 "CPAN ( http://www.cpan.org ).");
824 $self->{"rnd"} = int(rand(1000000));
825 $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
827 $self->set_tag_style('<%', '%>');
833 my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
835 my ($form, $new_contents) = ($self->{"form"}, "");
837 my $ary = $self->_get_loop_variable($var, 1, @indices);
839 for (my $i = 0; $i < scalar(@{$ary}); $i++) {
840 $form->{"__first__"} = $i == 0;
841 $form->{"__last__"} = ($i + 1) == scalar(@{$ary});
842 $form->{"__odd__"} = (($i + 1) % 2) == 1;
843 $form->{"__counter__"} = $i + 1;
844 my $new_text = $self->parse_block($text, (@indices, $i));
845 return undef unless (defined($new_text));
846 $new_contents .= $start_tag . $new_text . $end_tag;
848 map({ delete($form->{"__${_}__"}); } qw(first last odd counter));
850 return $new_contents;
854 my ($self, $text, $pos, $var, $not) = @_;
857 $pos = 0 unless ($pos);
859 while ($pos < length($text)) {
862 next if (substr($text, $pos - 1, 5) ne '<%');
864 if ((substr($text, $pos + 4, 2) eq 'if') || (substr($text, $pos + 4, 3) eq 'for')) {
867 } elsif ((substr($text, $pos + 4, 4) eq 'else') && (1 == $depth)) {
869 $self->{"error"} = '<%else%> outside of <%if%> / <%ifnot%>.';
873 my $block = substr($text, 0, $pos - 1);
874 substr($text, 0, $pos - 1) = "";
875 $text =~ s!^\<\%[^\%]+\%\>!!;
876 $text = '<%if' . ($not ? " " : "not ") . $var . '%>' . $text;
878 return ($block, $text);
880 } elsif (substr($text, $pos + 4, 3) eq 'end') {
883 my $block = substr($text, 0, $pos - 1);
884 substr($text, 0, $pos - 1) = "";
885 $text =~ s!^\<\%[^\%]+\%\>!!;
887 return ($block, $text);
896 $main::lxdebug->enter_sub();
898 my ($self, $contents, @indices) = @_;
900 my $new_contents = "";
902 while ($contents ne "") {
903 if (substr($contents, 0, 1) eq "<") {
904 $contents =~ m|^<[^>]+>|;
906 substr($contents, 0, length($&)) = "";
908 if ($tag =~ m|<table:table-row|) {
909 $contents =~ m|^(.*?)(</table:table-row[^>]*>)|;
912 substr($contents, 0, length($1) + length($end_tag)) = "";
914 if ($table_row =~ m|\<\%foreachrow\s+(.*?)\%\>|) {
917 substr($table_row, length($`), length($&)) = "";
919 my ($t1, $t2) = $self->find_end($table_row, length($`));
921 $self->{"error"} = "Unclosed <\%foreachrow\%>." unless ($self->{"error"});
922 $main::lxdebug->leave_sub();
926 my $new_text = $self->parse_foreach($var, $t1 . $t2, $tag, $end_tag, @indices);
927 if (!defined($new_text)) {
928 $main::lxdebug->leave_sub();
931 $new_contents .= $new_text;
934 my $new_text = $self->parse_block($table_row, @indices);
935 if (!defined($new_text)) {
936 $main::lxdebug->leave_sub();
939 $new_contents .= $tag . $new_text . $end_tag;
943 $new_contents .= $tag;
947 $contents =~ /^[^<]+/;
950 my $pos_if = index($text, '<%if');
951 my $pos_foreach = index($text, '<%foreach');
953 if ((-1 == $pos_if) && (-1 == $pos_foreach)) {
954 substr($contents, 0, length($text)) = "";
955 $new_contents .= $self->substitute_vars($text, @indices);
959 if ((-1 == $pos_if) || ((-1 != $pos_foreach) && ($pos_if > $pos_foreach))) {
960 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_foreach), @indices);
961 substr($contents, 0, $pos_foreach) = "";
963 if ($contents !~ m|^\<\%foreach (.*?)\%\>|) {
964 $self->{"error"} = "Malformed <\%foreach\%>.";
965 $main::lxdebug->leave_sub();
971 substr($contents, 0, length($&)) = "";
974 ($block, $contents) = $self->find_end($contents);
976 $self->{"error"} = "Unclosed <\%foreach\%>." unless ($self->{"error"});
977 $main::lxdebug->leave_sub();
981 my $new_text = $self->parse_foreach($var, $block, "", "", @indices);
982 if (!defined($new_text)) {
983 $main::lxdebug->leave_sub();
986 $new_contents .= $new_text;
989 $new_contents .= $self->substitute_vars(substr($contents, 0, $pos_if), @indices);
990 substr($contents, 0, $pos_if) = "";
992 if ($contents !~ m|^\<\%if\s*(not)?\s+(.*?)\%\>|) {
993 $self->{"error"} = "Malformed <\%if\%>.";
994 $main::lxdebug->leave_sub();
998 my ($not, $var) = ($1, $2);
1000 substr($contents, 0, length($&)) = "";
1002 ($block, $contents) = $self->find_end($contents, 0, $var, $not);
1004 $self->{"error"} = "Unclosed <\%if${not}\%>." unless ($self->{"error"});
1005 $main::lxdebug->leave_sub();
1009 my $value = $self->{"form"}->{$var};
1010 for (my $i = 0; $i < scalar(@indices); $i++) {
1011 last unless (ref($value) eq "ARRAY");
1012 $value = $value->[$indices[$i]];
1015 if (($not && !$value) || (!$not && $value)) {
1016 my $new_text = $self->parse_block($block, @indices);
1017 if (!defined($new_text)) {
1018 $main::lxdebug->leave_sub();
1021 $new_contents .= $new_text;
1027 $main::lxdebug->leave_sub();
1029 return $new_contents;
1033 $main::lxdebug->enter_sub();
1037 my $form = $self->{"form"};
1042 if ($form->{"IN"} =~ m|^/|) {
1043 $file_name = $form->{"IN"};
1045 $file_name = $form->{"templates"} . "/" . $form->{"IN"};
1048 my $zip = Archive::Zip->new();
1049 if (Archive::Zip::AZ_OK != $zip->read($file_name)) {
1050 $self->{"error"} = "File not found/is not a OpenDocument file.";
1051 $main::lxdebug->leave_sub();
1055 my $contents = $zip->contents("content.xml");
1057 $self->{"error"} = "File is not a OpenDocument file.";
1058 $main::lxdebug->leave_sub();
1062 my $rnd = $self->{"rnd"};
1063 my $new_styles = qq|<style:style style:name="TLXO${rnd}BOLD" style:family="text">
1064 <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>
1066 <style:style style:name="TLXO${rnd}ITALIC" style:family="text">
1067 <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/>
1069 <style:style style:name="TLXO${rnd}UNDERLINE" style:family="text">
1070 <style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/>
1072 <style:style style:name="TLXO${rnd}STRIKETHROUGH" style:family="text">
1073 <style:text-properties style:text-line-through-style="solid"/>
1075 <style:style style:name="TLXO${rnd}SUPER" style:family="text">
1076 <style:text-properties style:text-position="super 58%"/>
1078 <style:style style:name="TLXO${rnd}SUB" style:family="text">
1079 <style:text-properties style:text-position="sub 58%"/>
1083 $contents =~ s|</office:automatic-styles>|${new_styles}</office:automatic-styles>|;
1084 $contents =~ s|[\n\r]||gm;
1086 my $new_contents = $self->parse_block($contents);
1087 if (!defined($new_contents)) {
1088 $main::lxdebug->leave_sub();
1092 # $new_contents =~ s|>|>\n|g;
1094 $zip->contents("content.xml", $new_contents);
1096 my $styles = $zip->contents("styles.xml");
1098 my $new_styles = $self->parse_block($styles);
1099 if (!defined($new_contents)) {
1100 $main::lxdebug->leave_sub();
1103 $zip->contents("styles.xml", $new_styles);
1106 $zip->writeToFileNamed($form->{"tmpfile"}, 1);
1109 if ($form->{"format"} =~ /pdf/) {
1110 $res = $self->convert_to_pdf();
1113 $main::lxdebug->leave_sub();
1117 sub is_xvfb_running {
1118 $main::lxdebug->enter_sub();
1123 my $dfname = $self->{"userspath"} . "/xvfb_display";
1126 $main::lxdebug->message(LXDebug::DEBUG2, " Looking for $dfname\n");
1127 if ((-f $dfname) && open(IN, $dfname)) {
1132 my $xauthority = <IN>;
1136 $main::lxdebug->message(LXDebug::DEBUG2, " found with $pid and $display\n");
1138 if ((! -d "/proc/$pid") || !open(IN, "/proc/$pid/cmdline")) {
1139 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #1\n");
1140 unlink($dfname, $xauthority);
1141 $main::lxdebug->leave_sub();
1146 if ($line !~ /xvfb/i) {
1147 $main::lxdebug->message(LXDebug::DEBUG2, " no/wrong process #2\n");
1148 unlink($dfname, $xauthority);
1149 $main::lxdebug->leave_sub();
1153 $ENV{"XAUTHORITY"} = $xauthority;
1154 $ENV{"DISPLAY"} = $display;
1156 $main::lxdebug->message(LXDebug::DEBUG2, " not found\n");
1159 $main::lxdebug->leave_sub();
1165 $main::lxdebug->enter_sub();
1169 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_xvfb()\n");
1171 my $display = $self->is_xvfb_running();
1174 $main::lxdebug->leave_sub();
1179 while ( -f "/tmp/.X${display}-lock") {
1182 $display = ":${display}";
1183 $main::lxdebug->message(LXDebug::DEBUG2, " display $display\n");
1185 my $mcookie = `mcookie`;
1186 die("Installation error: mcookie not found.") if ($? != 0);
1189 $main::lxdebug->message(LXDebug::DEBUG2, " mcookie $mcookie\n");
1191 my $xauthority = "/tmp/.Xauthority-" . $$ . "-" . time() . "-" . int(rand(9999999));
1192 $ENV{"XAUTHORITY"} = $xauthority;
1194 $main::lxdebug->message(LXDebug::DEBUG2, " xauthority $xauthority\n");
1196 system("xauth add \"${display}\" . \"${mcookie}\"");
1198 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started (xauth: $!)";
1199 $main::lxdebug->leave_sub();
1203 $main::lxdebug->message(LXDebug::DEBUG2, " about to fork()\n");
1207 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1208 exec($main::xvfb_bin, $display, "-screen", "0", "640x480x8", "-nolisten", "tcp");
1211 $main::lxdebug->message(LXDebug::DEBUG2, " parent dont sleeping\n");
1214 my $dfname = $self->{"userspath"} . "/xvfb_display";
1215 if (!open(OUT, ">$dfname")) {
1216 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started ($dfname: $!)";
1217 unlink($xauthority);
1219 $main::lxdebug->leave_sub();
1222 print(OUT "$pid\n$display\n$xauthority\n");
1225 $main::lxdebug->message(LXDebug::DEBUG2, " parent re-testing\n");
1227 if (!$self->is_xvfb_running()) {
1228 $self->{"error"} = "Conversion to PDF failed because OpenOffice could not be started.";
1229 unlink($xauthority, $dfname);
1231 $main::lxdebug->leave_sub();
1235 $main::lxdebug->message(LXDebug::DEBUG2, " spawn OK\n");
1237 $main::lxdebug->leave_sub();
1242 sub is_openoffice_running {
1243 $main::lxdebug->enter_sub();
1245 system("./scripts/oo-uno-test-conn.py $main::openofficeorg_daemon_port " .
1246 "> /dev/null 2> /dev/null");
1248 $main::lxdebug->message(LXDebug::DEBUG2, " is_openoffice_running(): $?\n");
1250 $main::lxdebug->leave_sub();
1255 sub spawn_openoffice {
1256 $main::lxdebug->enter_sub();
1260 $main::lxdebug->message(LXDebug::DEBUG2, "spawn_openoffice()\n");
1262 my ($try, $spawned_oo, $res);
1265 for ($try = 0; $try < 15; $try++) {
1266 if ($self->is_openoffice_running()) {
1274 $main::lxdebug->message(LXDebug::DEBUG2, " Child daemonizing\n");
1276 open(STDIN, '/dev/null');
1277 open(STDOUT, '>/dev/null');
1278 my $new_pid = fork();
1280 my $ssres = setsid();
1281 $main::lxdebug->message(LXDebug::DEBUG2, " Child execing\n");
1282 my @cmdline = ($main::openofficeorg_writer_bin,
1283 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1285 "-accept=socket,host=localhost,port=" .
1286 $main::openofficeorg_daemon_port . ";urp;");
1290 $main::lxdebug->message(LXDebug::DEBUG2, " Parent after fork\n");
1295 sleep($try >= 5 ? 2 : 1);
1299 $self->{"error"} = "Conversion from OpenDocument to PDF failed because " .
1300 "OpenOffice could not be started.";
1303 $main::lxdebug->leave_sub();
1308 sub convert_to_pdf {
1309 $main::lxdebug->enter_sub();
1313 my $form = $self->{"form"};
1315 my $filename = $form->{"tmpfile"};
1316 $filename =~ s/.odt$//;
1317 if (substr($filename, 0, 1) ne "/") {
1318 $filename = getcwd() . "/${filename}";
1321 if (substr($self->{"userspath"}, 0, 1) eq "/") {
1322 $ENV{'HOME'} = $self->{"userspath"};
1324 $ENV{'HOME'} = getcwd() . "/" . $self->{"userspath"};
1327 if (!$self->spawn_xvfb()) {
1328 $main::lxdebug->leave_sub();
1333 if (!$main::openofficeorg_daemon) {
1334 @cmdline = ($main::openofficeorg_writer_bin,
1335 "-minimized", "-norestore", "-nologo", "-nolockcheck",
1337 "file:${filename}.odt",
1338 "macro://" . (split('/', $filename))[-1] .
1339 "/Standard.Conversion.ConvertSelfToPDF()");
1341 if (!$self->spawn_openoffice()) {
1342 $main::lxdebug->leave_sub();
1346 @cmdline = ("./scripts/oo-uno-convert-pdf.py",
1347 $main::openofficeorg_daemon_port,
1355 $form->{"tmpfile"} =~ s/odt$/pdf/;
1357 unlink($filename . ".odt");
1359 $main::lxdebug->leave_sub();
1364 unlink($filename . ".odt", $filename . ".pdf");
1365 $self->{"error"} = "Conversion from OpenDocument to PDF failed. " .
1368 $main::lxdebug->leave_sub();
1373 my ($self, $variable) = @_;
1374 my $form = $self->{"form"};
1375 my $iconv = $self->{"iconv"};
1377 $variable = $main::locale->quote_special_chars('Template/OpenDocument', $variable);
1379 # Allow some HTML markup to be converted into the output format's
1380 # corresponding markup code, e.g. bold or italic.
1381 my $rnd = $self->{"rnd"};
1382 my %markup_replace = ("b" => "BOLD", "i" => "ITALIC", "s" => "STRIKETHROUGH",
1383 "u" => "UNDERLINE", "sup" => "SUPER", "sub" => "SUB");
1385 foreach my $key (keys(%markup_replace)) {
1386 my $value = $markup_replace{$key};
1387 $variable =~ s|\<${key}\>|<text:span text:style-name=\"TLXO${rnd}${value}\">|gi; #"
1388 $variable =~ s|\</${key}\>|</text:span>|gi;
1391 return $iconv->convert($variable);
1394 sub get_mime_type() {
1395 if ($self->{"form"}->{"format"} =~ /pdf/) {
1396 return "application/pdf";
1398 return "application/vnd.oasis.opendocument.text";
1402 sub uses_temp_file {
1407 ##########################################################
1411 ##########################################################
1413 package XMLTemplate;
1417 @ISA = qw(HTMLTemplate);
1420 #evtl auskommentieren
1423 return $type->SUPER::new(@_);
1427 my ($self, $variable) = @_;
1428 my $form = $self->{"form"};
1430 $variable = $main::locale->quote_special_chars('Template/XML', $variable);
1432 # Allow no markup to be converted into the output format
1433 my @markup_replace = ('b', 'i', 's', 'u', 'sub', 'sup');
1435 foreach my $key (@markup_replace) {
1436 $variable =~ s/\<(\/?)${key}\>//g;
1442 sub get_mime_type() {
1445 if ($self->{"form"}->{"format"} =~ /elsterwinston/i) {
1446 return "application/xml ";
1447 } elsif ($self->{"form"}->{"format"} =~ /elstertaxbird/i) {
1448 return "application/x-taxbird";
1454 sub uses_temp_file {
1455 # tempfile needet for XML Output