1 | #!/usr/bin/perl
|
---|
2 | # ipdb/cgi-bin/newcity.cgi
|
---|
3 | # Add new city to database
|
---|
4 | ###
|
---|
5 | # SVN revision info
|
---|
6 | # $Date: 2010-08-04 20:08:41 +0000 (Wed, 04 Aug 2010) $
|
---|
7 | # SVN revision $Rev: 459 $
|
---|
8 | # Last update by $Author: kdeugau $
|
---|
9 | ###
|
---|
10 | # Copyright (C) 2004-2010 - Kris Deugau
|
---|
11 |
|
---|
12 | use strict;
|
---|
13 | use warnings;
|
---|
14 | #use CGI::Carp qw(fatalsToBrowser);
|
---|
15 | use CGI::Simple;
|
---|
16 | use HTML::Template;
|
---|
17 | use DBI;
|
---|
18 | #use CommonWeb qw(:ALL);
|
---|
19 | #use POSIX qw(ceil);
|
---|
20 | use NetAddr::IP;
|
---|
21 |
|
---|
22 | use Sys::Syslog;
|
---|
23 |
|
---|
24 | # don't remove! required for GNU/FHS-ish install from tarball
|
---|
25 | ##uselib##
|
---|
26 |
|
---|
27 | use MyIPDB;
|
---|
28 |
|
---|
29 | openlog "IPDB","pid","$IPDB::syslog_facility";
|
---|
30 |
|
---|
31 | # Collect the username from HTTP auth. If undefined, we're in a test environment.
|
---|
32 | my $authuser;
|
---|
33 | if (!defined($ENV{'REMOTE_USER'})) {
|
---|
34 | $authuser = '__temptest';
|
---|
35 | } else {
|
---|
36 | $authuser = $ENV{'REMOTE_USER'};
|
---|
37 | }
|
---|
38 |
|
---|
39 | # Set up the CGI object...
|
---|
40 | my $q = new CGI::Simple;
|
---|
41 | # ... and get query-string params as well as POST params if necessary
|
---|
42 | $q->parse_query_string;
|
---|
43 |
|
---|
44 | # Convenience; saves changing all references to %webvar
|
---|
45 | ##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
|
---|
46 | my %webvar = $q->Vars;
|
---|
47 |
|
---|
48 | my ($dbh,$errstr) = connectDB_My;
|
---|
49 | my $sth;
|
---|
50 |
|
---|
51 | $ENV{HTML_TEMPLATE_ROOT} = '../templates';
|
---|
52 |
|
---|
53 | my $page = HTML::Template->new(filename => "newcity.tmpl");
|
---|
54 |
|
---|
55 | if ($webvar{city}) {
|
---|
56 | if ($webvar{pop} eq 'on') {
|
---|
57 | $sth = $dbh->prepare("insert into cities (city,routing) values (?,'y')");
|
---|
58 | } else {
|
---|
59 | $sth = $dbh->prepare("insert into cities (city,routing) values (?,'n')");
|
---|
60 | }
|
---|
61 | ##fixme: don't allow duplicate cities
|
---|
62 | $sth->execute($webvar{city});
|
---|
63 | $page->param(city => $webvar{city});
|
---|
64 | if ($sth->err) {
|
---|
65 | $page->param(err => $sth->errstr);
|
---|
66 | my $msg = "$authuser could not add city '$webvar{city}' to database: ".$sth->errstr;
|
---|
67 | mailNotify($dbh, 'f:nci', "IPDB city add failure", $msg);
|
---|
68 | syslog "err", $msg;
|
---|
69 | } else {
|
---|
70 | syslog "notice", "$authuser added city/location '$webvar{pop}'".
|
---|
71 | (($webvar{pop} eq 'on') ? ' as POP location' : '');
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | print "Content-type: text/html\n\n";
|
---|
76 |
|
---|
77 | print $page->output;
|
---|
78 |
|
---|
79 | finish($dbh);
|
---|
80 |
|
---|