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

Last change on this file was 635, checked in by Dominic Hargreaves, 13 years ago

licence tastic

  • Property svn:executable set to *
File size: 3.3 KB
Line 
1#!/usr/bin/perl
2#
3# Copyright (c) 2006 Dominic Hargreaves
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8# and/or sell copies of the Software, and to permit persons to whom the
9# Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21#
22use strict;
23use warnings;
24
25use CGI::Fast qw/:standard -debug/;
26
27# Find our private perl libraries
28use FindBin;
29use lib "$FindBin::Bin/../perllib";
30use NPEMap;
31
32# Set up database handler to try and make sure it's ready for the first
33# request
34# No point in handling errors here since they'll get handled by the request
35# handler
36my $dbh = setup_dbh();
37
38my $returnBaseURL = '';
39
40my $cgi;
41# Process incoming requests
42REQUEST: while ($cgi = new CGI::Fast) {
43
44    # If we're given return URL parameters, basic sanity check to stop
45    # funny business
46    my $returnURL= '/tiles/map.html';
47    if (defined $ENV{HTTP_REFERER}) {
48        $returnURL = $ENV{HTTP_REFERER};
49    }
50
51    my $returnlink = "<a href='$returnURL'>Go back to the map</a>";
52
53    # In case the database went away, make sure we have a connection
54    unless ($dbh = setup_dbh()) {
55        print_html_err('Error setting up database connection', $returnlink);
56        next REQUEST;
57    }
58
59    # Input validation
60    unless (defined $cgi->param('postcode')) {
61        print_html_err ("Postcode identifier not supplied", $returnlink);
62        next REQUEST;
63    }
64
65    # Check that the requested ID exists
66    my $sth = $dbh->prepare('SELECT id FROM postcodes WHERE id = ? AND NOT deleted');
67    unless ($sth->execute($cgi->param('postcode'))) {
68        print_html_err('Database error when checking for the postcode', $returnlink);
69        next REQUEST;
70    }
71
72    unless ($sth->rows) {
73        print_html_err('A postcode with that identifier does not exist, or has already been deleted.', $returnlink);
74        next REQUEST;
75    }
76
77    $sth = $dbh->prepare('INSERT INTO bad_postcodes (postcode, ip, reason, reporter_email) VALUES (?, ?, ?, ?)');
78    if ($sth->execute($cgi->param('postcode'), $ENV{'REMOTE_ADDR'}, $cgi->param('reason') || undef, $cgi->param('email') || undef)) {
79        print "Content-type: text/html\n\n";
80        print "<body><p>Thank you for reporting that post code bad</p>\n";
81        next REQUEST;
82    } else {
83        print STDERR "DB error: " . $dbh->errstr . "\n";
84        print_html_err("Database error when adding your data :(", $returnlink);
85        next REQUEST;
86    }
87}
88
89# No more requests to serve, so tidy up
90$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.