]> git.sesse.net Git - ccbs/blob - html/ccbs.pm
Add hidden tournament and round form elements.
[ccbs] / html / ccbs.pm
1 package ccbs;
2 use Template;
3 use CGI;
4 use DBI;
5 use strict;
6 use warnings;
7
8 our $ccbs_dbdebug = 0;
9         
10 sub print_header {
11         print CGI::header(-type=>'application/xhtml+xml; charset=utf-8');
12 }
13 sub print_see_other {
14         my $location = shift;
15
16         print CGI::header(-status=>'303 See other',
17                           -location=>'http://ccbs.sesse.net/' . $location,
18                           -type=>'text/html; charset=utf-8');
19 }
20
21 sub db_connect {
22         $ccbs_dbdebug = defined(shift) ? 1 : 0;
23
24         my $dbh = DBI->connect("dbi:Pg:dbname=ccbs;host=sql.samfundet.no", "ccbs", "GeT|>>B_")
25                 or die "Couldn't connect to database";
26         $dbh->{RaiseError} = 1;
27         return $dbh;
28 }
29
30 sub db_fetch_all {
31         my ($dbh, $sql, @parms) = @_;
32         my $q = $dbh->prepare($sql)
33                 or die "Could not prepare query: " . $dbh->errstr;
34         $q->execute(@parms)
35                 or die "Could not execute query: " . $dbh->errstr;
36
37         if ($ccbs_dbdebug) {
38                 warn $sql;
39                 warn "params=" . join(', ', @parms);
40         }
41         
42         my @ret = ();
43         while (my $ref = $q->fetchrow_hashref()) {
44                 if ($ccbs_dbdebug) {
45                         my $dbstr = "";
46                         for my $k (sort keys %$ref) {
47                                 $dbstr .= " " . $k . "=" . $ref->{$k};
48                         }
49                         warn $dbstr;
50                 }
51                 push @ret, $ref;
52         }
53
54         $q->finish;
55         return \@ret;
56 }
57         
58 sub process_template {
59         my ($page, $title, $vars) = @_;
60         $vars->{'page'} = $page;
61         $vars->{'title'} = $title;
62         
63         my $config = {
64                 INCLUDE_PATH => 'templates/',
65                 INTERPOLATE  => 1,
66                 POST_CHOMP   => 1,
67                 EVAL_PERL    => 1,
68         };
69         my $template = Template->new($config);
70
71         my $output = '';
72         $template->process('main.tmpl', $vars, \$output)
73                 or die $template->error();
74
75         print $output;
76 }
77
78 sub user_error {
79         my $msg = shift;
80
81         ccbs::print_header();
82         ccbs::process_template('user-error.tmpl', 'Feil',
83                 { message => $msg });
84
85         exit;
86 }
87
88 $SIG{__DIE__} = sub {
89         # Gosh! Net::Resolver::DNS is brain-damaged.
90         my $msg = shift;
91         return if $msg =~ m#Win32/Registry.pm#;
92
93         ccbs::print_header();
94         ccbs::process_template('error.tmpl', 'Internal Server Error',
95                 { message => $msg });
96 };
97
98 1;