<?php //发送重置密码连接
//fetchpassword.php,getadminpassword.php,resetpass.php
require_once('_approot.php');	

$adminemail=inputsafe(strtolower(trim($_POST['adminemail'])));
$ls=trim($_POST['ls']);
$admin=inputsafe($_POST['admin']);
$code=$_POST['code'];   //验证码
$maxFecthNum=8; //一天内最多取回8次


function send_mail($to, $subject, $message) { 
 
	$headers = 'MIME-Version: 1.0' . "\r\n"; 
	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; 
	if(mail($to, $subject, $message, $headers)){
		return true;
	}
	
} 


echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";

if($ls=="forget")
{
		$opendb=new mydb();
			$sqlstr="select * from ls_config where  lower(feedbackEmail)='".$adminemail."'";
		$query1=$opendb->query($sqlstr);
		$rowsconfig=mysqli_fetch_assoc($query1);
		$myrows=mysqli_num_rows($query1); 
		
		$sqlstradmin="select * from ls_admin where adminName='".$admin."'";
		$queryadmin=$opendb->query($sqlstradmin);
		$rowsadmin=mysqli_fetch_assoc($queryadmin);
		$adminnum=mysqli_num_rows($queryadmin);
		//echo $adminemail."--".$rowsconfig['feedbackEmail'];
		
		if(strtolower($code) != strtolower($_SESSION["code"]))
		{
			echo "
					<script>
						alert('验证码不正确，请重新输入');
						location='fetchpassword.php';	
					</script>
					";
		}
		else
		{
		
			  if($adminnum==0)
			  {
				  echo "
						  <script>
							  alert(' 对不起，用户名错误，无法找回密码！');
							  location='fetchpassword.php';	
						  </script>
						  ";
			  }
	  
	  
			  if($myrows==0)
			  {
						  echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
						  echo "
						  <script>
							  alert(' 对不起，Email地址错误！');
							  location='fetchpassword.php';	
						  </script>
						  ";
			  }
			  else
			  {
				  $username=$rowsadmin['adminName'];
				  $website= "http://".$_SERVER ['HTTP_HOST'];
				  $companyname=$rowsconfig['companyName'];
				  $manager=$rowsconfig['manager'];
			  
				  // 3天前的 取回信息被清除
				  $select_sql2="delete from ls_fetchpass  where date(datetime)<'".date('Y-m-d',(time() - 3*86400))."' "; 
				  $select_query2=$opendb->query($select_sql2);
			  
			  
				  $select_sql2="select * from ls_fetchpass  where date(datetime)='".date('Y-m-d')."' ";
				  $select_query2=$opendb->query($select_sql2);
				  $select_rows2=mysqli_num_rows($select_query2); //总数
				  if($select_rows2>$maxFecthNum)
				  { $ls_msg="今天取回密码超过".$maxFecthNum."次，明天再试";}
				  else
				  {
					  $urltag = genRandomString(32);
					  $insert_sql="insert into ls_fetchpass(adminName,urlTagStr,datetime) values ('".$username."','".$urltag."','".date('Y-m-d H:m:s')."') "; 
					  //echo $insert_sql;
					  $insert_query=$opendb->query($insert_sql);
					  $subject="网站".$website."后台密码取回";
					  $content=$username." :\r\n您好，<br/><br/>请点击下面链接重置网站密码：\r\n <br/><a href='".$website."/ls_admin/resetpass.php?urltag=".$urltag."'>".$website."/ls_admin/resetpass.php?urltag=".$urltag."</a><br/><br/>\r\n 重置后请保存好您的密码。\r\n <br/><br/>蓝信网络<br/>".date('Y-m-d');
					  
					  $Receiptor=$adminemail;
					  $Receiptor2=$rowsconfig['feedbackEmail'];
					  $Name=$userrealname;
					  $Subject=$subject;
					  $Content=$content;
					  //echo $Subject."<br/>".$Content;
					  if(send_mail($Receiptor,$Subject,$Content) && send_mail($Receiptor2,$Subject,$Content))
					  {
						  echo "
						  <script>
							  alert('密码已经发送到您的网站邮箱:".$Receiptor."和反馈邮箱:".$Receiptor2."，请注意查收，谢谢使用蓝信外贸网站管理系统');
							  location='index.php';	
						  </script>
						  ";
				  
					  }
				  }//if($select_rows2>$maxFecthNum)
			  }//if($myrows==0)
		} //if(strtolower($code) != strtolower($_SESSION["code"]))
 
} 

 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>找回密码</title>
</head>
<body>
<?=$ls_msg?>
</body>
</html>
