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

if (isset($_POST['id'])) {
	$id = $_POST['id'];
	$user = $_POST['user'];
	$fullname = $_POST['fullname'];
	$ip = $_POST['ip'];

	$pwd = "";

	if ($_POST['pwd1'] != "") {
		$pwd1 = $_POST['pwd1'];
		$pwd2 = $_POST['pwd2'];

		if ($pwd1 == $pwd2) {
			$pwd = "pass = ENCRYPT('$pwd1'),";
		} else {
			$pwd = -1;
		}
	}

	if ($pwd == -1) {
		print "ERRORE le password devono coincidere";
	} else {
		if ($id) {
			$query = "	UPDATE
						proxy_utenti
					SET
						user = '$user',
						fullname = '$fullname',
						ip = '$ip',
						$pwd
						modifica = NOW()
					WHERE
						id = $id	
			";
		} else {
			$query = "	INSERT INTO
						proxy_utenti
					SET
						user = '$user',
						fullname = '$fullname',
						ip = '$ip',
						$pwd
						data = NOW(),
						modifica = NOW(),
						attivo = 1
			";
		}

		$res = mysql_query( $query, $DB_ID );
		if ($id == "") {
			$id = mysql_insert_id();
?>
			<script type="text/javascript">
				document.location.href='users_edit.php?id=<?php print $id ?>'
			</script>
<?php
			exit;
		}
	}
} elseif (isset($_GET['id'])) {
	$id = $_GET['id'];
}
?>
<script type="text/javascript">
function getvalue() {
	MyIFrame = document.getElementById("chkpwd");
	MyIFrame.src = "users_pwd.php";
}
</script>

<?php
//     <iframe id="chkpwd" style="display:none; visibility:hidden;"></iframe>
?>

<form name="" method="post">
<input type="hidden" name="id" value="<?php print $id ?>">
<?php

$query = "	SELECT 
			id,
			user,
			fullname,
			attivo,
			DATE_FORMAT(data, '%d.%m.%Y %H:%i:%s') AS data,
			DATE_FORMAT(modifica, '%d.%m.%Y %H:%i:%s') AS modifica,
			ip
		FROM
			proxy_utenti
		WHERE
			id = $id
	";

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

$query = "	SELECT
			ip
		FROM
			proxy_pool
		";
$res = mysql_query( $query, $DB_ID );

$tabella = new html (0,"90%",array(3,16,2,20,2,15,2,16,2,10,0));
$tabella->intestazione (array("","Utente","","Nome Completo","","Data attivazione","","Ultima Modifica","","IP",""));
?>
	<tr>
<?php
if ($dato['attivo']) {
?>
		<td class="col3btn"><a href="proxy_gest.php?azione=user_down&id=<?php print $dato['id']?>"><img src="<?php print $CONF['base_url'] ?>/img/attivo.gif" ALT="Utente Attivo" TITLE="Utente Attivo"></a></td>
<?php	} else { ?>
		<td class="col3btn"><a href="proxy_gest.php?azione=user_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></td>
<?php	} ?>
		<td class="colip"><input type="text" size="10" name=user value="<?php print $dato['user'] ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input type="text" size="20" name=fullname value="<?php print $dato['fullname'] ?>"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['data'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><?php print $dato['modifica'] ?></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip">
			<select name="ip">
				<option value="" <?php if ($dato['ip'] == "") { print "selected=\"selected\"";}?>>Nessuno</option>
<?php	while ($dato_ip = mysql_fetch_array ( $res )) { ?>
				<option value="<?php print $dato_ip['ip']?>" <?php if ($dato['ip'] == $dato_ip['ip']) { print "selected=\"selected\"";}?>><?php print $dato_ip['ip']?></option>
<?php	} ?>
			</select>
		</td>
	</tr>
<?php
$tabella->riga();
$tabella->intestazione (array("","Nuova Password","","Conferma Nuova Password","","","","","","",""));
?>
	<tr>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input type="password" size="15" name=pwd1 id=pwd1></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input type="password" size="15" name=pwd2 id=pwd2></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="spazioh"><input type="button" value="Genera password" onclick="window.open('users_pwd.php','_blank','width=1,toolbar=0,resizable=0,scrollbars=no,height=1,top=0,left=0,visible=none');"></td>
		<td class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="colip"><input id=pwd size="15"  type=text></p></td>
		<td colspan="3" class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php $tabella->riga(); ?>
	<tr><td class="spaziov" colspan=11><input type="submit" value="Conferma"></td></tr>

<?php $tabella->close(); ?>
</form>
<?php view_footer(); ?>
