<?php
require_once("global.php");

if($action=='send')
{
	$rs=$db->get_one("SELECT M.*,D.* FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[username]='$atc_username'");
	if(!$rs){
		showerr("帳號有誤,不存在");
	}elseif(!$rs[email]){
		showerr("當前帳號沒有設置郵箱,請聯系統管理員幫你修改密碼!");
	}
	if(!$webdb[mymd5])
	{
		$webdb[mymd5]=rands(10);
		$db->query("REPLACE INTO {$pre}config (`c_key`,`c_value`) VALUES ('mymd5','$webdb[mymd5]')");
		write_file(PHP168_PATH."php168/config.php","\$webdb['mymd5']='$webdb[mymd5]';",'a');
	}
	$newpwd=strtolower(rands(8));
	$md5_id=mymd5("{$rs[$TB[username]]}\t{$rs[$TB[password]]}\t$newpwd");
	$Title="來自“{$webdb[webname]}”的郵件,取回密碼!!";
	$Content="你在“{$webdb[webname]}”的帳號是“{$rs[$TB[username]]}”,你的新密碼是︰“{$newpwd}”,請點擊此以下網址,激活新密碼,點擊激活後,才可以生效。<br><br><A HREF='$webdb[www_url]/do/sendpwd.php?job=getpwd&md5_id=$md5_id' target='_blank'>$webdb[www_url]/do/sendpwd.php?job=getpwd&md5_id=$md5_id</A>";

	if($webdb[MailType]=='smtp')
	{
		if(!$webdb[MailServer]||!$webdb[MailPort]||!$webdb[MailId]||!$webdb[MailPw])
		{
			showmsg("請管理員先設置郵件服務器");
		}
		require_once(PHP168_PATH."inc/class.mail.php");
		$smtp = new smtp($webdb[MailServer],$webdb[MailPort],true,$webdb[MailId],$webdb[MailPw]);
		$smtp->debug = false;

		if($smtp->sendmail($rs[email],$webdb[MailId], $Title, $Content, "HTML"))
		{
			$succeeNUM++;
		}
	}
	else
	{
		if(mail($rs[email], $Title, $Content))
		{
			$succeeNUM++;
		}
	}
	if($succeeNUM)
	{
		refreshto("./","新密碼已經succeed發送到你的郵箱:“{$rs[email]}”，請注意查收!",5);
	}
	else
	{
		showerr("郵件發送失敗，可能你的郵箱有誤,或者是服務器發送郵件*-*能有問題！！");
	}
}
elseif($job=='getpwd')
{
	list($username,$password,$newpassword)=explode("\t",mymd5($md5_id,'DE'));
	if($db->get_one("SELECT M.*,D.* FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[username]='$username' AND M.$TB[password]='$password'"))
	{
		$newpassword=pwd_md5($newpassword);
		$db->query("UPDATE $TB[table] SET $TB[password]='$newpassword' WHERE $TB[username]='$username'");
		refreshto("login.php","恭喜你，新密碼激活succeed，請盡快登錄修改密碼!",10);
	}
	else
	{
		showerr("新密碼激活失敗!");
	}
}

require(PHP168_PATH."inc/head.php");
require(html("sendpwd"));
require(PHP168_PATH."inc/foot.php");
?>