<?php


session_start();
include("include/config.php");
include("include/functions/import.php");
STemplate::assign('issignuppage',"1");

if($_REQUEST[register]!="")
{
	$username = stripslashes($_REQUEST[username]);
	$password = stripslashes($_REQUEST[password]);
	$confirmpassword = stripslashes($_REQUEST[confirmpassword]);
	$imagecode = stripslashes($_REQUEST[imagecode]);
	$email = stripslashes($_REQUEST[email]);
	$gender = stripslashes($_REQUEST[gender]);
	
	if ($username == "")
	{
		$error="$lang[571]";
	}
	elseif(!verify_valid_email($email))
	{
		$error = "$lang[498]";
	}
	elseif (!verify_email_unique($email))
	{
		$error = "$lang[500]";
	}
	elseif ($gender == "" || !is_numeric($gender))
	{
		$error="$lang[572]";
	}
	elseif ($password == "")
	{
		$error="$lang[573]";
	}
	elseif ($confirmpassword == "")
	{
		$error="$lang[574]";
	}
	elseif ($password != $confirmpassword)
	{
		$error="$lang[575]";
	}
	elseif($imagecode != $_SESSION[imagecode])
	{
		$error="$lang[576]";
	}
	else
	{
		$md5pass = md5($password);
		$query="INSERT INTO members SET email='".mysql_real_escape_string($email)."',username='".mysql_real_escape_string($username)."',gender='".mysql_real_escape_string($gender)."', password='".mysql_real_escape_string($md5pass)."', addtime='".time()."', lastlogin='".time()."'";
		$result=$conn->execute($query);
		$userid = mysql_insert_id();
		
		if($userid != "" && is_numeric($userid) && $userid > 0)
		{
			$query="SELECT * from members WHERE USERID='".mysql_real_escape_string($userid)."'";
			$result=$conn->execute($query);
		
	    	SESSION_REGISTER("USERID");$_SESSION[USERID]=$result->fields['USERID'];
			SESSION_REGISTER("EMAIL");$_SESSION[EMAIL]=$result->fields['email'];
			SESSION_REGISTER("USERNAME");$_SESSION[USERNAME]=$result->fields['username'];
			SESSION_REGISTER("VERIFIED");$_SESSION[VERIFIED]=$result->fields['verified'];
			SESSION_REGISTER("FAMILYFILTER");$_SESSION[FAMILYFILTER]=$result->fields['familyfilter'];
			
			// Generate Verify Code Begin
			$verifycode = generateCode(5).time();
			$query = "INSERT INTO members_verifycode SET USERID='".mysql_real_escape_string($_SESSION[USERID])."', code='$verifycode'";
            $conn->execute($query);
			if(mysql_affected_rows()>=1)
			{
				$proceedtoemail = true;
			}
			else
			{
				$proceedtoemail = false;
			}
			// Generate Verify Code End
			
			// Send Welcome E-Mail Begin
			if ($proceedtoemail)
			{
                STemplate::assign('verifycode',$verifycode);
                $sendto = $_SESSION[EMAIL];
                $sendername = $config['site_name'];
				STemplate::assign('sendername',$sendername);
                $from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='welcomeemail'";
                $executequery = $conn->execute($query);
                $subject = $executequery->fields['subject']." ".$sendername;
                $sendmailtemplate = $executequery->fields['template'];
                $sendmailbody=STemplate::fetch($sendmailtemplate);
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
			}
			// Send Welcome E-Mail End

			header("Location:$config[baseurl]");exit;

		}
		else
		{
			$error="$lang[577]";
		}
	}
}

if($_REQUEST[authenticate]!="")
{
	$username = stripslashes($_REQUEST[lusername]);
	$password = stripslashes($_REQUEST[lpassword]);
	
	if($username=="")
	{
		$error="$lang[578]";
	}
	elseif($password=="")
	{
		$error="$lang[579]";
	}
	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()."' WHERE username='".mysql_real_escape_string($username)."'";
			$conn->execute($query);
	    	SESSION_REGISTER("USERID");$_SESSION[USERID]=$result->fields['USERID'];
			SESSION_REGISTER("EMAIL");$_SESSION[EMAIL]=$result->fields['email'];
			SESSION_REGISTER("USERNAME");$_SESSION[USERNAME]=$result->fields['username'];
			SESSION_REGISTER("VERIFIED");$_SESSION[VERIFIED]=$result->fields['verified'];
			SESSION_REGISTER("FAMILYFILTER");$_SESSION[FAMILYFILTER]=$result->fields['familyfilter'];

			if($_REQUEST['go']!="")
			{
				$goto = stripslashes($_REQUEST[go]);
				$gotopage=$goto.".php";
			}
			else
			{
				$gotopage="index.php";
			}

			header("Location:$config[baseurl]/$gotopage");exit;

		}
	}
}

$pagetitle = "$lang[224]";
STemplate::assign('pagetitle',$pagetitle);

STemplate::assign('message',$message);
STemplate::assign('error',$error);

//TEMPLATES BEGIN
STemplate::display('header.tpl');
STemplate::display('login.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>