<?php


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

if($_REQUEST[forgotusername]!="")
{
	$email = stripslashes($_REQUEST[email]);
	
	if($email == "")
	{
		$error = "$lang[559]";
	}
	elseif(!verify_valid_email($email))
	{
		$error = "$lang[498]";
	}
	else
	{
		$query="SELECT * FROM members WHERE email='".mysql_real_escape_string($email)."'";
		$executequery=$conn->execute($query);
		
		if(mysql_affected_rows()>=1)
		{
			$gotusername = $executequery->fields['username'];
			$gotstatus = $executequery->fields['status'];
			
			if ($gotstatus == "0")
			{
				$proceedtoemail = false;
				$error = "$lang[560]";
			}
			else
			{
				$proceedtoemail = true;
			}
			
		}
		else
		{
			$error = "$lang[561]";
		}
		
		if($proceedtoemail && $error == "")
		{
				STemplate::assign('gotusername',$gotusername);
                $sendto = $executequery->fields['email'];;
                $sendername = $config['site_name'];
				STemplate::assign('sendername',$sendername);
                $from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='forgotusername'";
                $executequery = $conn->execute($query);
                $subject = $sendername." ".$executequery->fields['subject'];
                $sendmailtemplate = $executequery->fields['template'];
                $sendmailbody=STemplate::fetch($sendmailtemplate);
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
				
				$showmessage = "$lang[562]";
		}
	}
}




if($_REQUEST[resetpassword]!="")
{
	$username = stripslashes($_REQUEST[lusername]);
	
	if($username == "")
	{
		$error = "$lang[563]";
	}
	else
	{
		$query="SELECT * FROM members WHERE username='".mysql_real_escape_string($username)."'";
		$executequery=$conn->execute($query);
		
		if(mysql_affected_rows()>=1)
		{
			$gotusername = $executequery->fields['username'];
			$gotstatus = $executequery->fields['status'];
			$gotuserid = $executequery->fields['USERID'];
			
			if ($gotstatus == "0")
			{
				$proceedtoemail = false;
				$error = "$lang[560]";
			}
			else
			{
				$proceedtoemail = true;
			}
			
		}
		else
		{
			$error = "$lang[564]";
		}
		
		if($proceedtoemail && $error == "")
		{
			$passcode = generateCode(5).time();
			STemplate::assign('passcode',$passcode);
			$query = "INSERT INTO members_passcode SET USERID='".mysql_real_escape_string($gotuserid)."', code='$passcode'";
            $conn->execute($query);
			if(mysql_affected_rows()>=1)
			{
				$passcodeadded = true;
			}
			else
			{
				$passcodeadded = false;
			}
				
			if ($passcodeadded)
			{
				STemplate::assign('gotusername',$gotusername);
                $sendto = $executequery->fields['email'];;
                $sendername = $config['site_name'];
				STemplate::assign('sendername',$sendername);
                $from = $config['site_email'];
				$query = "SELECT * FROM sendmail WHERE EID='confirmforgotpass'";
                $executequery = $conn->execute($query);
                $subject = $sendername." ".$executequery->fields['subject'];
                $sendmailtemplate = $executequery->fields['template'];
                $sendmailbody=STemplate::fetch($sendmailtemplate);
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
				
				$showmessage = "$lang[565]";
			}
			else
			{
				$error = "$lang[566]";
			}
		}
	}
}

$pagetitle = "$lang[567] // $lang[158]";
STemplate::assign('pagetitle',$pagetitle);

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

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