<?php
include("require.php");
if(!isset($_COOKIE["core_user_name"]))
  Header("Location: login.php?action=login&last=message.php");
else
{
  $phpcolor->table=$core_mysql_prefix."message";
  if($_GET['action']=="read")
  {
    submitfilter(1,$_GET['id']);
    $phpcolor->field="theme,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();
      $tpl -> assign("message", $message);
    }
    else
      Header("Location: error.php?id=2");
  }
  elseif($_GET['action']=="delete")
  {
    submitfilter(1,$_GET['id']);
    $phpcolor->field="addressee";
    $phpcolor->where="id = '".$_GET['id']."'";
    $rs = $phpcolor->exist(1);
    if($rs['addressee'] == $_COOKIE['core_user_name'])
    {
	  $phpcolor->delete();
      Header("Location: message.php");
    }
    else
      Header("Location: error.php?id=2");
  }
  elseif($_GET['action']=="writesubmit")
  {
    $phpcolor->table=$core_mysql_prefix."user";
    $phpcolor->field="*";
    $phpcolor->where="username = '".$_POST['addressee']."'";
    if(($_POST['addressee']=="" or $_POST['theme']=="" or $_POST['content']=="") || !$phpcolor->exist(2) || $_POST['addressee']==$_COOKIE['core_user_name'])
      Header("Location: error.php?id=1&last=message.php?action=write");
    else
    {
	  $_POST['theme']=html_replace($_POST['theme']);
      $_POST['content']=autolink(nl2br(html_replace($_POST['content'])));
	  $_POST['addressee']=submitfilter(2,$_POST['addressee']);
	  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
	  $phpcolor->table=$core_mysql_prefix."message";
      $phpcolor->field="theme, content, addresser, addressee, sent";
      $phpcolor->where="'".$_POST['theme']."', '".$_POST['content']."', '".$_COOKIE['core_user_name']."', '".$_POST['addressee']."', '".$time."'";
      $phpcolor->write();
      Header("Location: message.php");
    }
  }
  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']*10).",10";
      $message=$phpcolor->assignment();
      $page=pages($count,10,$_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']*10).",10";
      $message=$phpcolor->assignment();
      $page=pages($count,10,$_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']*10).",10";
      $message=$phpcolor->assignment();
      $page=pages($count,10,$_GET['page'],'message','','','0');
	}
	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");
}
?>