]> git.sesse.net Git - ccbs/blob - html/ccbs.pm
6dd7b1325421bee056436a69b018483de9f5a0d8
[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         POSIX::setlocale( &POSIX::LC_CTYPE , "nb_NO.UTF-8" );
19         POSIX::setlocale( &POSIX::LC_MESSAGES , "nb_NO.UTF-8" );
20         Locale::gettext::bindtextdomain("ccbs", "po");
21         Locale::gettext::textdomain("ccbs");
22 }
23
24 our $ccbs_dbdebug = 0;
25
26 # Set this flag to disable any admin tasks -- it's quite crude, but hey :-)
27 our $ccbs_noadmin = 0;
28
29 # Hack to get the non-templatized gettext stuff working
30 *_ = sub {
31         return Locale::gettext::gettext(@_);
32 };
33
34 sub print_header {
35         print CGI::header(-type=>'text/html; charset=utf-8');
36 }
37 sub print_see_other {
38         my $location = shift;
39
40         print CGI::header(-status=>'303 See other',
41                           -location=>'http://ccbs.sesse.net/' . $location,
42                           -type=>'text/html; charset=utf-8');
43 }
44
45 sub db_connect {
46         $ccbs_dbdebug = defined(shift) ? 1 : 0;
47
48         my $dbh = DBI->connect("dbi:Pg:dbname=ccbs;host=www.positivegaming.com", "ccbs", "GeT|>>B_")
49                 or die "Couldn't connect to database";
50         $dbh->{RaiseError} = 1;
51         return $dbh;
52 }
53
54 sub db_fetch_all {
55         my ($dbh, $sql, @parms) = @_;
56         my $q = $dbh->prepare($sql)
57                 or die "Could not prepare query: " . $dbh->errstr;
58         $q->execute(@parms)
59                 or die "Could not execute query: " . $dbh->errstr;
60
61         if ($ccbs_dbdebug) {
62                 warn $sql;
63                 warn "params=" . join(', ', @parms);
64         }
65         
66         my @ret = ();
67         while (my $ref = $q->fetchrow_hashref()) {
68                 if ($ccbs_dbdebug) {
69                         my $dbstr = "";
70                         for my $k (sort keys %$ref) {
71                                 $dbstr .= " " . $k . "=" . $ref->{$k};
72                         }
73                         warn $dbstr;
74                 }
75                 push @ret, $ref;
76         }
77
78         $q->finish;
79         return \@ret;
80 }
81         
82 sub process_template {
83         my ($page, $title, $vars) = @_;
84         $vars->{'page'} = $page;
85         $vars->{'title'} = $title;
86         $vars->{'public'} = $ccbs_noadmin;
87         $vars->{'timetogenerate'} = sprintf "%.3f", Time::HiRes::tv_interval($start_time);
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;