#!/usr/bin/perl

use CGI;
use DBI;
##use strict;
unshift(@INC, '.');
use ipac_cfg;

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";             
my $net_id;
my $new_ip;
my $dbh;
my $user = $ENV{REMOTE_USER};

if ($ENV{HTTPS} ne "on" || $user ne "admin") {
    print $q->header(-type=>"text/html;", -status=>'403 Forbidden'),
	    $q->start_html('Forbidden'),
	    $q->h1('You dont have permission to access this script'),
	    $q->end_html;
    exit;
}

$net_id = $q->param('net');

if (($q->param('new_ip') ne "") || ($net_id eq "")) {
	print "Location: $cgi/br_net?net=$net_id\n\n" ;
}

print  $q->header,
       $q->start_html(-title=>'Add host/network definition');

print "<table><tr><td>".
	"<IMG BORDER=0 ALT=\"A zdes tipa kartinka, no ti ee ne vidish\"".
	"SRC=\"$path/poweredby.png\"></td><td>".
	"<center><font color=\"#ff0000\" size=6>Ipac-ng add host/network definiton</font></center>".
	"</td></tr></table><A HREF=\"http://sf.net/projects/ipac-ng\">".
    	"ipac-ng home page</A>&nbsp;&nbsp;";
print "<A HREF=\"$path/stat\">Go to main page</A>&nbsp;&nbsp;";
print $ipac_cfg::menu;

$dbh = DBI->connect("dbi:Pg:dbname=$ipac_cfg::pg_dbname",                  
                        $ipac_cfg::pg_login, $ipac_cfg::pg_pwd,            
                                     { RaiseError => 1, AutoCommit => 0 });

print "<table align=center CELLPADDING=2 CELLSPACING=1 BORDER=0>";
print "<tr>", $q->td(['host/network', '&nbsp']);print "</tr>";

print $q->start_form;
print "<tr>";
print $q->td([
	    $q->textfield('new_ip', "", 20, 60, -override=>1), "&nbsp;&nbsp;",
	    $q->submit()]),
	$q->hidden('net', $net_id);
print "</tr></table>";
print $q->endform();

$new_ip=$q->param('new_ip');

if ($new_ip eq "") {
	print $q->end_html;
	$dbh->disconnect or warn $dbh->errstr;
	exit;
}

$dbh->do(q{insert into nets (ip_net, net_id) values (?, ?)}, 
	    			    undef, $new_ip, $net_id)
					    or die $dbh->errstr;
$dbh->commit();
$dbh->disconnect  or warn $dbh->errstr;
print $q->end_html;
