Ignore:
Timestamp:
Oct 29, 2006, 7:35:19 PM (15 years ago)
Author:
Dominic Hargreaves
Message:

Make CGIs use shared error functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/npemap.org.uk/cgi/submit.fcgi

    r234 r238  
    1111use Geo::Postcode;
    1212
    13 use vars qw($dbname $dbhost $dbuser $dbpass);
    14 
    1513# Find our private perl libraries
    1614use FindBin;
     
    1816use NPEMap;
    1917use NPEMap::Postcodes;
    20 
    21 sub print_err;
    2218
    2319# Set up database handler to try and make sure it's ready for the first
     
    4743    # In case the database went away, make sure we have a connection
    4844    unless ($dbh = setup_dbh()) {
    49         print_err('Error setting up database connection', $returnlink);
     45        print_html_err('Error setting up database connection', $returnlink);
    5046        next REQUEST;
    5147    }
     
    5652    foreach my $field (@fields) {
    5753        unless (defined $cgi->param($field)) {
    58             print_err ("Parameter '$field' missing", $returnlink);
     54            print_html_err ("Parameter '$field' missing", $returnlink);
    5955            next REQUEST;
    6056        }
     
    6460    if (($cgi->param('easting') > 700000) or
    6561        ($cgi->param('easting') < 0)) {
    66         print_err ("Parameter 'easting' must be an integer between 0 and 700,000", $returnlink);
     62        print_html_err ("Parameter 'easting' must be an integer between 0 and 700,000", $returnlink);
    6763        next REQUEST;
    6864    } else {
     
    7369    if (($cgi->param('northing') > 1300000) or
    7470        ($cgi->param('northing') < 0)) {
    75         print_err("Parameter 'northing' must be an integer between 0 and 1,300,000", $returnlink);
     71        print_html_err("Parameter 'northing' must be an integer between 0 and 1,300,000", $returnlink);
    7672        next REQUEST;
    7773    } else {
     
    9187
    9288    unless ((length($trimmed_2)) == 1 or (length($trimmed_2) == 0) or (length($trimmed_2) == 3) or (!defined $trimmed_2)) {
    93         print_err("The postcode format is not valid", $returnlink);
     89        print_html_err("The postcode format is not valid", $returnlink);
    9490        next REQUEST;
    9591    }
     
    111107        $inward .= $fourth if defined $fourth;
    112108    } else {
    113         print_err("The postcode format is not valid", $returnlink);
     109        print_html_err("The postcode format is not valid", $returnlink);
    114110        next REQUEST;
    115111    }
     
    121117    $sth = $dbh->prepare('SELECT raw_postcode_outward FROM postcodes WHERE raw_postcode_outward = ? AND raw_postcode_inward = ? AND easting = ? AND northing = ? AND ip = ? AND NOT deleted');
    122118    unless ($sth->execute($cgi->param('postcode1'), $cgi->param('postcode2'), $easting, $northing, $ENV{'REMOTE_ADDR'})) {
    123         print_err('Database error when checking for duplicate data :(', $returnlink);
     119        print_html_err('Database error when checking for duplicate data :(', $returnlink);
    124120        next REQUEST;
    125121    }
    126122
    127123    if ($sth->rows) {
    128         print_err('You, or someone with the same IP address, have already submitted this postcode with these co-ordinates.', $returnlink);
     124        print_html_err('You, or someone with the same IP address, have already submitted this postcode with these co-ordinates.', $returnlink);
    129125        next REQUEST;
    130126    }
     
    142138    } else {
    143139        print STDERR "DB error: " . $dbh->errstr . "\n";
    144         print_err("Database error when adding your data :(", $returnlink);
     140        print_html_err("Database error when adding your data :(", $returnlink);
    145141        next REQUEST;
    146142    }
     
    152148# No more requests to serve, so tidy up
    153149$dbh->disconnect;
    154 
    155 # Helper routines
    156 sub print_err($$) {
    157     my $err = shift;
    158     my $returnlink = shift;
    159     print "Content-type: text/html\n\n";
    160     print "<html><head><title>Error submitting</title></head>\n";
    161     print "<body><p>The following error occurred whilst submitting data:\n";
    162     print CGI::escapeHTML($err);
    163     print "</p><p>Your input was:</p>\n<ul>";
    164     foreach my $field (@fields) {
    165         my $param = $cgi->param($field) || '';
    166         print "<li>$field: " . $param . "</li>\n";
    167     }
    168     print "</ul>\n";
    169     print "<p>$returnlink</p>\n";
    170     print "</body></html>\n";
    171 }
Note: See TracChangeset for help on using the changeset viewer.