<?

require("datafile/config.php");
require("global.php");
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
p,td,input,select{ font-size: 9pt; color:#333333;}
a {color:#333333;font-size: 9pt;text-decoration:none; }
a:hover {color:#000000;text-decoration:underline overline;}
a:active {color:#FF6699;}
-->
</style>
</head>
<body bgcolor=FFFFFF leftmargin=0 topmargin=0 marginwidth=0>
<p>
<?
if (!$login_status) {echo "对不起，您还未登录"; exit;}
$user=$HTTP_SESSION_VARS['vibid'];
if ($job=="read") {
	if (file_exists($id_unique."/".$user.".rec")) {
		$receivelist=file($id_unique."/".$user.".rec");
		list($author,$title,$time,$content,$isread)=explode("|",$receivelist[$msg]);
		$ctime=getfulldate($time);
		echo "<br><font color=333366>作者：</font>$author<br><font color=333366>题目：</font>$title<br>$ctime<br>$content";
		echo "<br><br><font color=333366>选项： [<a href='messanger.php?job=write&target=$author'>回复</a>] [<a href='messanger.php?job=receivebox'>返回</a>]</font>";
		$count=count($receivelist);
		$fp=fopen($id_unique."/".$user.".rec","w");
		for ($i=0; $i<$count; $i++) {
			if ($i!=$msg) fputs($fp,$receivelist[$i]);
			else fputs($fp,"$author|$title|$time|$content|1|\n");
		}
		fclose($fp);
	}
}
if ($job=="readsnd") {
	if (file_exists($id_unique."/".$user.".snd")) {
		$sendlist=file($id_unique."/".$user.".snd");
		list($ruser,$title,$time,$content)=explode("|",$sendlist[$msg]);
		$ctime=getfulldate($time);
		echo "<br><font color=333366>接收用户：</font>$ruser<br><font color=333366>题目：</font>$title<br>$ctime<br>$content";
		echo "<br><br><font color=333366>选项： [<a href='messanger.php?job=write&target=$ruser'>另发信息</a>] [<a href='messanger.php?job=sendbox'>返回</a>]</font>";
		$count=count($sendlist);
	}
}
if ($job=="receivebox") {
	if (file_exists($id_unique."/".$user.".rec")) {
		$receivelist=file($id_unique."/".$user.".rec");
		$count=count($receivelist);
		if ($count<=1 && empty($receivelist[0])) {echo"<br>空的收件箱，您没有任何短消息";exit;}
		echo '<table bgcolor=F3F3F3 width=205 border=0 cellspacing=0 cellpadding=2 align=center><tr align=center><td width="67"><b><font color=444444>作者</font></b></td><td width="110"><b><font color=444444>题目</font></b></td><td width="26"><font color=333366>已读</font></td></tr></table>';
		sline();
		for ($i=0; $i<$count; $i++) {
			list($author,$title,$time,$content,$isread)=explode("|",$receivelist[$i]);
			if ($isread) $isread="是"; else $isread="否";
			echo "<table width=205 border=0 cellspacing=0 cellpadding=2 align=center><tr align=center><td width=67><acronym title='给用户发一个短信息'><a href='messanger.php?job=write&target=$author'>$author</a></acronym></td><td width=110><a href='messanger.php?job=read&msg=$i'>$title</a></td><td width=26>$isread</td></tr></table>";
			sline();
		}
	}
	else {echo "<br>空的收件箱，您没有任何短消息"; exit; }
}
if ($job=="write") {
	if (empty($step)) print_form();
	elseif($step==2) {
		if (empty($content) || empty($title) || empty($ruser) || !file_exists("$id_unique/$ruser")) {
			echo "对不起，发生资料错误,请 <a href='javascript:history.back(1);'>返回检查</a>";
			exit;
		}
		if (file_exists("$id_unique/$ruser.rec")) $msg=file("$id_unique/$ruser.rec");
		else $msg[0]="";

		$content=stripslashes(safe_convert($content));
		$title=stripslashes(safe_convert($title));
		$new="$user|$title|$timestamp|$content|0|\n";

		$oldcount=count($msg);
		if ($oldcount>$short_msg_max) {
			for ($i=$short_msg_max; $i<$oldcount; $i++) unset($msg);
		}
		
		$old=implode("",$msg);
		writetofile("$id_unique/$ruser.rec",$new.$old);

		if (file_exists("$id_unique/$user.snd")) $old=readfromfile("$id_unique/$user.snd");
		else $old="";

		$new="$ruser|$title|$timestamp|$content|\n";
		writetofile("$id_unique/$user.snd",$new.$old);

		echo "<br>已经完成，<a href='messanger.php?job=receivebox'>点这里返回收件箱</a>";
		exit;
	}
}
if ($job=="clear") {
	if (file_exists("$id_unique/$user.rec")) unlink("$id_unique/$user.rec");
	if (file_exists("$id_unique/$user.snd")) unlink("$id_unique/$user.snd");
	echo "<br>您的所有短信息已被成功清空";
	exit;
}
if ($job=="sendbox") {
	if (file_exists($id_unique."/".$user.".rec")) {
		$receivelist=file($id_unique."/".$user.".snd");
		$count=count($receivelist);
		if ($count<=1 && empty($receivelist[0])) {echo"<br>空的收件箱，您没有任何短消息";exit;}
		echo '<table bgcolor=F3F3F3 width=205 border=0 cellspacing=0 cellpadding=2 align=center><tr align=center><td width="60"><b>接收用户</b></td><td width="145"><b>题目</b></td></tr></table>';
		sline();
		for ($i=0; $i<$count; $i++) {
			list($ruser,$title,$time,$content)=explode("|",$receivelist[$i]);
			echo "<table width=205 border=0 cellspacing=0 cellpadding=2 align=center><tr align=center><td width=60><acronym title='给用户发一个短信息'><a href='messanger.php?job=write&target=$ruser'>$ruser</a></acronym></td><td width=145><a href='messanger.php?job=readsnd&msg=$i'>$title</a></td></tr></table>";
			sline();
		}
	}
	else {echo "<br>空的发件箱，您没有任何短消息"; exit; }
}

function sline() {
print ("<table cellspacing=0 cellpadding=0 width=205 align=center bgcolor=#777777 border=0><tr><td height=1></td></tr></table>");
}
function print_form() {
global $target;
?>
<table width=205 border=0 cellspacing=0 cellpadding=0>
  <tr bgcolor=F3F3F3>
    <td>&nbsp;请填写信息</td>
  </tr><form action=messanger.php method=POST>
  <tr bgcolor=#FFFFFF>
    <td>
    <br>
      接受用户：
      <input <? if (isset($target)) echo "value='$target'"; ?> style="background-color:#ffffff; color:#9999FF; border: 1 double #B4B4B4" onMouseOver = "this.style.backgroundColor = '#E5F0FF'" onMouseOut ="this.style.backgroundColor =''" type=text size=10 name="ruser"><br>
      &nbsp; &nbsp;&nbsp;题目：
      <input style="background-color:#ffffff; color:#9999FF; border: 1 double #B4B4B4" onMouseOver = "this.style.backgroundColor = '#E5F0FF'" onMouseOut ="this.style.backgroundColor =''" type=text size=10 name="title">
      <br>
      信息内容：<br>
      <textarea style="background-color:#ffffff; border: 1 double #B4B4B4" onMouseOver = "this.style.backgroundColor = '#f3f3f3'" onMouseOut = "this.style.backgroundColor = ''" name="content" cols=25 rows=3></textarea>
      <br>
    </td>
  </tr>
<tr bgcolor=FFFFFF>
<td><input style="color: #000000; background-color: #f3f3f3; border-style: solid; border-width: 1" onMouseOver ="this.style.backgroundColor='#FFC864'" onMouseOut ="this.style.backgroundColor='#f3f3f3'" type=submit value="提 交">
<input type=hidden name=job value=write>
<input type=hidden name=step value=2>
</td>
  </tr></form>
</table>
<?}