]> git.imager.perl.org - bse.git/blob - site/cgi-bin/printable.pl
allow metadata to be defined for new products
[bse.git] / site / cgi-bin / printable.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use FindBin;
4 use lib "$FindBin::Bin/modules";
5 use CGI qw(:standard);
6 use Carp; # 'verbose'; # remove the 'verbose' in production
7 use BSE::TB::Articles;
8 use BSE::Cfg;
9 use BSE::DB;
10 use BSE::Template;
11
12 my $cfg = BSE::Cfg->new;
13 BSE::DB->init($cfg);
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
21 #my $articles = BSE::TB::Articles->new;
22
23 my $article = BSE::TB::Articles->getByPkey($id)
24   or error_page("No article with id $id found");
25
26 eval "use $article->{generator}";
27 die $@ if $@;
28 my $generator = $article->{generator}->new(articles=>'BSE::TB::Articles', cfg => $cfg,
29                                           top => $article);
30
31 my $template = param('template');
32
33 # make sure they don't try to work outside the 
34 my $file;
35 my $type;
36 if ($template) {
37   if ($template =~ m!^/! || $template =~ /\.\./ || $template !~ /\.tmpl$/i) {
38     error_page("Invalid template name '$template'");
39   }
40
41   $file = file_from_template($template);
42   -e $file
43     or error_page("Cannot find template '$template'");
44
45   $type = $cfg->entry('printable types', $template)
46     || BSE::Template->html_type($cfg);
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);
53     last if $file && -e $file;
54   }
55   -e $file or error_page("No template available for this page");
56   $type = BSE::Template->html_type($cfg);
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
66 print "Content-Type: $type\n\n";
67 print $generator->generate_low($text, $article, 'BSE::TB::Articles');
68
69
70 sub error_page {
71   my ($error) = @_;
72   $error ||= "Unknown error";
73
74   my %article;
75   my @cols = BSE::TB::Article->columns;
76   @article{@cols} = ('') x @cols;
77   $article{id} = -10;
78   $article{title} = "Error";
79   $article{parentid} = -1;
80
81   require BSE::Generate::Article;
82   my $gen = BSE::Generate::Article->new(cfg=>$cfg, top => \%article);
83   my %acts;
84   %acts = 
85     (
86      $gen->baseActs('BSE::TB::Articles', \%acts, \%article),
87      error => sub { CGI::escapeHTML($error) },
88     );
89   
90   BSE::Template->show_page('error', $cfg, \%acts);
91   exit;
92 }
93
94 sub file_from_template {
95   my ($template) = @_;
96
97   return BSE::Template->find_source("printable/$template", $cfg);
98 }