<?php
CURSCRIPT != 'wap' && exit('FORBIDDEN');
$action = in_array($action,array('sendmail','sendsms','resetpage','resetpwd')) ? $action : '';
$authcodesettings = read_static_cache('authcodesettings');
require MYMPS_MEMBER.'/include/common.func.php';
if($action == 'sendmail'){
	
	$email = isset($email) ? mhtmlspecialchars($email): '';
	if($authcodesettings['forgetpass'] == 1 && !$randcode = mymps_chk_randcode($checkcode)){
		redirectmsg("验证码输入错误","?mod=forgetpass");
		exit;
	}
	empty($email)  && redirectmsg("请填写电子邮箱地址","?mod=forgetpass");
	//if(mgetcookie('emailsended'.$email) == 1){redirectmsg("休息一会再发送邮件");}
	$user_info = $db ->getRow("SELECT * FROM `{$db_mymps}member` WHERE email = '$email'");
	if ($user_info['userid']){
		require MYMPS_INC.'/email.fun.php';
		$code = base64_encode($user_info['id'].".".md5($user_info['id'].'+'.$user_info['userpwd']).'.'.$timestamp);
		
		globalassign();
		if (send_pwd_email($user_info['id'], $user_info['userid'], $email, $code,$mayimobile['mobiledomain'])){
			//msetcookie("emailsended".$userinfo['email'],1);
			//globalassign();
include mayi_tpl('member_forgetpass_2');
			redirectmsg("邮件已发送，请您立即登陆该邮箱并按信中提示修改密码。","?mod=login");
		}else{
			$msg = '发送邮件失败，请联系客服：'.$mymps_global["SiteTel"].'重设密码！';
			redirectmsg($msg,$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
			exit;
		}
	
	}else{
	
		$msg = '该电子邮箱或用户名不存在！请联系客服';
		redirectmsg($msg,$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
		exit;
	
	}
	
}elseif($action == 'sendsms'){
	
	require MYMPS_ROOT.'/include/sms.fun.php';
	$mobile = isset($mobile) ? mhtmlspecialchars($mobile) : '';
	if($authcodesettings['forgetpass'] == 1 && !$randcode = mymps_chk_randcode($checkcode)) redirectmsg('验证码输入错误，请返回重新输入',$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
	empty($mobile)  && redirectmsg("请填写您的手机号码！","?mod=forgetpass");
	$user_info = $db ->getRow("SELECT * FROM `{$db_mymps}member` WHERE mobile = '$mobile'");
	if ($user_info['userid']){
		
		$smsconfig = get_sms_config();
		$smsconfig['sms_service'] && include MYMPS_ROOT.'/include/'.$smsconfig['sms_service'].'/mymps.php';
		
		if(!send_pwdsms($smsconfig['sms_user'],$smsconfig['sms_pwd'],$mobile,$smsconfig['sms_pwdtpl'])){
			
			$msg = '验证码发送失败！请联系客服';
			redirectmsg($msg,$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
			exit;
			
		}else{
			
			$uid = $user_info['id'];
			$userid = $user_info['userid'];
			$email=$user_info['email'];
			globalassign();
include mayi_tpl('member_forgetpass_2');
			
		}
	}else{
	
		$msg = '该手机号尚未注册用户！请联系客服';
		redirectmsg($msg,$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
		exit;
	
	}
	
}elseif($action == 'resetpwd'){
	$uid = $uid ? intval($uid) : '';
	$userid = $userid ? trim($userid) : '';
	$userpwd = $userpwd ? trim($userpwd) : '';
	$email = $email ? trim($email) : '';
	$mobile = $mobile ? trim($mobile) : '';
	$smscheckcode = $smscheckcode ? intval($smscheckcode) : '';
	if($mymps_global['cfg_member_verify'] == 4){
		if(!mymps_chk_smsrandcode($smscheckcode,$mobile)) redirectmsg('密码修改失败，手机验证码输入不正确或与手机号不匹配！',$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
	}
	if(empty($userpwd)) errormsg('请输入你的新密码！');
	if(PASSPORT_TYPE == 'phpwind'){
		require MYMPS_ROOT.'/pw_client/uc_client.php';
		$pw_user = uc_user_get($userid);
		$result = uc_user_edit($pw_user['uid'], $pw_user['username'], '', md5($userpwd), '');
	} elseif(PASSPORT_TYPE == 'ucenter') {
		require MYMPS_ROOT.'/uc_client/client.php';
		$result =  uc_user_edit($userid, $userpwd, $userpwd, $email, 1);
	}
	
	if (!empty($userpwd)){
		$db->query("UPDATE `{$db_mymps}member` SET userpwd='".md5($userpwd)."' WHERE id = '$uid'");
		redirectmsg('密码修改成功！请输入账号密码重新登录',$mayimobile['mobiledomain'].'/index.php?mod=login');
		exit;
	} else {
		redirectmsg('密码修改失败，请联系客服',$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
		exit;
	}
	
	
} elseif(empty($action)){
	
	if(${'amp;code'}) $code = ${'amp;code'};
	$uid = $uid ? intval($uid) : '';
	$code = $code ? mhtmlspecialchars($code) : '';
	if($code){
		$arr = explode('.',base64_decode($code));
		$uid = $arr[0];
		$user_info = $db -> getRow("SELECT id,userid,userpwd,email FROM `{$db_mymps}member` WHERE id = '$uid'");
		
		if(($timestamp - $arr[2] < 1800) && $arr[1] == md5($user_info['id'].'+'.$user_info['userpwd'])){
			$userid = $user_info['userid'];
			$uid = $user_info['id'];
			$email = $user_info['email'];
			
			globalassign();
include mayi_tpl('member_forgetpass_2');
		} else {
			$status = 'error';
			$msg = '该密码重设链接已失效！';
			redirectmsg($msg,$mayimobile['mobiledomain'].'/index.php?mod=forgetpass');
		}
	} else {
		$mymps_imgcode = $authcodesettings['forgetpass'];
		globalassign();
include mayi_tpl('member_forgetpass');
	}
	
}
?>