<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Admin');
view_top();
$FW_conf = carica_conf ("Firewall");

if (isset($_POST['nome'])) {
	$nome 		= 	$_POST['nome'];
	$vpn_id 	= 	$_POST['vpn_id'];
	$fw_port 	= 	$_POST['fw_port'];
	$int_id 	= 	$_POST['int_id'];
	$int_port 	= 	$_POST['int_port'];
	$proto 		= 	$_POST['proto'];

	$query = "	INSERT INTO
				firewall_versointerno
			SET
				nome = '$nome',
				id_firewall_vpn = $vpn_id,
				fw_port = $fw_port,
				id_firewall_macchine = $int_id,
				int_port = $int_port,
				proto = '$proto',
				data = NOW(),
				attivo = 1
	";
	$res = mysql_query( $query, $DB_ID );
}

$query = "	SELECT
			firewall_macchine.id,
			proxy_pool.ip,
			proxy_pool.nome
		FROM
			firewall_macchine
		JOIN
			proxy_pool
			ON
				firewall_macchine.id_proxy_pool = proxy_pool.id
		ORDER BY
			nome
";
$res_macchine = mysql_query( $query, $DB_ID );

$query = "	SELECT
			id,
			utenza,
			ip,
			tipo
		FROM
			firewall_vpn
		WHERE
			attivo = 1
		ORDER BY
			utenza
";
$res_vpn = mysql_query( $query, $DB_ID );

$query = "      SELECT  
                        fw_port
                FROM
                        firewall_versointerno
";
$res_ip = mysql_query( $query, $DB_ID );
while( $row = mysql_fetch_assoc( $res_ip) ) { $listaportefw[] = $row['fw_port']; }

$tabella = new html (0,"90%",array(3,3,2,15,2,15,8,2,15,5,2,5,2,15,0));
?>
<form name="" method="post">
	<tr>
		<td rowspan=2 colspan=2 class="spazioh"><input type="image" src="<?php print $CONF['base_url'] ?>/img/addresource.png" border="0" alt="Inserisci" /></td>
		<td rowspan=2 class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">Nome regola</td>
		<td rowspan=2 class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">VPN</td>
		<td class="descrizione_c">Porta FW</td>
		<td rowspan=2 class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">Macchina interna</td>
		<td class="descrizione_c">Porta</td>
		<td rowspan=2 class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione_c">Proto</td>
		<td rowspan=2 colspan=3 class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
	<tr>
		<td class="colip"><input type="text" size="14" name="nome"></td>
		<td class="colip">
			<select name="vpn_id">
				<option value="" selected="selected">---</option>
<?php while ($dato = mysql_fetch_array ( $res_vpn )) { ?>
				<option value="<?php print $dato['id'] ?>"><?php if ($dato['utenza'] != "") { print $dato['utenza']; } else { print $dato['ip']; } ?></option>
<?php } ?>
			</select>
		</td>
		<td class="colip">
			<select name="fw_port">
				<option value="" selected="selected">---</option>
<?php for ($i = $FW_conf['FWPort_min']; $i <= $FW_conf['FWPort_max']; $i++ ) {
        if (in_array($i, $listaportefw) ) { 
		$aster = "*";
	} else { 
		$aster = "";
	} 
?>
				<option value="<?php print $i ?>"><?php print $i.$aster ?></option>
<?php } ?>
			</select>
		</td>
		<td class="colip">
			<select name="int_id">
				<option value="" selected="selected">---</option>
<?php while ($dato = mysql_fetch_array ( $res_macchine )) { ?>
				<option value="<?php print $dato['id'] ?>"><?php if ($dato['nome'] != "") { print $dato['nome']; } else { print $dato['ip']; } ?></option>
<?php } ?>
			</select>
		</td>
		<td class="colip"><input type="text" size="3" name="int_port"></td>
		<td class="colip">
			<select name="proto">
				<option value="" selected="selected">---</option>
				<option value="tcp">TCP</option>
				<option value="udp">UDP</option>
			</select>
		</td>
	</tr>
</form>
<?php
$tabella->riga();
$query = "	SELECT 
			firewall_versointerno.id,
			firewall_versointerno.nome,
			firewall_versointerno.proto,
			firewall_vpn.utenza,
			firewall_vpn.ip AS vpn_ip,
			proxy_pool.ip AS int_ip,
			proxy_pool.nome AS int_nome,
			firewall_versointerno.int_port,
			firewall_versointerno.fw_port,
			DATE_FORMAT(firewall_versointerno.data, '%d.%m.%Y %H:%i:%s') AS data,
			firewall_versointerno.attivo
		FROM 
			firewall_versointerno
		JOIN
			firewall_macchine
			ON
				firewall_versointerno.id_firewall_macchine = firewall_macchine.id
		JOIN
			proxy_pool
			ON
				firewall_macchine.id_proxy_pool = proxy_pool.id
		JOIN
			firewall_vpn
			ON
				firewall_versointerno.id_firewall_vpn = firewall_vpn.id
		ORDER BY
			firewall_versointerno.nome
	";

$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {
?>
	<tr>
		<td rowspan=2 class="col3btn">
<?php	if ($dato['attivo']) { ?>
		<a href="firewall_gest.php?azione=regolaint_down&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/attivo.gif" ALT="Regola attiva" TITLE="Regola attiva"></a>
<?php	} else { ?>
		<a href="firewall_gest.php?azione=regolaint_up&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/non_attivo.gif" ALT="Regola non attiva" TITLE="Regola non attiva"></a>
<?php	} ?>
		</td>
		<td rowspan=2 class="spazioh"><a href="firewall_gest.php?azione=regolaint_rm&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/trash.png"></a></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td rowspan=2 class="descrizione"><?php print $dato['nome'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione"><?php print $dato['utenza'] ?></td>
		<td rowspan=2 class="descrizione_c"><?php print $dato['fw_port'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione"><?php print $dato['int_nome'] ?></td>
		<td rowspan=2 class="descrizione_c"><?php print $dato['int_port'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td rowspan=2 class="descrizione_c"><?php print $dato['proto'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td rowspan=2 class="descrizione"><?php print $dato['data'] ?></td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		</td>
	</tr>
	<tr>
		<td class="textl"><?php print $dato['vpn_ip'] ?></td>
		<td class="textl"><?php print $dato['int_ip'] ?></td>
	</tr>
<?php
$tabella->riga();
} 
$tabella->close();
view_footer(); 
?>
