]> git.sesse.net Git - ccbs/blob - html/ccbs.pm
Add a simple web interface for adding/updating short titles. (No backend yet.)
[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 strict;
8 use warnings;
9
10 our $start_time;
11
12 BEGIN {
13         $start_time = [Time::HiRes::gettimeofday()];
14 }
15
16 our $ccbs_dbdebug = 0;
17
18 # Set this flag to disable any admin tasks -- it's quite crude, but hey :-)
19 our $ccbs_noadmin = 0;
20         
21 sub print_header {
22         print CGI::header(-type=>'text/html; charset=utf-8');
23 }
24 sub print_see_other {
25         my $location = shift;
26
27         print CGI::header(-status=>'303 See other',
28                           -location=>'http://ccbs.sesse.net/' . $location,
29                           -type=>'text/html; charset=utf-8');
30 }
31
32 sub db_connect {
33         $ccbs_dbdebug = defined(shift) ? 1 : 0;
34
35         my $dbh = DBI->connect("dbi:Pg:dbname=ccbs;host=altersex.samfundet.no", "ccbs", "GeT|>>B_")
36                 or die "Couldn't connect to database";
37         $dbh->{RaiseError} = 1;
38         return $dbh;
39 }
40
41 sub db_fetch_all {
42         my ($dbh, $sql, @parms) = @_;
43         my $q = $dbh->prepare($sql)
44                 or die "Could not prepare query: " . $dbh->errstr;
45         $q->execute(@parms)
46                 or die "Could not execute query: " . $dbh->errstr;
47
48         if ($ccbs_dbdebug) {
49                 warn $sql;
50                 warn "params=" . join(', ', @parms);
51         }
52         
53         my @ret = ();
54         while (my $ref = $q->fetchrow_hashref()) {
55                 if ($ccbs_dbdebug) {
56                         my $dbstr = "";
57                         for my $k (sort keys %$ref) {
58                                 $dbstr .= " " . $k . "=" . $ref->{$k};
59                         }
60                         warn $dbstr;
61                 }
62                 push @ret, $ref;
63         }
64
65         $q->finish;
66         return \@ret;
67 }
68         
69 sub process_template {
70         my ($page, $title, $vars) = @_;
71         $vars->{'page'} = $page;
72         $vars->{'title'} = $title;
73         $vars->{'public'} = $ccbs_noadmin;
74         $vars->{'timetogenerate'} = sprintf "%.3f", Time::HiRes::tv_interval($start_time);
75         
76         my $config = {
77                 INCLUDE_PATH => 'templates/',
78                 INTERPOLATE  => 1,
79                 POST_CHOMP   => 1,
80                 EVAL_PERL    => 1,
81         };
82         my $template = Template->new($config);
83
84         my $output = '';
85         $template->process('main.tmpl', $vars, \$output)
86                 or die $template->error();
87
88         print $output;
89 }
90
91 sub user_error {
92         my $msg = shift;
93
94         ccbs::print_header();
95         ccbs::process_template('user-error.tmpl', 'Feil',
96                 { message => $msg });
97
98         exit;
99 }
100 sub admin_only {
101         user_error("Beklager, databasen står i no-admin-mode.") if ($ccbs_noadmin);
102 }
103
104 $SIG{__DIE__} = sub {
105         # Gosh! Net::Resolver::DNS is brain-damaged.
106         my $msg = shift;
107         return if $msg =~ m#Win32/Registry.pm#;
108
109         ccbs::print_header();
110         ccbs::process_template('error.tmpl', 'Internal Server Error',
111                 { message => HTML::Entities::encode_entities($msg) });
112 };
113
114 1;