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

Last change on this file since 135 was 135, checked in by David Sheldon, 15 years ago

Ok, the bug reporting script might work basically now.

  • Property svn:executable set to *
File size: 2.6 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/;
11
12use vars qw($dbname $dbhost $dbuser $dbpass);
13
14sub print_err;
15sub setup_dbh;
16
17# Read in database config
18my $config = 'npemap.conf';
19do $config or die "Can't read $config!\n";
20
21# Set up database handler to try and make sure it's ready for the first
22# request
23# No point in handling errors here since they'll get handled by the request
24# handler
25my $dbh;
26setup_dbh();
27
28my $returnBaseURL = '';
29
30my $cgi;
31# Process incoming requests
32REQUEST: while ($cgi = new CGI::Fast) {
33
34    # If we're given return URL parameters, basic sanity check to stop
35    # funny business
36    my $returnlink = '<a href="' . $returnBaseURL . '/">Go back</a>';
37
38    # In case the database went away, make sure we have a connection
39    unless (setup_dbh()) {
40        print_err('Error setting up database connection', $returnlink);
41        next REQUEST;
42    }
43
44    # Input validation
45    unless (defined $cgi->param('comment')) {
46        print_err ("Please supply a comment");
47        next REQUEST;
48    }
49    my $type = $cgi->param('type');
50    my $email = $cgi->param('email');
51    my $comment= $cgi->param('comment');
52    my $subject = "[$type] comment from npemap.org.uk ";
53    my $message = <<"END";
54
55From $email
56
57$comment
58
59END
60    sendEmail('webmaster@npemap.org.uk', 'webmaster@npemap.org.uk', $subject, $message) ;
61    print "Content-type: text/html\n\n";
62    print "<html><head><title>Thanks</title></head>";
63    print "<body><p>Thanks for submitting your comment.</p><P><a href='/'>Home</a></p></body></html>";
64
65   
66
67}
68
69# No more requests to serve, so tidy up
70$dbh->disconnect;
71
72# Helper routines
73sub print_err($$) {
74    my $err = shift;
75    my $returnlink = shift;
76    print "Content-type: text/html\n\n";
77    print "<html><head><title>Error submitting</title></head>\n";
78    print "<body><p>The following error occurred whilst submitting data:\n";
79    print CGI::escapeHTML($err);
80    print "</body></html>\n";
81}
82
83sub setup_dbh {
84    # $dbh is global
85    my $data_source = "dbi:Pg:dbname=$dbname";
86    $data_source .= ";host=$dbhost" if $dbhost;
87    return $dbh = DBI->connect_cached($data_source, $dbuser, $dbpass);
88}
89
90# Simple Email Function
91# ($to, $from, $subject, $message)
92sub sendEmail
93{
94    my ($to, $from, $subject, $message) = @_;
95    my $sendmail = '/usr/lib/sendmail';
96    open(MAIL, "|$sendmail -oi -t");
97    print MAIL "From: $from\n";
98    print MAIL "To: $to\n";
99    print MAIL "Subject: $subject\n\n";
100    print MAIL "$message\n";
101    close(MAIL);
102}
Note: See TracBrowser for help on using the repository browser.