<?php
/* * * copyright 2004-2012 José Américo Rio * * */
if(!session_id()) session_start();
header('Content-Type: text/html; charset=iso-8859-1');
if (empty ($anterior)){$anterior=$PHP_SELF;}

function auth($login='', $password=''){
        $anterior=$_POST['anterior'];
		
		$cookie_name = 'ec';
		$cookie_time = (3600 * 24 * 30); // 30 days
        
        //session_start();
		 
        
		if ( @is_array ( $_SESSION["eclesia"] ))
			if(@is_numeric($_SESSION['eclesia']['idu'])){
				//print "já logado";
			 	return true;
		}
        if (!(empty($login)) && !(empty($password)) ){ 
				include_once("bo/config/config.inc.php");
				$dblink = mysql_connect($host , $user , $pass );
				mysql_select_db($database, $dblink ) ;

				
				$login=urldecode ($login);
				
				if(get_magic_quotes_gpc() == true) {
					$login=stripslashes($login);
				}
				$login=mysql_real_escape_string($login);
				
				$data_hoje=date("Y-m-d");
				
               $sql="select * from utilizador where email='$login' and numero='$password' and data_inicio <='$data_hoje' and data_fim >='$data_hoje' and estado=1";	

               $result=mysql_query($sql);

			   if(($result) && (mysql_num_rows($result) == 1)) {
					$tauth=mysql_fetch_array($result);
					$_SESSION["eclesia"]['idu']=$tauth['id'];
					$_SESSION["eclesia"]['nome']=$tauth['nome'];
					$_SESSION["eclesia"]['email']=$tauth['email'];
					

					if ($_POST['auto']){
						setcookie ($cookie_name, 
						md5($tauth['email']), 
						time() + $cookie_time, '/');
					}
						

					print "<script>location.reload();</script>";
					//print "login";
                    return true;
			    }
        }
        
        unset ($_SESSION["eclesia"]);
        return false;

}


if (!auth ($_POST['login'], $_POST['password'] )) {
        print "O seu login e password não correspondem";     
}

?>