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

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

Takes the following params:

  • postcode (id of the bad postcode)
  • email (email address of reporter - optional)
  • reason (free text reason - optional)
  • Property svn:executable set to *
File size: 3.2 KB
Line 
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
25my $returnBaseURL = 'http://www.npemap.org.uk';
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
33   
34    my $returnlink = '<a href="/tiles/map.html?' .int($cgi->param('easting')/1000). ",".int($cgi->param('northing')/1000)  . ',1">Go back</a>';   
35
36    if (defined $cgi->param('returnX') and ($cgi->param('returnX') =~ /\d+/) and
37        defined $cgi->param('returnY') and ($cgi->param('returnY') =~ /\d+/) ) {
38        $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html?' . $cgi->param('returnX') . ',' . $cgi->param('returnY') . ',1">Go back to the map</a>';
39    }
40
41
42    # In case the database went away, make sure we have a connection
43    unless (setup_dbh()) {
44        print_err('Error setting up database connection', $returnlink);
45        next REQUEST;
46    }
47
48    # Input validation
49    unless (defined $cgi->param('postcode')) {
50        print_err ("Postcode identifier not supplied");
51        next REQUEST;
52    }
53
54    # Check that the requested ID exists
55    my $sth = $dbh->prepare('SELECT id FROM postcodes WHERE id = ?');
56    unless ($sth->execute($cgi->param('postcode'))) {
57        print_err('Database error when checking for the postcode', $returnlink);
58        next REQUEST;
59    }
60
61    unless ($sth->rows) {
62        print_err('A postcode with that identifier does not exist.', $returnlink);
63        next REQUEST;
64    }
65
66    $sth = $dbh->prepare('INSERT INTO bad_postcodes (postcode, ip, reason, reporter_email) VALUES (?, ?, ?, ?)');
67    if ($sth->execute($cgi->param('postcode'), $ENV{'REMOTE_ADDR'}, $cgi->param('reason') || undef, $cgi->param('email') || undef)) {
68        print "Content-type: text/html\n\n";
69        print "<html><head><title>Thank you</title></head>\n";
70        print "<body><p>Thank you for reporting the bad post code!</p>\n";
71        print "<p>$returnlink</p>\n";
72        print "</body></html>";
73        next REQUEST;
74    } else {
75        print STDERR "DB error: " . $dbh->errstr . "\n";
76        print_err("Database error when adding your data :(", $returnlink);
77        next REQUEST;
78    }
79}
80
81# No more requests to serve, so tidy up
82$dbh->disconnect;
83
84# Helper routines
85sub print_err($$) {
86    my $err = shift;
87    my $returnlink = shift;
88    print "Content-type: text/html\n\n";
89    print "<html><head><title>Error submitting</title></head>\n";
90    print "<body><p>The following error occurred whilst submitting data:\n";
91    print CGI::escapeHTML($err);
92    print "<p>$returnlink</p>\n";
93    print "</body></html>\n";
94}
95
96sub setup_dbh {
97    # $dbh is global
98    my $data_source = "dbi:Pg:dbname=$dbname";
99    $data_source .= ";host=$dbhost" if $dbhost;
100    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
101}
102
Note: See TracBrowser for help on using the repository browser.