<?php
if(!defined('MOD'))  die("access die");
$works = $_GET['works'];
$rules = intval($_GET['rules']);
$step = intval($_POST['step']);
$param = $_POST['param'];

$param['filter'] = @implode( ',', $param['filter']);
$param['cookie'] = trim($param['cookie']);

$turn_url = "system.php?sys=collect&channelid={$channelid}";

$field = array(
	array('name','cid','pic','charset','cookie','auto_filename'),
	array('index_type','onepage','manypage','oneurl','url_start','url_end','link_rule','link_start','link_with','jclinks'),
	array('body_rule','author','source','pubtime','filter', 'filter_text', 'len','formss', 'form_code', 'form_next')
);

foreach($param as $key=>$val){
	if(!in_array($key, $field[$step])){
		unset($param[$key]);
	}
}

if(empty($param)) qbase::showmsg( "数据提交错误" );
if(isset($param['index_type']) && empty( $param['index_type'] )) qbase::showmsg( "请选择分页规则" );
if(isset($param['form_code']) && isset($param['form_next']) && empty( $param['form_code']) && empty( $param['form_next'] )) unset( $param['formss'] );
if(isset($param['index_type'])){
	switch ( $param['index_type'] )
	{
		case 1:
			$param['onepage'] = trim($param['onepage']);
			if(strlen($param['onepage']) < 7) qbase::showmsg( "需要填写一个网页地址" );
		break;

		case 2:
			$param['manypage'] = trim($param['manypage']);
			if(strlen($param['manypage']) < 7) qbase::showmsg( "需要填写网页连接,一行一个地址" );
		break;

		case 3:
			$param['oneurl'] = trim($param['oneurl']);
			if(strlen($param['onepage']) < 7) qbase::showmsg( "需要填写采集地址" );
			$param['url_end'] = intval($param['url_end']);
			$param['url_start'] = intval($param['url_start']);
			if ($param['url_end']  < 1) qbase::showmsg( "请输入翻页的有效范围" );
		break;
	}
}

if ( $works == "edit" ){
	$sql = qbase::createSql("".TABLEPRE."collect_rule", $param , 'update') . " where `id` = '{$rules}' ";
	qdb::Query( $sql );
	$turn_text = '修改规则成功！！！';
}else{
	$param['channelid'] = $channelid;
	$sql = qbase::createSql("".TABLEPRE."collect_rule", $param , 'insert') ;
	$rules = qdb::Query( $sql , true);
	$turn_text = '添加规则成功！！！';
}
$url = "system.php?sys=collect&work=edit_rules&rules={$rules}&channelid={$channelid}&step=";
cjMsg();
function cjMsg(){
	global $step, $turn_text, $turn_url,$url, $rules;
	if($step == 2){
		qdb::query("update ".TABLEPRE."collect_rule set status = 1 where id = '{$rules}' ");
		$result = array('status'=>200, 'data'=>$turn_url);
	}else{
		$step++;
		$result = array('status'=>200, 'data'=>$url . $step);
	}
	echo qbase::xmlPrint(qbase::jsonencode($result));
}
?>