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

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

More cleanups

  • Property svn:executable set to *
File size: 5.6 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 CGI::Fast qw/:standard -debug/;
10use Geo::Postcode;
11
12# Find our private perl libraries
13use FindBin;
14use lib "$FindBin::Bin/../perllib";
15use NPEMap;
16use NPEMap::Postcodes;
17
18# Set up database handler to try and make sure it's ready for the first
19# request
20# No point in handling errors here since they'll get handled by the request
21# handler
22my $dbh = setup_dbh();
23
24my @fields = qw(easting northing postcode1 postcode2);
25my $returnBaseURL = '';
26
27my $googleAnalytics = <<"HERE";
28<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
29</script>
30<script type="text/javascript">
31_uacct = "UA-732621-2";
32if (typeof urchinTracker == 'function') urchinTracker();
33</script>
34HERE
35my $cgi;
36# Process incoming requests
37REQUEST: while ($cgi = new CGI::Fast) {
38
39    # If we're given return URL parameters, basic sanity check to stop
40    # funny business
41   
42    my $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html">Go back to the map</a>';   
43
44    if (defined $cgi->param('easting') and ($cgi->param('easting') =~ /\d+/) and
45        defined $cgi->param('northing') and ($cgi->param('northing') =~ /\d+/) ) {
46        $returnlink = '<a href="' . $returnBaseURL . '/tiles/map.html#' . int($cgi->param('easting')/1000). ",".int($cgi->param('northing')/1000)  . ',1">Go back to the map</a>';
47    }
48
49
50    # In case the database went away, make sure we have a connection
51    unless ($dbh = setup_dbh()) {
52        print_html_err('Error setting up database connection', $returnlink);
53        next REQUEST;
54    }
55
56    my ($easting, $northing);
57
58    # Input validation
59    foreach my $field (@fields) {
60        unless (defined $cgi->param($field)) {
61            print_html_err ("Parameter '$field' missing", $returnlink);
62            next REQUEST;
63        }
64    }
65
66    # Is the Easting in a valid range?
67    if (($cgi->param('easting') > 700000) or
68        ($cgi->param('easting') < 0)) {
69        print_html_err ("Parameter 'easting' must be an integer between 0 and 700,000", $returnlink);
70        next REQUEST;
71    } else {
72        $easting = $cgi->param('easting');
73    }
74
75    # Is the Northing in a valid range?
76    if (($cgi->param('northing') > 1300000) or
77        ($cgi->param('northing') < 0)) {
78        print_html_err("Parameter 'northing' must be an integer between 0 and 1,300,000", $returnlink);
79        next REQUEST;
80    } else {
81        $northing = $cgi->param('northing');
82    }
83
84    my $sth;
85
86    # Now validate the postcode input format
87    my ($trimmed_1, $trimmed_2) = ($cgi->param('postcode1'), $cgi->param('postcode2'));
88    $trimmed_1 =~ s/^\s+//;
89    $trimmed_2 =~ s/^\s+//;
90    $trimmed_1 =~ s/\s+$//;
91    $trimmed_2 =~ s/\s+$//;
92    my $raw_postcode = $trimmed_1;
93    $raw_postcode .= ' ' . $trimmed_2 if defined $trimmed_2;
94
95    unless ((length($trimmed_2)) == 1 or (length($trimmed_2) == 0) or (length($trimmed_2) == 3) or (!defined $trimmed_2)) {
96        print_html_err("The postcode format is not valid", $returnlink);
97        next REQUEST;
98    }
99
100    my $postcode = Geo::Postcode->new($raw_postcode);
101    my ($first, $second, $third, $fourth) = @{$postcode->fragments};
102    my ($outward, $inward);
103
104    if ($postcode->valid) {
105        # We have a complete postcode; input it straight into the database
106        $outward = $first . $second;
107        $inward = $third . $fourth;
108    } elsif($postcode->valid_fragment) {
109        # We have a valid fragment; let's build up what we can
110        # We are guaranteed to have the first two
111        $outward = $first . $second;
112        $inward = '';
113        $inward .= $third if defined $third;
114        $inward .= $fourth if defined $fourth;
115    } else {
116        print_html_err("The postcode format is not valid", $returnlink);
117        next REQUEST;
118    }
119
120    # Check for a duplicate.
121    # We want to collect duplicates from different IP addresses as a kind of
122    # corroboration factor; this just catches accidental double-submission
123    # really.
124    $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');
125    unless ($sth->execute($cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'})) {
126        print_html_err('Database error when checking for duplicate data :(', $returnlink);
127        next REQUEST;
128    }
129
130    if ($sth->rows) {
131        print_html_err('You, or someone with the same IP address, have already submitted this postcode with these co-ordinates.', $returnlink);
132        next REQUEST;
133    }
134
135    $sth = $dbh->prepare('INSERT INTO postcodes (outward, inward, raw_postcode_outward, raw_postcode_inward, easting, northing, ip, source, user_agent) VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?)');
136    if ($sth->execute($outward, $inward, $cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'}, $ENV{'HTTP_USER_AGENT'})) {
137        print "Content-type: text/html\n\n";
138        print "<html><head><title>Thank you</title></head>\n";
139        print "<body><p>Thank you for telling us where your post code is!</p>\n";
140        print "<p>$returnlink</p>\n";
141        print $googleAnalytics;
142        print "</body></html>";
143        # Any extra actions
144        build_home_stats($dbh);
145        next REQUEST;
146    } else {
147        print STDERR "DB error: " . $dbh->errstr . "\n";
148        print_html_err("Database error when adding your data :(", $returnlink);
149        next REQUEST;
150    }
151
152# WE NEVER GET HERE
153
154}
155
156# No more requests to serve, so tidy up
157$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.