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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
#!/usr/bin/perl -w -I../perllib
# alert.cgi:
# Alert code for FixMyStreet
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: matthew@mysociety.org. WWW: http://www.mysociety.org
#
# $Id: alert.cgi,v 1.16 2007-09-18 08:27:05 matthew Exp $
use strict;
use Standard;
use Digest::SHA1 qw(sha1_hex);
use CrossSell;
use mySociety::Alert;
use mySociety::AuthToken;
use mySociety::Config;
use mySociety::EmailUtil qw(is_valid_email);
use mySociety::Gaze;
use mySociety::MaPit;
use mySociety::VotingArea;
use mySociety::Web qw(ent);
sub main {
my $q = shift;
my $out = '';
my $title = 'Confirmation';
if ($q->param('signed_email')) {
$out = alert_signed_input($q);
} elsif (my $token = $q->param('token')) {
my $data = mySociety::AuthToken::retrieve('alert', $token);
if ($data->{id}) {
$out = alert_token($q, $data);
} else {
$out = $q->p(_(<<EOF));
Thank you for trying to confirm your alert. We seem to have a problem ourselves
though, so <a href="/contact">please let us know what went on</a> and we'll look into it.
EOF
}
} elsif ($q->param('rss')) {
$out = alert_rss($q);
return unless $out;
} elsif ($q->param('email')) {
$out = alert_do_subscribe($q, $q->param('email'));
} elsif ($q->param('id')) {
$out = alert_updates_form($q);
} elsif ($q->param('pc')) {
$title = 'Local RSS feeds and email alerts';
$out = alert_list($q);
} else {
$title = 'Local RSS feeds and email alerts';
$out = alert_front_page($q);
}
print Page::header($q, title => $title);
print $out;
print Page::footer();
}
Page::do_fastcgi(\&main);
sub alert_list {
my ($q, @errors) = @_;
my %input = map { $_ => scalar $q->param($_) } qw(pc email);
my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } qw(pc email);
my ($x, $y, $e, $n, $error) = Page::geocode($input{pc});
return Page::geocode_choice($error, '/alert') if ref($error) eq 'ARRAY';
return alert_front_page($q, $error) if $error;
my $errors = '';
$errors = '<ul id="error"><li>' . join('</li><li>', @errors) . '</li></ul>' if @errors;
my @types = (@$mySociety::VotingArea::council_parent_types, @$mySociety::VotingArea::council_child_types);
my %councils = map { $_ => 1 } @$mySociety::VotingArea::council_parent_types;
my $areas = mySociety::MaPit::get_voting_areas_by_location({easting=>$e, northing=>$n}, 'polygon', \@types);
$areas = mySociety::MaPit::get_voting_areas_info([ keys %$areas ]);
my ($options);
if (keys %$areas == 2) {
# One-tier council
my (@options, $council, $ward);
foreach (values %$areas) {
if ($councils{$_->{type}}) {
$council = $_;
} else {
$ward = $_;
}
}
push @options, [ 'council', $council->{area_id}, Page::short_name($council->{name}),
"Problems within $council->{name}" ];
push @options, [ 'ward', $council->{area_id}.':'.$ward->{area_id}, Page::short_name($council->{name}) . '/'
. Page::short_name($ward->{name}), "Problems within $ward->{name} ward" ];
$options = '<div>' . $q->ul({id=>'rss_feed'},
alert_list_options($q, @options)
);
} elsif (keys %$areas == 4) {
# Two-tier council
my (@options, $county, $district, $c_ward, $d_ward);
foreach (values %$areas) {
if ($_->{type} eq 'CTY') {
$county = $_;
} elsif ($_->{type} eq 'DIS') {
$district = $_;
} elsif ($_->{type} eq 'CED') {
$c_ward = $_;
} elsif ($_->{type} eq 'DIW') {
$d_ward = $_;
}
}
push @options,
[ 'area', $district->{area_id}, Page::short_name($district->{name}), $district->{name} ],
[ 'area', $district->{area_id}.':'.$d_ward->{area_id}, Page::short_name($district->{name}) . '/'
. Page::short_name($d_ward->{name}), "$d_ward->{name} ward, $district->{name}" ],
[ 'area', $county->{area_id}, Page::short_name($county->{name}), $county->{name} ],
[ 'area', $county->{area_id}.':'.$c_ward->{area_id}, Page::short_name($county->{name}) . '/'
. Page::short_name($c_ward->{name}), "$c_ward->{name} ward, $county->{name}" ];
$options = '<div id="rss_list">';
$options .= $q->p($q->strong('Problems within the boundary of:')) .
$q->ul(alert_list_options($q, @options));
@options = ();
push @options,
[ 'council', $district->{area_id}, Page::short_name($district->{name}), $district->{name} ],
[ 'ward', $district->{area_id}.':'.$d_ward->{area_id}, Page::short_name($district->{name}) . '/' . Page::short_name($d_ward->{name}),
"$district->{name}, within $d_ward->{name} ward" ],
[ 'council', $county->{area_id}, Page::short_name($county->{name}), $county->{name} ],
[ 'ward', $county->{area_id}.':'.$c_ward->{area_id}, Page::short_name($county->{name}) . '/'
. Page::short_name($c_ward->{name}), "$county->{name}, within $c_ward->{name} ward" ];
$options .= $q->p($q->strong('Or problems reported to:')) .
$q->ul(alert_list_options($q, @options));
$options .= '</div>
<div id="rss_buttons">
';
} else {
# Hopefully impossible in the UK!
throw Error::Simple('An area with three tiers of council? Impossible!');
}
my ($lat, $lon) = mySociety::GeoUtil::national_grid_to_wgs84($e, $n, 'G');
my $dist = mySociety::Gaze::get_radius_containing_population($lat, $lon, 200000);
$dist = int($dist*10+0.5)/10;
<<EOF;
<h1>Local RSS feeds and email alerts for ‘$input_h{pc}’</h1>
<form id="alerts" method="post" action="/alert">
<input type="hidden" name="type" value="local">
<input type="hidden" name="pc" value="$input_h{pc}">
<p>We have a variety of RSS feeds for local problems. The easiest is our simple geographic one:</p>
<p id="rss_local">
<input type="radio" name="feed" id="local:$x:$y" value="local:$x:$y">
<label for="local:$x:$y">Problems within ${dist}km of this location</label> (a default
distance which covers roughly 200,000 people)
<a href="/rss/$x,$y"><img src="/i/feed.png" width="16" height="16" title="RSS feed of nearby problems" alt="RSS feed" border="0"></a>
</p>
<p id="rss_local_alt">
(alternatively the RSS feed can be customised, within <a href="/rss/$x,$y/2">2km</a> / <a href="/rss/$x,$y/5">5km</a>
/ <a href="/rss/$x,$y/10">10km</a> / <a href="/rss/$x,$y/20">20km</a>)
</ul>
<p>Or you can subscribe to a feed based upon what ward or council you’re in. Simply
select which feed you’d like and click the button. If you’d prefer,
these feeds are also available as email alerts – just enter your email
address below.</p>
$options
$errors
<p><input type="submit" name="rss" value="Give me an RSS feed"></p>
<p id="alert_or">or</p>
<p>Your email: <input type="text" id="email" name="email" value="$input_h{email}" size="30"></p>
<p><input type="submit" name="alert" value="Subscribe me to an email alert"></p>
</div>
</form>
EOF
}
sub alert_list_options {
my $q = shift;
my $out = '';
my $feed = $q->param('feed') || '';
foreach (@_) {
my ($type, $vals, $rss, $text) = @$_;
(my $vals2 = $rss) =~ tr{/+}{:_};
my $id = $type . ':' . $vals . ':' . $vals2;
$out .= '<li><input type="radio" name="feed" id="' . $id . '" ';
$out .= 'checked ' if $feed eq $id;
$out .= 'value="' . $id . '"> <label for="' . $id . '">' . $text
. '</label> <a href="/rss/';
$out .= $type eq 'area' ? 'area' : 'reports';
$out .= '/' . $rss . '"><img src="/i/feed.png" width="16" height="16"
title="RSS feed of local problems at ' . $text . '" alt="RSS feed" border="0"></a>';
}
return $out;
}
sub alert_front_page {
my $q = shift;
my $error = shift;
my $errors = '';
$errors = '<ul id="error"><li>' . $error . '</li></ul>' if $error;
my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } qw(pc);
my $out = <<EOF;
<h1>Local RSS feeds and email alerts</h1>
$errors
<form method="get" action="/alert">
<p>To find out what local alerts we have, please enter your UK postcode or street name here:
<input type="text" name="pc" value="$input_h{pc}">
<input type="submit" value="Look up">
</form>
EOF
return $out;
}
sub alert_rss {
my $q = shift;
my $feed = $q->param('feed');
return alert_list($q, 'Please select the feed you want') unless $feed;
if ($feed =~ /^area:(?:\d+:)+(.*)$/) {
(my $id = $1) =~ tr{:_}{/+};
print $q->redirect('/rss/area/' . $id);
return;
} elsif ($feed =~ /^(?:council|ward):(?:\d+:)+(.*)$/) {
(my $id = $1) =~ tr{:_}{/+};
print $q->redirect('/rss/reports/' . $id);
return;
} elsif ($feed =~ /^local:(\d+):(\d+)$/) {
print $q->redirect('/rss/' . $1 . ',' . $2);
return;
} else {
return alert_list($q, 'Illegal feed selection');
}
}
sub alert_updates_form {
my ($q, @errors) = @_;
my @vars = qw(id email);
my %input = map { $_ => $q->param($_) || '' } @vars;
my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars;
my $out = '';
if (@errors) {
$out .= '<ul id="error"><li>' . join('</li><li>', @errors) . '</li></ul>';
}
$out .= $q->p(_('Receive email when updates are left on this problem.'));
my $label = _('Email:');
my $subscribe = _('Subscribe');
$out .= <<EOF;
<form action="alert" method="post">
<label class="n" for="alert_email">$label</label>
<input type="text" name="email" id="alert_email" value="$input_h{email}" size="30">
<input type="hidden" name="id" value="$input_h{id}">
<input type="hidden" name="type" value="updates">
<input type="submit" value="$subscribe">
</form>
EOF
return $out;
}
sub alert_signed_input {
my $q = shift;
my ($salt, $signed_email) = split /,/, $q->param('signed_email');
my $email = $q->param('email');
my $id = $q->param('id');
my $secret = scalar(dbh()->selectrow_array('select secret from secret'));
my $out;
if ($signed_email eq sha1_hex("$id-$email-$salt-$secret")) {
my $alert_id = mySociety::Alert::create($email, 'new_updates', $id);
mySociety::Alert::confirm($alert_id);
$out = $q->p(_('You have successfully subscribed to that alert.'));
$out .= CrossSell::display_advert($email);
} else {
$out = $q->p(_('We could not validate that alert.'));
}
return $out;
}
sub alert_token {
my ($q, $data) = @_;
my $id = $data->{id};
my $type = $data->{type};
my $email = $data->{email};
my $out;
if ($type eq 'subscribe') {
mySociety::Alert::confirm($id);
$out = $q->p(_('You have successfully confirmed your alert.'));
$out .= CrossSell::display_advert($email);
} elsif ($type eq 'unsubscribe') {
mySociety::Alert::delete($id);
$out = $q->p(_('You have successfully deleted your alert.'));
$out .= CrossSell::display_advert($email);
}
return $out;
}
sub alert_do_subscribe {
my ($q, $email) = @_;
my $type = $q->param('type');
my @errors;
push @errors, _('Please enter a valid email address') unless is_valid_email($email);
push @errors, _('Please select the feed you want') if $type eq 'local' && !$q->param('feed');
if (@errors) {
return alert_updates_form($q, @errors) if $type eq 'updates';
return alert_list($q, @errors) if $type eq 'local';
return 'argh';
}
my $alert_id;
if ($type eq 'updates') {
my $id = $q->param('id');
$alert_id = mySociety::Alert::create($email, 'new_updates', $id);
} elsif ($type eq 'problems') {
$alert_id = mySociety::Alert::create($email, 'new_problems');
} elsif ($type eq 'local') {
my $feed = $q->param('feed');
if ($feed =~ /^area:(?:\d+:)?(\d+)/) {
$alert_id = mySociety::Alert::create($email, 'area_problems', $1);
} elsif ($feed =~ /^council:(\d+)/) {
$alert_id = mySociety::Alert::create($email, 'council_problems', $1, $1);
} elsif ($feed =~ /^ward:(\d+):(\d+)/) {
$alert_id = mySociety::Alert::create($email, 'ward_problems', $1, $2);
} elsif ($feed =~ /^local:(\d+):(\d+)/) {
$alert_id = mySociety::Alert::create($email, 'local_problems', $1, $2);
}
} else {
throw mySociety::Alert::Error('Invalid type');
}
my %h = ();
$h{url} = mySociety::Config::get('BASE_URL') . '/A/'
. mySociety::AuthToken::store('alert', { id => $alert_id, type => 'subscribe', email => $email } );
dbh()->commit();
return Page::send_email($email, undef, 'alert', %h);
}
|