<?php
require("user.php");

if(session_is_registered("adminusuario") == false) {

   header("Location: index.php?login=falhou&causa=".urlencode('Fa&ccedil;a login para acessar a p&aacute;gina'));
   exit;

} else {
	include "brain.php";

	if($_SESSION["sessionTempo"] < (time() - $tempo_sessao)) {
		session_unset();
		session_destroy();
		setcookie("nome_usuario", "", time()-3600);
		setcookie("nivel", "", time()-3600);
		setcookie("id_usuario", "", time()-3600);
		setcookie("usuario", "", time()-3600);
		header("Location: index.php?login=falhou&causa=".urlencode('Sess&atilde;o expirou. Refa&ccedil;a o login.'));
		exit;
	}
	$_SESSION["sessionTempo"] = time();

	
	$usuario = $_COOKIE["nome_usuario"];
	$admin = mysql_query("SELECT * FROM usuarios WHERE usuario='$usuario' AND ((nivel='1') OR (nivel='2') OR (nivel='3'))");
	$dados_admin = mysql_fetch_array($admin);
	if(mysql_num_rows($admin) != 1) {
	   header("Location: index.php?login=falhou&causa=".urlencode('Usu&aacute;rio sem Permiss&atilde;o').$usuario);
	   exit;
	}
	if($superadmin) {
		if ($_COOKIE["nivel"] != "1") {
		   header("Location: index2.php?info=falhou&causa=".urlencode('Usu&aacute;rio sem Permiss&atilde;o'));
		   exit;
		}
	}
	if($administrador) {
		if (($_COOKIE["nivel"] != "1") AND ($_COOKIE["nivel"] != "2")) {
		   header("Location: index2.php?info=falhou&causa=".urlencode('Usu&aacute;rio sem Permiss&atilde;o'));
		   exit;
		}
	}
}
?>