<?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[yz]){
		showerr("當前帳號已經激活了,你不能重復激活!");
	}elseif(!$atc_email){
		showerr("請輸入郵箱!");
	}
	if (!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$",$atc_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');
	}
	$md5_id=mymd5("{$rs[$TB[username]]}\t{$rs[$TB[password]]}");
	$Title="來自“{$webdb[webname]}”的郵件,請激活帳號!!";
	$Content="你在“{$webdb[webname]}”的帳號是“{$rs[$TB[username]]}”還沒激活,請點擊此以下網址,激活你的帳號。<br><br><A HREF='$webdb[www_url]/do/activate.php?job=activate&md5_id=$md5_id' target='_blank'>$webdb[www_url]/do/activate.php?job=activate&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($atc_email,$webdb[MailId], $Title, $Content, "HTML"))
		{
			$succeeNUM++;
		}
	}
	else
	{
		if(mail($atc_email, $Title, $Content))
		{
			$succeeNUM++;
		}
	}
	if($succeeNUM)
	{
		refreshto("./","系統已經succeed發送郵件到你的郵箱:“{$atc_email}”，請注意查收!",5);
	}
	else
	{
		showerr("郵件發送失敗，可能你的郵箱有誤,或者是服務器發送郵件*-*能有問題！！");
	}
}
elseif($job=='activate')
{
	list($username,$password)=explode("\t",mymd5($md5_id,'DE'));
	if($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]='$username' AND M.$TB[password]='$password'"))
	{
		$db->query("UPDATE {$pre}memberdata SET `yz`='1' WHERE uid='$rs[uid]'");
		refreshto("login.php","恭喜你，你的帳號“{$username}”激活succeed，請立即登錄，體驗會員特有的*-*能!",10);
	}
	else
	{
		showerr("帳號激活失敗!");
	}
}

if($username){
	extract( $db->get_one("SELECT D.email FROM $TB[table] M LEFT JOIN {$pre}memberdata D ON M.$TB[uid]=D.uid WHERE M.$TB[username]='$username'") );
}
require(PHP168_PATH."inc/head.php");
require(html("activate"));
require(PHP168_PATH."inc/foot.php");
?>