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

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

Better licence/copyright definition

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