<?php

function view_top_proxy () {
	global $CONF, $UTENTE;
	include_once ("top_proxy.php");
}

function view_footer_proxy () {
	global $CONF, $UTENTE;
	include_once ("footer_proxy.php");
}

function db_login_proxy($user,$pass) {
	global $DB_ID;
	$query = "SELECT id FROM proxy_utenti WHERE user = '$user' AND pass = ENCRYPT('$pass', LEFT(pass, 2)) AND attivo = 1";
	$res = mysql_query( $query, $DB_ID );
	$dato = mysql_fetch_array ( $res );
	$ritorno = $dato['id'];
	return $ritorno;
}

function login_proxy () {
	global $_SESSION, $_POST, $DB_ID, $CONF;
	session_name("authmail");
	session_start();
	if ($_SESSION['auth'] && ($_SESSION['time']+$CONF['temposessione']*60 >= time())) {
		if(!db_login_proxy ($_SESSION['username'], $_SESSION['passwd'])) {
			$tMessage = '<span class="error_msg">Dati di autenticazione errati</span>';
			$tUsername = $_SESSION['username'];
			$_SESSION['auth'] = 0;
			session_destroy();
			include ("userlogin.php");
			exit;
		} else {
			logga ($_SERVER['REQUEST_URI']);
			$_SESSION['time'] = time();
			return carica_utente_proxy ();
		}
	} else {
		if ($_POST['fUsername']) {
			$fUsername = $_POST['fUsername'];
	   		$fPassword = $_POST['fPassword'];
	   		if(db_login_proxy ($fUsername, $fPassword)) {
		      		$_SESSION = array();
		      		$_SESSION['passwd'] = $fPassword;
		      		$_SESSION['username'] = $fUsername;
		      		$_SESSION['auth'] = 1;
      				$_SESSION['time'] = time();
				session_regenerate_id(TRUE);
      				return carica_utente_proxy ();
   			} else {
   				$tMessage = 'Errore';
        		 	$tUsername = $fUsername;
				include ("userlogin.php");
        		 	exit;
   			}
		} else {
			if ($_SESSION['auth'] && ($_SESSION['time']+$CONF['temposessione']*60 < time())) {
				$tMessage = '<span class="error_msg">Sessione scaduta</span>';
				$tUsername = $_SESSION['username'];
				logga ("Sessione scaduta");
				include ("userlogin.php");
       			  	exit;
			} else {
				include ("userlogin.php");
       			  	exit;
			}
		}
	}
}

function carica_utente_proxy () {
	global $_SESSION, $CONF, $DB_ID;
	$query = "SELECT * FROM proxy_utenti where user = '".$_SESSION['username']."'";
	$res = mysql_query( $query, $DB_ID );
	$dati = mysql_fetch_array ( $res );

	return $dati;
}

function lista_moduli_proxy () {
	global $UTENTE, $DB_ID, $CONF;

	$menu[] =	array("Home", "main_proxy.php",0,"Home Page",0);
	$menu[]	=	array("Cambio password", "chkpasswd_proxy.php",0,"Cambio Password");

	$pezzi = preg_split('/\//', $_SERVER["PHP_SELF"], -2);
	$self = $pezzi[count($pezzi)-1];
?>
		<ul id="nav">
<?php 	for ($i=0; $i<count($menu);$i++) {
		if ($self == $menu[$i][1]) {
			$classe = "sel";
		} else {
			$classe = "nosel";
		}
?>
			<li>
				<a class="<?php print $classe ?>" href="<?php print $menu[$i][1]?>"><?php print $menu[$i][0]?></a>
<?php
		if ($menu[$i][2] != 0) {
			$query = "	SELECT 
						moduli_page.nome AS nome, 
						moduli_page.pagina AS pagina,
						moduli.directory AS directory
					FROM 
						moduli_page 
					JOIN
						moduli
						ON
							moduli.id = moduli_page.id_moduli
					WHERE 
						moduli_page.attivo = 1 
						AND
						moduli_page.livello <= ".$menu[$i][4]."
					AND 
						moduli_page.id_moduli = ".$menu[$i][2]." 
					ORDER BY 
						moduli_page.ordine
				";

			$res_page = mysql_query( $query, $DB_ID ); ?>
				<ul>
<?php			while ($page = mysql_fetch_array ($res_page)) { ?>
					<li><a href="<?php print $CONF['base_url']."/".$page['directory']."/".$page['pagina'] ?>"><?php print $page['nome'] ?></a></li>
<?php			} ?>
				</ul>

<?php		} ?>
			</li>
<?php	} ?>
		</ul>

<?php
}







?>
