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

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

Make CGIs use shared error functions.

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