]>
Commit | Line | Data |
---|---|---|
88fdaf77 TC |
1 | #!/usr/bin/perl -w |
2 | use strict; | |
f8282904 TC |
3 | use FindBin; |
4 | use lib "$FindBin::Bin/modules"; | |
88fdaf77 TC |
5 | use CGI qw(:standard); |
6 | use Carp; # 'verbose'; # remove the 'verbose' in production | |
e0ed81d7 | 7 | use BSE::TB::Articles; |
0b406a07 | 8 | use BSE::Cfg; |
efca965f | 9 | use BSE::DB; |
0b406a07 TC |
10 | use BSE::Template; |
11 | ||
12 | my $cfg = BSE::Cfg->new; | |
efca965f | 13 | BSE::DB->init($cfg); |
88fdaf77 TC |
14 | |
15 | my $id = param('id'); | |
16 | defined $id or $id = 1; | |
17 | # the reason to avoid creating an articles object is that this | |
18 | # loads all of the articles into memory | |
19 | # hopefully a print template won't include too many references to | |
20 | # other articles anyway | |
e0ed81d7 | 21 | #my $articles = BSE::TB::Articles->new; |
88fdaf77 | 22 | |
e0ed81d7 | 23 | my $article = BSE::TB::Articles->getByPkey($id) |
88fdaf77 TC |
24 | or error_page("No article with id $id found"); |
25 | ||
26 | eval "use $article->{generator}"; | |
27 | die $@ if $@; | |
e0ed81d7 | 28 | my $generator = $article->{generator}->new(articles=>'BSE::TB::Articles', cfg => $cfg, |
2d873eb6 | 29 | top => $article); |
88fdaf77 TC |
30 | |
31 | my $template = param('template'); | |
32 | ||
33 | # make sure they don't try to work outside the | |
34 | my $file; | |
0b406a07 | 35 | my $type; |
88fdaf77 TC |
36 | if ($template) { |
37 | if ($template =~ m!^/! || $template =~ /\.\./ || $template !~ /\.tmpl$/i) { | |
38 | error_page("Invalid template name '$template'"); | |
39 | } | |
40 | ||
0b406a07 TC |
41 | $file = file_from_template($template); |
42 | -e $file | |
88fdaf77 | 43 | or error_page("Cannot find template '$template'"); |
0b406a07 TC |
44 | |
45 | $type = $cfg->entry('printable types', $template) | |
46 | || BSE::Template->html_type($cfg); | |
88fdaf77 TC |
47 | } |
48 | else { | |
49 | for my $work ($article->{template}, 'printable.tmpl') { | |
50 | next unless $work =~ /\S/; | |
51 | $template = $work; | |
52 | $file = file_from_template($work); | |
220c179a | 53 | last if $file && -e $file; |
88fdaf77 TC |
54 | } |
55 | -e $file or error_page("No template available for this page"); | |
0b406a07 | 56 | $type = BSE::Template->html_type($cfg); |
88fdaf77 TC |
57 | } |
58 | ||
59 | open TMPLT, "< $file" | |
60 | or error_page("Cannot open template '$template'"); | |
61 | my $text = do { local $/; <TMPLT> }; | |
62 | close TMPLT; | |
63 | ||
64 | $text =~ s/<:\s*embed\s+(?:start|end)\s*:>//g; | |
65 | ||
0b406a07 | 66 | print "Content-Type: $type\n\n"; |
e0ed81d7 | 67 | print $generator->generate_low($text, $article, 'BSE::TB::Articles'); |
88fdaf77 | 68 | |
aefcabcb | 69 | |
88fdaf77 TC |
70 | sub error_page { |
71 | my ($error) = @_; | |
72 | $error ||= "Unknown error"; | |
73 | ||
220c179a | 74 | my %article; |
e0ed81d7 | 75 | my @cols = BSE::TB::Article->columns; |
220c179a TC |
76 | @article{@cols} = ('') x @cols; |
77 | $article{id} = -10; | |
78 | $article{title} = "Error"; | |
79 | $article{parentid} = -1; | |
80 | ||
39e87dbd AO |
81 | require BSE::Generate::Article; |
82 | my $gen = BSE::Generate::Article->new(cfg=>$cfg, top => \%article); | |
88fdaf77 TC |
83 | my %acts; |
84 | %acts = | |
85 | ( | |
e0ed81d7 | 86 | $gen->baseActs('BSE::TB::Articles', \%acts, \%article), |
88fdaf77 TC |
87 | error => sub { CGI::escapeHTML($error) }, |
88 | ); | |
aefcabcb TC |
89 | |
90 | BSE::Template->show_page('error', $cfg, \%acts); | |
88fdaf77 TC |
91 | exit; |
92 | } | |
93 | ||
94 | sub file_from_template { | |
95 | my ($template) = @_; | |
96 | ||
220c179a | 97 | return BSE::Template->find_source("printable/$template", $cfg); |
88fdaf77 | 98 | } |