diff options
Diffstat (limited to 'web')
-rwxr-xr-x | web/alert.cgi | 83 | ||||
-rwxr-xr-x | web/index.cgi | 98 | ||||
-rwxr-xr-x | web/rss.cgi | 74 |
3 files changed, 187 insertions, 68 deletions
diff --git a/web/alert.cgi b/web/alert.cgi new file mode 100755 index 000000000..e9a173273 --- /dev/null +++ b/web/alert.cgi @@ -0,0 +1,83 @@ +#!/usr/bin/perl -w + +# alert.cgi: +# Alert code for Neighbourhood Fix-It +# +# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org. WWW: http://www.mysociety.org +# +# $Id: alert.cgi,v 1.1 2007-01-26 01:01:23 matthew Exp $ + +use strict; +require 5.8.0; + +# Horrible boilerplate to set up appropriate library paths. +use FindBin; +use lib "$FindBin::Bin/../perllib"; +use lib "$FindBin::Bin/../../perllib"; + +use Page; +use mySociety::Alert; +use mySociety::AuthToken; +use mySociety::Config; +use mySociety::DBHandle qw(dbh select_all); + +BEGIN { + mySociety::Config::set_file("$FindBin::Bin/../conf/general"); + mySociety::DBHandle::configure( + Name => mySociety::Config::get('BCI_DB_NAME'), + User => mySociety::Config::get('BCI_DB_USER'), + Password => mySociety::Config::get('BCI_DB_PASS'), + Host => mySociety::Config::get('BCI_DB_HOST', undef), + Port => mySociety::Config::get('BCI_DB_PORT', undef) + ); +} + +sub main { + my $q = shift; + my $out = ''; + if (my $token = $q->param('token')) { + my $data = mySociety::AuthToken::retrieve('alert', $token); + if (my $id = $data->{id}) { + my $type = $data->{type}; + if ($type eq 'subscribe') { + mySociety::Alert::confirm($id); + $out = '<p>You have successfully confirmed your alert.</p>'; + } elsif ($type eq 'unsubscribe') { + mySociety::Alert::delete($id); + $out = '<p>You have successfully deleted your alert.</p>'; + } + } else { + $out = <<EOF; +<p>Thank you for trying to confirm your update or problem. 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 (my $email = $q->param('email')) { + # XXX: Need to validate email + my $type = $q->param('type'); + 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'); + } 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' } ); + dbh()->commit(); + $out = Page::send_email($email, undef, 'alert-confirm', %h); + } else { + $out = 'This should probably show some sort of subscribe page.'; + } + + print Page::header($q, 'Confirmation'); + print $out; + print Page::footer(); +} +Page::do_fastcgi(\&main); + diff --git a/web/index.cgi b/web/index.cgi index 9ab36002b..3218c1072 100755 --- a/web/index.cgi +++ b/web/index.cgi @@ -6,7 +6,7 @@ # Copyright (c) 2006 UK Citizens Online Democracy. All rights reserved. # Email: matthew@mysociety.org. WWW: http://www.mysociety.org # -# $Id: index.cgi,v 1.56 2007-01-24 20:55:30 matthew Exp $ +# $Id: index.cgi,v 1.57 2007-01-26 01:01:23 matthew Exp $ # TODO # Nothing is done about the update checkboxes - not stored anywhere on anything! @@ -32,7 +32,6 @@ use Page; use mySociety::AuthToken; use mySociety::Config; use mySociety::DBHandle qw(dbh select_all); -use mySociety::Email; use mySociety::GeoUtil; use mySociety::Util; use mySociety::MaPit; @@ -76,7 +75,7 @@ sub main { } elsif ($q->param('id')) { $title = 'Viewing a problem'; $out = display_problem($q); - } elsif ($q->param('pc')) { + } elsif ($q->param('pc') || ($q->param('x') && $q->param('y'))) { $title = 'Viewing a location'; $out = display_location($q); } else { @@ -122,7 +121,7 @@ EOF sub submit_update { my $q = shift; - my @vars = qw(id name email update updates fixed reopen); + my @vars = qw(id name email update fixed reopen); my %input = map { $_ => $q->param($_) || '' } @vars; my @errors; push(@errors, 'Please enter a message') unless $input{update}; @@ -134,8 +133,6 @@ sub submit_update { } return display_problem($q, @errors) if (@errors); - my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/update-confirm"); - my $id = dbh()->selectrow_array("select nextval('comment_id_seq');"); dbh()->do("insert into comment (id, problem_id, name, email, website, text, state, mark_fixed, mark_open) @@ -148,34 +145,13 @@ sub submit_update { $h{url} = mySociety::Config::get('BASE_URL') . '/C/' . mySociety::AuthToken::store('update', $id); dbh()->commit(); - my $email = mySociety::Email::construct_email({ - _template_ => $template, - _parameters_ => \%h, - From => [mySociety::Config::get('CONTACT_EMAIL'), 'Neighbourhood Fix-It'], - To => [[$input{email}, $input{name}]], - }); - my $result = mySociety::Util::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), $input{email}); - my $out; - if ($result == mySociety::Util::EMAIL_SUCCESS) { - $out = <<EOF; -<h1>Nearly Done! Now check your email...</h1> -<p>The confirmation email <strong>may</strong> take a few minutes to arrive — <em>please</em> be patient.</p> -<p>If you use web-based email or have 'junk mail' filters, you may wish to check your bulk/spam mail folders: sometimes, our messages are marked that way.</p> -<p>You must now click on the link within the email we've just sent you - -<br>if you do not, your update will not be posted.</p> -<p>(Don't worry - we'll hang on to your update while you're checking your email.)</p> -EOF - } else { - $out = <<EOF; -<p>I'm afraid something went wrong when we tried to send your email. Please click Back, check your details, and try again.</p> -EOF - } + my $out = Page::send_email($input{email}, $input{name}, 'update-confirm', %h); return $out; } sub submit_problem { my $q = shift; - my @vars = qw(council title detail name email phone pc easting northing updates); + my @vars = qw(council title detail name email phone pc easting northing); my %input = map { $_ => scalar $q->param($_) } @vars; my @errors; @@ -212,8 +188,6 @@ sub submit_problem { return display_form($q, @errors) if (@errors); - my $template = File::Slurp::read_file("$FindBin::Bin/../templates/emails/problem-confirm"); - my $id = dbh()->selectrow_array("select nextval('problem_id_seq');"); my $image; @@ -253,35 +227,14 @@ sub submit_problem { $h{url} = mySociety::Config::get('BASE_URL') . '/P/' . mySociety::AuthToken::store('problem', $id); dbh()->commit(); - my $email = mySociety::Email::construct_email({ - _template_ => $template, - _parameters_ => \%h, - From => [mySociety::Config::get('CONTACT_EMAIL'), 'Neighbourhood Fix-It'], - To => [[$input{email}, $input{name}]], - }); - my $result = mySociety::Util::send_email($email, mySociety::Config::get('CONTACT_EMAIL'), $input{email}); - my $out; - if ($result == mySociety::Util::EMAIL_SUCCESS) { - $out = <<EOF; -<h1>Nearly Done! Now check your email...</h1> -<p>The confirmation email <strong>may</strong> take a few minutes to arrive — <em>please</em> be patient.</p> -<p>If you use web-based email or have 'junk mail' filters, you may wish to check your bulk/spam mail folders: sometimes, our messages are marked that way.</p> -<p>You must now click on the link within the email we've just sent you - -<br>if you do not, your problem will not be posted on the site.</p> -<p>(Don't worry - we'll hang on to your information while you're checking your email.)</p> -EOF - } else { - $out = <<EOF; -<p>I'm afraid something went wrong when we tried to send your email. Please click Back, check your details, and try again.</p> -EOF - } + my $out = Page::send_email($input{email}, $input{name}, 'problem-confirm', %h); return $out; } sub display_form { my ($q, @errors) = @_; my ($pin_x, $pin_y, $pin_tile_x, $pin_tile_y); - my @vars = qw(title detail name email phone updates pc easting northing x y skipped council); + my @vars = qw(title detail name email phone pc easting northing x y skipped council); my %input = map { $_ => $q->param($_) || '' } @vars; my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars; my @ps = $q->param; @@ -355,7 +308,7 @@ EOF . $areas_info->{$councils[0]}->{name} . '</strong>.</p>'; $out .= '<input type="hidden" name="council" value="' . $councils[0] . '">'; } else { - my $e = 'team@neighbourhoodfixit.com'; # XXX from config + my $e = mySociety::Config::get('CONTACT_EMAIL'); my $list = join(', ', map { $areas_info->{$_}->{name} } @$all_councils); my $n = @$all_councils; $out .= '<p>We do not yet have details for the council'; @@ -380,7 +333,6 @@ exact location of the problem (ie. on a wall or the floor), and so on.</p>'; if (@errors) { $out .= '<ul id="error"><li>' . join('</li><li>', @errors) . '</li></ul>'; } - my $updates = (!defined($q->param('updates')) || $input{updates}) ? ' checked' : ''; my $back = NewURL($q, submit_map => undef, "tile_$pin_tile_x.$pin_tile_y.x" => undef, "tile_$pin_tile_x.$pin_tile_y.y" => undef, skipped => undef); $out .= <<EOF; @@ -398,8 +350,6 @@ exact location of the problem (ie. on a wall or the floor), and so on.</p>'; <small>(optional, so the council can get in touch)</small></div> <div><label for="form_photo">Photo:</label> <input type="file" name="photo" id="form_photo"></div> -<div class="checkbox"><input disabled type="checkbox" name="updates" id="form_updates" value="1"$updates> -<label for="form_updates"><s>Receive email when updates are left on this problem</s></label></div> <div class="checkbox"><input type="submit" name="submit_problem" value="Submit"></div> </fieldset> @@ -461,6 +411,7 @@ EOF $out .= <<EOF; </ol> <h2>Recent problems reported within 10km</h2> + <p><a href="/rss/$x,$y?choose=1">RSS feed</a></p> <ol id="current" start="$list_start"> EOF foreach (@$current) { @@ -493,11 +444,11 @@ EOF sub display_problem { my ($q, @errors) = @_; - my @vars = qw(id name email update updates fixed reopen x y); + my @vars = qw(id name email update fixed reopen x y); my %input = map { $_ => $q->param($_) || '' } @vars; my %input_h = map { $_ => $q->param($_) ? ent($q->param($_)) : '' } @vars; - $input{x} = $input{x} + 0; - $input{y} = $input{y} + 0; + $input{x} += 0; + $input{y} += 0; # Get all information from database my $problem = dbh()->selectrow_arrayref( @@ -532,18 +483,18 @@ EOF $out .= '<p align="center"><img src="/photo?id=' . $input{id} . '"></p>'; } - my $back = NewURL($q, id=>undef); + my $back = NewURL($q, id=>undef, x=>$x_tile, y=>$y_tile); $out .= '<p align="right"><a href="' . $back . '">Back to listings</a></p>'; # Display updates my $updates = select_all( - "select id, name, extract(epoch from whenposted) as whenposted, text, mark_fixed, mark_open + "select id, name, extract(epoch from created) as created, text, mark_fixed, mark_open from comment where problem_id = ? and state='confirmed' - order by whenposted desc", $input{id}); + order by created desc", $input{id}); if (@$updates) { $out .= '<div id="updates"> <h2>Updates</h2>'; foreach my $row (@$updates) { - $out .= "<div><em>Posted by $row->{name} at " . prettify_epoch($row->{whenposted}); + $out .= "<div><em>Posted by $row->{name} at " . prettify_epoch($row->{created}); $out .= ', marked fixed' if ($row->{mark_fixed}); $out .= ', reopened' if ($row->{mark_open}); $out .= '</em>'; @@ -551,12 +502,25 @@ EOF } $out .= '</div>'; } + $out .= <<EOF; +<h2>Follow this problem</h2> +<ul> +<li><a href="/rss/$input_h{id}?choose=1">RSS feed of updates on this problem</a> +<li>Receive email when updates are left on this problem. +<form action="alert" method="post"> +<label class="n" for="alert_email">Email:</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> +</ul> +EOF $out .= '<h2>Provide an update</h2>'; if (@errors) { $out .= '<ul id="error"><li>' . join('</li><li>', @errors) . '</li></ul>'; } - $updates = (!defined($q->param('updates')) || $input{updates}) ? ' checked' : ''; my $fixed = ($input{fixed}) ? ' checked' : ''; my $reopen = ($input{reopen}) ? ' checked' : ''; my $fixedline = $state eq 'fixed' ? qq{ @@ -578,8 +542,6 @@ EOF <div><label for="form_update">Update:</label> <textarea name="update" id="form_update" rows="7" cols="30">$input_h{update}</textarea></div> $fixedline -<div class="checkbox"><input type="checkbox" disabled name="updates" id="form_updates" value="1"$updates> -<label for="form_updates"><s>Receive email when updates are left on this problem</s></label></div> <div class="checkbox"><input type="submit" value="Post"></div> </fieldset> </form> diff --git a/web/rss.cgi b/web/rss.cgi new file mode 100755 index 000000000..757f98e85 --- /dev/null +++ b/web/rss.cgi @@ -0,0 +1,74 @@ +#!/usr/bin/perl -w + +# rss.cgi: +# RSS for Neighbourhood Fix-It +# +# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved. +# Email: matthew@mysociety.org. WWW: http://www.mysociety.org +# +# $Id: rss.cgi,v 1.1 2007-01-26 01:01:23 matthew Exp $ + +use strict; +require 5.8.0; + +# Horrible boilerplate to set up appropriate library paths. +use FindBin; +use lib "$FindBin::Bin/../perllib"; +use lib "$FindBin::Bin/../../perllib"; +use URI::Escape; + +use Page; +use mySociety::Config; +use mySociety::DBHandle qw(dbh); +use mySociety::Alert; +use mySociety::Web; + +BEGIN { + mySociety::Config::set_file("$FindBin::Bin/../conf/general"); + mySociety::DBHandle::configure( + Name => mySociety::Config::get('BCI_DB_NAME'), + User => mySociety::Config::get('BCI_DB_USER'), + Password => mySociety::Config::get('BCI_DB_PASS'), + Host => mySociety::Config::get('BCI_DB_HOST', undef), + Port => mySociety::Config::get('BCI_DB_PORT', undef) + ); +} + +sub main { + my $q = shift; + my $type = $q->param('type') || ''; + my $choose = $q->param('choose'); + if ($choose) { + print Page::header($q, 'Choose RSS feed'); + my $url = $ENV{SCRIPT_URI}; + $url = uri_escape($url); + print <<EOF; +<ul> +<li><a href="http://www.bloglines.com/sub?url=$url">Bloglines</a> +<li><a href="http://google.com/reader/view/feed/$url">Google Reader</a> +<li><a href="http://add.my.yahoo.com/content?url=$url">My Yahoo!</a> +<li><a href="http://my.msn.com/addtomymsn.armx?id=rss&ut=$url&tt=CENTRALDIRECTORY&ru=http://rss.msn.com">My MSN</a> +<li><a href="http://127.0.0.1:5335/system/pages/subscriptions?url=$url">Userland</a> +<li><a href="http://127.0.0.1:8888/index.html?add_url=$url">Amphetadesk</a> +<li><a href="http://www.feedmarker.com/admin.php?do=add_feed&url=$url">Feedmarker</a> +<li><a href="$ENV{SCRIPT_URL}">Plain RSS</a> +</ul> +EOF + print Page::footer(); + return; + } + if ($type eq 'local_problems') { + my $x = ($q->param('x') * 5000 / 31); + my $y = ($q->param('y') * 5000 / 31); + mySociety::Alert::generate_rss($type, $x, $y); + } elsif ($type eq 'new_updates') { + my $id = $q->param('id'); + mySociety::Alert::generate_rss($type, $id); + } elsif ($type eq 'new_problems') { + mySociety::Alert::generate_rss($type); + } else { + throw Error::Simple('Unknown alert type') unless $type; + } +} +Page::do_fastcgi(\&main); + |