<?php
	//引入公共方法
	require("foundation/module_users.php");
	require("foundation/module_mypals.php");
	require("foundation/aanti_refresh.php");
	require("foundation/aintegral.php");
	require("foundation/adata_count.php");
	require("api/base_support.php");
	//引入语言包
	$mb_langpackage=new msgboardlp;

	//变量取得
	$to_user_id = short_check(get_argg('user_id'));
	$message = short_check(get_argp('CONTENT'));
	$from_user_id=get_sess_userid();
	$from_user_name=get_sess_username();
	$from_user_ico=get_sess_userico();
	$restore=0;
	if(strlen($message) >=500){
		action_return(0,$mb_langpackage->mb_add_exc,-1);exit;
	}
	
	if(get_argp('user_id')){
		$to_user_id = short_check(get_argp('user_id'));
		if($to_user_id!=$from_user_id){
			$restore=1;
		}
	}

	//数据表定义区
	$t_message = $tablePreStr."msgboard";
	$t_users = $tablePreStr."users";
	$t_holerpals = $tablePreStr."pals_mine";

	$dbo = new dbex;
	
	//读写分离定义函数
	dbtarget('r',$dbServs);
	
	$holder_pals=getHodlerpals($dbo,$t_holerpals,$to_user_id);
	$p=strstr($holder_pals,",".$from_user_id.",");
	
	$select_items=array('inputmess_limit');
	$user_info=get_user_info_item($dbo,$select_items,$t_users,$to_user_id);
	
	if(!$user_info){
		action_return(0,$mb_langpackage->mb_add_err,"modules.php?app=msgboard_more&user_id=$to_user_id");exit;
	}
	
	if($user_info['inputmess_limit']==2 and $from_user_id!=$to_user_id){
		action_return(0,$mb_langpackage->mb_add_pvw,"-1");
	}else if($user_info['inputmess_limit']==1 and !$p and $from_user_id!=$to_user_id){
		action_return(0,$mb_langpackage->mb_add_pvw,"-1");
	}else{
		//读写分离定义函数
		dbtarget('w',$dbServs);

		//留言
		$sql = "insert into $t_message(`from_user_id`,`from_user_name`,`from_user_ico`,`to_user_id`,`message`,`add_time`) values($from_user_id,'$from_user_name','$from_user_ico',$to_user_id,'$message',now())";
		if($dbo -> exeUpdate($sql)){
			//缓存功能区
			require($webRoot.$baseLibsPath."action/msgboard_list_update.action.cache.php");					
			increase_integral($dbo,$int_com_msg,$from_user_id);
			update_count($dbo,$to_user_id,"msg_num",1);
			if($restore==1){
				$sql=$sql = "insert into $t_message(`from_user_id`,`from_user_name`,`from_user_ico`,`to_user_id`,`message`,`add_time`) values($from_user_id,'$from_user_name','$from_user_ico',$from_user_id,'$message',now())";
				$dbo->exeUpdate($sql);
				update_count($dbo,$from_user_id,"msg_num",1);
			}
			if($to_user_id!=$from_user_id){
				api_proxy("message_set","remind",$mb_langpackage->mb_remind,$to_user_id,0,"modules.php?app=msgboard_more",7);
			}
			action_return(1,"","-1");
		}
	}
?>
