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

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

Make everything use the modules

  • Property svn:executable set to *
File size: 2.8 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
19sub print_err;
20
21# Set up database handler to try and make sure it's ready for the first
22# request
23# No point in handling errors here since they'll get handled by the request
24# handler
25my $dbh = setup_dbh();
26
27my $returnBaseURL = '';
28
29my $cgi;
30# Process incoming requests
31REQUEST: while ($cgi = new CGI::Fast) {
32
33    # If we're given return URL parameters, basic sanity check to stop
34    # funny business
35    my $returnURL= '/tiles/map.html';
36    if (defined $ENV{HTTP_REFERER}) {
37        $returnURL = $ENV{HTTP_REFERER};
38    }
39
40    my $returnlink = "<a href='$returnURL'>Go back to the map</a>";
41
42    # In case the database went away, make sure we have a connection
43    unless ($dbh = 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 = ? AND NOT deleted');
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, or has already been deleted.', $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}
Note: See TracBrowser for help on using the repository browser.