<?php

(!defined('IN_PowerBB')) ? die() : '';

$CALL_SYSTEM				=	array();
$CALL_SYSTEM['REQUEST'] 	= 	true;

include('common.php');

define('CLASS_NAME','PowerBBPasswordMOD');

class PowerBBPasswordMOD
{
	function run()
	{
		global $PowerBB;

		if ($PowerBB->_GET['index'])
		{
			$this->Index();
		}
		elseif ($PowerBB->_GET['forget'])
		{
			$this->_PasswordChange();
		}
		elseif ($PowerBB->_GET['pass_change'])
		{
			$this->_PasswordChange_1();
		}

		$PowerBB->functions->GetFooter();
	}

	function Index()
	{
		global $PowerBB;

	    $PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Completion_changing_the_password']);

	   	$PowerBB->functions->AddressBar($PowerBB->_CONF['template']['lang']['Completion_changing_the_password']);

		if (empty($PowerBB->_GET['code']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_url_not_true']);
		}

		if ($PowerBB->_GET['id'])
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_url_not_true']);
		}



		$ReqArr 			= 	array();
		$ReqArr['where'] 	= 	array('random_url',$PowerBB->_GET['code']);

		$RequestInfo = $PowerBB->request->GetRequestInfo($ReqArr);

		if (!$RequestInfo)
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_demand_does_not_exist']);
		}

		$MemberArr 			= 	array();
		$MemberArr['where'] 	= 	array('username',$RequestInfo['username']);

		$ForgetMemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);


		 $PowerBB->_CONF['member_row']['new_password'] = md5($PowerBB->_CONF['member_row']['new_password']);

		$PassArr 			= 	array();
		$PassArr['field'] 	= 	array();

		$PassArr['field']['password'] 	= 	$PowerBB->_CONF['member_row']['new_password'];
		$PassArr['where'] 				= 	array('username',$RequestInfo['username']);

		$UpdatePassword = $PowerBB->member->UpdateMember($PassArr);


		if ($UpdatePassword)
		{
		  $PowerBB->template->assign('member_id',$ForgetMemberInfo['id']);
          $PowerBB->template->display('forget_password_change');
		}


	}

	function _PasswordChange()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Ongoing_process']);

		//////////

		// Check if the information aren't empty
		if (empty($PowerBB->_POST['new_password']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Please_fill_in_all_the_information']);
		}

		//////////

		   $PowerBB->_POST['new_password'] = md5($PowerBB->_POST['new_password']);

			$PassArr 				= 	array();
			$PassArr['field']		=	array();
			$PassArr['field']['password'] 			= 	$PowerBB->_POST['new_password'];
			$PassArr['where'] = array('id',$PowerBB->_POST['member_id']);

			$UpdatePassword = $PowerBB->member->UpdateMember($PassArr);

			if ($UpdatePassword)
			{
                $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['password_was_changed_successfully']);
				$PowerBB->functions->redirect('index.php');
			}

	}

	function _PasswordChange_1()
	{
		global $PowerBB;

		$PowerBB->functions->ShowHeader($PowerBB->_CONF['template']['lang']['Ongoing_process']);

		if (empty($PowerBB->_GET['code']))
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_url_not_true']);
		}

		$ReqArr 			= 	array();
		$ReqArr['where'] 	= 	array('random_url',$PowerBB->_GET['code']);

		$RequestInfo = $PowerBB->request->GetRequestInfo($ReqArr);

		if (!$RequestInfo)
		{
			$PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Sorry_demand_does_not_exist']);
		}
		$MemberArr 			= 	array();
		$MemberArr['where'] 	= 	array('username',$RequestInfo['username']);

		$MemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);


		//////////

		   $MemberInfo['new_password'] = md5($MemberInfo['new_password']);

			$PassArr 				= 	array();
			$PassArr['field']		=	array();
			$PassArr['field']['password'] 			= 	$MemberInfo['new_password'];
			$PassArr['where'] = array('id',$MemberInfo['id']);

			$UpdatePassword = $PowerBB->member->UpdateMember($PassArr);

			if ($UpdatePassword)
			{
                $PowerBB->functions->msg($PowerBB->_CONF['template']['lang']['password_was_changed_successfully']);
				$PowerBB->functions->redirect('index.php');
			}

	}

}

?>
