1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
package PgCommitFest::Request;
require CGI::Fast;
require PgCommitFest::DB;
require PgCommitFest::WebControl;
require Template;
require Template::Plugin::HTML;
use strict;
use warnings;
die "Must set PGCOMMITFEST_ROOT to root directory of installation!\n"
if !defined $ENV{'PGCOMMITFEST_ROOT'} || ! -d $ENV{'PGCOMMITFEST_ROOT'};
our $ROOT = $ENV{'PGCOMMITFEST_ROOT'};
our $template = Template->new({ 'INCLUDE_PATH' => $ROOT . '/template',
'FILTERS' => { 'htmlsafe' => \&PgCommitFest::WebControl::escape } });
our $PG = defined $ENV{'PGCOMMITFEST_DB'} ? $ENV{'PGCOMMITFEST_DB'}
: 'dbi:Pg:dbname=pgcommitfest user=pgcommitfest';
our $PGUSERNAME = '';
our $PGPASSWORD = '';
$CGI::POST_MAX = 65536;
$CGI::DISABLE_UPLOADS = 1; # No need for uploads at present.
sub new {
my ($class, $db) = @_;
my $cgi = CGI::Fast->new();
return undef if !defined $cgi;
bless {
'cgi' => $cgi,
'control' => {},
'control_list' => [],
'db' => undef,
'error_list' => [],
'header' => {
'Content-type' => 'text/html; charset=utf-8',
'Cache-Control' => 'no-cache',
'Pragma' => 'no-cache',
},
'link' => [],
'response_sent' => 0,
'rss_alternate' => undef,
'title' => '',
}, $class;
}
sub add_link {
my ($self, $url, $name, $confirm) = @_;
push @{$self->{'link'}}, [ $url, $name, $confirm ];
}
sub add_control {
my ($self, $name, $type, $display_name, %args) = @_;
my $control = PgCommitFest::WebControl->new($name, $type, $display_name,
%args);
push @{$self->{'control_list'}}, $control;
$self->{'control'}{$name} = $control;
}
sub authenticate {
my ($self, %option) = @_;
if (!defined $self->{'authenticate'} && defined $self->cookie('session')) {
$self->{'authenticate'} =
$self->db->select_one(<<EOM, $self->cookie('session'));
SELECT s.*, p.is_administrator FROM session s
LEFT JOIN user_privilege p ON s.userid = p.userid
WHERE s.id = ?
EOM
}
if (!defined $self->{'authenticate'} && $option{'require_login'}) {
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
my $uri = $ENV{'REQUEST_URI'};
$uri =~ s/[^A-Za-z0-9]/sprintf "%%%x", ord($&)/ge;
$self->redirect('/action/login?uri=' . $uri);
}
$self->redirect('/action/login');
}
if (defined $self->{'authenticate'} && $option{'require_administrator'}
&& ! $self->{'authenticate'}{'is_administrator'}) {
$self->error_exit(<<EOM);
This function is available only to administators.
EOM
}
return $self->{'authenticate'};
}
sub cgi {
my ($self, $n) = @_;
return scalar($self->{'cgi'}->param($n));
}
sub cgi_id {
my ($self, $n) = @_;
$n = 'id' if !defined $n;
my $v = $self->cgi($n);
if (defined $v && $v !~ /^\d+$/) {
$self->error_exit('Invalid parameter.');
}
return $v;
}
sub cgi_required_id {
my ($self, $n) = @_;
$n = 'id' if !defined $n;
my $v = $self->cgi($n);
if (!defined $v || $v !~ /^\d+$/) {
$self->error_exit('Invalid parameter.');
}
return $v;
}
sub cookie {
my ($self, $n) = @_;
return scalar($self->{'cgi'}->cookie($n));
}
sub control {
my ($self, $n) = @_;
return $self->{'control'}{$n};
}
sub db {
my ($self) = @_;
return $self->{'db'} if ref $self && defined $self->{'db'};
my $db = PgCommitFest::DB->connect($PG, $PGUSERNAME, $PGPASSWORD);
$self->{'db'} = $db if ref $self;
return $db;
}
sub db_is_connected {
my ($self) = @_;
defined $self->{'db'};
}
sub error {
my ($self, $fmt, @arg) = @_;
push @{$self->{'error_list'}}, sprintf($fmt, @arg);
}
sub error_exit {
my ($self, $error) = @_;
$self->render_template('error', { 'error_list' => [ $error ] });
die "DONE\n";
}
sub eval_template {
my ($self, $name, $stash) = @_;
my $content;
$template->process($name . '.tt2', $stash, \$content)
|| die $template->error();
return $content;
}
sub generate_headers {
my ($self) = @_;
my @header;
while (my ($header, $value) = each %{$self->{'header'}}) {
push @header, "$header: $value";
}
return join("\r\n", @header) . "\r\n\r\n";
}
sub header {
my ($self, $header, $value) = @_;
$self->{'header'}{$header} = $value;
}
sub is_error {
my ($self) = @_;
return (@{$self->{'error_list'}} != 0);
}
sub initialize_controls {
my ($self, $default) = @_;
my %value;
# It's important that we process these controls in the same order that they
# were added, so that the user doesn't see the error messages in a funny
# order.
for my $control (@{$self->{'control_list'}}) {
my $n = $control->name;
$n .= '_id' if $control->istype('select');
$value{$n} = $control->set($self, $default);
}
return %value;
}
sub redirect {
my ($self, $url) = @_;
$self->header('Status', 302);
$self->header('Location', $url);
print $self->generate_headers();
$self->{'response_sent'} = 1;
die "DONE\n";
}
sub render_template {
my ($self, $file, $vars) = @_;
# Generate data. We generate the whole response before sending it,
# because an error at this stage is possible, but it's hard to do anything
# reasonable if we've already begun sending the response back to Apache.
my %stash;
my $content = $self->generate_headers();
my $uri = '';
my $scriptname = $ENV{'REQUEST_URI'};
$scriptname =~ s/\?.*//;
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
if ($scriptname ne '/action/login') {
my $req = $ENV{'REQUEST_URI'};
$req =~ s/[^A-Za-z0-9]/sprintf "%%%x", ord($&)/ge;;
$uri = '?uri=' . $req;
}
else {
$uri = '?' . $ENV{'QUERY_STRING'};
}
}
%stash = %$vars if defined $vars;
$stash{'authenticate'} = $self->authenticate();
$stash{'control'} = $self->{'control'};
$template->process('header.tt2', {
'authenticate' => $self->authenticate(),
'link' => $self->{'link'},
'rss_alternate' => $self->{'rss_alternate'},
'title' => $self->{'title'},
'error_list' => $self->{'error_list'},
'script_name' => $scriptname,
'uri' => $uri,
}, \$content) || die $template->error();
$template->process($file . '.tt2', \%stash, \$content)
|| die $template->error();
$template->process('footer.tt2', {}, \$content)
|| die $template->error();
# Send the results, unless we already did.
die 'response already sent' if $self->{'response_sent'};
$self->{'response_sent'} = 1;
print $content;
}
sub response_sent {
my ($self) = @_;
$self->{'response_sent'};
}
sub set_rss_alternate {
my ($self, $name, $url) = @_;
$self->{'rss_alternate'} = { 'name' => $name, 'url' => $url };
}
sub set_title {
my ($self, $fmt, @args) = @_;
$self->{'title'} = sprintf($fmt, @args);
}
1;
|