<?php
require('top.php');
require(R_P.'require/f_gather.php');
require(R_P.'require/f_dealWith.php');
require(R_P.'require/f_channel.php');


InitGP(array('action','g_id','for_page',));
$sql = "select * from ".$db_tablepre."gather WHERE g_id=".$g_id;
$g_rs = sql_return($sql,2);
//exit($db->ErrorMsg());
//var_dump($g_rs );

foreach ($g_rs as $key1 => $value1){
	if (!is_int($key1) && $key1 != 'g_id') {
		$$key1 = stripslashes(unserialize($value1));
	}
}

$_SESSION[$g_id.'gather_num'] || $_SESSION[$g_id.'gather_num'] = $gather_num = 0;//采集计数器

//是否多页采集
if ($g_page) {
	list($start_page,$end_page) = explode('-',$g_p_rules);
	if ($action == 'test') {
		$_SESSION[$g_id.'session_page'] = $session_page = 1;
		$_SESSION[$g_id.'gather_num'] = $gather_num = 0;
	}
	elseif ($_SESSION[$g_id.'session_page']) {
		if ($_SESSION[$g_id.'session_page'] >= $end_page) {
			$_SESSION[$g_id.'session_page'] = $session_page = 1;
			$_SESSION[$g_id.'gather_num'] = $gather_num = 0;
		}else $session_page = $_SESSION[$g_id.'session_page'];
	}else $session_page = $start_page;
	strpos($g_url,'{DATA}') ? $g_url_rep = str_replace('{DATA}',$session_page,$g_url) : Showmsg('g_page_2');
}else $g_url_rep = $g_url;

if ($for_page) {
	$start_readnum = $for_page;
}else {
	$content = open($g_url_rep);
	$content  || Showmsg('g_err_1');
	$_SESSION[$g_id.'linkCount'] = $linkCount = getLinks($g_list,$content, $g_url_chk);
	$start_readnum = 0;       //设置開始讀取所要的条数
}

$url_array = readUrl($start_readnum);
print $url_array[0]  ;


foreach ($url_array as $url){


	if ($action == 'start') {
		$sql_c_chk = "SELECT r_url FROM ".$db_tablepre."gather_record  WHERE r_url='".$url."'";
		$g_chk  &&  $rs = sql_return($sql_c_chk,3);
		if ($rs){
			$oldgather = 'yes';
			continue;
		}
	}

	//exit;

	//截取页面内容;
	$content = open($url);
	$sql_1 = '';    $sql_2 = '';


	$gather_arr_type = array('g_title','g_content','g_source');

	foreach ($gather_arr_type as  $value){


		$Data = getData($$value,$content);


		$value_content = str_replace('g_','content_',$value);
		$Data = dealWith($value_content,$Data);//对数值变量进行处理;
		if (strpos($Data,'|')){
			$Data_img = explode('|',$Data);
			$sql_1 .= "content_img,content_img_url,";
			$sql_2 .= "1,'".$Data_img[1]."',";
			//content_img/content_img_url
			$Data = $Data_img[0];
		}

		//累加SQL语句START;
		$sql_1 .= $value_content.",";
		$sql_2 .= "'".$Data."',";
		//累加SQL语句END;

		//test输出;
		$Data_arr["$value_content"] =  $Data;

		if ($value_content == 'content_title' && strlen($Data) <= 2) $empty_gather = 'yes';
	}

	if ($empty_gather == 'yes') continue;




	//如不是test,采集入库 ;
	if ($action == 'start') {
		channelChk($g_channel);

		$sql = "SELECT BC_Tschannel FROM ".$db_tablepre."channel WHERE BC_id=".$g_channel;
		$result = sql_return($sql,2);

		$sql_1 .= 'content_uid,content_channel,content_bc_id,content_uptimes ';
		$sql_2 .= '1,'.$g_channel.', '.$result['BC_Tschannel'].','.$timestamp;

		$sql = "INSERT INTO ".$db_tablepre."content (".$sql_1.") VALUES(".$sql_2.")" ;
		$query = sql_return($sql);

		$sql2 = "INSERT INTO ".$db_tablepre."gather_record (r_url,r_up_id,r_g_id,r_uptimes) VALUES('".$url."',".sql_return($query,'Insert_ID').",".$g_id.",'".$timestamp."')";
		$query && sql_return($sql2) && $add = 'yes';

	}else {
		$output_test = '' ;
		foreach ($Data_arr as $ks => $vs){
			$output_test .=  $ks.':'.$vs.'<br>';
		}

	}


	//采集计数器++
	$add == 'yes' ? $gather_num = $_SESSION[$t.$g_id.'gather_num']++ : $_SESSION[$t.$g_id.'gather_num'] = $gather_num = 0;
}









//采集成功增加页数	加一
if (!$for_page) {
	$url_to_page_s = $self_template.'.php?action='.$action.'&g_id='.$g_id.'&for_page=1';
}
elseif ($for_page < $_SESSION[$g_id.'linkCount']) {
	$for_page++;
	$url_to_page_s = $self_template.'.php?action='.$action.'&g_id='.$g_id.'&for_page='.$for_page;
}


require PrintEot($self_template,'A');

?>