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

if (isset($_POST['id_utenti'])) {
	$id_utenti = $_POST['id_utenti'];
	$id_macchine = $_POST['id_macchine'];
	$id_acl = $_POST['id_acl'];
	if (isset($_POST['perm'])) {
		$perm = $_POST['perm'];
	} else {
		$perm = 0;
	}
	$query = "	INSERT INTO
				condes_regole
			SET
				id_utenti = $id_utenti,
				id_condes_macchine = $id_macchine,
				id_proxy_acl_list = $id_acl,
				permanente = $perm,
				data = NOW(),
				attivo = 1
	";
	$res = mysql_query( $query, $DB_ID );
}

$query = "	SELECT  
			utenti.id,
			utenti.utente
		FROM
			utenti
		JOIN
			permessi
			ON
				permessi.id_utenti = utenti.id
		JOIN
			livelli
			ON
				(livelli.nome = 'Utente' OR livelli.nome = 'Admin')
				AND
				permessi.accesso <= livelli.livello
		WHERE   
			permessi.id_moduli = ".$MODULO['id']."
			AND
			utenti.attivo = 1
		GROUP BY
			utente
		ORDER BY
			utente
";
$res_utenti = mysql_query( $query, $DB_ID );

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

$query = "	SELECT
			id,
			nome
		FROM
			proxy_acl_list
		WHERE
			attivo = 1
		ORDER BY
			sys DESC,
			nome
";
$res_acl = mysql_query( $query, $DB_ID );

$tabella = new html (0,"90%",array(3,3,2,15,2,20,2,15,2,5,0));
?>
<form name="" method="post">
	<tr>
		<td colspan=2 class="spazioh"><input type="image" src="<?php print $CONF['base_url'] ?>/img/addresource.png" border="0" alt="Inserisci" /></td>

		<td class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh">
			<select name="id_utenti">
				<option value="" selected="selected">---</option>
<?php while ($dato = mysql_fetch_array ( $res_utenti )) { ?>
				<option value="<?php print $dato['id'] ?>"><?php print $dato['utente'] ?></option>
<?php } ?>
			</select>
		</td>

		<td class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh">
			<select name="id_macchine">
				<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']." (".$dato['ip'].")";
									} else{
										print $dato['ip'];
									} ?></option>
<?php } ?>
			</select>
		</td>

		<td class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh">
			<select name="id_acl">
				<option value="" selected="selected">---</option>
<?php while ($dato = mysql_fetch_array ( $res_acl )) { ?>
				<option value="<?php print $dato['id'] ?>"><?php print $dato['nome'] ?></option>
<?php } ?>
			</select>
		</td>

		<td class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="radio"><input type="checkbox" name="perm" value="1"></td>

		<td class="spaziov"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
</form>
<?php
$query = "	SELECT 
			condes_regole.id,
			utenti.utente,
			proxy_pool.nome,
			proxy_pool.ip,
			proxy_acl_list.nome AS acl,
			condes_regole.permanente,
			condes_regole.attivo
		FROM 
			condes_regole
		JOIN
			utenti
			ON
				condes_regole.id_utenti = utenti.id
		JOIN
			condes_macchine
			ON
				condes_regole.id_condes_macchine = condes_macchine.id
		JOIN
			proxy_pool
			ON
				condes_macchine.id_proxy_pool = proxy_pool.id
		JOIN
			proxy_acl_list
			ON
				condes_regole.id_proxy_acl_list = proxy_acl_list.id
		ORDER BY
			utenti.utente,
			-proxy_pool.nome DESC,
			proxy_pool.ip
	";

$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {
?>
	<tr>
		<td rowspan=2 class="col3btn">
<?php	if ($dato['attivo']) { ?>
		<a href="condes_gest.php?azione=regola_down&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/attivo.gif" ALT="Utente attivo" TITLE="Utente attivo"></a>
<?php	} else { ?>
		<a href="condes_gest.php?azione=regola_up&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/non_attivo.gif" ALT="Utente non attivo" TITLE="Utente non attivo"></a>
<?php	} ?>
		</td>
		<td rowspan=2 class="spazioh"><a href="condes_gest.php?azione=regola_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 class="descrizione">Utenza</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">Macchina</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">ACL</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="radio">Perm</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['utente'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="textl"><?php print $dato['ip']; if ($dato['nome'] != "") print "(".$dato['nome'].")" ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="textl"><?php print $dato['acl'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="textc"><?php if ($dato['permanente']) print "Si"; else print "No"; ?></td>
	</tr>
<?php
$tabella->riga();
} 
$tabella->close();
view_footer(); 
?>
