#!/usr/bin/perl

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

my $q = new CGI;
my $path="https://" . $q->server_name();
my $cgi=$path . "/cgi-bin";             
my $dbh;
my $sth;
my @row;
my $options = "0";
my $user = $ENV{REMOTE_USER};
my $serv_name;
my @tariffs;
my @nets;

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;
}

if ($q->param('serv_name') ne "") {
        print "Location: $cgi/br_services\n\n";
}

if (($q->param('ser_id') ne "") && ($q->param('tar_id') ne "")) {
        $serv_id = $q->param('ser_id');                          
        $tar_id = $q->param('tar_id');                           
        $net_id = $q->param('net_id');                           
} else {                                                         
        exit;                                                    
}                                                                

print	$q->header,
	$q->start_html(-title=>'Edit a service');

print "<table><tr><td>".
	"<IMG BORDER=0 ALT=\"powerdby logo\"".
	"SRC=\"$path/poweredby.png\"></td><td>".
	"<center><font color=\"#ff0000\" size=6>Ipac-ng edit service</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;

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

$sth = $dbh->prepare("select tariff_name from tariffs_names order by tariff_name");
$sth->execute() or die $sth->errstr;
while (@row = $sth->fetchrow_array) {
        push(@tariffs, $row[0]);
}
$sth->finish;
$sth = $dbh->prepare("select net_name from nets_names order by net_name");
$sth->execute() or die $sth->errstr;
while (@row = $sth->fetchrow_array) {
        push(@nets, $row[0]);
}
$sth->finish;

$sth = $dbh->prepare("select serv_name from services_names where serv_id=?");
$sth->execute($serv_id) or die $sth->errstr;
@row = $sth->fetchrow_array;
$serv_name = $row[0];
$sth->finish;

$sth = $dbh->prepare("select serv_name,dest,proto,tariff_name,net_name,agent,".
			"sport, dport, iface ".
			"from services,tariffs_names,nets_names,services_names ".
			"where ".
			"services.serv_id=services_names.serv_id and ".
			"services.net_id=nets_names.net_id and ".
			"services.tariff_id=tariffs_names.tariff_id and ".
			"services.serv_id=? and ".
			"services.net_id=? and ".
			"services.tariff_id=?");
$sth->execute($serv_id, $net_id, $tar_id) or die $sth->errstr;
@row = $sth->fetchrow_array;
$sth->finish;
print $q->startform, "<table>",
	$q->Tr([
		$q->td(['Service name', $q->textfield('serv_name', $row[0], 20, 60, -override=>1)]),
		$q->td(['Direction', $q->popup_menu('direction', ['in', 'out'], $row[1])]),
		$q->td(['Proto', $q->popup_menu('proto', ['tcp', 'udp', 'all', 'icmp'], $row[2])]),
		$q->td(['Tariff', $q->popup_menu('tariff', \@tariffs, $row[3])]),
		$q->td(['Network', $q->popup_menu('net', \@nets, $row[4])]),
		$q->td(['Accounting agent', $q->textfield('agent', $row[5])]),
		$q->td(['Source port', $q->textfield('sport', $row[6])]),
		$q->td(['Destination port', $q->textfield('dport', $row[7])]),
		$q->td(['Interface', $q->textfield('iface', $row[8])])
	]), "</table>",
	$q->hidden('ser_id', $serv_id),
	$q->hidden('tar_id', $tar_id),
	$q->hidden('net_id', $net_id),
	$q->submit(),
	$q->endform;

$serv_name=$q->param('serv_name');
$direction=$q->param('direction');
$proto=$q->param('proto');
$tariff=$q->param('tariff');
$net=$q->param('net');
$agent=$q->param('agent');
$sport=$q->param('sport');
$dport=$q->param('dport');
$iface=$q->param('iface');

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

$sth = $dbh->prepare("select serv_name from services_names where serv_name=?");
$sth->execute($serv_name) or die $sth->errstr;
@row=$sth->fetchrow_array;
$sth->finish;
if ($row[0] eq "") {
	$dbh->do(q{insert into services_names (serv_name) values (?)}, 
					undef, $serv_name);
}
$dbh->do(q{delete from services where serv_id=? and net_id=? and tariff_id=?}, 
                        undef, $serv_id, $net_id, $tar_id) or die $dbh->errstr;
$sth = $dbh->prepare("select tariff_id from tariffs_names where tariff_name=?");
$sth->execute($tariff) or die $sth->errstr;
@row=$sth->fetchrow_array;
$sth->finish;
$tar_id=$row[0];

$sth = $dbh->prepare("select net_id from nets_names where net_name=?");
$sth->execute($net) or die $sth->errstr;
@row=$sth->fetchrow_array;
$sth->finish;
$net_id=$row[0];

$sth = $dbh->prepare("select serv_id from services_names where serv_name=?");
$sth->execute($serv_name) or die $sth->errstr;
@row=$sth->fetchrow_array;
$sth->finish;
$serv_id=$row[0];

$dbh->do(q{insert into services (serv_id, dest, proto, tariff_id, net_id, 
		agent, sport, dport, iface) values (?,?,?,?,?,?,?,?,?)}, undef,
		$serv_id, $direction, $proto, $tar_id, $net_id, $agent,
		$sport, $dport, $iface);
$dbh->commit;
$dbh->disconnect  or warn $dbh->errstr;
reconfig();
print $q->end_html;
