<?php
if (!defined('MOD')) die("access die");
require_once(R_P . '/config/admin_options.php');
$id = intval($_GET['id']);
$rules = intval($_GET['rules']);
$sql = "SELECT * FROM " . TABLEPRE . "collect_rule WHERE id = '$rules' limit 1";
$row_rule = qdb :: QueryFirst($sql);
$channelid = $row_rule['channelid'];
$channelselect = $channel[$channelid]['channelselect'];
$len = $row_rule['len'];

$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']);

$sql = "SELECT id, title, url FROM  " . TABLEPRE . "collect_links WHERE id='$id' limit 1";
$row = qdb :: queryFirst($sql);
$param = array();
require_once(R_P . "/include/class.tag.php");
require_once(R_P . "/include/class.qmecms.php");
$tag = new tag();

if (empty($row)) {
	$data = array('status' => 0, id => $id);
} else {
	$title_temp = $row['title'];
	$dateline = $_SERVER['REQUEST_TIME'];
	$left = $right = '';
	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;
		} 
	} 
	unset($buffer); 
	// 载人查询到的一个网址 如果载入失败 挑至下一个流程
	$temp = parse_url($row['url']);
	$host_url = $temp['scheme'] .'://'. $temp['host'] . $temp['path'] . '/';
	$buffer = qbase :: load($row['url'] , 0, '', $row_rule['cookie']);
	$detail = '';
	if ($buffer) {
		// 搜寻作者
		if (! empty($author)) {
			preg_match("/" . $author . "/is", $buffer, $regs_author);
		} else {
			$regs_author = array();
		} 
		if (! empty($pubtime)) {
			preg_match("/" . $pubtime . "/is", $buffer, $pubtime);
			$dateline = strtotime($pubtime[1]);
		}
		if (! empty($source)) {
			preg_match("/" . $source . "/is", $buffer, $regs_source);
		} else {
			$regs_source = array('','');
		} 

		// 搜寻内容
		preg_match("/" . $body_rule . "/is", $buffer, $regs); 
		// 搜寻替换内容

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

		/*记录已经获取的地址*/
		$links_map = array($row['url']);
		$content_map = array();

		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);
				$detail .= 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++;
			}
		}

		$detail = formaturl($detail, $host_url);
		$detail = stripslashes($detail);

	
		if (strlen($detail) > 100) {
			$iif = qdb :: QueryAlone("select count(*) from  " . TABLEPRE . "article_{$channelid} where cid='{$row_rule['cid']}' and subject='" . addslashes(trim($row['title'])). "' limit 1");
			if ($iif == 0) {
				$filter = $row_rule['filter'];
				$detail = HtmlGo(textFilterClean($detail, $row_rule['filter_text'] ), $row_rule['filter']);
				if ($row_rule['pic'] == 1) {
					$sPathFileName = '';
					$sPreFileName = '';
					InitUpload();
					DoCreateNewDir();
					$detail = ReplaceRemoteUrl($detail);

					$sPathFileName = trim($sPathFileName);
					if (strlen($sPathFileName) > 10) {
						$arr = explode('|', $sPathFileName);
						foreach($arr as $val) {
							if ($val = trim($val)) {
								$val = str_replace(W_P, '', $val);
								$fext = substr(strrchr($val, "."), 1);
								if(!in_array(strtolower($fext), array('jpg','gif','png','jpeg'))) continue;
								$thumb  = $val;
								break;
							} 
						} 
					} else {
						$thumb = '';
					} 
				} 

				if($thumb && is_file(R_P .'/'. $thumb)){
					require_once(R_P . "/include/class.resizeimage.php");
					$pic_width = intval($channel[$row_rule['channelid']]['pic_width']);
					$pic_height = intval($channel[$row_rule['channelid']]['pic_height']);
					$pic_cut = intval($channel[$row_rule['channelid']]['pic_cut']);	
					$real_file_name = R_P .'/'. $thumb;
					$web_filename = getSmallImageFile($thumb, '_thumb');
					$dstimg = R_P .'/'. $web_filename;
					$resize = new resizeimage($real_file_name);
					$rs = $resize->init();
					if($rs == 0){
						$resize->run($pic_width, $pic_height, $pic_cut, $dstimg);
						$resize->close();
						clearstatcache();
						$thumb = W_P . $web_filename;
					}else{
						$thumb = '';
					}
				}else{
					$thumb = '';
				}
				$detail = stripslashes($detail);
				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'));
				$detail = trim($html_clear->clear($detail));
				$message = addslashes($tidy -> clean($detail));
				$text_id = qdb :: Query("insert into " . TABLEPRE . "text (message) values ('{$message}')", true);
				if ($len > 0) $param['description'] = getDescription($detail, $len);
				$keywords = keyword($detail, $row['title']);
				$keyword = array();
				foreach($keywords as $val) {
					if (trim($val['name']) && $val['rank'] > 10) $keyword[] = trim($val['name']);
				} 
				$keyword = implode(" ", $keyword);
				if($row_rule['auto_filename']){
					$cms = new qmecms();
					$param['filename'] = $cms->autoFilename($row['title']);				
				}
				$param['cid'] = intval($row_rule['cid']);
				$param['channelid'] = intval($row_rule['channelid']);
				$param['subject'] = addslashes(trim($row['title']));
				$param['keyword'] = addslashes(trim($keyword));
				$param['source'] = addslashes(trim($regs_source[1]));
				$param['message'] = intval($text_id);
				$param['editor'] = addslashes(trim($admin_info['username']));
				$param['author'] = addslashes(trim($regs_author[1]));
				$param['dateline'] = intval($dateline);
				$param['thumb'] = addslashes(trim($thumb));
				$param['description'] = addslashes(trim($param['description']));
				
				$sql3 = qbase::createSql(TABLEPRE . "article_{$channelid}", $param, "insert");
				$aid = qdb :: Query($sql3, true);
				$tag -> addKeyTag($aid, $keywords);

				$data = array('status' => 4, 'id' => $aid, 'title' => $title_temp);

				qdb :: Query("delete FROM  " . TABLEPRE . "collect_links WHERE id='$id' limit 1");
				qdb :: Query("update " . TABLEPRE . "collect_rule set last_update='" . $_SERVER['REQUEST_TIME'] . "' WHERE id='$rules' limit 1");
			} else { // 存在相同标题的数据
				qdb :: Query("delete FROM  " . TABLEPRE . "collect_links WHERE id='$id' limit 1");
				$data = array('status' => 3, 'id' => $id, 'title' => $title_temp);
			} 
		} else { // 没有采集到文章内容
			$data = array('status' => 2, 'id' => $id, 'title' => $title_temp);
		} 
	} else { // 数据源错误
		$data = array('status' => 1, 'id' => $id, 'title' => $title_temp);
	} 
} 

echo qbase :: xmlPrint(qbase :: jsonencode($data));
exit();
?>