<?php


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

if ($_REQUEST[code] != "")
{
	if (strlen($_REQUEST[code]) != "15")
	{
		$error = "$lang[654]";
	}
	else
	{
		$code = stripslashes($_REQUEST[code]);
		$query="SELECT * from members_passcode WHERE code='".mysql_real_escape_string($code)."'";
		$result=$conn->execute($query);
		
		if($result->recordcount()>=1)
		{
			$userid = $result->fields['USERID'];
			
			$randompassword = generateCode(8);
			if ($randompassword != "" && $userid != "" && is_numeric($userid) && $userid > 0)
			{
				$newpassword = md5($randompassword);
				$query="UPDATE members SET password='".mysql_real_escape_string($newpassword)."' WHERE USERID='".mysql_real_escape_string($userid)."'";
				$executequery=$conn->execute($query);
				
				if(mysql_affected_rows()>=1)
				{
					$query="SELECT * from members WHERE USERID='".mysql_real_escape_string($userid)."'";
					$executequery=$conn->execute($query);
					$gotusername = $executequery->fields['username'];
					STemplate::assign('gotusername',$gotusername);
					STemplate::assign('randompassword',$randompassword);
                	$sendto = $executequery->fields['email'];;
                	$sendername = $config['site_name'];
					STemplate::assign('sendername',$sendername);
                	$from = $config['site_email'];
					$query = "SELECT * FROM sendmail WHERE EID='sendnewpassword'";
                	$executequery = $conn->execute($query);
                	$subject = $executequery->fields['subject']." ".$sendername;
                	$sendmailtemplate = $executequery->fields['template'];
                	$sendmailbody=STemplate::fetch($sendmailtemplate);
                	mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
					
					$showmessage = "$lang[655]";
					
					$query="DELETE FROM members_passcode WHERE code='".mysql_real_escape_string($code)."'";
					$executequery=$conn->execute($query);
				}
				else
				{
					$error = "$lang[656]";
				}
			}
			else
			{
				$error = "$lang[657]";
			}

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

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

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

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