<?php
/* * * copyright 2004-2005 José Américo Rio * * */
if (!session_id())session_start();
include_once("include/utils.inc.php");

$PHP_SELF=$_SERVER['PHP_SELF'];

if ($_REQUEST['cmd']=="logout") {
	 
	 
	include_once("config/config.inc.php");
	$dblink = @mysql_connect($host , $user , $pass );
	mysql_select_db($database, $dblink );	 
//	$idUser = $_SESSION['authdata']['id'];
//	$data = date("Y-m-d");
//	$hora = date("H:i");
//	$sql="insert into log (idUser, accao, data, hora) 
//	 	  values ('$idUser', 'logout', '$data', '$hora')";
//	 mysql_query($sql);	 
	 updateLog("Logout");
	 
	 session_unregister("authdata");
	 unset($_SESSION['authdata']);
	 //session_destroy();
	 //unset($user);
}

if (empty ($anterior)){$anterior=$PHP_SELF;}

function auth($login='', $password=''){
        global $_SESSION, $anterior;
		//if (!session_id())session_start();
        $check= ( !(empty($login)));

		if ( is_array ( $_SESSION["authdata"] )) {
				return true;

        } elseif ($check){ 
				
				include_once("config/config.inc.php");
				$dblink = mysql_connect($host , $user , $pass );
				mysql_select_db($database, $dblink );
				
               	$Epassword=md5($password);
				
				if(get_magic_quotes_gpc() == true) {
					$login=stripslashes($login);
				}
				$login=mysql_real_escape_string($login);
				
               $sql="select * from user where login='$login' and password='$Epassword' and admin='1'";
               $result=mysql_query($sql);
			   $_SESSION['errologin'] = 0;
			   if(($result) && (mysql_num_rows($result) == 1)) {
			   		
					$tauth=mysql_fetch_array($result);
					$_SESSION['authdata']['id']=$tauth['id'];
					$_SESSION['authdata']['login']=$tauth['login'];
					$_SESSION['authdata']['admin']=$tauth['admin'];
					$_SESSION['authdata']['super']=$tauth['super'];
					$_SESSION['authdata']['nome']=$tauth['nome'];
					$_SESSION['authdata']['id_grupo']=$tauth['id_grupo'];

//					$idUser = $_SESSION['authdata']['id'];
//					$data = date("Y-m-d");
//					$hora = date("H:i");
//					$sql="insert into log (idUser, accao, data, hora) 
//						  values ('$idUser', 'login', '$data', '$hora')";
//					mysql_query($sql);
					updateLog("Login");	 
                    return true;
				}
	            unset ($_SESSION['authdata']);   
	            $_SESSION['errologin'] = 1;
				header ("Location: login.php");
	            return false;
        } else { // nao está autenticado e campo password vazio
			header ("Location: login.php");
            return false;
        }
}

//////////////////////////////////////////////////////////////////
function loginform($error=false) {
	GLOBAL $PHP_SELF, $root;
	header ("Location: login.php");
//	header("Window-target:_top");
//	header("Window-Target:_top"); 
} //fim da função

if (!auth ($_POST['login'], $_POST['password'] )) {
    loginform (isset($_POST['login']) );
}

function logout(){
	//if (!session_id())session_start();
	 
	include_once("config/config.inc.php");
	$dblink = mysql_connect($host , $user , $pass );
	mysql_select_db($database, $dblink );
	 
	updateLog("Logout");
	
	session_unregister("authdata");
	unset($_SESSION['authdata']);
	//session_destroy();
}



?>