<?php
class forgetpwdAction extends security_normal {
	
	function action(){
		set_time_limit(300);
		$miceSession = new session();
		$sendresult='';
		global $conn;
		if($_POST){
			if($_POST['checkcode']!=$miceSession->get('checkcode')){
				$jsstring='Dialog.alert("验证码输入错误",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}

			if($_POST['loginname']==''){
				$jsstring='Dialog.alert("请填写登录名",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}

			if($_POST['email']==''){
				$jsstring='Dialog.alert("注册邮箱不能为空",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}

			if($_POST['loginname'] AND $_POST['email']){
				$user=new model_User();
				$user->setSubsection("WHERE loginname='".$_POST['loginname']."' AND email='".strtolower($_POST['email'])."'");
				$result=$user->doSelectOne();
			}

			$fstr='';
			if($result){
				$fstr=RandomString(16);
				$user->setForgetpwd($fstr);
				$user->doUpdate();
			}else{
				$jsstring='Dialog.alert("用户名或验证邮箱错误",function(){history.back(-1);});';
				MessageBox($jsstring);
				exit;
			}
			if(ConfigV('micefun','mail_send')==1 AND $fstr){
				$setpwdlink="<a href='".MICE_URL.'index.php/setpwd/'.$_POST['loginname'].'/'.$fstr."' target='_blank'>".MICE_URL.'index.php/setpwd/'.$_POST['loginname'].'/'.$fstr."</a>";			
				$mailtitle=$_POST['loginname'].','.ConfigV('micefun','forgetpwd_mail_title');
				$mailbodystr=ConfigA('micefun','forgetpwd_mail_body');
				$mailbodystr=str_replace('username', $_POST['loginname'], $mailbodystr);
				$mailbodystr=str_replace('setpwdlink', $setpwdlink, $mailbodystr);

				$mailbody="<HTML><HEAD><META http-equiv=Content-Type content='text/html; charset=utf-8'><style type='text/css'>";
				$mailbody=$mailbody."<!--";
				$mailbody=$mailbody."body,td,th {";
				$mailbody=$mailbody." font-size: 16px;";
				$mailbody=$mailbody." color: #000000;";
				$mailbody=$mailbody."}.red{ color:#FF0000;}";
				$mailbody=$mailbody."-->";
				$mailbody=$mailbody."</style></HEAD><BODY>";
				$mailbody=$mailbodystr;
				$mailbody=$mailbody."</BODY></HTML>";

				$mailarray = array(
					'mailto' => $_POST['email'],
					'title' => $mailtitle,
					'body' => $mailbody,
				);

				$sendresult='';
				//调用插件
				$plugin=new model_Plugin();
				$plugin->setSubsection('WHERE isopen=1 AND hook=\'mail\' ORDER BY orderid ASC,plugin_id DESC');
				$plugins=$plugin->doSelectOne();
				include_once MICE_DIR.'includes/plugin.php';
				if($plugins){
					if(file_exists(MICE_DIR.'plugins/'.$plugins->getName().'/index.php')){
						include_once MICE_DIR.'plugins/'.$plugins->getName().'/index.php';
						add_action('mail', $plugins->getName());
						$sendresult=do_action('mail',$mailarray);
						remove_action('mail', $plugins->getName());
					}
				}
			}else{
				$sendresult='本站暂不支持找回密码功能，请直接联系本站管理员';
			}
			$jsstring='Dialog.alert(\''.$sendresult.'\',function(){window.location.href="'.MICE_URL.'";window.event.returnValue = false;},300,100,"找回密码");';
			MessageBox($jsstring);
			exit;
		}

		$smarty = new Smarty();
		$smarty->load_filter('pre','micePreCompileIndex');
		$smarty->assign("name",$name);
		$smarty->assign("checkcode",MICE_URL.'index.php/checkcode');

		$filename = new getTPLFileName(__CLASS__);
		$tpl=$filename->TPLFileName;
		$smarty->display($tpl);
	}	
}
?>