<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Admin');
view_top();
?>
<form name="" method="post">
<?php
if (isset($_GET['id'])) { 
	$tabella = new html (0,"90%",array(3,15,2,25,2,2,12,2,12,2,10,2,10,0));
	$id = $_GET['id'];

	if ((isset($_POST['nomeold'])) && (isset($_POST['nome'])) && ($_POST['oldnome'] != $_POST['nome'])) {
		$query = "	UPDATE
					proxy_pool
				SET
					nome = '".$_POST['nome']."'
				WHERE
					id = $id
		";
		$res = mysql_query( $query, $DB_ID );
	}

	if (isset($_POST['tempo'])) {
		$tempo = $_POST['tempo'];
	} else {
		$tempo = "DAY";
	}

	$query = "	SELECT
				proxy_pool.attivo,
				proxy_pool.ip,
				proxy_pool.nome
			FROM
				proxy_pool
			WHERE
				proxy_pool.id = $id
		";

	$res = mysql_query( $query, $DB_ID );
	$pool = mysql_fetch_array ( $res );

	$query1 = "	SELECT 
				MIN(proxy_dati.data_destroy) AS first, 
				MAX(proxy_dati.data_destroy) AS last, 
				SUM(proxy_dati.inviati) AS inviati,
				SUM(proxy_dati.ricevuti) AS ricevuti
			FROM
				proxy_dati
			WHERE
				ip = '".$pool['ip']."'
			AND
			proxy_dati.data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
		";

	$query2 = " 	SELECT
				MIN(proxy_dati_storico.data_destroy) AS first, 
				MAX(proxy_dati_storico.data_destroy) AS last, 
				SUM(proxy_dati_storico.inviati) AS inviati,
				SUM(proxy_dati_storico.ricevuti) AS ricevuti
			FROM
				proxy_dati_storico
			WHERE
				ip = '".$pool['ip']."'
			AND
				proxy_dati_storico.data_destroy >= DATE_SUB(NOW(), INTERVAL 1 $tempo)
		";

	$res1 = mysql_query( $query1, $DB_ID );
	$data1 = mysql_fetch_array ( $res1 );
	$res2 = mysql_query( $query2, $DB_ID );
	$data2 = mysql_fetch_array ( $res2 );

	if (is_null ($data1['first'])) {
		$first = $data2['first'];
	} elseif (is_null ($data2['first'])) {
		$first = $data1['first'];
	} else {
		$first = min ($data1['first'], $data2['first']);
	}

	if (is_null ($data1['last'])) {
		$last  = $data2['last'];
	} elseif (is_null ($data2['last'])) {
		$last  = $data1['last'];
	} else {
		$last  = max ($data1['last'], $data2['last']);
	}
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td>
			<select name="tempo" onchange="this.form.submit()">
				<option value="HOUR" <?php if ($tempo == "HOUR") {print "selected=\"selected\""; } ?>>Ultima Ora</option>
				<option value="DAY" <?php if ($tempo == "DAY") {print "selected=\"selected\""; } ?>>Ultimo Giorno</option>
				<option value="WEEK" <?php if ($tempo == "WEEK") {print "selected=\"selected\""; } ?>>Ultima Settimana</option>
				<option value="MONTH" <?php if ($tempo == "MONTH") {print "selected=\"selected\""; } ?>>Ultimo Mese</option>
				<option value="YEAR" <?php if ($tempo == "YEAR") {print "selected=\"selected\""; } ?>>Ultimo Anno</option>
			</select>
		</td>
		<td colspan="12" class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php	
$tabella->riga();
$tabella->intestazione (array("", "IP", "", "Nome", "", "", "Inizio dati", "", "Fine Dati", "", "Inviati", "", "Ricevuti", ""));
?>
	<tr>
<?php
	if ($pool['attivo']) {
?>
		<td class="col3btn"><a href="proxy_gest.php?azione=pool_rem&id=<?php print $pool['ip'] ?>"><img src="<?php print $CONF['base_url'] ?>/img/non_attivo.gif" ALT="Utente limitato" TITLE="Utente limitato"></a></td>
<?php	} else { ?>
		<td class="col3btn"><a href="proxy_gest.php?azione=pool_ins&id=<?php print $pool['ip'] ?>"><img src="<?php print $CONF['base_url'] ?>/img/attivo.gif" ALT="Utente non limitato" TITLE="Utente non limitato"></a></td>
<?php	} ?>
		<td class="colip"><?php print $pool['ip'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<input type="text" name="nome" value="<?php print $pool['nome'] ?>">
			<input type="hidden" name="nomeold" value="<?php print $pool['nome'] ?>">
		</td>
		<td class="spazioh">
			<input name="modifica" type="image" src="<?php print $CONF['base_url'] ?>/img/applica.png" alt="Modifica" title="Modifica">
		</td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $first ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $last ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colspeed"><?php print byteConvert($data1['inviati']+$data2['inviati']) ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colspeed"><?php print byteConvert($data1['ricevuti']+$data2['ricevuti']) ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php	
$tabella->riga(); ?>
	<tr><td colspan=14 align="center"><img src="<?php print $CONF['base_url'] ?>/proxy/graph.php?ip=<?php print $pool['ip'] ?>&tempo=<?php print $tempo ?>"></td></tr>
<?php	
$tabella->riga(); 
} else {
	if (isset($_POST['ip'])) {
		$ip = $_POST['ip'];
		$nome = $_POST['nome'];
		if (isset($_POST['attivo'])) {
			$attivo = $_POST['attivo'];
		} else {
			$attivo = 0;
		}

		$query = "	INSERT INTO
					proxy_pool
				SET
					ip = '$ip',
					nome = '$nome',
					pool = 1,
					attivo = $attivo,
					ins = NOW()
		";
		$res = mysql_query( $query, $DB_ID );

		if (!$res) {
			$error_code = mysql_errno();
			if ($error_code == 1062) { ?>
	<tr><td class="spaziov" colspan=12><center>Indirizzo IP gia' esistente</center></td></tr>
<?php			}
		} else { 
			$query = "SELECT id FROM proxy_pool WHERE ip = '$ip'";
			$res = mysql_query( $query, $DB_ID );
			$ritorno = mysql_fetch_array ( $res );




?>
	<script type="text/javascript">
           window.location = "pool_acl.php?id=<?php print $ritorno['id']?>"
      </script>
<?php		exit;
		}
	}
$tabella = new html (0,"90%",array(2,15,2,15,2,5,0));
$tabella->intestazione (array("", "IP", "", "Nome", "", "Limitazione", ""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input type="text" size="15" name="ip"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input type="text" size="15" name="nome"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="radio"><input type="checkbox" name="attivo" value="1"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php	$tabella->riga(); ?>
	<tr>
		<td align="center" colspan="7">
			<input type="submit" value="Aggiungi">
			&nbsp;&nbsp;&nbsp;
			<input type="button" value="Annulla" onclick="location.href = 'pool_lista.php';"></button>
		</td>
	</tr>
<?php 
} 
$tabella->close(); ?>
</form>
<?php 
view_footer(); 
?>
