<?php
require(dirname(__FILE__)."/"."global.php");

if(!$lfjid){
	showerr("你還沒登錄");
}

if($webdb[passport_type]=='dzbbs7'){
	uc_pm_location($lfjuid);
}
if( ereg("^pwbbs",$webdb[passport_type]) )
{
	if(is_table("{$TB_pre}msgc")){
		$newpw=1;
	}else{
		$newpw=0;
	}
}

$linkdb=array(
			"收件箱"=>"?job=list",
			"寫新消息"=>"?job=send",
			"清空收件箱"=>"?job=clean"
			);
if($job=='list')
{	
	if(!$page){
		$page=1;
	}
	$rows=20;
	$min=($page-1)*$rows;
	if( ereg("^pwbbs",$webdb[passport_type]) )
	{
		if($newpw){
			$SQL="SELECT A.*,B.* FROM {$TB_pre}msg A LEFT JOIN {$TB_pre}msgc B ON A.mid=B.mid WHERE A.`touid`='$lfjuid' AND A.type='rebox' ORDER BY A.mid DESC LIMIT $min,$rows";
		}else{
			$SQL="SELECT * FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' ORDER BY mid DESC LIMIT $min,$rows";
		}		
		$showpage=getpage("{$TB_pre}msg","WHERE `touid`='$lfjuid' AND type='rebox'","?job=$job",$rows);
	}
	elseif( ereg("^dzbbs",$webdb[passport_type]) )
	{
		$SQL="SELECT pmid AS mid,msgfrom AS username,msgfromid AS fromuid,msgtoid AS touid,new AS ifnew,subject AS title,dateline AS mdate,message AS content FROM {$TB_pre}pms WHERE `msgtoid`='$lfjuid' AND folder='inbox' ORDER BY pmid DESC LIMIT $min,$rows";
		$showpage=getpage("{$TB_pre}pms","WHERE `msgtoid`='$lfjuid' AND folder='inbox'","?job=$job",$rows);
	}
	else
	{
		$SQL="SELECT * FROM `{$pre}pm` WHERE `touid`='$lfjuid' AND type='rebox' ORDER BY mid DESC LIMIT $min,$rows";
		$showpage=getpage("`{$pre}pm`","WHERE `touid`='$lfjuid' AND type='rebox'","?job=$job",$rows);
	}
	
	$query = $db->query($SQL);
	while($rs = $db->fetch_array($query))
	{
		if($rs[ifnew]){
			$rs[state]="<A style='color:red;'>未讀</A>";
		}else{
			$rs[state]="已讀";
		}
		$rs[mdate]=date("Y-m-d H:i",$rs[mdate]);
		$listdb[]=$rs;
	}
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/pm/list.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($job=='read')
{
	if( ereg("^pwbbs",$webdb[passport_type]) )
	{
		if($newpw){
			$SQL="SELECT A.*,B.* FROM {$TB_pre}msg A LEFT JOIN {$TB_pre}msgc B ON A.mid=B.mid WHERE A.`touid`='$lfjuid' AND A.type='rebox' AND A.mid='$mid'";
		}else{
			$SQL="SELECT * FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'";
		}
		
		$db->query("UPDATE {$TB_pre}msg SET `ifnew`=0 WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'");
		$rs=$db->get_one("SELECT * FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' AND ifnew=1");
		if(!$rs){
			$db->query("UPDATE $TB[table] SET newpm=0 WHERE uid='$lfjuid'");
		}
	}
	elseif( ereg("^dzbbs",$webdb[passport_type]) )
	{
		$SQL="SELECT pmid AS mid,msgfrom AS username,msgfromid AS fromuid,msgtoid AS touid,new AS ifnew,subject AS title,dateline AS mdate,message AS content FROM {$TB_pre}pms WHERE `msgtoid`='$lfjuid' AND folder='inbox' AND pmid='$mid'";
		$db->query("UPDATE {$TB_pre}pms SET `new`=0 WHERE `msgtoid`='$lfjuid' AND folder='inbox' AND pmid='$mid'");
		$rs=$db->get_one("SELECT * FROM {$TB_pre}pms WHERE `msgtoid`='$lfjuid' AND folder='inbox' AND new=1");
		if(!$rs){
			$db->query("UPDATE $TB[table] SET newpm=0 WHERE uid='$lfjuid'");
		}
	}
	else
	{
		$SQL="SELECT * FROM `{$pre}pm` WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'";
		$db->query("UPDATE `{$pre}pm` SET `ifnew`=0 WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'");
	}
	$rsdb=$db->get_one($SQL);
	$rsdb[mdate]=date("Y-m-d H:i",$rsdb[mdate]);

	require_once(PHP168_PATH."inc/encode.php");
	$rsdb[content]=format_text($rsdb[content]);

	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/pm/read.htm");
	require(dirname(__FILE__)."/"."foot.php");
}
elseif($job=='del')
{
	if( ereg("^pwbbs",$webdb[passport_type]) )
	{
		if($newpw){
			$SQL="DELETE FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'";
			if($db->get_one("SELECT * FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'")){
				$db->query("DELETE FROM {$TB_pre}msgc WHERE mid='$mid'");
			}
		}else{
			$SQL="DELETE FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'";
		}
		
	}
	elseif( ereg("^dzbbs",$webdb[passport_type]) )
	{
		$SQL="DELETE FROM {$TB_pre}pms WHERE `msgtoid`='$lfjuid' AND folder='inbox' AND pmid='$mid'";
	}
	else
	{
		$SQL="DELETE FROM `{$pre}pm` WHERE `touid`='$lfjuid' AND type='rebox' AND mid='$mid'";
	}

	$db->query($SQL);
	refreshto("pm.php?job=list","刪除succeed",1);
}
elseif($job=='clean')
{
	if( ereg("^pwbbs",$webdb[passport_type]) )
	{
		$SQL="DELETE FROM {$TB_pre}msg WHERE `touid`='$lfjuid' AND type='rebox' ";
		$db->query("UPDATE $TB[table] SET newpm=0 WHERE uid='$lfjuid'");
	}
	elseif( ereg("^dzbbs",$webdb[passport_type]) )
	{
		$SQL="DELETE FROM {$TB_pre}pms WHERE `msgtoid`='$lfjuid' AND folder='inbox' ";
		$db->query("UPDATE $TB[table] SET newpm=0 WHERE uid='$lfjuid'");
	}
	else
	{
		$SQL="DELETE FROM `{$pre}pm` WHERE `touid`='$lfjuid' AND type='rebox' ";
	}
	if(!$step){
		require("head.php");
		echo "<CENTER><A HREF='?job=$job&step=2'>你確實要清空收件箱嗎?不可恢復</A></CENTER>";
		exit;
	}
	$db->query($SQL);
	refreshto("pm.php?job=list","收件箱已清空",1);
}
elseif($job=='send')
{
	if($step==2)
	{
		$rsdb=$db->get_one("SELECT $TB[uid] AS uid FROM $TB[table] WHERE $TB[username]='$touser' ");
		if(!$rsdb)
		{
			showerr("當前用戶不存在");
		}
		if(!$title){
			showerr("標題不能為空");
		}
		if(strlen($array[title])>100){
			showerr("標題太長了!");
		}
		$array[touid]=$rsdb[uid];
		$array[fromuid]=$lfjuid;
		$array[fromer]=$lfjid;
		$array[title]=filtrate($title);
		$array[content]=filtrate($content);
		pm_msgbox($array);
		refreshto("?job=list","發送succeed",1);
	}
	if($uid){
		@extract($db->get_one("SELECT $TB[username] AS username  FROM $TB[table] WHERE $TB[uid]='$uid'"));
	}
	require(dirname(__FILE__)."/"."head.php");
	require(dirname(__FILE__)."/"."template/pm/send.htm");
	require(dirname(__FILE__)."/"."foot.php");
}

?>