source: trunk/npemap.org.uk/cgi/problem.fcgi @ 222

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

Make everything use the modules

  • Property svn:executable set to *
File size: 2.5 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 MIME::Lite;
11use Email::Valid;
12use CGI::Fast qw/:standard -debug/;
13
14use vars qw($dbname $dbhost $dbuser $dbpass);
15
16# Find our private perl libraries
17use FindBin;
18use lib "$FindBin::Bin/../perllib";
19use NPEMap;
20
21sub print_err;
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 = setup_dbh();
28
29my $returnBaseURL = '';
30
31my $cgi;
32# Process incoming requests
33REQUEST: while ($cgi = new CGI::Fast) {
34
35    # If we're given return URL parameters, basic sanity check to stop
36    # funny business
37    my $returnlink = '<a href="' . $returnBaseURL . '/">Go back</a>';
38
39    # In case the database went away, make sure we have a connection
40    unless ($dbh = setup_dbh()) {
41        print_err('Error setting up database connection', $returnlink);
42        next REQUEST;
43    }
44
45    # Input validation
46    unless (defined $cgi->param('comment')) {
47        print_err ("Please supply a comment");
48        next REQUEST;
49    }
50
51    if (defined $cgi->param('email') and ($cgi->param('email') ne '') and !(Email::Valid->address($cgi->param('email')))) {
52        print_err ("Invalid email address supplied");
53        next REQUEST;
54    }
55
56    my $type = $cgi->param('type') || 'none';
57    my $from = $cgi->param('email') || 'webmaster@npemap.org.uk';
58    my $comment = $cgi->param('comment');
59    my $subject = "[$type] comment from npemap.org.uk ";
60
61    my $msg = MIME::Lite->new(
62        From => $from,
63        To => 'webmaster@npemap.org.uk',
64        Subject => $subject,
65        Data => $comment);
66   
67    unless ($msg->send) {
68        print_err ('Sorry, there was an error sending your message. Please email webmaster@npemap.org.uk instead.');
69        next REQUEST;
70    }
71
72    print "Content-type: text/html\n\n";
73    print "<html><head><title>Thanks</title></head>";
74    print "<body><p>Thanks for submitting your comment.</p><P><a href='/'>Home</a></p></body></html>";
75
76   
77
78}
79
80# No more requests to serve, so tidy up
81$dbh->disconnect;
82
83# Helper routines
84sub print_err($$) {
85    my $err = shift;
86    my $returnlink = shift;
87    print "Content-type: text/html\n\n";
88    print "<html><head><title>Error submitting</title></head>\n";
89    print "<body><p>The following error occurred whilst submitting data:\n";
90    print CGI::escapeHTML($err);
91    print "</body></html>\n";
92}
Note: See TracBrowser for help on using the repository browser.