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

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

Fix problem

  • 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
16sub print_err;
17sub setup_dbh;
18
19# Read in database config
20my $config = 'npemap.conf';
21do $config or die "Can't read $config!\n";
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;
28setup_dbh();
29
30my $returnBaseURL = '';
31
32my $cgi;
33# Process incoming requests
34REQUEST: while ($cgi = new CGI::Fast) {
35
36    # If we're given return URL parameters, basic sanity check to stop
37    # funny business
38    my $returnlink = '<a href="' . $returnBaseURL . '/">Go back</a>';
39
40    # In case the database went away, make sure we have a connection
41    unless (setup_dbh()) {
42        print_err('Error setting up database connection', $returnlink);
43        next REQUEST;
44    }
45
46    # Input validation
47    unless (defined $cgi->param('comment')) {
48        print_err ("Please supply a comment");
49        next REQUEST;
50    }
51
52    if (defined $cgi->param('email') and ($cgi->param('email') ne '') and !(Email::Valid->address($cgi->param('email')))) {
53        print_err ("Invalid email address supplied");
54        next REQUEST;
55    }
56
57    my $type = $cgi->param('type') || 'none';
58    my $from = $cgi->param('email') || 'webmaster@npemap.org.uk';
59    my $comment = $cgi->param('comment');
60    my $subject = "[$type] comment from npemap.org.uk ";
61
62    my $msg = MIME::Lite->new(
63        From => $from,
64        To => 'webmaster@npemap.org.uk',
65        Subject => $subject,
66        Data => $comment);
67    $msg->send;
68
69    print "Content-type: text/html\n\n";
70    print "<html><head><title>Thanks</title></head>";
71    print "<body><p>Thanks for submitting your comment.</p><P><a href='/'>Home</a></p></body></html>";
72
73   
74
75}
76
77# No more requests to serve, so tidy up
78$dbh->disconnect;
79
80# Helper routines
81sub print_err($$) {
82    my $err = shift;
83    my $returnlink = shift;
84    print "Content-type: text/html\n\n";
85    print "<html><head><title>Error submitting</title></head>\n";
86    print "<body><p>The following error occurred whilst submitting data:\n";
87    print CGI::escapeHTML($err);
88    print "</body></html>\n";
89}
90
91sub setup_dbh {
92    # $dbh is global
93    my $data_source = "dbi:Pg:dbname=$dbname";
94    $data_source .= ";host=$dbhost" if $dbhost;
95    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
96}
Note: See TracBrowser for help on using the repository browser.