<?php

/**
 *      【橙创家校通v4.0】 (C)2010-2012 Tuntron Inc.
 *      
 *    	 http://www.tuntron.com   QQ：1121251729
 *
 *      &找回密码模块&
 */
 
if (!defined('IS_RUN')) die('Forbidden');

switch($c){
	
	case "":
		$Modname = '通过邮箱找回密码';
		
		break;
		
	case "byMobile":
	
		$Modname = '通过手机号码找回密码';
		
		$no_temp = 1;
		require_once Template("forgetpwdbyM");
		
		break;
		
	case "send_verify":
		
		$mobile = _post('mobile','');

		if(!$mobile){
			$json['status'] = 0;
			$json['msg'] = "手机号码不能为空！";	
		}else{
			
			$userarr = G_class('user')->read_by_mobile($mobile);
			if(is_array($userarr)){
				$verify_code = rand(100000,999999);
				G_class('user')->update_resetpwd($mobile,$verify_code,'phonenum');
				
				//发送短信
				 $repost['mobile'] = $mobile;
		  		 $repost['content'] = '您请求重置密码，校验码'.$verify_code.'[请勿告知他人]，如非本人操作请致电：'.$_G['siteinfo']['SiteTel'];
				 if(time()-$_SESSION['SendVerifyTime']<60){
						$l_time = 60-(time()-$_SESSION['SendVerifyTime']);
						$json['status'] = 0;
						$json['msg'] = "请过".$l_time."秒后重新获取短信！";	
						outputJson($json);
					}
				 if(G_class('sendsms')->Ajax_send_verify($repost,$userarr['uid'])=='100'){
					session_start();
					$_SESSION['SendVerifyTime']=time();
					 
					$json['status'] = 1;
					$json['msg'] = "手机校验码发送成功，请注意查收！";
				 }else{
					$json['status'] = 0;
					$json['msg'] = "手机校验码发送失败！";
				 }
				//发送短信
			}else{
				$json['status'] = 0;
				$json['msg'] = "您输入的手机号码还未被绑定！";
			}
		}
		
		outputJson($json);
		
		break;
	
	//执行
	case "do":
	
		$mobile	= trim($_POST['mobile']);
		$email	= trim($_POST['email']);
		
		$verifycode= _post('verifycode','','trim'); 
		
		if(strtolower($_SESSION['VerifyCode'])!==strtolower($verifycode)) {
				$_SESSION['VerifyCode']='';
				Note('验证码输入错误！');
			};
		
		if($email){
		$emailNum = G_class('user')->check_email_exists($email);

		if($email==''){
			Note('Email输入不能为空^_^');
		}elseif($emailNum == '0'){
			Note("Email不存在，你可能还没有注册^_^");
		}else{
			
			//随机MD5加密
			$resetpwd = md5(rand());
		
			G_class('user')->update_resetpwd($email,$resetpwd);
			
			$userarr = G_class('user')->read_by_email($email);
			//发送邮件
			$subject = '用户密码找回-'.$_G['siteinfo']['SiteName'];
			
			$content = $userarr['realname'].'，你好！<br /><br /><br />点击以下链接并根据页面提示完成密码重设：<br /><a href="'.SITEURL.'?mod=login&do=resetpwd&mail='.$email.'&code='.$resetpwd.'">'.SITEURL.'?mod=login&do=resetpwd&mail='.$email.'&code='.$resetpwd.'</a><br><span style="font-size:14px;color:#999999">如无法点击，请将链接拷贝到浏览器地址栏中直接访问。</span>';
			
			$result = G_class('mail')->postMail($email,$subject,$content);
			//发送邮件
			if($result == '0'){
				Note("邮件发送失败，请联系管理员！");
			}elseif($result == '1'){
				Note("我们已向".$email." 发送密码重置邮件",'>>点击返回<<',SITEURL);
			}
			
		}
		}elseif($mobile){
			
		$resetpwd = _post('resetpwd','');
		
		$userNum = G_class('user')->find(array(
			'phonenum'=>$mobile,
			'resetpwd'=>$resetpwd,
		));
		if($mobile=='' || $resetpwd==''){
			Note("重置密码所需条件不够！");
		}elseif($userNum[0] == 0){
			Note("校验码错误或失效！");
		}else{
			$user = G_class('user')->read_by_mobile($mobile);
		}
		$Modname = '重设密码';
		$no_temp = 1;
		require_once Template("resetpwd");
		
		}
		break;
	
	
}

if($no_temp<0) require_once Template('forgetpwd');

?>