<?php
require './include/common.inc.php';
sajax_init();
sajax_export("checkuser","checkemail","resetpass");
sajax_handle_client_request();
//$GLOBALS['sajax_debug_mode'] = 1;

$frame = isset($frame) ? trim($frame) : "checkinfo";

if($_userid) showmessage($LANG['you_have_logined']);

if($HS['enableserverpassport'])
{
	$getpasswordurl = $HS['passport_serverurl'].$HS['passport_getpasswordurl'];
	$addstr = $PHP_QUERYSTRING ? $PHP_QUERYSTRING : 'forward='.$PHP_REFERER;
	$getpasswordurl .= (strpos($getpasswordurl, '?') ? '&' : '?').$addstr;
	header('location:'.$getpasswordurl);
	exit;
}

$seo_title = $LANG['get_password_back'];


if($frame == "checkinfo")
{
   include template($mod, 'getpassword');
}
elseif($frame == "sending")
{
  $sends = trim($sendc);
	$sends = base64_decode($sends);
	list($sendchk,$username,$email) = explode('|', $sends);

  $chknow = $PHP_TIME - $sendchk;
  if ($chknow>120) showmessage("请求超时，请重试！");
	if(is_badword($username)) showmessage("非法请求！");

  $r = $db->get_one("SELECT email FROM ".TABLE_MEMBER." WHERE username='$username' LIMIT 0,1");
  if ($email != $r[email]) showmessage("非法请求！");
  
	$authstr = $member->make_authstr($username);
	$authurl = linkurl($MOD['linkurl'], 1)."getpassword.php?frame=reseting&username=".urlencode($username)."&authstr=$authstr";
	$title = $HS['sitename'].$LANG['get_password_back'];
	$content = tpl_data($mod, 'getpassword_mailcheck');

	require HS_ROOT.'/include/mail.inc.php';
  sendmail($email, $title, stripslashes($content));
  include template($mod, 'getpassword');
}
elseif($frame == "reseting")
{
	if(is_badword($username)) showmessage($LANG['username_not_accord_with_critizen']);
	$authstr = trim($authstr);
    if(strlen($authstr) != 32) showmessage($LANG['verify_string_not_correct']);

	if(!$member->check_authstr($username, $authstr)) showmessage($LANG['verify_string_not_correct']);

    include template($mod, 'getpassword');
}
elseif($action == 'chk_email')
{
if(!is_email($str))
{
    echo 1;
 }
 else
 {
    echo 0;
 }
}

function resetpass($username,$authstr,$password,$repassword)
{
	global $db,$member;
	if(!$username) return "errusr";
	if(!$password) return "nopass";
	$authstr = trim($authstr);
	if(!$authstr || strlen($authstr) != 32) return "errchk";
	if($password != $repassword) return "difpass";
	if(strlen($password)<5 || strlen($password)>20) return "lenpass";
	$password = hsmd5($password);
	
	$r = $db->query("UPDATE ".TABLE_MEMBER." SET password='$password' WHERE username='$username' AND authstr='$authstr' ");
	$member->make_authstr($username);
	if($db->affected_rows())
	{
	  return "true";
	}else{
	  return "unknow";
	}
}

function checkuser($name,$chkcode)
{
	global $db,$session;
	if($name == '') return "nouser";
	if($chkcode == '') return "nocode";
	if(!is_object($session)) $session = new hs_session();
	if(strtolower($_SESSION['checkcode']) != strtolower($chkcode))
	{
	  unset($_SESSION['checkcode']);
	  return "errcode";
	}
	
	$r = $db->get_one("SELECT userid,email FROM ".TABLE_MEMBER." WHERE username='$name' LIMIT 0,1");
	if(!$r)
	{
	  return "erruser";
	}else{
	  list($email1,$email2) = explode('@', "$r[email]");
    $myemail = "******@".$email2;
    
    return '<DIV class=tipsbox>请输入注册时填写的邮箱地址：</DIV><div class="tipsboxC" id="rmsg" style="margin:20px 200px;display:none"><span class="font06"></span></div><TABLE class=regform cellSpacing=0 cellPadding=0 width="80%" align=center border=0><TBODY><TR><TH>邮箱： </TH><TD><INPUT class=inputA id=aemail> <SPAN class=font04>您的原邮箱地址为:'.$myemail.'</SPAN></TD></TR><TR><TH></TH><TD><input id="iduser" type="hidden" value="'.$r[userid].'" /><input type="button" class="hsbtn" value="确认" onclick="checkemail()"></TD></TR></TBODY></TABLE></DIV>';
	}
}

function checkemail($iduser,$pemail)
{
	global $db,$MOD,$PHP_TIME;
	if($iduser == '') return "unaccess";
	if($pemail == '') return "noemail";
	
	$re = $db->get_one("SELECT username FROM ".TABLE_MEMBER." WHERE userid='$iduser' AND email='$pemail'");
	if(!$re)
	{
	  return "erremail";
	}else{
	  $chkt = $PHP_TIME;
	  $sendc = $chkt.'|'.$re['username'].'|'.$pemail;
	  $sendc = base64_encode($sendc);
    return linkurl($MOD['linkurl'], 1)."getpassword.php?frame=sending&sendc=".$sendc;
	}
}
?>