<?php
/*
'**************************************************************************************************
' 软件名称: 搜一次 Content Management System
' 版本编号: Version 1.X
' 官方网站: http://www.yixiu.cc
' 官方论坛：http://bbs.yixiu.cc
' 版权所有: 搜一次运营团队
' 郑重声明:
'    1、任何个人或组织不得在未经授权的情况下删除、修改、拷贝本软件及其他副本上一切关于版权的信息；
'    2、搜一次网络事业部保留此软件的法律追究权利
' @author YGH 2012-12-13
'**************************************************************************************************
*/
include "../../admin_loginstate.php";
include "../../../include/conn.php";
include "../../../include/function.php";
include "./collect.function.php";
include "../../admin_version.php";

$action = isset($_REQUEST['action']) && in_array($_REQUEST['action'], array('add','save','collect')) ? $_REQUEST['action'] : NULL;
$page = (isset($_REQUEST['page']) && $_REQUEST['page']=='2') ? 2 : 1;

if (!$action) {
	exit('非法传参');
}

switch ($action) {
	case 'add':
		switch($page){
			case 1:
				$name = inputFilter($_POST['name']);
				$targeturl = inputFilter($_POST['targeturl']);
				$charset = intval($_POST['charset']);
				$player = inputFilter($_POST['player']);
				$collecttype = intval($_POST['collecttype']);
				$collecturl = inputFilter($_POST['collecturl']);
				$type = (isset($_POST['type'])&&is_array($_POST['type'])) ? intval(array_sum($_POST['type'])) : 0;
				$list = $_POST['list'];
				$listhref = $_POST['listhref'];
				$pictype = 0;
				//$pictype = (intval($_POST['pictype'])==1) ? 1 : 0;
				//$pic = ($pictype) ? $_POST['pic'] : '';
				
				if (empty($name) || empty($targeturl) || empty($collecturl)) {
					echo '<script>alert("不能有空的填写框"); window.back(-1);</script>';exit;
				}
				
				if ($collecttype!=4 && !(contentFilter($list) || contentFilter($listhref)) ) {
					//echo '<script>alert("您填写的采集规则无法采集到列表内容"); window.back(-1);</script>';exit;
				}
				
				if ($collecttype==3) {
					$collecturl = str_ireplace("\r\n", ',', trim($collecturl));
				}
				
				//将上述条件合一，存储在缓存中
				$data = compact('name','targeturl','charset','player','collecttype','collecturl','type','list','listhref','pictype');
				
				$sql = 'insert into `s_vodrule` '.array_to_sql($data,'add');
				$db->query($sql);
				$id = $db->insert_id();

				echo '<script>window.location.href="'.('./ruleadd_2.php?id='.$id.'&pictype='.$pictype).'";</script>';
				exit;
			break;
			
			case 2:
				$id = intval($_POST['id']);
				$title = $_POST['title'];
				$state = $_POST['state'];
				$classMode = intval($_POST['classMode'])==1 ? 1 : 0;
				$class = $_POST['class'.($classMode+1)];
				$areaMode = intval($_POST['areaMode'])==1 ? 1 : 0;
				$area = $_POST['area'.($areaMode+1)];
				$languageMode = intval($_POST['languageMode'])==1 ? 1 : 0;
				$language = $_POST['language'.($classMode+1)];
				$direct = $_POST['direct'];
				$author = $_POST['author'];
				$year = $_POST['year'];
				$pic = $_POST['pic'];
				$content = $_POST['content'];
				$playrange = $_POST['playrange'];
				$playmode = $_POST['playmode'];
				$webplayurl = $_POST['webplayurl'];
				$urlmode = intval($_POST['urlMode']);
				$urlreplace = $_POST['urlreplace'];
				$playurl = $_POST['playurl'];
				$contentreplace = $_POST['contentreplace'];
				$data = compact('title','state','class','area','language','direct','author','year','pic','content','playrange','playmode','webplayurl','urlmode','urlreplace','playurl','contentreplace');
				
				$sql = 'update `s_vodrule` SET '.array_to_sql($data,'update').' where id='.$id;
				$db->query($sql);
				if ($db->affected_rows()) {
					echo '<script>window.location.href="./rulelist.php";</script>';
					exit;
				}
			break;
		}		
	break;
	
	case 'save':
		switch($page){
			case 1:
				$id = intval($_POST['id']);
				if (!$id) {
					exit('没有参数');
				}
				$name = inputFilter($_POST['name']);
				$targeturl = inputFilter($_POST['targeturl']);
				$charset = intval($_POST['charset']);
				$player = inputFilter($_POST['player']);
				$collecttype = intval($_POST['collecttype']);
				$collecturl = inputFilter($_POST['collecturl']);
				$type = (isset($_POST['type'])&&is_array($_POST['type'])) ? intval(array_sum($_POST['type'])) : 0;
				$list = $_POST['list'];
				$listhref = $_POST['listhref'];
				$pictype = 0;
				/*$pictype = (intval($_POST['pictype'])==1) ? 1 : 0;
				$pic = ($pictype) ? $_POST['pic'] : '';*/
				if ($collecttype==3) {
					$collecturl = str_ireplace("\r\n", ',', $collecturl);
				}
				$data = compact('name','targeturl','charset','player','collecttype','collecturl','type','list','listhref','pictype');
				$sql = 'update `s_vodrule` SET '.array_to_sql($data,'update').' where id='.$id;
				$db->query($sql);
				echo '<script>window.location.href="'.('./ruleadd_2.php?id='.$id.'&edit='.$id.'&pictype='.$pictype).'";</script>';
				exit;
			break;
			
			case 2:
				$id = intval($_POST['id']);
				$title = $_POST['title'];
				$state = $_POST['state'];
				$classMode = intval($_POST['classMode'])==1 ? 1 : 0;
				$class = $_POST['class'.($classMode+1)];
				$areaMode = intval($_POST['areaMode'])==1 ? 1 : 0;
				$area = $_POST['area'.($areaMode+1)];
				$languageMode = intval($_POST['languageMode'])==1 ? 1 : 0;
				$language = $_POST['language'.($classMode+1)];
				$direct = $_POST['direct'];
				$author = $_POST['author'];
				$year = $_POST['year'];
				$pic = $_POST['pic'];
				$content = $_POST['content'];
				$playrange = $_POST['playrange'];
				$playmode = $_POST['playmode'];
				$webplayurl = $_POST['webplayurl'];
				$urlmode = intval($_POST['urlMode']);
				$urlreplace = $_POST['urlreplace'];
				$playurl = $_POST['playurl'];
				$contentreplace = $_POST['contentreplace'];
				$data = compact('title','state','class','area','language','direct','author','year','pic','content','playrange','playmode','webplayurl','urlmode','urlreplace','playurl','contentreplace');
				
				$sql = 'update `s_vodrule` SET '.array_to_sql($data,'update').' where id='.$id;
				$db->query($sql);
				if ($db->affected_rows()) {
					echo '<script>window.location.href="./rulelist.php";</script>';
					exit;
				}
			break;
		}
	
	break;
	
	case 'collect':
		$id = intval($_POST['id']);
		$pagebegin = isset($_POST['pagebegin']) ? intval($_POST['pagebegin']) : 1;
		$pageend = isset($_POST['pageend']) ? intval($_POST['pageend']) : 1;
		
		if (!$id) {
			exit('非法传参');
		}
		$sql = 'select * from s_vodrule where id='.$id;
		$data = $db->getAll($sql);
		if (!$data) {
			exit('非法传参');
		}
		$data = $data[0];
		if ($data['collecttype']%2==0 && ($pagebegin<1 || $pagebegin>$pageend)) {
			echo '<script>alert("页码填写错误"); window.back(-1);</script>';exit;
		}
		
		//如果没有{$内容}视为使用固定值
		//如果有多个{$内容}，只取第一个，其它的均为变量
		//如果为空，则视为不采集
		//确定过滤都是非贪婪匹配
		//以下就是为规则生成正则匹配式
		//iscurl 是否跳过 ispreg 是否从内容中提取 preg 正则表达式
		$filler = array('list','listhref','title','state','class','direct','author','area','language','year','pic','content','playrange','webplayurl','playurl');
		foreach ($filler as $key) {
			$strpos = strpos($data[$key], '{$内容}');
			if ($strpos!==false) {
				$pregstr = substr($data[$key], 0, $strpos+7).str_replace('{$内容}', '{$过滤}', substr($data[$key], $strpos+7));
				$pregstr = addcslashes($pregstr, '[].*\\/|$+?^=!-\'\"');
				$pregstr = '/'.str_replace(array('{\\$内容}','{\\$过滤}'), '(.*?)', $pregstr).'/is';
				$data[$key] = array('iscurl'=>1, 'ispreg'=>1,'preg'=>$pregstr);
			} elseif (!empty($data[$key])) {
				$data[$key] = array('iscurl'=>1, 'ispreg'=>0,'preg'=>$data[$key]);
			} else {
				$data[$key] = array('iscurl'=>0, 'ispreg'=>0,'preg'=>'');
			}
		}
		if ($data['collecttype']==3) {
			$pageend = substr_count($data['collecturl'],',');
		}
		$data['pagebegin'] = $pagebegin;
		$data['pageend'] = $pageend;
		
		//查询采集绑定，放入缓存中
		$result = $db->query('select S_ID,collectbind from s_class where S_SystemID=1');
		$collectbind = array();
		while ($row = $db->fetch_array($result)) {
			$key = explode(',', $row['collectbind']);
			foreach ($key as $value) {
				$collectbind[$value] = $row['S_ID'];
			}
		}
		$data['collectbind'] = &$collectbind;

		$cacheid = uniqid();
		$cacheFile = '../../../cache/rule_'.$cacheid.'.txt';
		if (file_put_contents($cacheFile, serialize($data))) {
			echo '<script>alert("规则缓存成功，开始采集"); window.location.href="./rulecollect.php?id='.$data['id'].'&cacheid='.$cacheid.'";</script>';exit;
		}else{
			echo 'ERROR';
		}
	break;
}


/**
 * 验证提交是否含有采集规则
 */
function contentFilter($txt){
	if (is_array($txt)) {
		foreach ($txt as $key=>$value) {
			if (!contentFilter($value)) {
				return false;
			}
		}
	} else {
		$start = strpos($txt, '{$内容}');
		if ($start===false) {
			return false;
		}
	}
}

