<?
require"ygl.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(!isset($myclass)){echo"对不起，没有该班级！！";exit;}
if(!isset($ses_user_id)||!isset($ses_user_psd)){echo"对不起，你没有登陆！！";exit;}
$user_login_id=$ses_user_id;
$user_login_psd=$ses_user_psd;
if(!haveclass($myclass)){echo"对不起，没有该班级！！";exit;}
if(!ynjoinclass($myclass,$user_login_id)){echo"对不起，你没有加入该班！！";exit;}
readclass($myclass);
read_user_zl($user_login_id);
gotNewMessage($user_id);
################
function sline() {
print ("<table cellspacing=0 cellpadding=0 width=205 align=center bgcolor=#777777 border=0><tr><td height=1></td></tr></table>");
}
##########
if ($menu=="write") {
   if (empty($step)) print_form();
   elseif($step==2)
   {
   if (empty($content)||empty($title)||empty($ruser))//||!file_exists("class/$myclass/msg/$ruser"))
       {
       echo "对不起，发生资料错误,请 <a href='javascript:history.back(1);'>返回检查</a>";
       exit;
       }
   if (file_exists("class/$myclass/msg/$ruser.rec")) $msg=file("class/$myclass/msg/$ruser.rec");
      else $msg[0]="";
   $content=stripslashes(safe_cover($content));
   $title=stripslashes(safe_cover($title));
   $new="$user_id|$title|$stemp|$content|0|\n";
   $oldcount=count($msg);
   if ($oldcount>30)
       {
       for ($i=30; $i<$oldcount; $i++) unset($msg);
       }
   $old=implode("",$msg);
   writetofile("class/$myclass/msg/$ruser.rec",$new.$old);
   if (file_exists("class/$myclass/msg/$user_id.snd")) $old=read_file("class/$myclass/msg/$user_id.snd");
   else $old="";
   $new="$ruser|$title|$stemp|$content|\n";
   writetofile("class/$myclass/msg/$user_id.snd",$new.$old);{errorview("消息已发送！");exit;}

   exit;
   }
}
#######
if ($menu=="sendbox") {
        if (file_exists("class/$myclass/msg/$user_id.snd")) {
                $receivelist=file("class/$myclass/msg/$user_id.snd");
                $count=count($receivelist);
                if ($count<=1 && empty($receivelist[0])) {echo"<br>空的收件箱，您没有任何短消息";exit;}
                echo "<table bgcolor=#ffffff 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?myclass=$myclass&menu=write&target=$ruser'>$ruser</a></acronym></td><td width=145><a href='messanger.php?myclass=$myclass&menu=readsnd&msg=$i'>$title</a></td></tr></table>";
                        sline();
                }
        }
        else {echo "<br>空的发件箱，您没有任何短消息"; exit; }
}
##########
if ($menu=="read") {
        if (file_exists("class/$myclass/msg/$user_id.rec")) {
                $receivelist=file("class/$myclass/msg/$user_id.rec");
                list($author,$title,$time,$content,$isread)=explode("|",$receivelist[$msg]);
                echo "<br><font color=5A6633>作者：</font>$author<br><font color=5A6633>题目：</font>$title<br>$ctime<br>$content";
                echo "<br><br><font color=5A6633>选项： [<a href=messanger.php?myclass=$myclass&menu=write&target=$author>回复</a>] [<a href=messanger.php?myclass=$myclass&menu=receivebox>返回</a>]</font>";
                $count=count($receivelist);
                $fp=fopen("class/$myclass/msg/$user_id.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 ($menu=="readsnd") {
        if (file_exists("class/$myclass/msg/$user_id.snd")) {
                $sendlist=file("class/$myclass/msg/$user_id.snd");
                list($ruser,$title,$time,$content)=explode("|",$sendlist[$msg]);
                echo "<br><font color=5A6633>接收用户：</font>$ruser<br><font color=5A6633>题目：</font>$title<br>$ctime<br>$content";
                echo "<br><br><font color=5A6633>选项： [<a href='messanger.php?myclass=$myclass&menu=write&target=$ruser'>另发信息</a>] [<a href='messanger.php?job=sendbox'>返回</a>]</font>";
                $count=count($sendlist);
        }
}
#########
if ($menu=="receivebox") {
        if (file_exists("class/$myclass/msg/$user_id.rec")) {
                $receivelist=file("class/$myclass/msg/$user_id.rec");
                $count=count($receivelist);
                if ($count<=1 && empty($receivelist[0])) {echo"<br>空的收件箱，您没有任何短消息";exit;}
                echo "<table bgcolor=#ffffff 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=5A6633>已读</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?myclass=$myclass&menu=write&target=$author'>$author</a></acronym></td><td width=110><a href='messanger.php?myclass=$myclass&menu=read&msg=$i'>$title</a></td><td width=26>$isread</td></tr></table>";
                        sline();
                }
        }
        else {echo "<br>空的收件箱，您没有任何短消息"; exit; }
}
###########
if ($menu=="clear") {
        if (file_exists("class/$myclass/msg/$user_id.rec")) unlink("class/$myclass/msg/$user_id.rec");
        if (file_exists("class/$myclass/msg/$user_id.snd")) unlink("class/$myclass/msg/$user_id.snd");
        echo "<br>您的所有短信息已被成功清空";
        exit;
}
###########
function print_form() {
global $target,$myclass;
?>
<table width=205 border=0 cellspacing=0 cellpadding=0>
  <tr bgcolor=#ffffff>
    <td>请填写信息</td>
  </tr><form action=messanger.php?myclass=<?echo $myclass;?> method=POST>
  <tr bgcolor=#ffffff>
    <td>

      接受用户：
      <input <? if (isset($target)) echo "value='$target'"; ?> style="background-color:#FEFEFF; 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:#FEFEFF; 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:#FEFEFF; 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=menu value=write>
<input type=hidden name=step value=2>
</td>
  </tr></form>
</table>
<?
}
?>