<?php
$rules  = $_GET['rules'];
$pageno = $_GET['pageno'];
$fetch_type = intval($_GET['fetch_type']);
if ( empty( $_GET['pageno'] )) $pageno = 1;
else $pageno = intval($_GET['pageno']);
$pages  = intval( $_GET['threadGet']);
$total  = 0;
$string = new String();
$sql    = "SELECT * FROM ".TABLEPRE."collect_rule WHERE id = '$rules'";
$row    = qdb::QueryFirst( $sql );
$links_rule = $string->pregLinks( $row['link_rule'] );
$url = array();
switch ( $row['index_type'] )
{
	case 1:
		$url[] = $row['onepage'];
		$total = 1;
		break;

	case 2:
		$manypage = explode( "\n", $row['manypage'] );
		$url = $manypage;
		if($row['onepage']) array_unshift($url, $row['onepage']);
		$url = array_unique($url);
		$total = count($url);
		
		break;
	case 3:
		$rowurl = explode( '[分页]', $row['oneurl'] );
		if (!is_numeric( $row['url_start'])) $row['url_start'] = 1;
		$row['url_start'] = intval($row['url_start']);
		$row['url_end'] = intval($row['url_end']);
		if($row['url_end'] >= $row['url_start']){
			for ( $a = $row['url_start']; $a <= $row['url_end']; $a++ ){
				$url[] = $rowurl[0] . $a . $rowurl[1];
			}
			if($row['onepage']) array_unshift($url, $row['onepage']);
		}else{
			for ( $a = $row['url_end']; $a <= $row['url_start']; $a++ ){
				$url[] = $rowurl[0] . $a . $rowurl[1];
			}
			if($row['onepage']) array_push($url, $row['onepage']);
		}
		$url = array_unique($url);
		$total = count($url);
		if($fetch_type) $url = array($row['onepage']);
}

if($pageno > $total){
	qdb::Query( "OPTIMIZE TABLE `links`" );
	$data = array( 'status'=>1, 'pageno'=>$total, 'total'=>$total, data=>array(), over=>1);
	ob_clean();
	echo qbase::xmlPrint(qbase::jsonencode( $data));
	exit();
}

$link = trim( $url[$pageno - 1] );
if ( $row['cookie'] != "" and $acookis != 1 ){
	if ( preg_match( "/\n/", $row['cookie'] )){
		$row['cookie'] = $string->fileFsock( $link, $row['cookie'] );
		$acookis = 1;
	}
}
$info = array();
$string->getUrl( $link);
$handles = qbase::load( $link, 0, '',  $row['cookie']);
$matches = array();
$time = $_SERVER['REQUEST_TIME'];
preg_match_all( "/".$links_rule."/ies", $handles, $matches );
for ( $i = 0; $i < count( $matches[1] ); $i++ ){
	if (  ! empty( $row['jclinks'] )){
		if ( $row['index_type'] == 3 ){
			$links1 = str_replace( '[分页号码]', $a, $row['jclinks'] );
		}
		$rowjclinks = explode( '[替换链接]', $row['jclinks'] );
		$matches[1][$i] = $rowjclinks[0].$matches[1][$i].$rowjclinks[1];
	}
	$url2 = $string->gethttp( $matches[1][$i] );
	$row['link_with'] = trim($row['link_with']);
	if($row['link_with']){
		if($row['link_start']){
			//不含有
			if(strpos($url2, $row['link_start']) !== false) continue;
		}else{
			//含有
			if(strpos($url2, $row['link_start']) === false) continue;
		}
	}
	$sql3 = "SELECT count(*) FROM ".TABLEPRE."collect_links WHERE `rules` ='$rules' AND `url` = '$url2' limit 1";
	$result_url = qdb::QueryAlone( $sql3 );
	$info[]  = array(strip_tags($matches[2][$i]), $url2);
	if ( $result_url  == 0 ){
		$title   = strip_tags($matches[2][$i]);
		$iif = qdb :: QueryAlone("select count(*) from  " . TABLEPRE . "article_{$row['channelid']} where cid='{$row['cid']}' and subject='" . $title. "' limit 1");
		if($iif) continue;
		$sql2 ="INSERT INTO ".TABLEPRE."collect_links (  `title` , `url` , `rules` , `date` ) VALUES ('$title', '$url2', '$row[id]', '$time')";
		qdb::Query( $sql2 );
	}
}


if( !empty( $info)){
	$data = array( 'status'=>1, 'pageno'=>$over, 'total'=>$total, 'data'=>$info, 'over'=>0);
}else{
	$data = array( 'status'=>4, 'total'=>$total, 'over'=>0, 'link'=>$link, 'handles'=>$handles );
}
if ( $end == 1 )
{//采集完毕
	$data = array( 'status'=>0, 'total'=>$total, 'over'=>1, 'data'=>$info);
}
ob_clean();
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
echo qbase::xmlPrint(qbase::jsonencode( $data));
?>
