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

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

Make CGIs use shared error functions.

  • Property svn:executable set to *
File size: 2.9 KB
RevLine 
[39]1#!/usr/bin/perl
[81]2#
[82]3# Copyright (c) 2006 Dominic Hargreaves
[81]4# See accompanying file "LICENCE" for licence details
[39]5
6use strict;
7use warnings;
8
9use DBI;
10use CGI::Fast qw/:standard -debug/;
11
[222]12# Find our private perl libraries
13use FindBin;
14use lib "$FindBin::Bin/../perllib";
15use NPEMap;
16
[39]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
[222]23my $dbh = setup_dbh();
[39]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
[222]34    unless ($dbh = setup_dbh()) {
[74]35        print_internal_err('Error setting up database connection');
[39]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");
[74]68        next REQUEST;
[39]69    }
70
[195]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");
[84]72   
[39]73    if ($sth->execute($cgi->param('mineasting'), $cgi->param('maxeasting'), $cgi->param('minnorthing'), $cgi->param('maxnorthing'))) {
74        my $hr = $sth->fetchall_hashref('postcode');
[45]75        print "Content-type: text/javascript\n\n";
76
77        foreach my $key (keys %$hr) {
78            my $hash = $$hr{$key};
[195]79            print "addMarker('$$hash{postcode}', $$hash{easting}, $$hash{northing}, [$$hash{id}, $$hash{source}]);\n";
[45]80        }
[84]81        print "completeMarkers();\n";
[39]82    } else {
[74]83        print_internal_err("Database error retrieving data");
[39]84    }
85
86}
87
88# No more requests to serve, so tidy up
89$dbh->disconnect;
90
91# Helper routines
[84]92sub print_err {
[39]93    my $err = shift;
[74]94    print header("text/plain", "400 $err");
95    print "Error: $err\n";
[39]96}
97
[84]98sub print_internal_err {
[74]99    my $err = shift;
100    print header("text/plain", "500 $err");
101    print "Error: $err\n";
102}
Note: See TracBrowser for help on using the repository browser.