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

Last change on this file was 635, checked in by Dominic Hargreaves, 13 years ago

licence tastic

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1#!/usr/bin/perl
2#
3# Copyright (c) 2006 Dominic Hargreaves
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the "Software"),
6# to deal in the Software without restriction, including without limitation
7# the rights to use, copy, modify, merge, publish, distribute, sublicense,
8# and/or sell copies of the Software, and to permit persons to whom the
9# Software is furnished to do so, subject to the following conditions:
10#
11# The above copyright notice and this permission notice shall be included in
12# all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20# IN THE SOFTWARE.
21#
22use strict;
23use warnings;
24
25use CGI::Fast qw/:standard -debug/;
26use Email::Valid;
27
28# Find our private perl libraries
29use FindBin;
30use lib "$FindBin::Bin/../perllib";
31use NPEMap;
32
33my $returnlink='<a href="/">Go back</a>';
34
35# Set up database handler to try and make sure it's ready for the first
36# request
37# No point in handling errors here since they'll get handled by the request
38# handler
39my $dbh = setup_dbh();
40
41my $cgi;
42# Process incoming requests
43REQUEST: while ($cgi = new CGI::Fast) {
44
45    # In case the database went away, make sure we have a connection
46    unless ($dbh = setup_dbh()) {
47        print_html_err('Error setting up database connection', $returnlink);
48        next REQUEST;
49    }
50
51    # Input validation
52    unless (defined $cgi->param('email')) {
53        print_html_err ("Email address not supplied", $returnlink);
54        next REQUEST;
55    }
56
57    unless(Email::Valid->address($cgi->param('email'))) {
58        print_html_err ("Sorry, your email address doesn't seem to be valid", $returnlink);
59        next REQUEST;
60    }
61
62    unless (defined $cgi->param('scotland') or defined $cgi->param('northernireland')) {
63        print_html_err ("Interest in neither Scotland nor Northern Ireland shown", $returnlink);
64        next REQUEST;
65    }
66
67    # Check that the email address doesn't already exist in our interest DB
68    my $sth = $dbh->prepare('SELECT email FROM interest WHERE email = ?');
69    unless ($sth->execute($cgi->param('email'))) {
70        print_html_err('Database error', $returnlink);
71        next REQUEST;
72    }
73
74    if ($sth->rows) {
75        print_html_err('We already have a record of your interest. Please email us if you wish to change or remove this.', $returnlink);
76        next REQUEST;
77    }
78
79    my $scotland = 'f';
80    my $northernireland = 'f';
81    if (defined $cgi->param('scotland')) {
82        $scotland = 't';
83    }
84    if (defined $cgi->param('northernireland')) {
85        $northernireland = 't';
86    }
87
88    $sth = $dbh->prepare('INSERT INTO interest (email, scotland, northernireland, ip) VALUES (?, ?, ?, ?)');
89    if ($sth->execute($cgi->param('email'), $scotland, $northernireland, $ENV{'REMOTE_ADDR'})) {
90        print "Content-type: text/html\n\n";
91        print "<html><head><title>Thank you</title></head>\n";
92        print "<body><p>Thank you for showing an interest in future plans!</p>\n";
93        print "<p>$returnlink</p>\n";
94        print "</body></html>";
95        next REQUEST;
96    } else {
97        print_html_err("Database error when adding your data :(", $returnlink);
98        next REQUEST;
99    }
100}
101
102# No more requests to serve, so tidy up
103$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.