<?php 
//引入小纸条模块功能
	require("foundation/adata_count.php");
	require("foundation/module_scrip.php");
	require("foundation/aanti_refresh.php");
	require("foundation/module_users.php");
	require("api/base_support.php");
	
//引入语言包
	$mp_langpackage=new mypalslp;
		
//变量区
	$mreq_touser=short_check(get_argg("other_id"));
 	$user_id=get_sess_userid();//发申请人id
	$user_name=get_sess_username();//发申请人姓名
	$user_sex=get_sess_usersex();//发申请人性别
	$userico=get_sess_userico();//发申请人的头像
	$user_mypals=get_sess_mypals();

//数据表定义区
	$t_users=$tablePreStr."users";
	$t_mypals=$tablePreStr."pals_mine";
	$t_pals_request=$tablePreStr."pals_request";

//添加好友是自己  
  if($user_id==$mreq_touser){
      echo $mp_langpackage->mp_not_self;
      exit();
  }
  
//定义读操作
		dbtarget('r',$dbServs);	
		$dbo=new dbex();

//是否已经提交了申请
	$sql="select user_id from $t_pals_request where user_id=$mreq_touser and req_id=$user_id";
	$is_req=$dbo->getRow($sql);
	if(!empty($is_req['user_id'])){
		echo $mp_langpackage->mp_wait_confirm;
		exit;
	}
	
//看看好友列里是否已经有该人
		$sql="select user_id from $t_mypals where user_id=$user_id and pals_id=$mreq_touser";
		$is_pals=$dbo->getRow($sql);
  if(!empty($is_pals['user_id'])){
      echo $mp_langpackage->mp_rep_req;
      exit();
  }	

//取得该人的资料信息
  $select_items=array("user_id","user_name","user_sex","user_ico","palsreq_limit");
  $user_row=get_user_info_item($dbo,$select_items,$t_users,$mreq_touser);
  if($user_row){
		$touser_id=$user_row['user_id'];
		$touser_name=$user_row['user_name'];
		$touser_sex=$user_row['user_sex'];
		$touser_ico=$user_row['user_ico'];
		$touser_pals_limit=$user_row['palsreq_limit'];
  }else{
		echo $mp_langpackage->mp_info_wrong;
		exit();
  }

//此人拒绝加入
if($touser_pals_limit==2){echo $mp_langpackage->mp_ref_add;exit;}

//定义写操作
	dbtarget('w',$dbServs);
if($touser_pals_limit==0){
	$sql="insert into $t_mypals (user_id,pals_id,pals_name,pals_sex,add_time,pals_ico,accepted) value($user_id,$mreq_touser,'$touser_name','$touser_sex',NOW(),'$touser_ico',1)";
	$dbo->exeUpdate($sql);
	if(empty($user_mypals)){
		set_sess_mypals($touser_id);
	}else{
		set_sess_mypals($user_mypals.",".$user_mypals);
	}
	send_scrip($dbo,$user_name,$user_id,$userico,$mreq_touser,3);
	update_count($dbo,$user_id,"pals_num",1);
	echo "success";
}

//验证加入
if($touser_pals_limit==1){
 $sql="insert into $t_mypals (user_id,pals_id,pals_name,pals_sex,add_time,pals_ico)"
                         ."value($user_id,$touser_id,'$touser_name','$touser_sex',NOW(),'$touser_ico')";
 $dbo->exeUpdate($sql);
 $from_pals_id=MYSQL_INSERT_ID();
 $sql="insert into $t_pals_request (user_id,req_id,req_name,req_sex,add_time,from_id,req_ico)"
                        ."value($touser_id,$user_id,'$user_name','$user_sex',NOW(),$from_pals_id,'$userico')";
 if($dbo->exeUpdate($sql)){
 	api_proxy("message_set","remind",$mp_langpackage->mp_remind,$mreq_touser,0,"modules.php?app=mypals_request",3);//提醒机制
 	require($webRoot.$baseLibsPath."action/mypals_update.action.cache.php");
  echo $mp_langpackage->mp_suc_reg;
 }else{
  $sql="delete from $t_mypals where id=$from_pals_id";
  $dbo->exeUpdate($sql);
  echo $mp_langpackage->mp_req_false;
 }
}
?>  