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