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