<?php
	!defined('DIRCMS_MEMBER') && exit('Access Denied!');
	$action=empty($action)?'inbox':trim($action);
	require_once(DIRCMS_ROOT.'include/list.class.php');
	$l=new dircmslist();
	switch($action)
	{
		case 'inbox':
			$where="`send_to_user`='{$_username}' OR `send_to_user`='#system#'";
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'message',$where,$orderby,$page,18);
			$pages=$l->pages;
			include member_tlp('msg_inbox');
			break;
		case 'outbox':
			$where="`send_from_user`='{$_username}'";
			$orderby="id DESC";
			$page=max(isset($page)?intval($page):1,1);
			$result=$l->getlist(DB_PRE.'message',$where,$orderby,$page,18);
			$pages=$l->pages;
			include member_tlp('msg_outbox');
			break;
		case 'read':
			if(isset($do_submit))
			{
				$msg=array_map('htmlspecialchars',$msg);
				require_once(DIRCMS_ROOT.'include/member.class.php');	
				$member=new member();
				if(!$member->get_userid($msg['send_to_user']))showmsg('用户'.$msg['send_to_user'].'不存在!');
				$msg['content']=trim($info['content']);
				$msg['send_from_user']=$_username;
				$msg['folder']='outbox';
				$msg['message_time']=TIME;
				$msg['replyid']=intval($id);
				if(strlen($msg['subject'])>=200)showmsg('主题过长!');
				if($msg['send_to_user']==$_username)showmsg('不能给自己发信息!');
				if(!$msg['content'])showmsg('信息内容不能为空!');
				$db->insert(DB_PRE.'message',$msg);
				$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`message`=`".DB_PRE."member`.`message`+1 WHERE `".DB_PRE."member`.`username`='{$msg['send_to_user']}'");
				$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`message`=`".DB_PRE."member_cache`.`message`+1 WHERE `".DB_PRE."member_cache`.`username`='{$msg['send_to_user']}'");
				showmsg('信息发送成功!','index.php?file=msg&action=outbox');
			}
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$form=new form();
			$val=$db->fetch_one("SELECT * FROM `".DB_PRE."message` WHERE `".DB_PRE."message`.`id`=".intval($id));
			if(!$val)showmsg('该信息不存在!');
			if($val['send_to_user']!=$_username && $val['send_from_user']!=$_username)showmsg('错误的请求!');
			$message=intval($_message-1)>=0?intval($_message-1):0;
			$db->update(DB_PRE.'message',array('status'=>1),'id='.intval($val['id']));
			$db->update(DB_PRE.'member',array('message'=>$message),'id='.intval($_userid));
			$db->update(DB_PRE.'member_cache',array('message'=>$message),'id='.intval($_userid));
			include member_tlp('msg_read');
			break;
		case 'send':
			if(isset($do_submit))
			{
				require_once(DIRCMS_ROOT.'include/member.class.php');	
				$member=new member();
				//if(!$member->get_userid($msg['send_to_user']))showmsg('用户'.$msg['send_to_user'].'不存在!');
				$msg=array_map('htmlspecialchars',$msg);
				$msg['content']=trim($info['content']);
				$msg['send_from_user']=$_username;
				$msg['folder']='outbox'; ////// 保存到发件箱 
				$msg['message_time']=TIME;
				if(strlen($msg['subject'])>=200 || strlen($msg['subject'])<2)showmsg('主题不能小于2个且不能大于200个字符!');
				if($msg['send_to_user']==$_username)showmsg('不能给自己发信息!');
				if(!$msg['content'])showmsg('信息内容不能为空!');
				$db->insert(DB_PRE.'message',$msg);
				$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`message`=`".DB_PRE."member`.`message`+1 WHERE `".DB_PRE."member`.`username`='{$msg['send_to_user']}'");
				$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`message`=`".DB_PRE."member_cache`.`message`+1 WHERE `".DB_PRE."member_cache`.`username`='{$msg['send_to_user']}'");
				showmsg('信息发送成功!');
			}
			require_once(DIRCMS_ROOT.'include/form.class.php');	
			$form=new form();
			include member_tlp('msg_send');
			break;
		case 'delete':
			if(!$id)showmsg('请选择需要删除的信息!');
			$id=is_array($id)?array_map('intval',$id):array(intval($id));
			$_mcount=max(intval($_message-sizeof($id)),0);
			foreach($id as $_id)
			{
				$r=$db->fetch_one("SELECT send_from_user,send_to_user FROM ".DB_PRE."message WHERE id=".$_id);
				if(!$r || ($r['send_from_user']!=$_username && $r['send_to_user']!=$_username))showmsg_nourl('错误的请求!');
				if($r['status']==0)
				{
					$db->query("UPDATE `".DB_PRE."member` SET `".DB_PRE."member`.`message`=".$_mcount." WHERE `".DB_PRE."member`.`username`='{$_username}'"); // 更新统计数据
					$db->query("UPDATE `".DB_PRE."member_cache` SET `".DB_PRE."member_cache`.`message`=".$_mcount." WHERE `".DB_PRE."member_cache`.`username`='{$_username}'");
				}
				if($r['send_to_user']!='#system#' || $_groupid==1)$db->mysql_delete(DB_PRE.'message',$_id);
			}
			showmsg('操作成功!');
			break;
		default:
			showmsg('Bad Request!!!',$DIRCMS['site_url']);
			break;
	}
?>