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

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

Make everything use the modules

  • Property svn:executable set to *
File size: 5.7 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/;
11use Geo::Postcode;
12
13use vars qw($dbname $dbhost $dbuser $dbpass);
14
15# Find our private perl libraries
16use FindBin;
17use lib "$FindBin::Bin/../perllib";
18use NPEMap;
19
20sub print_err;
21
22# Set up database handler to try and make sure it's ready for the first
23# request
24# No point in handling errors here since they'll get handled by the request
25# handler
26my $dbh = setup_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 ($dbh = 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 ($trimmed_1, $trimmed_2) = ($cgi->param('postcode1'), $cgi->param('postcode2'));
84    $trimmed_1 =~ s/^\s+//;
85    $trimmed_2 =~ s/^\s+//;
86    $trimmed_1 =~ s/\s+$//;
87    $trimmed_2 =~ s/\s+$//;
88    my $raw_postcode = $trimmed_1;
89    $raw_postcode .= ' ' . $trimmed_2 if defined $trimmed_2;
90
91    unless ((length($trimmed_2)) == 1 or (length($trimmed_2) == 0) or (length($trimmed_2) == 3) or (!defined $trimmed_2)) {
92        print_err("The postcode format is not valid", $returnlink);
93        next REQUEST;
94    }
95
96    my $postcode = Geo::Postcode->new($raw_postcode);
97    my ($first, $second, $third, $fourth) = @{$postcode->fragments};
98    my ($outward, $inward);
99
100    if ($postcode->valid) {
101        # We have a complete postcode; input it straight into the database
102        $outward = $first . $second;
103        $inward = $third . $fourth;
104    } elsif($postcode->valid_fragment) {
105        # We have a valid fragment; let's build up what we can
106        # We are guaranteed to have the first two
107        $outward = $first . $second;
108        $inward = '';
109        $inward .= $third if defined $third;
110        $inward .= $fourth if defined $fourth;
111    } else {
112        print_err("The postcode format is not valid", $returnlink);
113        next REQUEST;
114    }
115
116    # Check for a duplicate.
117    # We want to collect duplicates from different IP addresses as a kind of
118    # corroboration factor; this just catches accidental double-submission
119    # really.
120    $sth = $dbh->prepare('SELECT raw_postcode_outward FROM postcodes WHERE raw_postcode_outward = ? AND raw_postcode_inward = ? AND easting = ? AND northing = ? AND ip = ? AND NOT deleted');
121    unless ($sth->execute($cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'})) {
122        print_err('Database error when checking for duplicate data :(', $returnlink);
123        next REQUEST;
124    }
125
126    if ($sth->rows) {
127        print_err('You, or someone with the same IP address, have already submitted this postcode with these co-ordinates.', $returnlink);
128        next REQUEST;
129    }
130
131    $sth = $dbh->prepare('INSERT INTO postcodes (outward, inward, raw_postcode_outward, raw_postcode_inward, easting, northing, ip, source) VALUES (?, ?, ?, ?, ?, ?, ?, 0)');
132    if ($sth->execute($outward, $inward, $cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'})) {
133        print "Content-type: text/html\n\n";
134        print "<html><head><title>Thank you</title></head>\n";
135        print "<body><p>Thank you for telling us where your post code is!</p>\n";
136        print "<p>$returnlink</p>\n";
137        print "</body></html>";
138        next REQUEST;
139    } else {
140        print STDERR "DB error: " . $dbh->errstr . "\n";
141        print_err("Database error when adding your data :(", $returnlink);
142        next REQUEST;
143    }
144}
145
146# No more requests to serve, so tidy up
147$dbh->disconnect;
148
149# Helper routines
150sub print_err($$) {
151    my $err = shift;
152    my $returnlink = shift;
153    print "Content-type: text/html\n\n";
154    print "<html><head><title>Error submitting</title></head>\n";
155    print "<body><p>The following error occurred whilst submitting data:\n";
156    print CGI::escapeHTML($err);
157    print "</p><p>Your input was:</p>\n<ul>";
158    foreach my $field (@fields) {
159        my $param = $cgi->param($field) || '';
160        print "<li>$field: " . $param . "</li>\n";
161    }
162    print "</ul>\n";
163    print "<p>$returnlink</p>\n";
164    print "</body></html>\n";
165}
Note: See TracBrowser for help on using the repository browser.