<?php
/**************************************************************************************************
|
| iDocScript - Document Sharing Script
| http://www.idocscript.com
| webmaster@idocscript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.idocscript.com/eula.html and to be bound by it.
|
| Copyright (c) iDocScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");

$redirect = base64_decode(cleanit($_REQUEST['redirect']));
$redirect = cleanit($redirect);
STemplate::assign('redirect',cleanit($_REQUEST['redirect']));

if ($_SESSION['USERID'] != "" && $redirect != "")
{
	header("Location:$redirect");exit;
}

if($_REQUEST['authenticate']!="")
{
	$username = cleanit($_REQUEST['lusername']);
	$password = cleanit($_REQUEST['lpassword']);
	
	STemplate::assign('lusername',$username);
	STemplate::assign('lpassword',$password);
	
	if($username=="")
	{
		$error=$lang['571'];
	}
	elseif($password=="")
	{
		$error=$lang['573'];
	}
	else
	{
		$encryptedpassword = md5($password);
		$query="SELECT * from members WHERE username='".mysql_real_escape_string($username)."' and password='".mysql_real_escape_string($encryptedpassword)."'";
		$result=$conn->execute($query);
		
		if($result->recordcount()<1)
		{
			$error=$lang['580'];
		}
		elseif($result->fields['status']=="0")
		{
			$error = $lang['581'];
		}

		if($error=="")
		{
			$query="update members set lastlogin='".time()."', lip='".$_SERVER['REMOTE_ADDR']."' WHERE username='".mysql_real_escape_string($username)."'";
			$conn->execute($query);
	    	$_SESSION['USERID']=$result->fields['USERID'];
			$_SESSION['EMAIL']=$result->fields['email'];
			$_SESSION['USERNAME']=$result->fields['username'];
			$_SESSION['VERIFIED']=$result->fields['verified'];
			$_SESSION['FAMILYFILTER']=$result->fields['familyfilter'];
			if($redirect == "")
			{
				header("Location:$config[baseurl]");exit;
			}
			else
			{
				header("Location:$redirect");exit;
			}

		}
	}
}

$pagetitle = $lang['224'];
STemplate::assign('pagetitle',$pagetitle);
STemplate::assign('message',$message);
STemplate::assign('error',$error);
STemplate::display('header.tpl');
STemplate::display('login.tpl');
STemplate::display('footer.tpl');
?>