<?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) 2011 iDocScript.com. All rights reserved.
|**************************************************************************************************/

include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

if($_REQUEST['forgotusername']!="")
{
	$email = cleanit($_REQUEST['email']);
	STemplate::assign('email',$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'];				
				$subject = $lang['639'];
				$sendmailbody = $lang['640'].",<br><br>";
				$sendmailbody .= $lang['641']."<br>".$lang['696'].": ".$gotusername."<br><br>";
				$sendmailbody .= $lang['578'].",<br>";
				$sendmailbody .= $sendername;
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
				$message = $lang['562'];
		}
	}
}

if($_REQUEST['resetpassword']!="")
{
	$username = cleanit($_REQUEST['lusername']);
	STemplate::assign('lusername',$username);
	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 = "DELETE FROM members_passcode WHERE USERID='".mysql_real_escape_string($gotuserid)."'";
            $conn->execute($query);
			$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'];
				$subject = $sendername." ".$lang['697'];
				$sendmailbody = $lang['698']." ".$gotusername.",<br><br>";
				$sendmailbody .= $lang['699']."<br><br>";
				$sendmailbody .= "<a href='".$thebaseurl."/resetpassword/".$passcode."'>".$thebaseurl."/resetpassword/".$passcode."</a><br><br>";
				$sendmailbody .= $lang['578'].",<br>";
				$sendmailbody .= $sendername;
                mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc="");
				$message = $lang['565'];
			}
			else
			{
				$error = $lang['566'];
			}
		}
	}
}

$pagetitle = $lang['567']." | ".$lang['158'];
STemplate::assign('pagetitle',$pagetitle);
STemplate::assign('message',$message);
STemplate::assign('error',$error);

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