<?php
!function_exists('html') && exit('ERR');

if($job=="send"&&$Apower[message_send])
{
	$group_send=group_box("Group",'');
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/message/menu.htm");
	require(dirname(__FILE__)."/"."template/message/send.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($job=="test"&&$Apower[message_send])
{
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/message/menu.htm");
	require(dirname(__FILE__)."/"."template/message/test.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($action=="send"&&$Apower[message_send])
{
	if($page<1)
	{
		$page=1;
		if(!$Group){
			showmsg("你必須選擇一個用戶組");
		}
		$Group=implode(",",$Group);
		if($Num<1){
			$Num=1;
		}
		if(!$Title){
			showmsg("標題不能為空");
		}
		if(!$Content){
			showmsg("內容不能為空");
		}
		$show="<?php
\$Group='$Group';
\$Num='$Num';
\$Title='$Title';
\$Content='$Content';
		";
		write_file(PHP168_PATH."cache/message_cache.php",$show);
	}
	else
	{
		include_once(PHP168_PATH."cache/message_cache.php");
	}
	$Title=addslashes($Title);
	$Content=addslashes($Content);

	$rows=$Num;
	$min=($page-1)*$rows;
	$query = $db->query("SELECT D.uid,M.$TB[username] AS username FROM {$pre}memberdata D LEFT JOIN $TB[table] M ON D.uid=M.$TB[uid] WHERE D.groupid IN ($Group) LIMIT $min,$rows");
	while($rs = $db->fetch_array($query)){
		$array[touid]=$rs[uid];
		$array[fromuid]=0;
		$array[fromer]="系統消息";
		$array[title]=addslashes($Title);
		$array[content]=addslashes($Content);
		pm_msgbox($array);
		$ck++;
	}
	$page++;
	
	if($ck++)
	{
		echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=?lfj=$lfj&action=$action&page=$page&succeeNUM=$succeeNUM&failNUM=$failNUM'>";
		exit;
	}
	else
	{
		unlink(PHP168_PATH."cache/message_cache.php");
		$succeeNUM=intval($succeeNUM);
		$failNUM=intval($failNUM);
		jump("站內消息發送完畢","index.php?lfj=$lfj&job=send",30);	
	}
}
elseif($action=="test"&&$Apower[message_send])
{
	if($page<1)
	{
		$page=1;
		if(!$Usrdb){
			showmsg("會員帳號不能為空");
		}
		$Group=implode(",",$Group);
		if($Num<1){
			$Num=100;
		}
		if(!$Title){
			showmsg("標題不能為空");
		}
		if(!$Content){
			showmsg("內容不能為空");
		}
		$show="<?php
\$Num='$Num';
\$Usrdb='$Usrdb';
\$Title='$Title';
\$Content='$Content';
		";
		write_file(PHP168_PATH."cache/message_cache.php",$show);
	}
	else
	{
		include_once(PHP168_PATH."cache/message_cache.php");
	}
	$Title=addslashes($Title);
	$Content=addslashes($Content);
	$rows=$Num;
	$min=($page-1)*$rows;
	$detail=explode("\r\n",$Usrdb);
	for($i=$min;$i<($min+$rows);$i++)
	{
		if(!$username=$detail[$i])
		{
			continue;
		}
		$rs = $db->get_one("SELECT D.uid,M.$TB[username] AS username FROM {$pre}memberdata D LEFT JOIN $TB[table] M ON D.uid=M.$TB[uid] WHERE M.$TB[username]='$username' LIMIT $min,$rows");
		if(!$rs[uid])
		{
			continue;
		}
		$array[touid]=$rs[uid];
		$array[fromuid]=0;
		$array[fromer]="系統消息";
		$array[title]=addslashes($Title);
		$array[content]=addslashes($Content);
		pm_msgbox($array);
		$ck++;
	}
	$page++;
	
	if($ck++)
	{
		echo "<META HTTP-EQUIV=REFRESH CONTENT='0;URL=?lfj=$lfj&action=$action&page=$page&succeeNUM=$succeeNUM&failNUM=$failNUM'>";
		exit;
	}
	else
	{
		unlink(PHP168_PATH."cache/message_cache.php");
		$succeeNUM=intval($succeeNUM);
		$failNUM=intval($failNUM);
		jump("站內消息發送完畢","index.php?lfj=$lfj&job=$action",30);	
	}
}
?>