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