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

Last change on this file since 45 was 45, checked in by David Sheldon, 15 years ago

output javascript method calls, a little less verbose

  • Property svn:executable set to *
File size: 2.7 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use DBI;
7use CGI::Fast qw/:standard -debug/;
8use JSON;
9
10sub print_err;
11sub setup_dbh;
12
13# Set up database handler to try and make sure it's ready for the first
14# request
15# No point in handling errors here since they'll get handled by the request
16# handler
17my $dbh;
18setup_dbh();
19
20
21my @fields = qw(mineasting minnorthing maxeasting maxnorthing);
22my $max_distance = 50000; # in metres
23
24my $cgi;
25# Process incoming requests
26REQUEST: while ($cgi = new CGI::Fast) {
27
28    # In case the database went away, make sure we have a connection
29    unless (setup_dbh()) {
30        print_err('Error setting up database connection');
31        next REQUEST;
32    }
33
34    # Input validation
35    foreach my $field (@fields) {
36        unless (defined $cgi->param($field)) {
37            print_err ("Parameter '$field' missing");
38            next REQUEST;
39        }
40    }
41
42    # Are the Eastings in a valid range?
43    foreach (qw(mineasting maxeasting)) {
44        if (($cgi->param($_) > 700000) or
45            ($cgi->param($_) < 0)) {
46            print_err ("Parameter '$_' must be an integer between 0 and 700,000");
47            next REQUEST;
48        }
49    }
50
51    # Are the Northings in a valid range?
52    foreach (qw(minnorthing maxnorthing)) {
53        if (($cgi->param($_) > 1300000) or
54            ($cgi->param($_) < 0)) {
55            print_err("Parameter '$_' must be an integer between 0 and 1,300,000");
56            next REQUEST;
57        }
58    }
59   
60    # Is the box too big?
61    if (($cgi->param('maxeasting') - $cgi->param('mineasting') > $max_distance) or ($cgi->param('maxnorthing') - $cgi->param('minnorthing') > $max_distance)) {
62        print_err("The requested box is too large");
63    }
64
65    my $sth = $dbh->prepare("SELECT outward || ' ' || inward AS postcode, easting, northing FROM postcodes WHERE easting >= ? AND easting <= ? AND northing >= ? AND northing <= ?");
66    if ($sth->execute($cgi->param('mineasting'), $cgi->param('maxeasting'), $cgi->param('minnorthing'), $cgi->param('maxnorthing'))) {
67        my $hr = $sth->fetchall_hashref('postcode');
68        print "Content-type: text/javascript\n\n";
69
70        foreach my $key (keys %$hr) {
71            my $hash = $$hr{$key};
72            print "addMarker('$$hash{postcode}', $$hash{easting}, $$hash{northing});\n";
73        }
74        #print objToJson($hr) . "\n";
75    } else {
76        print_err("Database error retrieving data");
77    }
78
79}
80
81# No more requests to serve, so tidy up
82$dbh->disconnect;
83
84# Helper routines
85sub print_err($) {
86    my $err = shift;
87    my $obj = { 'error' => $err };
88    print "Content-type: text/x-json\n\n";
89    print objToJson($obj) . "\n";
90}
91
92sub setup_dbh {
93    # $dbh is global
94    return $dbh = DBI->connect_cached("dbi:Pg:dbname=npemaps;host=127.0.0.1","npemaps","npemaps");
95}
Note: See TracBrowser for help on using the repository browser.