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

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

Make everything use the modules

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