source: trunk/npemap.org.uk/cgi/submit.fcgi @ 81

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

Licence my contributions under the MIT licence

  • Property svn:executable set to *
File size: 5.6 KB
Line 
1#!/usr/bin/perl
2#
3# See accompanying file "LICENCE" for licence details
4
5use strict;
6use warnings;
7
8use DBI;
9use CGI::Fast qw/:standard -debug/;
10use Geo::Postcode;
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 @fields = qw(easting northing postcode1 postcode2);
29my $returnBaseURL = '';
30
31my $cgi;
32# Process incoming requests
33REQUEST: while ($cgi = new CGI::Fast) {
34
35    # If we're given return URL parameters, basic sanity check to stop
36    # funny business
37   
38    my $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html">Go back to the map</a>';   
39
40    if (defined $cgi->param('easting') and ($cgi->param('easting') =~ /\d+/) and
41        defined $cgi->param('northing') and ($cgi->param('northing') =~ /\d+/) ) {
42        $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html?' . int($cgi->param('easting')/1000). ",".int($cgi->param('northing')/1000)  . ',1">Go back to the map</a>';
43    }
44
45
46    # In case the database went away, make sure we have a connection
47    unless (setup_dbh()) {
48        print_err('Error setting up database connection', $returnlink);
49        next REQUEST;
50    }
51
52    my ($easting, $northing);
53
54    # Input validation
55    foreach my $field (@fields) {
56        unless (defined $cgi->param($field)) {
57            print_err ("Parameter '$field' missing", $returnlink);
58            next REQUEST;
59        }
60    }
61
62    # Is the Easting in a valid range?
63    if (($cgi->param('easting') > 700000) or
64        ($cgi->param('easting') < 0)) {
65        print_err ("Parameter 'easting' must be an integer between 0 and 700,000", $returnlink);
66        next REQUEST;
67    } else {
68        $easting = $cgi->param('easting');
69    }
70
71    # Is the Northing in a valid range?
72    if (($cgi->param('northing') > 1300000) or
73        ($cgi->param('northing') < 0)) {
74        print_err("Parameter 'northing' must be an integer between 0 and 1,300,000", $returnlink);
75        next REQUEST;
76    } else {
77        $northing = $cgi->param('northing');
78    }
79
80    my $sth;
81
82    # Now validate the postcode input format
83    my $raw_postcode = $cgi->param('postcode1');
84    $raw_postcode .= ' ' . $cgi->param('postcode2') if defined $cgi->param('postcode2');
85    my $trimmed_postcode = $raw_postcode; 
86    $trimmed_postcode =~ s/^\s+//;
87    $trimmed_postcode =~ s/\s+$//;
88    my $postcode = Geo::Postcode->new($trimmed_postcode);
89    my ($first, $second, $third, $fourth) = @{$postcode->fragments};
90    my ($outward, $inward);
91
92    if ($postcode->valid) {
93        # We have a complete postcode; input it straight into the database
94        $outward = $first . $second;
95        $inward = $third . $fourth;
96    } elsif($postcode->valid_fragment) {
97        # We have a valid fragment; let's build up what we can
98        # We are guaranteed to have the first two
99        $outward = $first . $second;
100        $inward = '';
101        $inward .= $third if defined $third;
102        $inward .= $fourth if defined $fourth;
103    } else {
104        print_err("The postcode format is not valid", $returnlink);
105        next REQUEST;
106    }
107
108    # Check for a duplicate.
109    # We want to collect duplicates from different IP addresses as a kind of
110    # corroboration factor; this just catches accidental double-submission
111    # really.
112    $sth = $dbh->prepare('SELECT raw_postcode_outward FROM postcodes WHERE raw_postcode_outward = ? AND raw_postcode_inward = ? AND easting = ? AND northing = ? AND ip = ?');
113    unless ($sth->execute($cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'})) {
114        print_err('Database error when checking for duplicate data :(', $returnlink);
115        next REQUEST;
116    }
117
118    if ($sth->rows) {
119        print_err('You, or someone with the same IP address, have already submitted this postcode with these co-ordinates.', $returnlink);
120        next REQUEST;
121    }
122
123    $sth = $dbh->prepare('INSERT INTO postcodes (outward, inward, raw_postcode_outward, raw_postcode_inward, easting, northing, ip, source) VALUES (?, ?, ?, ?, ?, ?, ?, 0)');
124    if ($sth->execute($outward, $inward, $cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'})) {
125        print "Content-type: text/html\n\n";
126        print "<html><head><title>Thank you</title></head>\n";
127        print "<body><p>Thank you for telling us where your post code is!</p>\n";
128        print "<p>$returnlink</p>\n";
129        print "</body></html>";
130        next REQUEST;
131    } else {
132        print STDERR "DB error: " . $dbh->errstr . "\n";
133        print_err("Database error when adding your data :(", $returnlink);
134        next REQUEST;
135    }
136}
137
138# No more requests to serve, so tidy up
139$dbh->disconnect;
140
141# Helper routines
142sub print_err($$) {
143    my $err = shift;
144    my $returnlink = shift;
145    print "Content-type: text/html\n\n";
146    print "<html><head><title>Error submitting</title></head>\n";
147    print "<body><p>The following error occurred whilst submitting data:\n";
148    print CGI::escapeHTML($err);
149    print "</p><p>Your input was:</p>\n<ul>";
150    foreach my $field (@fields) {
151        my $param = $cgi->param($field) || '';
152        print "<li>$field: " . $param . "</li>\n";
153    }
154    print "</ul>\n";
155    print "<p>$returnlink</p>\n";
156    print "</body></html>\n";
157}
158
159sub setup_dbh {
160    # $dbh is global
161    my $data_source = "dbi:Pg:dbname=$dbname";
162    $data_source .= ";host=$dbhost" if $dbhost;
163    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
164}
165
Note: See TracBrowser for help on using the repository browser.