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

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

Better licence/copyright definition

  • Property svn:executable set to *
File size: 2.9 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
14sub print_err;
15sub setup_dbh;
16
17# Read in database config
18my $config = 'npemap.conf';
19do $config or die "Can't read $config!\n";
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;
26setup_dbh();
27
28my $returnBaseURL = '';
29
30my $cgi;
31# Process incoming requests
32REQUEST: while ($cgi = new CGI::Fast) {
33
34    # If we're given return URL parameters, basic sanity check to stop
35    # funny business
36    my $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html">Go back to the map</a>';
37
38    # In case the database went away, make sure we have a connection
39    unless (setup_dbh()) {
40        print_err('Error setting up database connection', $returnlink);
41        next REQUEST;
42    }
43
44    # Input validation
45    unless (defined $cgi->param('postcode')) {
46        print_err ("Postcode identifier not supplied");
47        next REQUEST;
48    }
49
50    # Check that the requested ID exists
51    my $sth = $dbh->prepare('SELECT id FROM postcodes WHERE id = ?');
52    unless ($sth->execute($cgi->param('postcode'))) {
53        print_err('Database error when checking for the postcode', $returnlink);
54        next REQUEST;
55    }
56
57    unless ($sth->rows) {
58        print_err('A postcode with that identifier does not exist.', $returnlink);
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";
72        print_err("Database error when adding your data :(", $returnlink);
73        next REQUEST;
74    }
75}
76
77# No more requests to serve, so tidy up
78$dbh->disconnect;
79
80# Helper routines
81sub print_err($$) {
82    my $err = shift;
83    my $returnlink = shift;
84    print "Content-type: text/html\n\n";
85    print "<html><head><title>Error submitting</title></head>\n";
86    print "<body><p>The following error occurred whilst submitting data:\n";
87    print CGI::escapeHTML($err);
88    print "<p>$returnlink</p>\n";
89    print "</body></html>\n";
90}
91
92sub setup_dbh {
93    # $dbh is global
94    my $data_source = "dbi:Pg:dbname=$dbname";
95    $data_source .= ";host=$dbhost" if $dbhost;
96    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
97}
98
Note: See TracBrowser for help on using the repository browser.