<?php
if(!defined('MOD'))  die("access die");
$rules = $_GET['rules'];
$string = new String();
$sql = "SELECT * FROM ".TABLEPRE."collect_rule WHERE id = '$rules'";
$row = qdb::QueryFirst( $sql );
$links_rule = $string->pregLinks( $row['link_rule']);
switch ( $row['index_type'] )
{
	case 1:
		$url = trim( $row['onepage'] );
		break;

	case 2:
		$manypage = explode( "\n", $row['manypage'] );
		$url = trim( $manypage[0] );
		break;

	case 3:
		$rowurl = explode( '[分页]', $row['oneurl'] );
		$url = $rowurl[0].$row['url_start'].$rowurl[1];
		$url = trim( $url );
}

$string->getUrl( $url ); 
if ( $row->cookie != "" and $acookis != 1 ){
	if ( preg_match( "/\n/", $row['cookie'] )){
		$row['cookie'] = $string->fileFsock( $url, $row['cookie'] );
		$acookis = 1;
	}
}
$handles = qbase::load( $url , 0, '', $row['cookie']);
if ( $handles ){
	$test_file = '可以打开连接页！';
}else{
	qbase::showmsg("网络打开失败，请检查");
}
$matches = array();
$lists = array();

preg_match_all( "/".$links_rule."/ies", $handles, $matches );
for ( $i = 0; $i < sizeof( $matches[1] ); $i++ ){
	if (  ! empty( $row['jclinks'] )){
		if ( $row['index_type'] == 3 ){
			$row['jclinks'] = str_replace( '[分页号码]', $row['url_start'], $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;
		}
	}
	$title = strip_tags($matches[2][$i]);
	$url3 = htmlspecialchars( $url2 );
	$list['test_rules'] = $rules;
	$list['test_url'] = urlencode($url2);
	$list['test_urlname'] = $url3;
	$list['test_title'] = $title;
	$lists[] = $list;
}
$num = sizeof( $matches[1] );
$html_url = htmlspecialchars( $url);
include qbase::template( $path . '/' .  "test_link_work" );
?>
