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

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

Add interest CGI

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