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

Last change on this file since 237 was 237, checked in by Dominic Hargreaves, 14 years ago

Move bad-postcode to using shared error function

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/perl
2#
3# Copyright (c) 2006 Dominic Hargreaves
4# See accompanying file "LICENCE" for licence details
5
6use strict;
7use warnings;
8
9use DBI;
10use CGI::Fast qw/:standard -debug/;
11
12use vars qw($dbname $dbhost $dbuser $dbpass);
13
14# Find our private perl libraries
15use FindBin;
16use lib "$FindBin::Bin/../perllib";
17use NPEMap;
18
19# Set up database handler to try and make sure it's ready for the first
20# request
21# No point in handling errors here since they'll get handled by the request
22# handler
23my $dbh = setup_dbh();
24
25my $returnBaseURL = '';
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    my $returnURL= '/tiles/map.html';
34    if (defined $ENV{HTTP_REFERER}) {
35        $returnURL = $ENV{HTTP_REFERER};
36    }
37
38    my $returnlink = "<a href='$returnURL'>Go back to the map</a>";
39
40    # In case the database went away, make sure we have a connection
41    unless ($dbh = setup_dbh()) {
42        print_html_err('Error setting up database connection', $returnlink);
43        next REQUEST;
44    }
45
46    # Input validation
47    unless (defined $cgi->param('postcode')) {
48        print_html_err ("Postcode identifier not supplied", $returnlink);
49        next REQUEST;
50    }
51
52    # Check that the requested ID exists
53    my $sth = $dbh->prepare('SELECT id FROM postcodes WHERE id = ? AND NOT deleted');
54    unless ($sth->execute($cgi->param('postcode'))) {
55        print_html_err('Database error when checking for the postcode', $returnlink);
56        next REQUEST;
57    }
58
59    unless ($sth->rows) {
60        print_html_err('A postcode with that identifier does not exist, or has already been deleted.', $returnlink);
61        next REQUEST;
62    }
63
64    $sth = $dbh->prepare('INSERT INTO bad_postcodes (postcode, ip, reason, reporter_email) VALUES (?, ?, ?, ?)');
65    if ($sth->execute($cgi->param('postcode'), $ENV{'REMOTE_ADDR'}, $cgi->param('reason') || undef, $cgi->param('email') || undef)) {
66        print "Content-type: text/html\n\n";
67        print "<html><head><title>Thank you</title></head>\n";
68        print "<body><p>Thank you for reporting the bad post code!</p>\n";
69        print "<p>$returnlink</p>\n";
70        print "</body></html>";
71        next REQUEST;
72    } else {
73        print STDERR "DB error: " . $dbh->errstr . "\n";
74        print_html_err("Database error when adding your data :(", $returnlink);
75        next REQUEST;
76    }
77}
78
79# No more requests to serve, so tidy up
80$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.