<?php
 require ('../e/class/connect.php');
 require ('../e/class/db_sql.php');
//初始化插件设置

$userhistory=0;  //是否只能注册用户使用观看记录，1为是，0为不限制
$scorecookie=1; //是否限制评分

//初始化插件设置
$link=db_connect();
$empire=new mysqlquery;
$add=$_GET;
$callback=$add[callback];
$movieid=(int)$add[movieid];
$playid=(int)$add[playid];
$name=strip_tags($add[name]);
$title=strip_tags($add[title]);
$url=urlencode($add[url]);
$playurl=urlencode($add[playurl]);
$score=(int)$add[score];
$id=(int)$add[id];
$uid=(int)getcvar('mluserid');
$f_exp='||';
$r_exp='::::::';

$action=$add[action];
if($action=='addhistory'){            //name||url||title::::::
	
	$cookie=getcvar('moviehistory');
	if(!$name||!$url||!$title){  echo $callback.'({"isok":"0"})'; exit();}
	if(!$cookie){
			$setcookie=$name.$f_exp.$url.$f_exp.$title;
	}
	else{	
	$setcookie=$name.$f_exp.$url.$f_exp.$title;
		$cr=explode($r_exp,$cookie);
		$cookiestr='';
		for($i=0;$i<count($cr);$i++){
				$r=explode($f_exp,$cr[$i]);
				$rname=$r[0];  $rurl=$r[1]; $rtitle=$r[2];
				if($name==$rname){
						$rname=''; $rurl=''; $rtitle='';
						
				}else{
				$cookiestr.=$r_exp.$rname.$f_exp.$rurl.$f_exp.$rtitle;
				}
	
		}
		$setcookie=$setcookie.$cookiestr;
	}
	$set=esetcookie('moviehistory',$setcookie);


	if($uid){
		require ("../e/member/class/user.php");
		require ("../user/class/fun.php");
		$set=add_userhistory($name,$playurl,$title,$movieid,$playid,$uid);
	}
	   
	if($set){
		echo $callback.'({"isok":"1"})';
	}
	
	else{
	   echo $callback.'({"isok":"0"})';
	}	
	
}

if($action=='gethistory'){
$cookie=getcvar('moviehistory');
$strhead='<ul class=\"ulHistoryList  clearfix\">';
$list="<li onmouseover=\\\"this.className=\'liHover\'\\\" onmouseout=\"this.className=\'\'\\\"><a target=_blank class=name href=\\\"[!--url--]\\\" title=\\\"[!--name--]\\\"><span class=\\\"sName\\\">[!--name--]</span><span class=\\\"sTime\\\">[!--title--]</span></a></li>";
$strend='</ul>';
		if($uid&&!$cookie){
			$sr="select * from {$dbtbpre}user_history where userid='$uid' order by newstime desc limit 5";
			$query=$empire->qeury($sr);
			$strmodel='';
			while($r=$empire->fetch($query)){
					$text=$list;
					$text=str_replace('[!--name--]',$r[moviename],$text);
					$text=str_replace('[!--title--]',$r[movietitle],$text);
					$text=str_replace('[!--url--]',$r[url],$text);
					$strmodel.=$text;
			}
			if($strmodel){
			$str=$strhead.$strmodel.$strend;
			}
			if($str){
			 echo $callback."({'isok':'1','history':'".$str."'})";
			 exit();
			}
			else{
			$str=$strhead.'<li><a>当前没有观看历史</a></li>'.$strend;	
			 echo $callback."({'nohistory':'1','isok':'1','history':'".$str."'})";
			 exit();
			}
		}
		
		if($userhistory==1)
		{
			
			$text=$strhead.'<li><a>只有网站注册用户才能使用</a></li>'.$strend;
			 echo $callback."({'nohistory':'1','isok':'1','history':'".$text."'})";
			
		}
		else{
			$cookie=unescape($cookie);
			$cookie=urldecode($cookie);
			$rr=explode($r_exp,$cookie);
			$strmodel='';
			for($i=0;$i<count($rr);$i++){
					$r=explode($f_exp,$rr[$i]);
					$moviename=$r[0];
					$movieurl=$r[1];
					$movietitle=$r[2];
					if($moviename&&$movietitle&&$movieurl){
					
							$text=$list;
					$text=str_replace('[!--name--]',$moviename,$text);
					$text=str_replace('[!--title--]',$movietitle,$text);
					$text=str_replace('[!--url--]',$movieurl,$text);	
					$strmodel.=$text;	
					
					}
					
			}
			
			if($strmodel){
			$str=$strhead.$strmodel.$strend;
			}
			if($str){
			 echo $callback."({'isok':'1','history':'".$str."'})";
			 exit();
			}
			else{
			$str=$strhead.'<li><a>当前没有观看历史</a></li>'.$strend;
			 echo $callback."({'nohistory':'1','isok':'1','history':\"".$str."\"})";
			 exit();
			}			
		}



}

	
if($action=='delhistory'){
	
	esetcookie('moviehistory','');
	echo $callback."({'nohistory':'1','isok':'1','history':\"".$str."\"})";

}
	
	
	
if($action=='score'){
	if($scorecookie==1){
	$c=getcvar('moviescore_'.$id);
	if($c){
	echo '2'; db_close(); exit();
		}
	}
	$query="select userstar,userstar1,userstar2,userstar3,userstar4,userstar5 from {$dbtbpre}ecms_movie where id='$id'";
	$r=$empire->fetch1($query);
	$addf='userstar'.$score;
	$r[$addf]=$r[$addf]+1;
	$zong=($r[userstar1]*1+$r[userstar2]*2+$r[userstar3]*3+$r[userstar4]*4+$r[userstar5]*5)*2;
	$k=$r[userstar1]+$r[userstar2]+$r[userstar3]+$r[userstar4]+$r[userstar5];
	$ping=round($zong/$k,1);
	$upquery="update {$dbtbpre}ecms_movie set userstar='$ping',".$addf."='$r[$addf]' where id='$id' ";
	$up=$empire->query($upquery);
	if($up){ echo '1'; 
	esetcookie('moviescore_'.$id,1,time()+3600);
	}
	else{  echo '2';  }
	
}
	
if($action=='videoscore'){
	
	$query="select userstar,userstar1,userstar2,userstar3,userstar4,userstar5 from {$dbtbpre}ecms_movie where id='$id'";
	$r=$empire->fetch1($query);
	$zong=($r[userstar1]*1+$r[userstar2]*2+$r[userstar3]*3+$r[userstar4]*4+$r[userstar5]*5)*2;
	$k=$r[userstar1]+$r[userstar2]+$r[userstar3]+$r[userstar4]+$r[userstar5];
	if($k!=0){
	$ping=round($zong/$k,1);
	}
	else{
		$ping='10.0';
	}
	if(substr_count($ping,'.')<1){
	$ping.='.0';
	}
	echo '{"s":"'.$ping.'","z":"'.$zong.'","r":"'.$k.'"}';
}
	
	


//用户增加收藏
function add_fav($userid,$movieid){
	
}





	
/////////////////////////////其他函数////////////////////////////
function unescape($str) {
  $str = rawurldecode($str);
  preg_match_all("/(?:%u.{4})|&#x.{4};|&#\d+;|.+/U",$str,$r);
  $ar = $r[0];

  foreach($ar as $k=>$v) {
    if(substr($v,0,2) == '%u'){
      $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,-4)));
	  if($ar[$k]=='')
	  {
		  $ar[$k]=substr($v,-6);
	  }
	}
    elseif(substr($v,0,3) == "&#x")
      $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
    elseif(substr($v,0,2) == "&#") {
      $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
    }
  }
  return join("",$ar);
}







db_close();
$empire=NULL;
?>