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