<?php
if(!$_GET[action]){exit;}
session_start();
require "../../e/class/connect.php";
require "../../e/class/db_sql.php";
	require "../../e/member/class/user.php";
	require "fun.php";
$link=db_connect();
$empire=new mysqlquery;
$action=$_GET[action];
$add=$_POST;


if($action=='dellook'){	
	$lookuserid=(int)$_GET[lookid];	
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_look where lookuserid='$lookuserid' and userid='$userid'";
	$del=$empire->query($query);	
	if($del){
	echo '{"msg":"删除关注成功","rcode":2}';
	}
	else{
	echo '{"msg":"删除关注失败","rcode":"-1"}';
	}	
	
}


if($action=='addlook'){	
	$lookuserid=(int)$_GET[lookid];
	$ur=user_returnlogin();if(!$ur[userid]) exit('{"msg":"请重新登陆","rcode":"-1"}');
	$userid=$ur[userid]; 
	if($lookuserid==$userid){exit('{"msg":"请不要关注自己噢","rcode":"-1"}');}
	$newstime=time();
	$totalquery="select count(*) as total from {$dbtbpre}movie_look where lookuserid='$lookuserid' and userid='$userid' ";
	$num=$empire->gettotal($totalquery);
	if($num) exit('{"msg":"已经添加过关注","rcode":"-1"}');
	
$query="insert into {$dbtbpre}movie_look (userid,lookuserid,newstime) values('$userid','$lookuserid','$newstime')";	
$up=$empire->query($query);
$fid=$empire->lastid();
if($up){
insertnew($fid,4,$userid);
echo '{"msg":"添加关注成功","rcode":"1","loveid":"'.$fid.'"}';
}
else echo '{"msg":"添加关注失败","rcode":"-1"}';	
	
}





if($action=='getlook'){
	$ur=user_returnlogin();if(!$ur[userid]) exit('[{"focus_uid":"0"}]');
	$userid=$ur[userid]; 
	$query="select * from {$dbtbpre}movie_look  where userid='$userid'";
	$query=$empire->query($query);
	
	while($r=$empire->fetch($query)){
		$lookid=$r[lookuserid];
		$listtemp.= '{"focus_uid":"'.$lookid.'"},';
	}
	$listtemp=substr($listtemp,0,strlen($listtemp)-1);
	echo'[';
	echo $listtemp;
	echo ']';
}

if($action=='sendmsg'){
	$title=$add[msgtitle];
	$msgtext=$add[msgtxt];
	$to_userid=$add[touid];
	$ur=user_returnlogin();if(!$ur[userid]) exit('{"msg":"请登陆后再发送消息","rcode":"-1"}');
	$from_userid=$ur[userid];
	$from_username=getnickname($from_userid);
	$to_username=getusername($to_userid);
	if($to_userid==$from_userid) exit('{"msg":"不可向自己发送信息","rcode":"-1"}');
	if(!$to_username) exit('{"msg":"目标用户错误","rcode":"-1"}');
	$issys=0;
	$isadmin=0;
	$msgtime=date('Y-m-d H:i:s',time());
	$query="insert into {$dbtbpre}enewsqmsg (title,msgtext,to_userid,to_username,from_userid,from_username,issys,isadmin,msgtime) values ('$title','$msgtext','$to_userid','$to_username','$from_userid','$from_username','$issys','$isadmin','$msgtime') ";
	$up=$empire->query($query);
	if($up) echo'{"msg":"发送消息成功","rcode":"1"}';
	else echo '{"msg":"发送消息失败","rcode":"-1"}';
		
}

if($action=='delmsg'){
	$idlist=$add[msgid];
	$num=count($idlist);
	for($i=0;$i<$num;$i++){
	$idlists.=$idlist[$i].',';
	}
	$idlists=substr($idlists,0,strlen($idlists)-1);
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}enewsqmsg where mid in (".$idlists.") and to_userid='$userid'";
	$del=$empire->query($query);

	if($del){
		echo '{"msg":"删除消息成功","rcode":"1"}';
		}
	else{
	echo '{"msg":"删除消息失败","rcode":"-1"}';
	}
}

if($action=='getmsg'){
$ur=user_returnlogin();
$userid=$ur[userid]; 
$listtemp='

<li><label class="label-checkbox" for="msg[!--mid--]" hidefocus><input type="checkbox" name="msgid[]" value="[!--mid--]" id="msg[!--mid--]" /></label><div class="user-txt-list-cnt"><h5>『短消息』[!--title--] by:[!--from--]<em class="date-time">([!--msgtime--])</em></h5><div class="mymsg-list-cnt">[!--msgtext--]</div></div></li>';
$listhtml='';
$query="select * from {$dbtbpre}enewsqmsg where to_userid='$userid' or to_username='0' order by mid desc limit 50";
$query=$empire->query($query);
	while($r=$empire->fetch($query)){
			$mid=$r[mid];
			$title=$r[title];
			$myusername=getnickname($userid);
			$msgtext=str_replace('[!--nickname--]','$myusername',$r[msgtext]);
			$issys=$r[issys];
			if($issys==1){$from='<a href="'.$public_r[newsurl].'">系统</a>';}
			else{$from='<a href="'.$public_r[newsurl].'user/i.php?id='.$r[from_userid].'">'.getnickname($r[from_userid]).'</a>';}
			$msgtime=$r[msgtime];
			$haveread=$r[haveread];
			if($haveread==0){$title='<b>'.$title.'</b>';}
			$temp=$listtemp;
			$temp=str_replace('[!--mid--]',$mid,$temp);
			$temp=str_replace('[!--title--]',$title,$temp);
			$temp=str_replace('[!--msgtext--]',$msgtext,$temp);
			$temp=str_replace('[!--msgtime--]',$msgtime,$temp);
			$temp=str_replace('[!--from--]',$from,$temp);
			if($r[mid]){
				$listhtml.=$temp;
				
			}
			
	}
$listend='<script type="text/javascript">
$(function(){ $(".label-checkbox").click(function(){setupLabel();});});$(".djck").click(function(e){ $obj=$(this);$id=$obj.attr("data");if(parseInt($obj.attr("see"))==0){ $.ajax({url:"class/iajax.php?action=seemsg&mid="+$id,success:function(r){if(r["comm_txt"]!=null){ $("#show"+$id).html(r["comm_txt"]).show();$obj.attr("see","1");}},dataType:"json"});}else{if($("#show"+$id).is(":hidden")){ $("#show"+$id).show();}else{ $("#show"+$id).hide();}}});
</script>';
	$up=$empire->query("update {$dbtbpre}enewsqmsg set haveread=1 where to_userid='$userid'");
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}




if($action=='getmyfava'){
$userid=(int)$_GET[uid]; 
$listtemp='
<li><a class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]" alt="[!--title--]" /></a><h5><a href="[!--titleurl--]">[!--title--]</a></h5></li>';
$listhtml='';
$query="select * from {$dbtbpre}movie_fava where userid='$userid' order by id desc limit 50";
$query=$empire->query($query);
	while($fr=$empire->fetch($query)){
			$loveid=$fr[id];
			$id=$fr[movieid];
			$classid=$fr[classid];
			$r=$empire->fetch1("select title,titleurl,titlepic,id,classid,typeset from {$dbtbpre}ecms_movie where classid='$classid' and id='$id'");
			$title=$r[title];
			$titleurl=$r[titleurl];
			$typeset=$r[typeset];
			$titlepic=$public_r[add_imgserver].$r[titlepic];
			$temp=$listtemp;
			$temp=str_replace('[!--titleurl--]',$titleurl,$temp);
			$temp=str_replace('[!--title--]',$title,$temp);
			$temp=str_replace('[!--titlepic--]',$titlepic,$temp);
			$temp=str_replace('[!--favaurl--]',$favaurl,$temp);
			$temp=str_replace('[!--typeset--]',$typeset,$temp);
			$temp=str_replace('[!--loveid--]',$loveid,$temp);
			if($r[id]){
				$listhtml.=$temp;
			}
			
	}
$listend='<div class="fn-clear" style="height:0; overflow: hidden;"></div>';
	
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}
if($action=='getmyremind'){
$userid=(int)$_GET[uid]; 
$listtemp='
<li><a class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]" alt="[!--title--]" /></a><h5><a href="[!--titleurl--]">[!--title--]</a></h5></li>';
$listhtml='';
$query="select * from {$dbtbpre}movie_remind where userid='$userid' order by id desc limit 50";
$query=$empire->query($query);
	while($fr=$empire->fetch($query)){
			$loveid=$fr[id];
			$id=$fr[movieid];
			$classid=$fr[classid];
			$r=$empire->fetch1("select title,titleurl,titlepic,id,classid,typeset from {$dbtbpre}ecms_movie where classid='$classid' and id='$id'");
			$title=$r[title];
			$titleurl=$r[titleurl];
			$typeset=$r[typeset];
			$titlepic=$public_r[add_imgserver].$r[titlepic];
			$temp=$listtemp;
			$temp=str_replace('[!--titleurl--]',$titleurl,$temp);
			$temp=str_replace('[!--title--]',$title,$temp);
			$temp=str_replace('[!--titlepic--]',$titlepic,$temp);
			$temp=str_replace('[!--favaurl--]',$favaurl,$temp);
			$temp=str_replace('[!--typeset--]',$typeset,$temp);
			$temp=str_replace('[!--loveid--]',$loveid,$temp);
			if($r[id]){
				$listhtml.=$temp;
			}
			
	}
$listend='<div class="fn-clear" style="height:0; overflow: hidden;"></div>';
	
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}
if($action=='getmyplaylog'){
$userid=(int)$_GET[uid]; 
$listtemp='
<li><a class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]" alt="[!--title--]" /></a><h5><a href="[!--titleurl--]">[!--title--]</a></h5></li>';
$listhtml='';
$query="select * from {$dbtbpre}movie_playlog where userid='$userid' order by newstime desc limit 50";
$query=$empire->query($query);
	while($fr=$empire->fetch($query)){
			$loveid=$fr[id];
			$id=$fr[movieid];
			$classid=$fr[classid];
			$r=$empire->fetch1("select title,titleurl,titlepic,id,classid,typeset from {$dbtbpre}ecms_movie where id='$id'");
			$title=$r[title];
			$titleurl=$r[titleurl];
			$typeset=$r[typeset];
			$titlepic=$public_r[add_imgserver].$r[titlepic];
			$temp=$listtemp;
			$temp=str_replace('[!--titleurl--]',$titleurl,$temp);
			$temp=str_replace('[!--title--]',$title,$temp);
			$temp=str_replace('[!--titlepic--]',$titlepic,$temp);
			$temp=str_replace('[!--favaurl--]',$favaurl,$temp);
			$temp=str_replace('[!--typeset--]',$typeset,$temp);
			$temp=str_replace('[!--loveid--]',$loveid,$temp);
			if($r[id]){
				$listhtml.=$temp;
			}
			
	}
$listend='<div class="fn-clear" style="height:0; overflow: hidden;"></div>';
	
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}
function showbox($msg,$type='-1'){
echo '{"msg":"'.$msg.'","rcode":'.$type.'}'; exit;
}
$empire=NULL;
db_close();
function toutf8($data){
if(is_array($data)){
return array_map('toutf8',$data);
}
return iconv('gbk','utf-8',$data);
}
?>