<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
set_magic_quotes_runtime(0);

include("oldinclude/dbcommon.php");

if(@$_POST["a"]=="logout" || @$_GET["a"]=="logout")
{
	session_unset();
	setcookie("username","",time()-365*1440*60);
	setcookie("password","",time()-365*1440*60);
	header("Location: login.php");
	exit();
}

include('oldlibs/Smarty.class.php');
$smarty = new Smarty();

##if @BUILDER.arrLanguages.len>1##
include("oldinclude/languages.php");

if (@$_POST["lang"])
	$_SESSION["language"]=@$_POST["lang"];
$smarty->assign("language",	mlang_getcurrentlang());
##endif##

$conn=db_connect();
//	Before Process event
if(function_exists("BeforeProcessLogin"))
	BeforeProcessLogin($conn);


$myurl=@$_SESSION["MyURL"];
unset($_SESSION["MyURL"]);

$defaulturl="";
##if CreateMenu##
		$defaulturl="menu.php";
##else##
	##foreach @BUILDER.Tables as @t filter @t.bMenuItem order @t.nMenuOrder##
		##if @first##
		##if @t.bList##
		$defaulturl="##@t.strShortTableName##_list.php";
		##elseif @t.nType==titREPORT##
		$defaulturl="##@t.strShortTableName##_report.php";
		##elseif @t.nType==titCHART##
		$defaulturl="##@t.strShortTableName##_chart.php";
		##else##
		$defaulturl="##@t.strShortTableName##_add.php";
		##endif##
		##endif##
	##endfor##
##endif##			
##if !@BUILDER.Tables[bMenuItem].len##
	##if @BUILDER.Tables.bList##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_list.php";
	##elseif @t.nType==titREPORT##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_report.php";
	##elseif @t.nType==titCHART##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_chart.php";
	##else##
		$defaulturl="##@BUILDER.Tables.strShortTableName##_add.php";
	##endif##
##endif##


##if @BUILDER.nLoginMethod==LOGIN_HARDCODED##
$cUserName = "##@BUILDER.strUserName s##";
$cPassword = "##@BUILDER.strPassword s##";
##endif##

##if @BUILDER.bAdminUser##
$cAdminUserID = "##@BUILDER.strAdminUserID s##";
##endif##

$message="";

$pUsername=postvalue("username");
$pPassword=postvalue("password");

if(@$_COOKIE["username"] || @$_COOKIE["password"])
	$smarty->assign("checked"," checked");

if (@$_POST["btnSubmit"] == "Login")
{
	if(@$_POST["remember_password"] == 1)
	{
		setcookie("username",$pUsername,time()+365*1440*60);
		setcookie("password",$pPassword,time()+365*1440*60);
		$smarty->assign("checked"," checked");
	}
	else
	{
		setcookie("username","",time()-365*1440*60);
		setcookie("password","",time()-365*1440*60);
		$smarty->assign("checked","");
	}
##if @BUILDER.nLoginMethod==LOGIN_HARDCODED##
//		 username and password are hardcoded
	$retval=true;
	if(function_exists("BeforeLogin"))
		$retval=BeforeLogin($pUsername,$pPassword);

	if($retval && !strcmp($cPassword, $pPassword) && !strcmp($cUserName, $pUsername))
	{
		$_SESSION["UserID"] = $pUsername;
		$_SESSION["AccessLevel"] = ACCESS_LEVEL_USER;
		
		if(function_exists("AfterSuccessfulLogin"))
		{
			$dummy=array();
			AfterSuccessfulLogin($pUsername,$pPassword,$dummy);
		}
		
		
		if($myurl)
			header("Location: ".$myurl);
		else
			header("Location: ".$defaulturl);
		return;
	}
	else
	{
		if(function_exists("AfterUnsuccessfulLogin"))
			AfterUnsuccessfulLogin($pUsername,$pPassword);
		$message = ##message INVALID_LOGIN##;
	}	
##else##
//   	 username and password are stored in the database
	$strUsername = (string)$pUsername;
	$strPassword = (string)$pPassword;
##if @BUILDER.bEncryptPasswords##
	$strPassword=md5($strPassword);
##endif##
	$sUsername=$strUsername;
	$sPassword=$strPassword;
	$rstemp=db_query("select * from ##@BUILDER.strLoginTable ts## where 1=0",$conn);
		
	if(FieldNeedQuotes($rstemp,$cUserNameField))
		$strUsername="'".db_addslashes($strUsername)."'";
	else
		$strUsername=(0+$strUsername);
	if(FieldNeedQuotes($rstemp,$cPasswordField))
		$strPassword="'".db_addslashes($strPassword)."'";
	else
		$strPassword=(0+$strPassword);
		$strSQL = "select * from ##@BUILDER.strLoginTable ts## where ".AddFieldWrappers($cUserNameField).
		"=".$strUsername." and ".AddFieldWrappers($cPasswordField).
		"=".$strPassword;
##if @BUILDER.bSendActivationLink##
		$strSQL.=" and ##@BUILDER.strActivationField ws##=1";
##endif##		
		if(function_exists("BeforeLogin"))
		if(!BeforeLogin($pUsername,$pPassword))
			$strSQL="select * from ##@BUILDER.strLoginTable ts## where 1<0";
	
	$rs=db_query($strSQL,$conn);
 	$data=db_fetch_array($rs);
   	if($data && @$data[$cUserNameField]==$sUsername && @$data[$cPasswordField]==$sPassword)
	{
		$_SESSION["UserID"] = $pUsername;
   		$_SESSION["AccessLevel"] = ACCESS_LEVEL_USER;

##if GroupSecurity && @BUILDER.strUserGroupField##
		$_SESSION["GroupID"] = $data["##@BUILDER.strUserGroupField s##"];
	##foreach @BUILDER.UserGroups as @u filter @u.bAdmin##
		if($_SESSION["GroupID"]=="##@u.strUserName s##")
	   		$_SESSION["AccessLevel"] = ACCESS_LEVEL_ADMINGROUP;
	##endfor##
##else##		
		$_SESSION["GroupID"] = "";
##endif##
		

##if @BUILDER.bAdminUser##
   		if($pUsername==$cAdminUserID)
			$_SESSION["AccessLevel"] = ACCESS_LEVEL_ADMIN;
##endif##


##foreach @BUILDER.tables as @t filter @t.strUsersTableOwnerID##
		##if @first##
			$_SESSION["OwnerID"] = $data["##@t.strUsersTableOwnerID s##"];
		##endif##
	$_SESSION["_##@t.strDataSourceTable s##_OwnerID"] = $data["##@t.strUsersTableOwnerID s##"];
##endfor##
	

		if(function_exists("AfterSuccessfulLogin"))
			AfterSuccessfulLogin($pUsername,$pPassword,$data);
		if($myurl)
			header("Location: ".$myurl);
		else
			header("Location: ".$defaulturl);
		return;
   	}
	else
	{
		if(function_exists("AfterUnsuccessfulLogin"))
			AfterUnsuccessfulLogin($pUsername,$pPassword);
		$message = ##message INVALID_LOGIN##;
	}
##endif##	
}

$_SESSION["MyURL"]=$myurl;
if($myurl)
	$smarty->assign("url",$myurl);
else
	$smarty->assign("url",$defaulturl);


if(@$_POST["username"] || @$_GET["username"])
	$smarty->assign("value_username","value=\"".htmlspecialchars($pUsername)."\"");
else
	$smarty->assign("value_username","value=\"".htmlspecialchars(refine(@$_COOKIE["username"]))."\"");


if(@$_POST["password"])
	$smarty->assign("value_password","value=\"".htmlspecialchars($pPassword)."\"");
else
	$smarty->assign("value_password","value=\"".htmlspecialchars(refine(@$_COOKIE["password"]))."\"");


if(@$_GET["message"]=="expired")
	$message = ##message SESSION_EXPIRED##;


$smarty->assign("message",$message);

$templatefile="login.htm";
if(function_exists("BeforeShowLogin"))
	BeforeShowLogin($smarty,$templatefile);

$smarty->display($templatefile);
?>