<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.syccms.com
' 官方论坛：http://bbs.syccms.com
' 版权所有: 搜一次运营团队
' 法律顾问: 杭州市元茂律师事务所 严飞律师
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
'	 3、本插件建立在您对目标站具有足够信任的基础上，采用原生态代码
'**************************************************************************************************
*/
header("Content-Type:text/html;charset=gbk");
define("CJNAME","bdzyz");//命名
define("PLAYBOX","bdhd");//播放器格式
define("CODING","UTF8");//目标站编码，简体是GBK，国际是UTF8
define("MARXMODE",'1');//马克斯模式，原来的如果是马克斯接口，设置为1，否则为0
define("URLAPI","http://www.bdzyz.com/xml/caiji.asp");//采集接口

require_once "../../../admin_loginstate.php";
require_once '../function.php';
require_once "../../../../include/conn.php";
require_once '../page.class.php';
set_time_limit(9999);

$arrServer = array('1'=>'动漫','2'=>'动作','3'=>'喜剧','4'=>'爱情','5'=>'科幻','6'=>'剧情','7'=>'恐怖','8'=>'战争','9'=>'纪录','10'=>'大陆剧','11'=>'欧美剧','12'=>'香港剧','13'=>'韩国剧','14'=>'综艺','41'=>'体育','42'=>'海外剧','43'=>'台湾剧','44'=>'日本剧');
$arrBind = array('1'=>'','2'=>'','3'=>'','4'=>'','5'=>'','6'=>'','7'=>'','8'=>'','9'=>'','10'=>'','11'=>'','12'=>'','13'=>'','14'=>'','41'=>'','42'=>'','43'=>'','44'=>'');

if(empty($_GET)){//防止出错
	header("location:?ac=list&pg=1");
}

if(isset($_POST['ids']) && !empty($_POST['ids']) && isset($_GET['vodids'])){//指定ID采集
	$ids=implode(',',$_POST['ids']);
	header("location:?ac=videolist&ids=".$ids."&edit=add");
}

$isclassify=0;
if(!file_exists(CJNAME."_class.php")){
	$isclassify=1;//如果不存在分类，则定义一个变量
}else{
	require_once ''.CJNAME.'_class.php';
}

$xml=''.URLAPI.'?ac=list&pg=1&h=&t=';
$tid=$_SERVER['QUERY_STRING'];
if(!empty($tid)){
	$xml=''.URLAPI.'?'.$tid.'';
}
if(isset($_GET['edit']) && $_GET['edit']=='key' && isset($_POST['keyword'])){
	$keyword=convert_encoding($_POST['keyword'],'UTF8','GBK');
	$keyword=urlencode($keyword);
	$xml=''.URLAPI.'?ac=list&wd='.$keyword.'';
}

$rss = simplexml_load_file($xml);
$nums=$rss->xpath("/rss/list");
$lists=$rss->xpath("/rss/list/video");

$urlArr=array();//初始化
parse_str($tid,$urlArr);
$urlRecordCount	= (int)$nums[0]['recordcount'];//当前共多少条数据
$urlPageIndex	= (int)$nums[0]['page'];//当前在第几页
$urlPageCount	= (int)$nums[0]['pagecount'];//当前共多少页

/*
 * 处理入库
 * @ S_Name		 xml获取到的影片名命名
 * @ S_VData	 xml获取到的影片地址
 *
 * @ S_SourceArr 数据库播放来源格式化成数组
 * @ S_SourceKey 播放格式在S_SourceArr所在的排位
 * @ S_VDataArr  数据库播放地址格式化成数组
 * @ S_New_Source	新的播放来源入库字符串
 * @ S_New_VData	新的播放地址入库字符串
 */

if(isset($_GET['edit']) && $_GET['edit']=='add' && $_GET['ac']=='videolist'){
	
	if($isclassify==1){
		message('你别坑我你还没有绑定分类呢');
	}
	
	
	if(!empty($urlArr['t']) || isset($urlArr['pg'])){
		if(empty($arrBind[$_GET['t']])){
			message('当前分类未获得绑定信息');
		}		
		echo '您正在采集 <font color="#FF0000">'.$arrServer[$urlArr["t"]].'</font> ，第 <font color="#FF0000">'.$urlArr['pg'].'</font> 页已经采集完成，进入下一页。。。。';
		echo '<br />';
	}
	
	foreach($lists as $list){
		$S_Name =magic_quotes(convert_encoding($list->name));
		$S_Pic	=$list->pic;
		$S_ClassID =$arrBind[''.$list->tid.''];
			if(empty($S_ClassID)){
				echo $S_Name.' 采集失败，没有获取到分类绑定消息。。。<br />';
				continue;
			}
		$S_Direct = convert_encoding($list->director);
		$S_Author = convert_encoding($list->actor);
		$S_Year = $list->year;
		$S_Area = convert_encoding($list->area);
		$S_Language = iconv_substr($S_Area,0,4);
		$S_Source = PLAYBOX;
		$S_VData=magic_quotes(convert_encoding($list->dl[0]->dd[0]));
			if(MARXMODE==1){
				$S_VData=substr($S_VData,0,-5);
				$S_VData=str_replace('$bdhd#','#',$S_VData);
			}
		$S_Remark =convert_encoding($list->state);
			if(!$S_Remark){
				$S_Remark='完结';
			}
		$S_Content=magic_quotes(convert_encoding($list->des));
		
		
		//查询是否存在同名数据
		$sql="SELECT S_ID,S_Source,S_VData  FROM s_video WHERE 1=1 AND S_Name='".$S_Name."'";
		$result=@mysql_query($sql) or die('错误'.__LINE__);
		
		if(mysql_num_rows($result)>0){
			$row=@mysql_fetch_assoc($result);
			
			$S_SourceArr=array();
			$S_VDataArr =array();
			$S_SourceArr=explode('$$',$row['S_Source']);
			$S_VDataArr=explode('$$$',$row['S_VData']);
			
			$S_SourceKey=array_search(PLAYBOX,$S_SourceArr);

			if(false !== $S_SourceKey){//是否存在指定的播放源，如果为真
				if($S_VDataArr[$S_SourceKey]==$S_VData){//如果资源地址一模一样，则跳过
					echo $S_Name.' 采集失败，这个资源没有更新。。。<br />';
					continue;
				}
				$S_VDataArr[$S_SourceKey]=$S_VData;
				ksort($S_VDataArr);
			}else{
				array_push($S_SourceArr,PLAYBOX);
				array_push($S_VDataArr,$S_VData);
			}
			$S_New_Source=implode('$$',$S_SourceArr);
			$S_New_VData =implode('$$$',$S_VDataArr);
			
			$sql=" UPDATE s_video SET S_Remark = '".$S_Remark."',S_AddTime = '".date("Y-m-d H:i:s")."',S_Source = '".$S_New_Source."',S_VData = '".$S_New_VData."' WHERE S_ID = '".$row['S_ID']."' ";
			@mysql_query($sql) or die('错误'.__LINE__);
			echo $S_Name.' 采集成功，已经更新指定数据。。。<br />';

		}else{
			$sql="";
			$sql.=" INSERT INTO s_video ( ";
			$sql.=" S_Name,S_Pic,S_ClassID,S_Direct,S_Author,S_Year,S_Area,S_Language,S_Source,S_VData,S_Remark,S_AddTime,S_Content";
			$sql.=" ) VALUES ";
			$sql.=" ('".$S_Name."','".$S_Pic."','".$S_ClassID."','".$S_Direct."','".$S_Author."','".$S_Year."','".$S_Area."','".$S_Language."','".$S_Source."','".$S_VData."','".$S_Remark."','".date("Y-m-d H:i:s")."','".$S_Content."') ";
			@mysql_query($sql) or die('错误'.__LINE__);
			echo $S_Name.' 采集成功，已经添加指定数据，继续采集下一条数据。。。<br />';
		}		
	}
	
	if(!empty($_GET['h']) || isset($_GET['ids'])){//如果采集当天或者采集指定
		message('采集完成');
	}
	
	if($urlArr['pg']<$urlPageCount){//如果当前页小于总页数，跳转到下一页
		$urlArr['pg']++;
	}
	if($urlArr['pg']>=$urlPageCount){//如果当前页就是最后页，提示完成
		message('采集完成');
	}
		
	if(!empty($urlArr['t']) || !isset($urlArr['pg'])){
		echo '您正在采集 <font color="#FF0000">'.$arrServer[$urlArr["t"]].'</font> ，第 <font color="#FF0000">'.$urlArr['pg'].'</font> 页已经采集完成，进入下一页。。。。';
		echo '<br />';
	}
	echo '<script type="text/javascript">location.href="?ac=videolist&pg='.$urlArr['pg'].'&h=&t='.$urlArr['t'].'&edit=add"</script>';
	
	exit();
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>搜一次资源库采集平台</title>
<link rel="stylesheet" type="text/css" href="../../../images/style.css"/>
<link rel="stylesheet" type="text/css" href="../../../images/core.css"/>
<style type="text/css">
table{
	border-collapse:collapse;
}
</style>
<script language="javascript" src="../../../js/jquery.js"></script>
<script language="javascript" src="../../../js/popup_layer.js"></script>
<script language="javascript">
$(function(){
	new PopupLayer({trigger:"#ele1",popupBlk:"#blk1",closeBtn:"#close1"});
	$('.checkboxall').click(function(){
		$('.checkboxtool').attr("checked",false);
		$("input[name='ids[]']").attr("checked",true);
	})
	$('.checkboxtool').click(function(){
		$('.checkboxall').attr("checked",false);
		$("input[name='ids[]']").each(function(){
			this.checked=!this.checked;
		})
	})
})	
</script>

</head>

<body>
<div>这是搜一次-板板开发的一个坑爹的采集平台，如果您没有这个播放器，一定不要采集，到时候伤心的是你，不是我</div>
<div>你千万要给我777权限，不然我怎么给你绑定分类，说到这里，偷偷告诉你：没有绑定分类的数据不会被采集哦</div>
<div>&nbsp;</div>
<div class="classdata">
<a href="<?php echo basename($_SERVER['PHP_SELF']); ?>">首页</a>
<?php foreach($arrServer as $key=>$value){ ?>
<a href="?ac=list&pg=1&h=&t=<?php echo $key; ?>"><?php echo $value;?></a>
<?php }?>
</div>
<?php $pageno=new Page($urlRecordCount,30,10,'pg');?>
<form method="post" action="">
<button type="button" onclick="javascript:this.form.action='?<?php echo $tid; ?>&edit=add&vodids=1';this.form.submit();">采集选中</button>
<button type="button" onclick="javascript:this.form.action='?ac=videolist&h=24&t=&edit=add';this.form.submit();">一键采集当天</button>
<button type="button" onclick="javascript:this.form.action='?ac=videolist&h=&t=<?php if(!empty($_GET['t'])){echo $_GET['t'];}?>&pg=1&edit=add';this.form.submit();" <?php if(empty($_GET['t'])){echo ' disabled="disabled"';} ?>>一键采集当前分类</button>
&nbsp;&nbsp;<button type="button" id="ele1" class="tigger" style="cursor:pointer;">分类绑定</button>
&nbsp;&nbsp;<input type="text" name="keyword" value="" size="10" /><button type="button" onclick="this.form.action='?edit=key';this.form.submit();">搜索</button>
<table border="1">
	<tr class="trPage">
		<td width="60"><span class="checkboxall">全选</span>&nbsp;<span class="checkboxtool">反选</span></td>
		<td colspan="4" class="pageno"><?php echo $pageno->showtool(); ?></td>
	</tr>
	<tr>
		<td><input type="checkbox" class="checkboxall" />&nbsp;<input type="checkbox" class="checkboxtool" /></td>
		<td>影片名</td>
		<!--<td>更新状态</td>-->
		<td>所属分类</td>
		<td>播放来源</td>
		<td>更新时间</td>
	</tr>
<?php
$i=1;
foreach($lists as $list){
?>
	<tr>
		<td><input type="checkbox" name="ids[]" value="<?php echo $list->id; ?>" />&nbsp;&nbsp;<?php echo $i++; ?></td>
		<td><?php echo convert_encoding($list->name); ?></td>
		<!--<td>&nbsp;</td>-->
		<td><?php echo convert_encoding($list->type); ?></td>
		<td><?php echo PLAYBOX; ?></td>
		<td><?php if(date("Y-m-d")==date("Y-m-d",strtotime($list->last))){echo '<font color="#FF0000">'.$list->last.'</font>';}else{echo $list->last;} ?></td>
	</tr>
<?php 
}
?>
	<tr>
		<td width="60"><span class="checkboxall">全选</span>&nbsp;<span class="checkboxtool">反选</span></td>
		<td colspan="4" class="pageno"><?php echo $pageno->showtool(); ?></td>
	</tr>
</table>
</form>

<?php 
$sql="SELECT S_ID,S_Name FROM s_class WHERE 1=1 AND S_SystemID='1' AND S_IsHide='0'";
$result=$db->query($sql);
$arrLocal=array();
if(!empty($result)){
	while($row=$db->fetch_array($result)){
		$arrLocal[$row['S_ID']]=$row['S_Name'];
	}
}

if(isset($_GET['edit']) && $_GET['edit']=='class'){
		
	foreach($arrServer as $key=>$value){
		$arrBind2[$key]=$_POST['change'.$key];
	}
	writetocache(CJNAME.'_class',getcachevars(array('arrBind'=>$arrBind2)),'./');
	
	message('设置成功');
}
?>
        <div id="blk1" class="blk" style="display:none;">
            <div class="main">
                <h2>分类绑定</h2>
                <a href="javascript:void(0)" id="close1" class="closeBtn">关闭</a>
				<form method="post" action="?ac=list&edit=class">
                <ul>
				<?php 
				foreach($arrServer as $key=>$value){ $list=$arrBind[$key];?>
                    <li><?php echo $value; ?>&nbsp;=>&nbsp;<select name="change<?php echo $key; ?>">
						<option value="">未选择</option><?php foreach($arrLocal as $keys=>$values){ ?>
						<option value="<?php echo $keys; ?>"<?php if($list==$keys){echo 'selected="selected"';}?>><?php echo $values ?></option><?php echo "\r\n"; }?>
					</select></li>
				<?php   } ?>
                </ul>
				&nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" name="submit">提交</button><br /><br />
				</form>
            </div>
        </div>
</body>
</html>