source: trunk/npemap.org.uk/cgi/bad-postcode.fcgi @ 71

Last change on this file since 71 was 71, checked in by Dominic Hargreaves, 15 years ago

Fix return link stuff (closes #16)

  • Property svn:executable set to *
File size: 2.8 KB
RevLine 
[63]1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use DBI;
7use CGI::Fast qw/:standard -debug/;
8
9use vars qw($dbname $dbhost $dbuser $dbpass);
10
11sub print_err;
12sub setup_dbh;
13
14# Read in database config
15my $config = 'npemap.conf';
16do $config or die "Can't read $config!\n";
17
18# Set up database handler to try and make sure it's ready for the first
19# request
20# No point in handling errors here since they'll get handled by the request
21# handler
22my $dbh;
23setup_dbh();
24
[71]25my $returnBaseURL = '';
[63]26
27my $cgi;
28# Process incoming requests
29REQUEST: while ($cgi = new CGI::Fast) {
30
31    # If we're given return URL parameters, basic sanity check to stop
32    # funny business
[71]33    my $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html">Go back to the map</a>';
[63]34
35    # In case the database went away, make sure we have a connection
36    unless (setup_dbh()) {
37        print_err('Error setting up database connection', $returnlink);
38        next REQUEST;
39    }
40
41    # Input validation
42    unless (defined $cgi->param('postcode')) {
43        print_err ("Postcode identifier not supplied");
44        next REQUEST;
45    }
46
47    # Check that the requested ID exists
48    my $sth = $dbh->prepare('SELECT id FROM postcodes WHERE id = ?');
49    unless ($sth->execute($cgi->param('postcode'))) {
50        print_err('Database error when checking for the postcode', $returnlink);
51        next REQUEST;
52    }
53
54    unless ($sth->rows) {
55        print_err('A postcode with that identifier does not exist.', $returnlink);
56        next REQUEST;
57    }
58
59    $sth = $dbh->prepare('INSERT INTO bad_postcodes (postcode, ip, reason, reporter_email) VALUES (?, ?, ?, ?)');
60    if ($sth->execute($cgi->param('postcode'), $ENV{'REMOTE_ADDR'}, $cgi->param('reason') || undef, $cgi->param('email') || undef)) {
61        print "Content-type: text/html\n\n";
62        print "<html><head><title>Thank you</title></head>\n";
63        print "<body><p>Thank you for reporting the bad post code!</p>\n";
64        print "<p>$returnlink</p>\n";
65        print "</body></html>";
66        next REQUEST;
67    } else {
68        print STDERR "DB error: " . $dbh->errstr . "\n";
69        print_err("Database error when adding your data :(", $returnlink);
70        next REQUEST;
71    }
72}
73
74# No more requests to serve, so tidy up
75$dbh->disconnect;
76
77# Helper routines
78sub print_err($$) {
79    my $err = shift;
80    my $returnlink = shift;
81    print "Content-type: text/html\n\n";
82    print "<html><head><title>Error submitting</title></head>\n";
83    print "<body><p>The following error occurred whilst submitting data:\n";
84    print CGI::escapeHTML($err);
85    print "<p>$returnlink</p>\n";
86    print "</body></html>\n";
87}
88
89sub setup_dbh {
90    # $dbh is global
91    my $data_source = "dbi:Pg:dbname=$dbname";
92    $data_source .= ";host=$dbhost" if $dbhost;
93    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
94}
95
Note: See TracBrowser for help on using the repository browser.