<?php
include("require.php");
if(!isset($_COOKIE["core_user_name"]))
  jump("login.php?action=login&last=message.php");
$phpcolor->table="message";
if($_GET['action']=="delete")
{
  submitfilter($_GET['id']);
  $phpcolor->field="addressee";
  $phpcolor->where="id = '".$_GET['id']."'";
  $rs = $phpcolor->exist(1);
  if($rs['addressee'] == $_COOKIE['core_user_name'])
  {
    $phpcolor->delete();
    jump("message.php");
  }
  jump("error.php?id=2");
}
if($_GET['action']=="allread")
{
  $phpcolor->field="isread = '1'";
  $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."'";
  $phpcolor->upgrade();
  jump("message.php");
}
if($_GET['action']=="alldel")
{
  $phpcolor->field="*";
  $phpcolor->where="isread = '1' and addressee = '".$_COOKIE["core_user_name"]."'";
  $phpcolor->delete();
  jump("message.php");
}
if($_GET['action']=="writesubmit")
{
  $phpcolor->table="user";
  $phpcolor->field="*";
  $phpcolor->where="username = '".$_POST['addressee']."'";
  if(($_POST['addressee']=="" or $_POST['content']=="") || !$phpcolor->exist(2) || $_POST['addressee']==$_COOKIE['core_user_name'])
    jump("error.php?id=1&last=message.php?action=write");
  $_POST['content']=autolink(nl2br($_POST['content']));
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $phpcolor->message($_POST['content'],$_COOKIE['core_user_name'],$_POST['addressee'],$time,1);
  jump("message.php");
}
//ÔÄ¶ÁÏûÏ¢
if($_GET['action']=="read")
{
  submitfilter($_GET['id']);
  $phpcolor->field="content,addresser,addressee,type";
  $phpcolor->where="id = '".$_GET['id']."'";
  $message = $phpcolor->exist(1);
  if($message['type']=="2")
    $message['content']=message_replace($message['content'],$core_readpage,$core_url);
  if($message['addressee'] == $_COOKIE['core_user_name'])
  {
    $phpcolor->field="isread = '1'";
    $phpcolor->upgrade();
  }
  else
    jump("error.php?id=2");
}
else
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  if($_GET['action']=="s")
  {
    $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."' and type=2 order by sent desc";
    $count=$phpcolor->exist(2);
    $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."' and type=2 order by sent desc limit ".($_GET['page']*15).",15";
    $message=$phpcolor->assignment();
    $page=pages($count,15,$_GET['page'],'message','&action=s','','0');
  }
  elseif($_GET['action']=="m")
  {
    $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."' and type=1 order by sent desc";
    $count=$phpcolor->exist(2);
    $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."' and type=1 order by sent desc limit ".($_GET['page']*15).",15";
    $message=$phpcolor->assignment();
    $page=pages($count,15,$_GET['page'],'message','&action=m','','0');
  }
  else
  {
    $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."' order by sent desc";
    $count=$phpcolor->exist(2);
    $phpcolor->where="addressee = '".$_COOKIE["core_user_name"]."' order by sent desc limit ".($_GET['page']*15).",15";
    $message=$phpcolor->assignment();
    $page=pages($count,15,$_GET['page'],'message','','','0');
  }
  for ($count=0;$count<count($message);$count++)
  { 
    $message[$count]['content']=str_replace("<br />"," ",$message[$count]['content']);
    $message[$count]['content']=$phpcolor->mysubstr($message[$count]['content'],0,50);
  }
  if(isset($_GET['addressee']))
    $tpl -> assign("addressee", $_GET['addressee']);
}
$tpl -> assign("message", $message); 
$tpl -> assign("page", $page);
$tpl -> assign("action", $_GET['action']);
$tpl -> assign("id", $_GET['id']);
$tpl -> assign("core_header_title", "¶ÌÏûÏ¢ - ");
$tpl -> display("$core_style/message.htm");
?>