<?php
define('THIS_SCRIPT', 'lostpass');
include_once 'global.php';
$lang = load_lang($lang, 'lostpass' );
global $title_num,$action,$stats,$title_num;
$stats = $lang['template.str2'].' - '.$fsetting['forumname'];//论坛标题;
$bbs_stat=$fsetting['forumname'];//论坛名称;
$title_num = array(
	0=>array('linktitle'=>$lang['template.str1'],'linkurl'=>'login.php')
	,1=>array('linktitle'=>$lang['template.str2'])
);
head(1,0,0,$title_num);// print head;
PageLoad();
footer();
function PageLoad(){
	global $dv,$db,$action,$step1,$step2,$step3,$Quesion,$answer,$lang,$username,$fsetting;
	if(($action=='step1' or $action=='step2') && $fsetting['forgetcode'] && !checkcercode()){
        showmsg($lang['tpl.forget.cercode_err']);	
        return false;
    }
	if($action=="step1"){
		step1();
	}elseif($action=="step2"){
		step2();
	}elseif($action=="step3"){
		step3();
	}
	include_once printout('lostpass');
}



function step1(){
	global $action,$username,$dv,$db,$Quesion,$answer,$lang,$fsetting;
	$step1_info=array();
	if($username==""){
		showmsg($lang['Error.str4']);
	exit;
	}
	else{
		$sql="select userquesion,useranswer,username,usergroupid from {$dv}user where username='".trim($username)."'";
		$result=$db->query($sql);
        $number =$db->num_rows($result);
		if($number < 1) {
			showmsg($lang['Error.str5']);
		exit;
        }
		else{
			$step1_info=$db->fetch_array($result);
			$Quesion=$step1_info['userquesion'];
		}
	}
}
function step2(){
	global $action,$username,$dv,$db,$Quesion,$answer,$lang,$fsetting;
	$step2_info=array();
	if($_POST['answer']==""){
		showmsg($lang['Error.str7']);
	exit;
	}
	else{
		$sql="select useranswer,userquesion,usertoday,userid from {$dv}user where username='".trim($username)."'";
		$result=$db->query($sql);
        $step2_info=$db->fetch_array($result);
		if($step2_info['useranswer']!=dv_md5(trim($answer))) {
			showmsg($lang['Error.str6']);
		exit;
        }
		else{
			$Quesion=$step2_info['userquesion'];
		}
	}
}
function step3(){
	global $action,$username,$dv,$db,$Quesion,$answer,$password,$repassword,$lang,$fsetting;
	$step3_info=array();
	$password=str_replace(" ","",$_POST['password']);
	$repassword=str_replace(" ","",$_POST['repassword']);
	if($answer==""){
		showmsg($lang['tmp.info35']);
	exit;		
	}
	else{
		$sql="select useranswer from {$dv}user where username='".trim($username)."'";
		$result=$db->query($sql);
        $step2_info=$db->fetch_array($result);
		if($step2_info['useranswer']!=dv_md5(trim($answer))) {
			showmsg($lang['Error.str6']);
		exit;
        }
		else{
			if($password=="" and $repassword==""){
				showmsg($lang['Error.str9']);
			exit;
			}
			elseif($password=="" and $repassword!=""){
				showmsg($lang['Error.str9']);
			exit;
			}
			elseif($password!="" and $repassword==""){
				showmsg($lang['Error.str10']);
			exit;
			}
			elseif($password!=$repassword){
				showmsg($lang['Error.str11']);
			exit;
			}
			elseif(strlen($password)>10 or strlen($password)<6){
				showmsg($lang['Error.str9']);
			exit;
			}
			else{
				$md5_password=dv_md5($password);
				$sql="update {$dv}user set userpassword='$md5_password' where username='".trim($username)."'";
				$result=$db->query($sql);
				$password=$password;
			}
		}
	}
}
?>