<?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=='addCollect'){
		$ur=$ur=user_returnlogin();
		if(!$ur[userid]) exit('0');	
		$id=(int)$_GET[mid];
		$r=$empire->fetch1("select classid,id from {$dbtbpre}ecms_movie where id='$id'");
		if(!$r[id]||!$r[classid]) exit('4');
		$userid=$ur[userid];
		$id=$r[id];
		$classid=$r[classid];
		$newstime=time();
		$totalquery="select count(*) as total from {$dbtbpre}movie_fava where movieid='$id' and userid='$userid' and classid='$classid'";
	    $num=$empire->gettotal($totalquery);
		if($num) exit('3');
	
$query="insert into {$dbtbpre}movie_fava (userid,classid,movieid,newstime) values('$userid','$classid','$id','$newstime')";	
$up=$empire->query($query);
$fid=$empire->lastid();
if($up){
insertnew($fid,3,$userid);
echo '1';
}
else echo '2';	
}
if($action=='addRss'){
		$ur=$ur=user_returnlogin();
		if(!$ur[userid]) exit('0');	
		$id=(int)$_GET[mid];
		$r=$empire->fetch1("select classid,id from {$dbtbpre}ecms_movie where id='$id'");
		if(!$r[id]||!$r[classid]) exit('4');
		$userid=$ur[userid];
		$id=$r[id];
		$classid=$r[classid];
		$newstime=time();
		$totalquery="select count(*) as total from {$dbtbpre}movie_remind where movieid='$id' and userid='$userid' and classid='$classid'";
	    $num=$empire->gettotal($totalquery);
		if($num) exit('3');
	
$query="insert into {$dbtbpre}movie_remind (userid,classid,movieid,newstime) values('$userid','$classid','$id','$newstime')";	
$up=$empire->query($query);
$fid=$empire->lastid();
if($up){
insertnew($fid,3,$userid);
echo '1';
}
else echo '2';	
}












if($action=='saveuserinfo'){
	$ur=$ur=user_returnlogin();
	if(!$ur[userid]) exit('{"msg":"请重新登陆","rcode":"-1"}');
	$userid=$ur[userid];
	$saytext=nl2br(RepFieldtextNbsp(RepPostStr(iconv("utf-8","gb2312",$add[saytext]))));
	$city=RepPostStr(iconv("utf-8","gb2312",$add[city_id]));
	$prov=RepPostStr(iconv("utf-8","gb2312",$add[prov_id]));
	$sex=(int)$add[sex];
	$edu=(int)$add[edu];
	$industry=(int)$add[industry];
	$year=(int)$add[year];
	$moon=(int)$add[moon];
	$day=(int)$add[day];
	$nickname=nl2br(RepFieldtextNbsp(RepPostStr(iconv("utf-8","gb2312",$add[nickname]))));
	$upquery="update {$dbtbpre}enewsmemberadd set city='$city',prov='$prov',sex='$sex',year='$year',moon='$moon',day='$day',saytext='$saytext',nickname='$nickname',edu='$edu',industry='$industry' where userid='$userid' ";
	$up=$empire->query($upquery);
	if($up)echo '{"msg":"\u66f4\u65b0\u6210\u529f","rcode":1}';
	else echo '{"msg":"更新失败","rcode":"-1"}';
}



if($action=='addfava'){
	$id=(int)$_GET[id];
	$r=$empire->fetch1("select classid,id from {$dbtbpre}ecms_movie where id='$id'");
	$classid=$r[classid];
	if(!$classid) exit('{"msg":"添加信息有误","rcode":"-1"}');
	$ur=user_returnlogin();if(!$ur[userid]) exit('{"msg":"请重新登陆","rcode":"-1"}');
	$userid=$ur[userid]; 
	$newstime=time();
	$totalquery="select count(*) as total from {$dbtbpre}movie_fava where movieid='$id' and userid='$userid' and classid='$classid'";
	$num=$empire->gettotal($totalquery);
	if($num) exit('{"msg":"已经添加过","rcode":"-1"}');
	
$query="insert into {$dbtbpre}movie_fava (userid,classid,movieid,newstime) values('$userid','$classid','$id','$newstime')";	
$up=$empire->query($query);
$fid=$empire->lastid();
if($up){
insertnew($fid,3,$userid);
echo '{"msg":"添加收藏成功","rcode":"1","loveid":"'.$fid.'"}';
}
else echo '{"msg":"添加收藏失败","rcode":"-1"}';	
}




if($action=='addremind'){	
	$id=(int)$_GET[id];
	$r=$empire->fetch1("select classid,id from {$dbtbpre}ecms_movie where id='$id'");
	$classid=$r[classid];
	if(!$classid) exit('{"msg":"添加信息有误","rcode":"-1"}');
	$ur=user_returnlogin();if(!$ur[userid]) exit('{"msg":"请重新登陆","rcode":"-1"}');
	$userid=$ur[userid]; 
	$newstime=time();
	$totalquery="select count(*) as total from {$dbtbpre}movie_remind where movieid='$id' and userid='$userid' and classid='$classid'";
	$num=$empire->gettotal($totalquery);
	if($num) exit('{"msg":"已经添加过","rcode":"-1"}');
	
$query="insert into {$dbtbpre}movie_remind (userid,classid,movieid,newstime) values('$userid','$classid','$id','$newstime')";	
$up=$empire->query($query);
$fid=$empire->lastid();
if($up){
insertnew($fid,2,$userid);
echo '{"msg":"添加订阅成功","rcode":"1","loveid":"'.$fid.'"}';
}
else echo '{"msg":"添加订阅失败","rcode":"-1"}';	
	
}
if($action=='delfava'){	
	$id=(int)$_GET[loveid];	
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_fava where id='$id' and userid='$userid'";
	$del=$empire->query($query);	
	if($del){
	echo '{"msg":"删除收藏成功","rcode":1}';
	}
	else{
	echo '{"msg":"删除收藏失败","rcode":"-1"}';
	}	
	
}
if($action=='postdelfava'){
	$idlist=$add[lid];
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_fava where id in (".$idlist.") and userid='$userid'";
	$del=$empire->query($query);
	if($del){
		echo '{"msg":"删除收藏成功","rcode":"1"}';
		}
	else{
	echo '{"msg":"删除收藏失败","rcode":"-1"}';
	}
}
if($action=='delremind'){	
	$id=(int)$_GET[loveid];	
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_remind where id='$id' and userid='$userid'";
	$del=$empire->query($query);	
	if($del){
	echo '{"msg":"删除订阅成功","rcode":1}';
	}
	else{
	echo '{"msg":"删除订阅失败","rcode":"-1"}';
	}	
	
}
if($action=='delplaylog'){	
	$id=(int)$_GET[id];	
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_playlog where id='$id' and userid='$userid'";
	$del=$empire->query($query);	
	if($del){
	echo '{"msg":"删除播放记录成功","rcode":1}';
	}
	else{
	echo '{"msg":"删除播放记录失败","rcode":"-1"}';
	}	
	
}
if($action=='postdelremind'){
	$idlist=$add[lid];
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_remind where id in (".$idlist.") and userid='$userid'";
	$del=$empire->query($query);
	if($del){
		echo '{"msg":"删除订阅成功","rcode":"1"}';
		}
	else{
	echo '{"msg":"删除订阅失败","rcode":"-1"}';
	}
}
if($action=='delallplaylog'){
	$idlist=$add[lid];
	$ur=user_returnlogin();
	$userid=$ur[userid];
	$query="delete from {$dbtbpre}movie_remind where userid='$userid'";
	$del=$empire->query($query);
	if($del){
		echo '{"msg":"删除播放记录成功","rcode":"1"}';
		}
	else{
	echo '{"msg":"删除播放记录失败","rcode":"-1"}';
	}
}
if($action=='getmyplaylog'){
$ur=user_returnlogin();
$userid=$ur[userid]; 
$listtemp='
<li>

					<p class="title"><a target="_blank" href="[!--titleurl--]">[!--title--]</a>/<a target="_blank" href="[!--lasturl--]">[!--name--]</a></p>

					<p class="morelook"><a target="_blank" href="[!--lasturl--]">重新观看</a>|<a target="_blank" href="[!--playurl--]">最新更新</a></p>

					<p class="time">[!--newstime--]</p>

					<a href="javascript:;" class="ui-del del" url="class/ajax.php?action=delplaylog&id=[!--logid--]" hidefocus>删除</a>

</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)){
			$logid=$fr[id];
			$newstime=date('Y-m-d H:i:s',$fr[newstime]);
			$name=$fr[name];
			$lasturl=urldecode($fr[lasturl]);
			$id=$fr[movieid];
			$classid=$fr[classid];
			$r=$empire->fetch1("select title,titleurl,id,playurl from {$dbtbpre}ecms_movie where id='$id'");
			$title=$r[title];
			$titleurl=$r[titleurl];
			$playurl=$r[playurl];
			$typeset=$r[typeset];
			$temp=$listtemp;
			$temp=str_replace('[!--playurl--]',$playurl,$temp);
			$temp=str_replace('[!--titleurl--]',$titleurl,$temp);
			$temp=str_replace('[!--title--]',$title,$temp);
			$temp=str_replace('[!--favaurl--]',$favaurl,$temp);
			$temp=str_replace('[!--logid--]',$logid,$temp);
			$temp=str_replace('[!--lasturl--]',$lasturl,$temp);
			$temp=str_replace('[!--name--]',$name,$temp);
			$temp=str_replace('[!--newstime--]',$newstime,$temp);
			if($r[id]){
				$listhtml.=$temp;
			}
			
	}
$listend='';
	
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}



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

<li><a target="_blank" class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]" alt="[!--title--]" />
  <label class="mask">&nbsp;</label>
  <label class="text">[!--typeset--]</label>
  </a>
  <h5><a target="_blank" href="[!--titleurl--]" title="[!--title--]">[!--title--]</a></h5>
  <div class="user-todo user-todo-check fn-clear">
    <label class="label-checkbox" for="checkbox[!--loveid--]" hidefocus>
      <input type="checkbox" name="loveids" value="[!--loveid--]" id="checkbox[!--loveid--]" />
    </label>
    <a class="ui-del fav-cancel" href="javascript:void(0);" data="class/ajax.php?action=delfava&loveid=[!--loveid--]">删除</a></div>
</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;
				$havedata=1;
			}
			
	}
$listend='<script type="text/javascript">
$(function(){
	$(".user-img-list li").hover(function(){
		$(this).find(".ui-del").show();
	},function(){
		$(this).find(".ui-del").hide();
	});
	
	$(".label-checkbox").click(function(){
		setupLabel();
	});
	setupLabel();
});
</script>';
	
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	$json[havedata]=$havedata;
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}

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

<li><a target="_blank" class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]" alt="[!--title--]" />
  <label class="mask">&nbsp;</label>
  <label class="text">[!--typeset--]</label>
  </a>
  <h5><a target="_blank" href="[!--titleurl--]" title="[!--title--]">[!--title--]</a></h5>
  <div class="user-todo user-todo-check fn-clear">
    <label class="label-checkbox" for="checkbox[!--loveid--]" hidefocus>
      <input type="checkbox" name="loveids" value="[!--loveid--]" id="checkbox[!--loveid--]" />
    </label>
    <a class="ui-del fav-cancel" href="javascript:void(0);" data="class/ajax.php?action=delremind&loveid=[!--loveid--]">删除</a></div>
</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;
				$havedata=1;
			}
			
	}
$listend='<script type="text/javascript">
$(function(){
	$(".user-img-list li").hover(function(){
		$(this).find(".ui-del").show();
	},function(){
		$(this).find(".ui-del").hide();
	});
	
	$(".label-checkbox").click(function(){
		setupLabel();
	});
	setupLabel();
});
</script>';
	
	$listhtml.=$listend;
	
	$json=array();
	$json[page]='';
	$json[ajaxtxt]=$listhtml;
	$json[havedata]=$havedata;
	$json=toutf8($json);
	$jsonp=json_encode($json);
	echo $jsonp;
}
if($action=='gethotfava'){
$listtemp='
<li><a target="_blank" class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]"/></a>
  <h5><a target="_blank" href="[!--titleurl--]">[!--title--]</a></h5>
  <div class="user-todo"><a class="ui-edit savelove fav-add" href="[!--favaurl--]">收藏</a></div>
</li>';
$listhtml='';
$query="select title,titleurl,titlepic,id,classid from {$dbtbpre}ecms_movie order by onclick desc limit 10";
$query=$empire->query($query);
	while($r=$empire->fetch($query)){
			$title=$r[title];
			$titleurl=$r[titleurl];
			$titlepic=$public_r[add_imgserver].$r[titlepic];
			$favaurl=$public_r[newsurl].'user/class/ajax.php?action=addfava&id='.$r[id];
			$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);
			$listhtml.=$temp;
		}	

		echo $listhtml;
}

if($action=='gethotremind'){
$listtemp='
<li><a target="_blank" class="play-img" href="[!--titleurl--]"><img src="[!--titlepic--]"/></a>
  <h5><a target="_blank" href="[!--titleurl--]">[!--title--]</a></h5>
  <div class="user-todo"><a class="ui-edit saveremind fav-add" href="[!--favaurl--]">订阅</a></div>
</li>';
$listhtml='';
$query="select title,titleurl,titlepic,id,classid from {$dbtbpre}ecms_movie order by onclick desc limit 10";
$query=$empire->query($query);
	while($r=$empire->fetch($query)){
			$title=$r[title];
			$titleurl=$r[titleurl];
			$titlepic=$public_r[add_imgserver].$r[titlepic];
			$favaurl=$public_r[newsurl].'user/class/ajax.php?action=addremind&id='.$r[id];
			$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);
			$listhtml.=$temp;
		}	

		echo $listhtml;
}

if($action=='addtalk'){

	$talktext=$add[comm_txt];
	$talktext=iconv('utf-8','gb2312',$talktext);
	if($talktext=='亲，个性签名能让你个性签哦 ^.^')
	
	{
		echo '{"rcode":-1,"msg":"\u63d0\u4ea4\u7684\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u8bf7\u586b\u5199\u63d0\u4ea4\u7684\u5185\u5bb9!"}';
		exit;
	}
	$return=inserttalk($talktext);
	if($return){
		echo '{"rcode":1,"msg":"\u63d0\u4ea4\u6210\u529f!"}';
	}
	else{
		echo '{"rcode":-1,"msg":"\u63d0\u4ea4\u7684\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\uff0c\u8bf7\u586b\u5199\u63d0\u4ea4\u7684\u5185\u5bb9!"}';

	}
}
if($action=='getallnew'){
$show=(int)$_GET[show];
$ur=user_returnlogin();
$userid=$ur[userid];
$nickname=$ur[nickname];
if(!$userid||!$ur){
echo '{"rode":-2,"txt":"","userid":"'.$userid.'"}'; exit;
}
$listtemp=getusernew($userid,$show);

if($listtemp) $recode=1; else $recode=-1;
$listtempend="<script>
$(document).ready(function(){
	$('.cmt-user').each(function (e){
		if($(this).attr('data')!=undefined){
			$(this).html('<a href=http://i.qire123.com/'+$(this).attr('data')+'>'+$(this).html()+'</a>');
		}
	});　
}); 
</script>";
//$listtemp=addslashes($listtemp);
//$listtemp.=$listtempend;

$json=array();
$json[rcode]=$recode;
$json[txt]=$listtemp;
$json[userid]=$userid;
$json=toutf8($json);
$jsonp=json_encode($json);
echo $jsonp;

//echo '{"rcode":'.$recode.',"text":"'.$listtemp.'","userid":"'.$userid.'"}'; exit;
}
if($action=='checkloginpost'){
require "../../e/memberconnect/memberconnectfun.php";	
	
	$username=trim($add['username']);
	$password=trim($add['password']);
	if(!$username||!$password)
	{
		echo '{"msg":"用户名或密码错误","rcode":-1}'; exit;
	}
	$tobind=(int)$add['tobind'];
	$username=RepPostVar($username);
	$password=RepPostVar($password);
	$r=$empire->fetch1("select ".eReturnSelectMemberF('*')." from ".eReturnMemberTable()." where ".egetmf('username')."='$username' limit 1");
	
	if(!$r['userid'])
	{
		showbox("用户名或密码错误");exit;
	}
	if(!eDoCkMemberPw($password,$r['password'],$r['salt']))
	{
		showbox("用户名或密码错误");exit;
	}
	if($r['checked']==0)
	{
      showbox("您的帐号还未通过审核");exit;
	}
	//绑定帐号
	if($tobind)
	{
		$_SESSION[apptype]=$add[apptype];
		$_SESSION[openid]=$add[openid];
	$num=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsmember_connect where userid='$r[userid]' and apptype='$add[apptype]' limit 1");
	if($num)
	{
		 showbox("此帐号已经绑定，不能重复绑定"); exit;
	}
		
		MemberConnect_BindUser($r['userid']);
	}
	$rnd=make_password(20);//取得随机密码
	//默认会员组
	if(empty($r['groupid']))
	{
		$r['groupid']=eReturnMemberDefGroupid();
	}
	$r['groupid']=(int)$r['groupid'];
	$lasttime=time();
	//IP
	$lastip=egetip();
	$usql=$empire->query("update ".eReturnMemberTable()." set ".egetmf('rnd')."='$rnd',".egetmf('groupid')."='$r[groupid]' where ".egetmf('userid')."='$r[userid]'");
	$empire->query("update {$dbtbpre}enewsmemberadd set lasttime='$lasttime',lastip='$lastip',loginnum=loginnum+1 where userid='$r[userid]'");
	//设置cookie
	$lifetime=(int)$add['lifetime'];
	$logincookie=0;
	if($lifetime)
	{
		$logincookie=time()+$lifetime;
	}
	$set1=esetcookie("mlusername",$username,$logincookie);
	$set2=esetcookie("mluserid",$r['userid'],$logincookie);
	$set3=esetcookie("mlgroupid",$r['groupid'],$logincookie);
	$set4=esetcookie("mlrnd",$rnd,$logincookie);	
	echo '{"msg":"\u767b\u5f55\u6210\u529f","redir":"center.php","rcode":1,"wantredir":1}'; exit;
}


if($action=='setremindconfig'){
	$sendemail=(int)$add[remindsendemail];
	$sendmsg=(int)$add[remindsendmsg];
	$email=RepPostStr($add[remindemail]);
	$ur=user_returnlogin();
	$userid=$ur[userid];
	if(!$userid){
	showbox("请重新登陆后再进行设置");
	}
	$query="update {$dbtbpre}enewsmember set remindsendemail='$sendemail',remindsendmsg='$sendmsg',remindemail='$email' where userid='$userid'";
	$up=$empire->query($query);
	if($up) showbox("设置成功",1);
	else showbox("设置失败");
	

}




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);
}
?>