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

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

Licence my contributions under the MIT licence

  • Property svn:executable set to *
File size: 3.2 KB
Line 
1#!/usr/bin/perl
2#
3# See accompanying file "LICENCE" for licence details
4
5use strict;
6use warnings;
7
8use DBI;
9use CGI::Fast qw/:standard -debug/;
10use Email::Valid;
11
12use vars qw($dbname $dbhost $dbuser $dbpass);
13
14sub print_err;
15sub setup_dbh;
16
17# Read in database config
18my $config = 'npemap.conf';
19do $config or die "Can't read $config!\n";
20
21my $returnlink='<a href="/">Go back</a>';
22
23# Set up database handler to try and make sure it's ready for the first
24# request
25# No point in handling errors here since they'll get handled by the request
26# handler
27my $dbh;
28setup_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 (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}
105
106sub setup_dbh {
107    # $dbh is global
108    my $data_source = "dbi:Pg:dbname=$dbname";
109    $data_source .= ";host=$dbhost" if $dbhost;
110    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
111}
112
Note: See TracBrowser for help on using the repository browser.