1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # Copyright (c) 2006 Dominic Hargreaves |
---|
4 | # See accompanying file "LICENCE" for licence details |
---|
5 | |
---|
6 | use strict; |
---|
7 | use warnings; |
---|
8 | |
---|
9 | use CGI::Fast qw/:standard -debug/; |
---|
10 | |
---|
11 | # Find our private perl libraries |
---|
12 | use FindBin; |
---|
13 | use lib "$FindBin::Bin/../perllib"; |
---|
14 | use NPEMap; |
---|
15 | |
---|
16 | sub print_err; |
---|
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 |
---|
22 | my $dbh = setup_dbh(); |
---|
23 | |
---|
24 | |
---|
25 | my @fields = qw(mineasting minnorthing maxeasting maxnorthing); |
---|
26 | my $max_distance = 50000; # in metres |
---|
27 | |
---|
28 | my $cgi; |
---|
29 | # Process incoming requests |
---|
30 | REQUEST: while ($cgi = new CGI::Fast) { |
---|
31 | |
---|
32 | # In case the database went away, make sure we have a connection |
---|
33 | unless ($dbh = setup_dbh()) { |
---|
34 | print_internal_err('Error setting up database connection'); |
---|
35 | next REQUEST; |
---|
36 | } |
---|
37 | |
---|
38 | # Input validation |
---|
39 | foreach my $field (@fields) { |
---|
40 | unless (defined $cgi->param($field)) { |
---|
41 | print_err ("Parameter '$field' missing"); |
---|
42 | next REQUEST; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | # Are the Eastings in a valid range? |
---|
47 | foreach (qw(mineasting maxeasting)) { |
---|
48 | if (($cgi->param($_) > 700000) or |
---|
49 | ($cgi->param($_) < 0)) { |
---|
50 | print_err ("Parameter '$_' must be an integer between 0 and 700,000"); |
---|
51 | next REQUEST; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | # Are the Northings in a valid range? |
---|
56 | foreach (qw(minnorthing maxnorthing)) { |
---|
57 | if (($cgi->param($_) > 1300000) or |
---|
58 | ($cgi->param($_) < 0)) { |
---|
59 | print_err("Parameter '$_' must be an integer between 0 and 1,300,000"); |
---|
60 | next REQUEST; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | # Is the box too big? |
---|
65 | if (($cgi->param('maxeasting') - $cgi->param('mineasting') > $max_distance) or ($cgi->param('maxnorthing') - $cgi->param('minnorthing') > $max_distance)) { |
---|
66 | print_err("The requested box is too large"); |
---|
67 | next REQUEST; |
---|
68 | } |
---|
69 | |
---|
70 | 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"); |
---|
71 | |
---|
72 | if ($sth->execute($cgi->param('mineasting'), $cgi->param('maxeasting'), $cgi->param('minnorthing'), $cgi->param('maxnorthing'))) { |
---|
73 | print "Content-type: text/javascript\n\n"; |
---|
74 | |
---|
75 | while(my @row = $sth->fetchrow_array) { |
---|
76 | my ($postcode,$easting,$northing,$id,$source) = @row; |
---|
77 | print "addMarker('$postcode', $easting, $northing, [$id, $source]);\n"; |
---|
78 | } |
---|
79 | print "completeMarkers();\n"; |
---|
80 | } else { |
---|
81 | print_internal_err("Database error retrieving data"); |
---|
82 | } |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | # No more requests to serve, so tidy up |
---|
87 | $dbh->disconnect; |
---|
88 | |
---|
89 | # Helper routines |
---|
90 | sub print_err { |
---|
91 | my $err = shift; |
---|
92 | print header("text/plain", "400 $err"); |
---|
93 | print "Error: $err\n"; |
---|
94 | } |
---|
95 | |
---|
96 | sub print_internal_err { |
---|
97 | my $err = shift; |
---|
98 | print header("text/plain", "500 $err"); |
---|
99 | print "Error: $err\n"; |
---|
100 | } |
---|