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

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

	$query = "	INSERT INTO
				firewall_versoesterno
			SET
				nome = '$nome',
				id_firewall_macchine = $int_id,
				int_port = $int_port,
				id_firewall_macchine_ext = $ext_id,
				ext_port = $ext_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,
			nome,
			ip
		FROM
			firewall_macchine_ext
		WHERE
			attivo = 1
		ORDER BY
			nome
";
$res_macchine_ext = mysql_query( $query, $DB_ID );

$tabella = new html (0,"90%",array(3,3,2,15,2,15,5,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">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">Macchina esterna</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="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" value=0></td>
		<td class="colip">
			<select name="ext_id">
				<option value="" selected="selected">---</option>
<?php while ($dato = mysql_fetch_array ( $res_macchine_ext )) { ?>
				<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="ext_port" value=0></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_versoesterno.id,
			firewall_versoesterno.nome,
			firewall_versoesterno.proto,
			proxy_pool.ip AS int_ip,
			proxy_pool.nome AS int_nome,
			firewall_versoesterno.int_port,
			firewall_macchine_ext.ip AS ext_ip,
			firewall_macchine_ext.nome AS ext_nome,
			firewall_versoesterno.ext_port,
			DATE_FORMAT(firewall_versoesterno.data, '%d.%m.%Y %H:%i:%s') AS data,
			firewall_versoesterno.attivo
		FROM 
			firewall_versoesterno
		JOIN
			firewall_macchine
			ON
				firewall_versoesterno.id_firewall_macchine = firewall_macchine.id
		JOIN
			proxy_pool
			ON
				firewall_macchine.id_proxy_pool = proxy_pool.id
		JOIN
			firewall_macchine_ext
			ON
				firewall_versoesterno.id_firewall_macchine_ext = firewall_macchine_ext.id
		ORDER BY
			firewall_versoesterno.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=regolaext_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=regolaext_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=regolaext_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['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 class="descrizione"><?php print $dato['ext_nome'] ?></td>
		<td rowspan=2 class="descrizione_c"><?php print $dato['ext_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['int_ip'] ?></td>
		<td class="textl"><?php print $dato['ext_ip'] ?></td>
	</tr>
<?php
$tabella->riga();
} 
$tabella->close();
view_footer(); 
?>
