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

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

Make CGIs use shared error functions.

  • Property svn:executable set to *
File size: 2.1 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
14# Find our private perl libraries
15use FindBin;
16use lib "$FindBin::Bin/../perllib";
17use NPEMap;
18
19# Set up database handler to try and make sure it's ready for the first
20# request
21# No point in handling errors here since they'll get handled by the request
22# handler
23my $dbh = setup_dbh();
24
25my $returnBaseURL = '';
26
27my $cgi;
28# Process incoming requests
29REQUEST: while ($cgi = new CGI::Fast) {
30
31    # If we're given return URL parameters, basic sanity check to stop
32    # funny business
33    my $returnlink = '<a href="' . $returnBaseURL . '/">Go back</a>';
34
35    # In case the database went away, make sure we have a connection
36    unless ($dbh = setup_dbh()) {
37        print_html_err('Error setting up database connection', $returnlink);
38        next REQUEST;
39    }
40
41    # Input validation
42    unless (defined $cgi->param('comment')) {
43        print_html_err ("Please supply a comment", $returnlink);
44        next REQUEST;
45    }
46
47    if (defined $cgi->param('email') and ($cgi->param('email') ne '') and !(Email::Valid->address($cgi->param('email')))) {
48        print_html_err ("Invalid email address supplied", $returnlink);
49        next REQUEST;
50    }
51
52    my $type = $cgi->param('type') || 'none';
53    my $from = $cgi->param('email') || 'webmaster@npemap.org.uk';
54    my $comment = $cgi->param('comment');
55    my $subject = "[$type] comment from npemap.org.uk ";
56
57    my $msg = MIME::Lite->new(
58        From => $from,
59        To => 'webmaster@npemap.org.uk',
60        Subject => $subject,
61        Data => $comment);
62   
63    unless ($msg->send) {
64        print_html_err ('Sorry, there was an error sending your message. Please email webmaster@npemap.org.uk instead.', $returnlink);
65        next REQUEST;
66    }
67
68    print "Content-type: text/html\n\n";
69    print "<html><head><title>Thanks</title></head>";
70    print "<body><p>Thanks for submitting your comment.</p><P><a href='/'>Home</a></p></body></html>";
71
72   
73
74}
75
76# No more requests to serve, so tidy up
77$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.