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

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

More cleanups

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