<?php
if(!defined('MOD'))  die("access die");
$rules = intval($_GET['rules']);
$urls =  urldecode($_GET['ceshitqurl']);
$sql = "SELECT * FROM ".TABLEPRE."collect_rule WHERE id = '$rules'";
$row_rule = qdb::QueryFirst( $sql );

$string = new String();
$body_rule = $string->pregBody( $row_rule['body_rule'] );
$author = $string->pregAuthor( $row_rule['author'] );
$source    = $string->pregSource( $row_rule['source'] );
$pubtime    = $string->pregPubtime( $row_rule['pubtime'] );

$temp = parse_url($urls);
$host_url = $temp['scheme'] .'://'. $temp['host'] . $temp['path'] . '/';

if ($row_rule['cookie'] != "" and $acookis != 1) {
	if (preg_match("/\n/", $row_rule['cookie'])) {
		$row_rule['cookie'] = $string -> fileFsock($row['url'], $row_rule['cookie']);
		$acookis = 1;
	} 
} 
$cont = '';
$buffer = qbase :: load($urls , 0, '', $row_rule['cookie']);

if ( !empty( $author )){
	preg_match( "/".$author."/is", $buffer, $regs_author );
}
if (  ! empty( $source )){
	preg_match( "/".$source."/is", $buffer, $regs_source );
}
if (  ! empty( $pubtime )){
	preg_match( "/".$pubtime."/is", $buffer, $regs_pubtime );
}
preg_match( "/".$body_rule."/is", $buffer, $regs );

$cont = HtmlGo(textFilterClean($regs[1], $row_rule['filter_text'] ), $row_rule['filter']);

/*记录已经获取的地址*/
$links_map = array($urls);

if($row_rule['formss'] == 1 && $row_rule['form_code']){
	$exp_rule= $string -> pregPage($row_rule['form_code']);
	preg_match("/" . $exp_rule . "/is", $buffer, $page_links_contents); 
	$page_links = qbase::get_object('links')->get_all_links($page_links_contents[1]);
	$page_links = array_unique($page_links);
	$run_index = 0; // 最多采集30页就跳出
	while(!empty($page_links) && $run_index < 30){
		$link_val = array_shift($page_links);
		$link_val = formatlink($link_val, $host_url);
		if(in_array($link_val , $links_map)) continue;
		$links_map[] = $link_val;
		$buffer_next = qbase :: load($link_val , 0, '', $row_rule['cookie']);
		preg_match("/" . $body_rule . "/is", $buffer_next, $regs_next);
		$cont .= HtmlGo(textFilterClean($regs_next[1], $row_rule['filter_text'] ), $row_rule['filter']);

		preg_match("/" . $exp_rule . "/is", $buffer_next, $page_links_content); 
		$page_links_next = qbase::get_object('links')->get_all_links($page_links_contents[1]);
		$page_links_next = array_unique($page_links_next);

		foreach($page_links_next as $val){
			if(in_array($val, $links_map) || in_array($val, $page_links)) continue;
			array_push($page_links, $val);
		}

		$run_index++;
	}
}

$filter  = $row_rule['filter'];
$author = $regs_author[1];
$source = $regs_source[1];
$pubtime = $regs_pubtime[1];

$cont = stripslashes($cont);
include_once(R_P . '/include/class.tidy.php');
include_once(R_P . '/include/class.html_clear.php');
$tidy = new tidy();
$html_clear = new html_clear(true);
$html_clear->set_forbidden_attr(array('style','size','class'));
$cont = trim($html_clear->clear($cont));
$cont = $tidy -> clean($cont);

$urls = $urls;
include qbase::template( $path . '/' .  "test_result" );
?>
