source: trunk/npemap.org.uk/cgi/get-postcodes.fcgi @ 259

Last change on this file since 259 was 259, checked in by Nick Burch, 15 years ago

A postcode isn't always unique, so we can't use hash fetch methods, otherwise we'll only see one instance of a postcode

  • 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
12# Find our private perl libraries
13use FindBin;
14use lib "$FindBin::Bin/../perllib";
15use NPEMap;
16
17sub print_err;
18
19# Set up database handler to try and make sure it's ready for the first
20# request
21# No point in handling errors here since they'll get handled by the request
22# handler
23my $dbh = setup_dbh();
24
25
26my @fields = qw(mineasting minnorthing maxeasting maxnorthing);
27my $max_distance = 50000; # in metres
28
29my $cgi;
30# Process incoming requests
31REQUEST: while ($cgi = new CGI::Fast) {
32
33    # In case the database went away, make sure we have a connection
34    unless ($dbh = setup_dbh()) {
35        print_internal_err('Error setting up database connection');
36        next REQUEST;
37    }
38
39    # Input validation
40    foreach my $field (@fields) {
41        unless (defined $cgi->param($field)) {
42            print_err ("Parameter '$field' missing");
43            next REQUEST;
44        }
45    }
46
47    # Are the Eastings in a valid range?
48    foreach (qw(mineasting maxeasting)) {
49        if (($cgi->param($_) > 700000) or
50            ($cgi->param($_) < 0)) {
51            print_err ("Parameter '$_' must be an integer between 0 and 700,000");
52            next REQUEST;
53        }
54    }
55
56    # Are the Northings in a valid range?
57    foreach (qw(minnorthing maxnorthing)) {
58        if (($cgi->param($_) > 1300000) or
59            ($cgi->param($_) < 0)) {
60            print_err("Parameter '$_' must be an integer between 0 and 1,300,000");
61            next REQUEST;
62        }
63    }
64   
65    # Is the box too big?
66    if (($cgi->param('maxeasting') - $cgi->param('mineasting') > $max_distance) or ($cgi->param('maxnorthing') - $cgi->param('minnorthing') > $max_distance)) {
67        print_err("The requested box is too large");
68        next REQUEST;
69    }
70
71    my $sth = $dbh->prepare("SELECT outward || ' ' || inward AS postcode, easting, northing,id,source FROM postcodes WHERE easting BETWEEN ? AND ? AND northing BETWEEN ? AND ? AND NOT deleted");
72   
73    if ($sth->execute($cgi->param('mineasting'), $cgi->param('maxeasting'), $cgi->param('minnorthing'), $cgi->param('maxnorthing'))) {
74        print "Content-type: text/javascript\n\n";
75
76        while(my @row = $sth->fetchrow_array) {
77                        my ($postcode,$easting,$northing,$id,$source) = @row;
78            print "addMarker('$postcode', $easting, $northing, [$id, $source]);\n";
79        }
80        print "completeMarkers();\n";
81    } else {
82        print_internal_err("Database error retrieving data");
83    }
84
85}
86
87# No more requests to serve, so tidy up
88$dbh->disconnect;
89
90# Helper routines
91sub print_err {
92    my $err = shift;
93    print header("text/plain", "400 $err");
94    print "Error: $err\n";
95}
96
97sub print_internal_err {
98    my $err = shift;
99    print header("text/plain", "500 $err");
100    print "Error: $err\n";
101}
Note: See TracBrowser for help on using the repository browser.