source: trunk/cgi-bin/newnode.cgi@ 955

Last change on this file since 955 was 955, checked in by Kris Deugau, 10 days ago

/trunk

Refine newnode.cgi a little to prevent duplicates and near-duplicates by
forcing all node names to upper case.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 2.2 KB
Line 
1#!/usr/bin/perl
2# ipdb/cgi-bin/newnode.cgi
3# Add new wifi tower or fibre demarc switch ("node") to database
4###
5# SVN revision info
6# $Date: 2025-04-10 21:22:24 +0000 (Thu, 10 Apr 2025) $
7# SVN revision $Rev: 955 $
8# Last update by $Author: kdeugau $
9###
10# Copyright (C) 2010 - Kris Deugau
11
12use strict;
13use warnings;
14#use CGI::Carp qw(fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use DBI;
18#use POSIX qw(ceil);
19use NetAddr::IP;
20use Sys::Syslog;
21
22# don't remove! required for GNU/FHS-ish install from tarball
23##uselib##
24
25# push "the directory the script is in" into @INC
26use FindBin;
27use lib "$FindBin::RealBin/";
28
29use MyIPDB;
30
31openlog "IPDB","pid","$IPDB::syslog_facility";
32
33# Collect the username from HTTP auth. If undefined, we're in a test environment.
34my $authuser;
35if (!defined($ENV{'REMOTE_USER'})) {
36 $authuser = '__temptest';
37} else {
38 $authuser = $ENV{'REMOTE_USER'};
39}
40
41# Set up the CGI object...
42my $q = new CGI::Simple;
43# ... and get query-string params as well as POST params if necessary
44$q->parse_query_string;
45
46# Convenience; saves changing all references to %webvar
47##fixme: tweak for handling <select multiple='y' size=3> (list with multiple selection)
48my %webvar = $q->Vars;
49
50my ($dbh,$errstr) = connectDB_My;
51my $sth;
52
53$ENV{HTML_TEMPLATE_ROOT} = '../templates';
54
55my $page = HTML::Template->new(filename => "newnode.tmpl");
56print "Content-type: text/html\n\n";
57
58if ($webvar{nodename}) {
59 $webvar{nodename} =~ tr/a-z/A-Z/;
60 $page->param(nodename => $webvar{nodename});
61 my @nc = $dbh->selectrow_array("SELECT node_ip FROM nodes WHERE node_name = ?", undef, $webvar{nodename});
62 if (@nc) {
63 $page->param(err => "Node with that name already exists with IP $nc[0].");
64 } else {
65 $sth = $dbh->prepare("insert into nodes (node_type,node_name,node_ip) values (?,?,?)");
66 $webvar{nodeip} =~ s/^\s*//;
67 $webvar{nodeip} =~ s/\s*$//;
68 $sth->execute($webvar{type}, $webvar{nodename}, $webvar{nodeip});
69 if ($sth->err) {
70 $page->param(err => $sth->errstr);
71 my $msg = "$authuser could not add node '$webvar{nodename}','$webvar{type}' to database: ".$sth->errstr;
72 mailNotify($dbh, 'f:nno', "IPDB node add failure", $msg);
73 syslog "err", $msg;
74 } else {
75 syslog "notice", "$authuser added node '$webvar{nodename}'";
76 }
77 } # @nc == 0
78}
79
80print $page->output;
81
82finish($dbh);
Note: See TracBrowser for help on using the repository browser.