]> git.sesse.net Git - ccbs/blob - html/ccbs.pm
7940dd92fbf028514947221cca49709e609c4a11
[ccbs] / html / ccbs.pm
1 package ccbs;
2 use Template;
3 use CGI;
4 use DBI;
5 use HTML::Entities;
6 use Time::HiRes;
7 use Locale::gettext;
8 use POSIX;
9 use strict;
10 use warnings;
11 require '../intl/Sesse::GettextizeTemplates.pm';
12
13 require '../config.pm';
14 -r '../config.local.pm' and require '../config.local.pm';
15
16 POSIX::setlocale( &POSIX::LC_CTYPE , $ccbs::config::lang );
17 POSIX::setlocale( &POSIX::LC_MESSAGES , $ccbs::config::lang );
18 Locale::gettext::bindtextdomain("ccbs", "po");
19 Locale::gettext::textdomain("ccbs");
20
21 our $start_time;
22
23 BEGIN {
24         $start_time = [Time::HiRes::gettimeofday()];
25 }
26
27 # Hack to get the non-templatized gettext stuff working
28 *_ = sub {
29         return Locale::gettext::gettext(@_);
30 };
31
32 sub print_header {
33         print CGI::header(-type=>'text/html; charset=utf-8');
34 }
35 sub print_see_other {
36         my $location = shift;
37
38         print CGI::header(-status=>'303 See other',
39                           -location=>'http://ccbs.sesse.net/' . $location,
40                           -type=>'text/html; charset=utf-8');
41 }
42
43 sub db_connect {
44         my $dbh = DBI->connect("dbi:Pg:dbname=$ccbs::config::dbname;host=$ccbs::config::dbhost", $ccbs::config::dbuser, $ccbs::config::dbpass)
45                 or die "Couldn't connect to database";
46         $dbh->{RaiseError} = 1;
47         return $dbh;
48 }
49
50 sub db_fetch_all {
51         my ($dbh, $sql, @parms) = @_;
52         my $q = $dbh->prepare($sql)
53                 or die "Could not prepare query: " . $dbh->errstr;
54         $q->execute(@parms)
55                 or die "Could not execute query: " . $dbh->errstr;
56
57         if ($config::ccbs::dbdebug) {
58                 warn $sql;
59                 warn "params=" . join(', ', @parms);
60         }
61         
62         my @ret = ();
63         while (my $ref = $q->fetchrow_hashref()) {
64                 if ($config::ccbs::dbdebug) {
65                         my $dbstr = "";
66                         for my $k (sort keys %$ref) {
67                                 $dbstr .= " " . $k . "=" . $ref->{$k};
68                         }
69                         warn $dbstr;
70                 }
71                 push @ret, $ref;
72         }
73
74         $q->finish;
75         return \@ret;
76 }
77         
78 sub process_template {
79         my ($page, $title, $vars) = @_;
80         $vars->{'page'} = $page;
81         $vars->{'title'} = $title;
82         $vars->{'public'} = $ccbs::config::noadmin;
83         $vars->{'timetogenerate'} = sprintf "%.3f", Time::HiRes::tv_interval($start_time);
84         
85         my $config = {
86                 INCLUDE_PATH => 'templates/',
87                 INTERPOLATE  => 1,
88                 POST_CHOMP   => 1,
89                 EVAL_PERL    => 1,
90                 FACTORY      => 'Sesse::GettextizeTemplates'
91         };
92         my $template = Template->new($config);
93
94         my $output = '';
95         $template->process($ccbs::config::main_template, $vars, \$output)
96                 or die $template->error();
97
98         print $output;
99 }
100
101 sub user_error {
102         my $msg = shift;
103
104         ccbs::print_header();
105         ccbs::process_template('user-error.tmpl', _('Error'),
106                 { message => $msg });
107
108         exit;
109 }
110 sub admin_only {
111         user_error(_("Sorry, the database is in no-admin-mode.")) if ($config::ccbs::noadmin);
112 }
113
114 $SIG{__DIE__} = sub {
115         # Gosh! Net::Resolver::DNS is brain-damaged.
116         my $msg = shift;
117         return if $msg =~ m#Win32/Registry.pm#;
118
119         ccbs::print_header();
120         ccbs::process_template('error.tmpl', _('Internal Server Error'),
121                 { message => HTML::Entities::encode_entities($msg) });
122 };
123
124 1;