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