source: trunk/npemap.org.uk/cgi/interest.fcgi @ 238

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

Make CGIs use shared error functions.

  • Property svn:executable set to *
File size: 2.7 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/;
11use Email::Valid;
12
13# Find our private perl libraries
14use FindBin;
15use lib "$FindBin::Bin/../perllib";
16use NPEMap;
17
18my $returnlink='<a href="/">Go back</a>';
19
20# Set up database handler to try and make sure it's ready for the first
21# request
22# No point in handling errors here since they'll get handled by the request
23# handler
24my $dbh = setup_dbh();
25
26my $cgi;
27# Process incoming requests
28REQUEST: while ($cgi = new CGI::Fast) {
29
30    # In case the database went away, make sure we have a connection
31    unless ($dbh = setup_dbh()) {
32        print_html_err('Error setting up database connection', $returnlink);
33        next REQUEST;
34    }
35
36    # Input validation
37    unless (defined $cgi->param('email')) {
38        print_html_err ("Email address not supplied", $returnlink);
39        next REQUEST;
40    }
41
42    unless(Email::Valid->address($cgi->param('email'))) {
43        print_html_err ("Sorry, your email address doesn't seem to be valid", $returnlink);
44        next REQUEST;
45    }
46
47    unless (defined $cgi->param('scotland') or defined $cgi->param('northernireland')) {
48        print_html_err ("Interest in neither Scotland nor Northern Ireland shown", $returnlink);
49        next REQUEST;
50    }
51
52    # Check that the email address doesn't already exist in our interest DB
53    my $sth = $dbh->prepare('SELECT email FROM interest WHERE email = ?');
54    unless ($sth->execute($cgi->param('email'))) {
55        print_html_err('Database error', $returnlink);
56        next REQUEST;
57    }
58
59    if ($sth->rows) {
60        print_html_err('We already have a record of your interest. Please email us if you wish to change or remove this.', $returnlink);
61        next REQUEST;
62    }
63
64    my $scotland = 'f';
65    my $northernireland = 'f';
66    if (defined $cgi->param('scotland')) {
67        $scotland = 't';
68    }
69    if (defined $cgi->param('northernireland')) {
70        $northernireland = 't';
71    }
72
73    $sth = $dbh->prepare('INSERT INTO interest (email, scotland, northernireland, ip) VALUES (?, ?, ?, ?)');
74    if ($sth->execute($cgi->param('email'), $scotland, $northernireland, $ENV{'REMOTE_ADDR'})) {
75        print "Content-type: text/html\n\n";
76        print "<html><head><title>Thank you</title></head>\n";
77        print "<body><p>Thank you for showing an interest in future plans!</p>\n";
78        print "<p>$returnlink</p>\n";
79        print "</body></html>";
80        next REQUEST;
81    } else {
82        print_html_err("Database error when adding your data :(", $returnlink);
83        next REQUEST;
84    }
85}
86
87# No more requests to serve, so tidy up
88$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.