<?php
if(!defined('R_P')) exit('Access Denied');
$pageno = intval($_GET['pageno']) > 0 ? intval($_GET['pageno']) : 1;
$limit = 10;
$offset = ($pageno - 1) * $limit;
$kid = intval($_GET['kid']);
$channelid = intval($_GET['channelid']);
$word = urldecode($_GET['word']);
$name = urldecode($_GET['name']);
$type = urldecode($_GET['type']);
$_GET['word'] = urldecode($_GET['word']);
if($type == 'tag'){
	if($kid > 0){
		$word = qdb::queryAlone("select name from  ".TABLEPRE."tag where kid = '{$kid}' ");
		if(empty($word)) qbase::showmsg("出错了，没有找到该关键词",  $_SET['skin'] , W_P);
	}elseif($word || $name){
		if(!$word) $word = $name;
		$kid = qdb::queryAlone("select kid from  ".TABLEPRE."tag where name = '{$word}' ");
		if(empty($kid)) qbase::showmsg("出错了，没有找到该关键词",  $_SET['skin'] , W_P);
	}else{
		qbase::showmsg("出错了，提交了错误的请求",  $_SET['skin'] , W_P);
	}
	$word_quote = preg_quote($word);
	$where = $channelid > 0 ? " and channelid = '{$channelid}' " : "";
	$temp = qdb::fetchAll("select id, channelid from ".TABLEPRE."tag_mapping where kid = '{$kid}' {$where} order by tid desc limit {$offset}, {$limit}");
	$data['total'] = qdb::queryAlone("select count(*) as num from ".TABLEPRE."tag_mapping where kid = '{$kid}' {$where} ");

	$ids_array = $ids = $value = $data = array();
	foreach($temp as $val) $ids_array[$val['channelid']][] = $val['id'];
	foreach($ids_array as $key=>$val){
		$ids[$key] = implode(',', $val); 
	}
	foreach($ids as $key=>$val){
		if(empty($val)) continue;
		$table = $_MODULE[$channel[$key]['channelselect']]['table_name'];
		if(empty($table)) continue;
		$value[$key] = qdb::fetchAll("select * from ".TABLEPRE."{$table}_{$key} where id IN ({$val})");
	}
	foreach($value as $channelid=>$vv){
		foreach($vv as $v){
			$val = array();
			if(false === ($temp = getCidInfo($v['channelid'], $v['cid']))) continue;
			unset($temp['description']);
			$v = array_merge($v, $temp);	
		
			$iif = !( $v['html'] || $v['pur_browse'] || $v['pay'] );
			
			$val['title'] = $v['title']; 
			$val['thumb'] = $v['thumb'];
			$val['parent_url'] = qbase::GetCidHtmlDirType($v['channelid'], $v['cid'], $v['ciddir'], $v['cfilename'],!($v['pur_browse'] || $v['category_html']));
			$val['url']  = qbase::GetHtmlDirType($v['channelid'], $v['id'], $v['ciddir'], $v['dateline'],$v['filename'],$v['ver'],$iif);

			$val['url_s'] = shorturl($val['url']);
			$val['subject'] = preg_replace("/{$word_quote}/is", "<font color=red><b>\\0</b></font>", $v['subject']);
			$val['description'] = preg_replace("/{$word_quote}/is", "<font color=red><b>\\0</b></font>", qbase::substrs(preg_replace("/\<(.*?)\>/ies","",$v['description']), 200));
			$val['dateline'] = $v['dateline'];
			$val['views'] = $v['views'];
			$val['thumb'] = $v['thumb'];
			$data['data'][] = $val;
		}
	}
}else{
	$word = preg_replace("/\'|\\\"|\\\\/", "", $word);
	if(empty($word)) qbase::showmsg("出错了，你没有输入搜索关键字",  $_SET['skin'] , W_P);
	$word_quote = preg_quote($word);
	$data = array('total'=>0, 'data' => array());
	$table = $_MODULE[$channel[$channelid]['channelselect']]['table_name'];
	if(!$table) qbase::showmsg("出错了，提交了错误的请求",  $_SET['skin'] , W_P);

	$table = "".TABLEPRE."" . $table . "_" . $channelid;
	$where = " where subject like '%{$word}%' ";
	$data['total'] = qdb::queryAlone("select count(*) as num from {$table} " . $where);
	$sql = "SELECT * FROM {$table} as a " . $where  . " order by views desc  limit {$offset}, {$limit}";
	$temp = qdb::fetchAll($sql);
	foreach($temp as $v){
		$val = array();
		if(false === ($temp = getCidInfo($v['channelid'], $v['cid']))) continue;
		unset($temp['description']);
		$v = array_merge($v, $temp);			
		$iif = !( $v['html'] || $v['pur_browse'] || $v['pay']);
		$val['parent_url'] = qbase::GetCidHtmlDirType($v['channelid'], $v['cid'], $v['ciddir'], $v['cfilename'],!($v['pur_browse'] || $v['category_html']));
		$val['title'] = $v['title'];
		$val['thumb'] = $v['thumb'];
		$val['url']  = qbase::GetHtmlDirType($v['channelid'], $v['id'], $v['ciddir'], $v['dateline'],$v['filename'],$v['ver'],$iif);
		$val['url_s'] = shorturl($val['url']);
		$val['subject'] = preg_replace("/{$word_quote}/is", "<font color=red><b>\\0</b></font>", $v['subject']);
		$val['description'] = preg_replace("/{$word_quote}/is", "<font color=red><b>\\0</b></font>", qbase::substrs(preg_replace("/\<(.*?)\>/ies","",$v['description']), 200));
		$val['dateline'] = $v['dateline'];
		$val['views'] = $v['views'];
		$val['thumb'] = $v['thumb'];
		$data['data'][] = $val;
	}
}
//记录搜索日志
if($pageno == 1){
	$_dateline = qbase::formatTime("Y-m-d", $_SERVER['REQUEST_TIME']);
	$num = qdb::queryAlone("select num from ".TABLEPRE."search_log where dateline = '{$_dateline}' and word = '{$word}' ");
	if($num == 0){
		qdb::query("replace into ".TABLEPRE."search_log (dateline, word, num) values ('{$_dateline}','{$word}', 1)");
	}else{
		$num++;
		qdb::query("update ".TABLEPRE."search_log set num = '{$num}' where dateline = '{$_dateline}' and word = '{$word}'");
	}
}
$pager = qbase::page($data['total'] , $pageno, $limit, 1);

include qbase::template($_SET['skin'] . '/tag');

function shorturl($url, $length = 100){
	if (strlen($url) > $length){
		$url = substr($url, 0, intval($length * 0.5)) . ' ... ' . substr($url, - intval($length * 0.3));
	}
	return $url;
}
?>