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

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

More cleanups

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