<?php
//验证登陆
function checklogin($userid=0){
if(!$userid) header ("location:/user/login.php");
}


function user_returnlogin($uid=0,$uname='',$urnd=''){
	global $empire,$dbtbpre,$public_r,$ecmsreurl,$ecms_config;
	if($uid)
	{$userid=(int)$uid;}
	else
	{$userid=(int)getcvar('mluserid');}
	if($uname)
	{$username=$uname;}
	else
	{$username=getcvar('mlusername');}
	$username=RepPostVar($username);
	if($urnd)
	{$rnd=$urnd;}
	else
	{$rnd=getcvar('mlrnd');}
	if(!$userid)
	{
return false;
	}
	$rnd=RepPostVar($rnd);
	$cr=$empire->fetch1("select ".eReturnSelectMemberF('userid,username,email,groupid,userfen,money,userdate,zgroupid,havemsg,checked,registertime')." from ".eReturnMemberTable()." where ".egetmf('userid')."='$userid' and ".egetmf('username')."='$username' and ".egetmf('rnd')."='$rnd' limit 1");
	if(!$cr['userid'])
	{
return false;
	}
	if($cr['checked']==0)
	{
return false;
	}
	//默认会员组
	if(empty($cr['groupid']))
	{
		$user_groupid=eReturnMemberDefGroupid();
		$usql=$empire->query("update ".eReturnMemberTable()." set ".egetmf('groupid')."='$user_groupid' where ".egetmf('userid')."='".$cr[userid]."'");
		$cr['groupid']=$user_groupid;
	}
	//是否过期
	if($cr['userdate'])
	{
		if($cr['userdate']-time()<=0)
		{
			OutTimeZGroup($cr['userid'],$cr['zgroupid']);
			$cr['userdate']=0;
			if($cr['zgroupid'])
			{
				$cr['groupid']=$cr['zgroupid'];
				$cr['zgroupid']=0;
			}
		}
	}
	$isqqcheckr=$empire->fetch1("select * from {$dbtbpre}enewsmember_connect where userid='$cr[userid]'and apptype='qq'");
	$isqqcheck=$isqqcheckr[id]?'1':'0';
	$ur2=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$cr[userid]'");
	$re[lasttime]=$ur2['lasttime'];
	$re[nickname]=$ur2['nickname'];
	$re[userpic]=$ur2['userpic'];
	$re[userid]=$cr['userid'];
	$re[isqqcheck]=$isqqcheck;
	$re[rnd]=$rnd;
	$re[username]=$cr['username'];
	$re[email]=$cr['email'];
	$re[userfen]=$cr['userfen'];
	$re[money]=$cr['money'];
	$re[groupid]=$cr['groupid'];
	$re[userdate]=$cr['userdate'];
	$re[zgroupid]=$cr['zgroupid'];
	$re[havemsg]=$cr['havemsg'];
	$re[registertime]=$cr['registertime'];
	return $re;
}
function getusernew($userid,$islook){
global $empire,$dbtbpre,$public_r;
//获取动态设置
$line=20; //调用多少条动态
//islook 为设置读取0 自己动态 1 关注的动态 2 自己和关注的动态
//获取动态设置结束
if($islook==1){
		$lookusers=getlookusers($userid);
		if($lookusers){
		$query="select * from {$dbtbpre}movie_new where userid in (".$lookusers.") order by id desc limit $line";
		 }
		else{ return "没有关注任何人"; }
}

elseif($islook==2){
$lookusers=getlookusers($userid);
if($lookusers){ $ls=$lookusers.','.$userid; }
else{ $ls=$userid; }
$query="select * from {$dbtbpre}movie_new where userid in (".$ls.") order by id desc limit $line";
}
else{
$query="select * from {$dbtbpre}movie_new where userid='$userid' order by id desc limit $line";
}
$query=$empire->query($query);
$listtemp;
while($newr=$empire->fetch($query)){
	$uid=$newr[userid];
	$nickname=getnickname($uid);
	$public_r[add_userdiydomain]='/user/i.php?id=';
	$userspace=$public_r[add_userdiydomain].$uid;
$headerimg=getuserpic($uid);	
   $type=$newr[type];
   $nid=$newr[nid];
   $tbname='';
   if($type==1){ $tbname='talk';} elseif($type==2){ $tbname='remind';} elseif($type==3){ $tbname='fava';} elseif($type==4) { $tbname='look';} elseif($type==5) { $tbname='playlog';}
   $newstime=$newr[newstime];
   $newstime=date('Y-m-d H:i:s',$newstime);
   $tr=$empire->fetch1("select * from {$dbtbpre}movie_".$tbname." where id='$nid' and userid='$uid' order by id desc");
	if($tr[id]){
				if($type==1){
					$talktext=$tr[talktext];
					$listtemp.='<li><div class="news-avatar"><a href="'.$userspace.'"><img alt="" src="'.$headerimg.'" /></a></div><div class="news-box"><div class="news-cnt"><div class="news-cnt"><a href="'.$userspace.'">'.$nickname.'</a><span>：</span>'.$talktext.'</div></div><div class="news-ft">'.$newstime.'</div></div></li>';
					}
				elseif($type==2){
					$rid=$tr[movieid];
					$rclassid=$tr[classid];
					$remindr=$empire->fetch1("select title,titleurl from {$dbtbpre}ecms_movie where classid='$rclassid' and id='$rid' limit 1");
					$title=$remindr[title];
					$titleurl=$remindr[titleurl];	
					$listtemp.='<li><div class="news-avatar"><a href="'.$userspace.'"><img alt="" src="'.$headerimg.'" /></a></div><div class="news-box"><div class="news-cnt"><div class="news-cnt"><a href="'.$userspace.'">'.$nickname.'</a><span>订阅了</span><a href="'.$titleurl.'">'.$title.'</a></div></div><div class="news-ft">'.$newstime.'</div></div></li>';
					}	
				elseif($type==3){
					$rid=$tr[movieid];
					$rclassid=$tr[classid];
					$remindr=$empire->fetch1("select title,titleurl from {$dbtbpre}ecms_movie where classid='$rclassid' and id='$rid' limit 1");
					$title=$remindr[title];
					$titleurl=$remindr[titleurl];	
					$listtemp.='<li><div class="news-avatar"><a href="'.$userspace.'"><img alt="" src="'.$headerimg.'" /></a></div><div class="news-box"><div class="news-cnt"><div class="news-cnt"><a href="'.$userspace.'">'.$nickname.'</a><span>收藏了</span><a href="'.$titleurl.'">'.$title.'</a></div></div><div class="news-ft">'.$newstime.'</div></div></li>';
					}	


				elseif($type==4){
					$lid=$tr[lookuserid];
	                $lnickname=getnickname($lid);
					$listtemp.='<li><div class="news-avatar"><a href="'.$userspace.'"><img alt="" src="'.$headerimg.'" /></a></div><div class="news-box"><div class="news-cnt"><div class="news-cnt"><a href="'.$userspace.'">'.$nickname.'</a><span>关注了</span><a href="'.$public_r[add_userdiydomain].$lid.'">'.$lnickname.'</a></div></div><div class="news-ft">'.$newstime.'</div></div></li>';
					}	

				elseif($type==5){
					$rid=$tr[movieid];
					$remindr=$empire->fetch1("select title,titleurl from {$dbtbpre}ecms_movie where id='$rid' limit 1");
					$title=$remindr[title];
					$titleurl=$remindr[titleurl];					
					$listtemp.='<li><div class="news-avatar"><a href="'.$userspace.'"><img alt="" src="'.$headerimg.'" /></a></div><div class="news-box"><div class="news-cnt"><div class="news-cnt"><a href="'.$userspace.'">'.$nickname.'</a><span>观看了</span><a href="'.$titleurl.'">'.$title.'</a></div></div><div class="news-ft">'.$newstime.'</div></div></li>';
					}	
			   }
	
}

	return $listtemp;		

}

function getnickname($uid){
	global $empire,$dbtbpre,$public_r;
	$ur2=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$uid'");
	$nickname=$ur2[nickname];	
	if(!$nickname){
	$ur3=$empire->fetch1("select * from {$dbtbpre}enewsmember where userid='$uid'");	
	$nickname=$ur3[username];
	}
	return $nickname;
}
function getusername($uid){
	global $empire,$dbtbpre,$public_r;
	$ur3=$empire->fetch1("select * from {$dbtbpre}enewsmember where userid='$uid'");	
	$nickname=$ur3[username];
	return $nickname;
}
function getuserinfoadd($uid){
	global $empire,$dbtbpre,$public_r;
	$ur=$empire->fetch1("select * from {$dbtbpre}enewsmemberadd where userid='$uid'");	
	return $ur;
}
function getuserinfo($uid){
	global $empire,$dbtbpre,$public_r;
	$ur=$empire->fetch1("select * from {$dbtbpre}enewsmember where userid='$uid'");	
	return $ur;
}
function getuserpic($uid){
	global $empire,$dbtbpre,$public_r;
	$ur=$empire->fetch1("select userpic from {$dbtbpre}enewsmemberadd where userid='$uid'");	
	$userpic=$ur[userpic];
	if(!$userpic){
	$userpic=$public_r[newsurl].'user/temp/images/avatar.jpg';
	}
	return $userpic;
}
function getlookusers($userid){
	global $empire,$dbtbpre,$public_r;	
	$lookquery=$empire->query("select * from {$dbtbpre}movie_look where userid='$userid'");
	while($lookr=$empire->fetch($lookquery)){
		$lookusers.=$lookr[lookuserid].',';
	}
	$lookusers=substr($lookusers,0,strlen($lookusers)-1);
	return $lookusers;
}
function inserttalk($saytext){
	global $empire,$dbtbpre,$public_r;
	$ur=user_returnlogin();
	if($ur[userid]){
		$saytext=nl2br(RepFieldtextNbsp(RepPostStr($saytext)));
		$userid=$ur[userid];
		$newstime=time();
		
	}else return;
$query="insert into {$dbtbpre}movie_talk (userid,talktext,newstime) values('$userid','$saytext','$newstime')";	
$up=$empire->query($query);

$id=$empire->lastid();
if($up){
	insertnew($id,1,$userid);
   return true;
}
else return;
}
function insertnew($nid,$type,$userid=0){
	global $empire,$dbtbpre,$public_r;
	if(!$userid){
	$ur=user_returnlogin();
	$userid=$ur[userid];
	if(!$userid) return false;
	}
	if(!$type) return false;
	if(!$nid) return false;
	$newstime=time();
	$query="insert into {$dbtbpre}movie_new (userid,type,newstime,nid) values('$userid','$type','$newstime','$nid')";
	$up=$empire->query($query); 
	if($up){ return true; } else  return ;
}
function add_userhistory($name,$url,$title,$movieid,$playid,$userid){
		global $empire,$dbtbpre,$public_r;
		if(!$userid){
		$ur=user_returnlogin();
		$userid=$ur[userid];
		if(!$userid) return false;
		}
	$totalquery="select count(*) as total from {$dbtbpre}movie_playlog where name='$title' and userid='$userid' and movieid='$movieid'";
	$num=$empire->gettotal($totalquery);
	if($num) return false;
	$totalquery2="select count(*) as total from {$dbtbpre}movie_playlog where  userid='$userid' and movieid='$movieid'";
	$num2=$empire->gettotal($totalquery2);
	$newstime=time();
	if($num2){
		$query="update {$dbtbpre}movie_playlog set name='$title',lasturl='$url',newstime='$newstime' where $movieid='$movieid' and userid='$userid'";
	}
	else{
	$query="insert into {$dbtbpre}movie_playlog (movieid,lasturl,userid,name,newstime) values('$movieid','$url','$userid','$title','$newstime')";		
	}
	$up=$empire->query($query);
	$id=$empire->lastid();
	
	if($up){ insertnew($id,5,$userid); return true; }
	else  return false;
}


/*订阅相关*/
function remind_sendmsg($userid,$username,$title,$msgtext){
		global $empire,$public_r,$dbtbpre;
					$from_username='';
					$from_userid='0';
					$issys='1';
					$isadmin='1';	
					$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','$userid','$username','$from_userid','$from_username','$issys','$isadmin','$msgtime')";
					$sm=$empire->query($query);
					if($sm) return true;
}

function getremindset($userid=0){
	global $empire,$public_r,$dbtbpre;
	$userid=(int)$userid;
	$sq="select * from {$dbtbpre}enewsmember where userid='$userid'";
	$ur=$empire->fetch1($sq);
	$return=array();
	$email=$ur[remindemail]?$ur[remindemail]:$ur[email];
	$return[email]=$email;
	$return[sendemail]=$ur[remindsendemail];
	$return[sendmsg]=$ur[remindsendmsg];
	return $return;	
}
?>