<?php
require(dirname(__FILE__)."/"."global.php");

//添加搜索关键字记录数据表
if(!is_table("{$pre}search_keyword")){
$db->query("CREATE TABLE IF NOT EXISTS `{$pre}search_keyword` (
  `sid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `keyword` varchar(30) NOT NULL DEFAULT '',
  `hits` int(10) NOT NULL DEFAULT '0',
  PRIMARY KEY (`sid`)
) ENGINE=MyISAM DEFAULT CHARSET=$dbcharset AUTO_INCREMENT=1");
}

require_once(ROOT_PATH."inc/class.inc.php");
$Guidedb=new Guide_DB;
$mid = intval($mid);
$Guidedb->mid=$mid;
$keyword=str_replace("%","\%",$keyword);
$keyword=trim($keyword);
$rows=10;
//以下针对多选框:为0时,扩大搜索范围,为1时,缩小搜索范围
$check_select_type = $webdb['field_checkbox_search'];
unset($fieldDB);
if($mid){
	$detail = unserialize(stripslashes($article_moduleDB[$mid]['config']));
	$fieldDB = $detail['field_db'];
}

if( ($_GET[type]||$_POST[type]) && !$keyword )
{
	showerr("关键字不能为空");
}
$type = filtrate($type);
$keyword = filtrate($keyword);

if( $_GET[keyword] || $_POST[keyword] )
{
	//搜索关键字添加记录
	if($db->get_one("SELECT * FROM {$pre}search_keyword WHERE keyword='$keyword'")){
		$db->query("UPDATE {$pre}search_keyword SET hits=hits+1 WHERE keyword='$keyword'");
	}else{
		$db->query("INSERT INTO `{$pre}search_keyword` (keyword,hits) VALUES ('$keyword',1) ");
	}

	$search_module=0;
	if(!$type)
	{
		$type='title';
	}
	if(!$web_admin)
	{
		if(!$groupdb[SearchArticleType]){
			showerr("你所在用户组,无权使用搜索!");
		}
		elseif($type=='content'&&$groupdb[SearchArticleType]!=2){
			showerr("你所在用户组,无权搜索文章内容!");
		}
	}

	if(!$web_admin&&$_POST[keyword]&&($timestamp-$_COOKIE[searchTime]<3))
	{
		showerr("3秒钟内,请不要重复提交查询");
	}
	setcookie("searchTime",$timestamp,$timestamp+3);

	$SQL=" A.yz=1 ";
	if($mid==-1){
		$SQL.=" AND A.mid='0' ";
	}elseif($mid){
		$SQL.=" AND A.mid='$mid' ";
	}


	if($type=='keyword')
	{
		unset($array);
		$query = $db->query("SELECT B.aid FROM {$pre}keywordid B LEFT JOIN {$pre}keyword A ON A.id=B.id WHERE A.keywords='$keyword'");
		while($rs = $db->fetch_array($query)){
			$array[]=$rs[aid];
		}
		if($array){
			$SQL.=" AND A.aid IN (".implode(',',$array).") ";
		}else{
			$SQL.=" AND 0 ";
		}
	}
	elseif($type=='title')
	{
		$SQL.=" AND BINARY A.title LIKE '%$keyword%' ";
	}
	elseif($type=='id')
	{
		$SQL.=" AND A.aid = '$keyword' ";
	}
	elseif($type=='username')
	{
		$SQL.=" AND BINARY A.username = '$keyword' ";
	}
	elseif($type=='content')
	{
		$SQL.=" AND BINARY B.content LIKE '%$keyword%' ";
	}
	elseif($type&&$mid&&is_table("{$pre}article_content_$mid")&&table_field("{$pre}article_content_$mid",$type))
	{
		$SQL.=" AND BINARY C.$type LIKE '%$keyword%' ";
		$search_module=1;
	}
	//搜索按照时间来处理-开始
	if($starttijd&&$eindtijd){
		$starttijd=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","mk_time('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$starttijd);
		$eindtijd=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","mk_time('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$eindtijd);
		if($starttijd<$eindtijd){
			$SQL.=" AND A.posttime>'$starttijd' AND A.posttime<'$eindtijd'";
		}else{
			showerr("时间格式不对");
		}
	}
	if($starttijd){
		$starttijd=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","mk_time('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$starttijd);
		$SQL.=" AND A.posttime>'$starttijd'";
	}
	if($eindtijd){
		$eindtijd=preg_replace("/([\d]+)-([\d]+)-([\d]+) ([\d]+):([\d]+):([\d]+)/eis","mk_time('\\4','\\5', '\\6', '\\2', '\\3', '\\1')",$eindtijd);
		$SQL.=" AND A.posttime<'$eindtijd'";
	}
	//搜索按照时间来处理-结束
	$search_url='';
	if($fieldDB){	
		$erp=$article_moduleDB[$mid][iftable]?$article_moduleDB[$mid][iftable]:'';		
		foreach( $postdb AS $key=>$value){
			if(!$fieldDB[$key]||$value==''){
				continue;
			}			
			$search_module=1;
			if(is_array($value)&&$fieldDB[$key]['form_type']=='checkbox'){
				$_ASQL='';
				$_BSQL='';
				foreach($value AS $v){
					$v = filtrate($v);
					$_ASQL.=" AND BINARY C.`$key` LIKE '%/#/$v/#/%' ";
					$_BSQL.=($_BSQL?' OR ':'')." BINARY C.`$key` LIKE '%/#/$v/#/%' ";
					$rsdb[$key][$v]=" checked ";
					$v=urlencode($v);
					$search_url.="&postdb[{$key}][]=$v";
				}
				$SQL.=$check_select_type==1?$_ASQL:" AND ($_BSQL) ";
			}else{	
				$value = filtrate($value);
				$SQL.=" AND C.`$key`='$value' ";
				$rsdb[$key][$value]=" selected ";
							 
				$value=urlencode($value);
				$search_url.="&postdb[{$key}]=$value";
			}
		}
	}

	if($page<1)
	{
		$page=1;
	}
	$min=($page-1)*$rows;

	$_SQL=" AND B.topic=1 ";
	if($fid){
		$_SQL=" AND A.fid='$fid' ";
	}
	
	$_moduleSql=$_moduleSqlSearch="";
	if($search_module==1)
	{
		$_moduleSql=" LEFT JOIN {$pre}article_content_$mid C ON A.aid=C.aid ";
		$_moduleSqlSearch=" C.*, ";
	}
	
	

	unset($listdb);

	$query = $db->query("SELECT SQL_CALC_FOUND_ROWS {$_moduleSqlSearch}B.*,A.* FROM {$pre}article$erp A LEFT JOIN {$pre}reply B ON A.aid=B.aid $_moduleSql WHERE $SQL $_SQL ORDER BY A.aid DESC LIMIT $min,$rows");
	$RS=$db->get_one("SELECT FOUND_ROWS()");
	$totalNum=$RS['FOUND_ROWS()'];
	while($rs = $db->fetch_array($query)){
		$rs[posttime] = date("Y-m-d",$rs[posttime]);
		if( $webdb[path] && ($webdb[NewsMakeHtml]==1) ){
			$webdb[path]='';
		}
		$rs[url]="$webdb[www_url]$webdb[path]/bencandy.php?fid=$rs[fid]&id=$rs[aid]";
		$listdb[] = $rs;
	}

	$showpage=getpage("","","search.php?fid=$fid&type=$type&$search_url&keyword=".urlencode($_POST[keyword]).urlencode($_GET[keyword])."",$rows,$totalNum);
	
	if(!$listdb)
	{
		//showerr("很抱歉,没有找到你要查询的内容");
	}

	
	$typedb[$type]=' selected ';
}


//$module_select="<select name='mid' onChange=\"window.location.href='?mid='+this.options[this.selectedIndex].value\"><option value='0'  style='color:#aaa;'>所有模型</option>";
$ckk=$mid?'':' selected ';
$module_select="<select name='mid' onChange=\"changemid(\$(this).val())\"><option value='0' $ckk>所有模型</option>";
$query = $db->query("SELECT * FROM {$pre}article_module WHERE ifclose=0 ORDER BY list DESC");
while($rs = $db->fetch_array($query)){
	$ckk=$mid==$rs[id]?' selected ':' ';
	$module_select.="<option value='$rs[id]' $ckk>$rs[name]</option>";
}
$module_select.="</select>";

if($webdb[sortNUM]>500){
	$fid_select=$Guidedb->Select("{$pre}sort","fid",$fid,'',0);
}else{
	$fid_select="<select name='fid' onChange=\"if(this.options[this.selectedIndex].value=='-1'){alert('你不能选择大分类');}\"><option value='0' style='color:#aaa;'>所有栏目</option>";
	$fid_select.=list_allsort(0,1,$fid,$mid);
	$fid_select.="</select>";	
}
//异步获取栏目
if($job=='getsorts'){
	die($fid_select);
}
//异步显示关键字
if($job=='getkeyword'){
	$listkeyword='';
	if($oldkeyword){
		$query = $db->query("SELECT keyword FROM {$pre}search_keyword WHERE keyword LIKE '$oldkeyword%' ORDER BY keyword ASC LIMIT 10");
		while($rs = $db->fetch_array($query)){
			$listkeyword.="<div onclick='selectthis(\$(this).html())'>$rs[keyword]</div>";
		}
	}
	die($listkeyword);
}


$type||$type='title';
$typedb[$type]=" checked ";


require(ROOT_PATH."inc/head.php");
//require(html($mid?"search_$mid":"search"));
require(html("search"));
require(ROOT_PATH."inc/foot.php");

//伪静态
if($webdb[NewsMakeHtml]==2)
{
	$content=ob_get_contents();
	ob_end_clean();
	ob_start();
	$content=fake_html($content);
	echo "$content";
}

//真静态
elseif($webdb[NewsMakeHtml]==1)
{
	$content=ob_get_contents();
	ob_end_clean();
	ob_start();			//备用
	$content=make_html($content,'N');
	echo "$content";
}


/*栏目列表 取决模型相关栏目*/
function list_allsort($fid,$Class,$ckfid,$fmid="0"){
	global $db,$pre,$listdb;
	$Class++;
	if(!$fmid){
	$query=$db->query("SELECT * FROM {$pre}sort WHERE fup='$fid' ORDER BY list DESC");
	}else{
	$query=$db->query("SELECT * FROM {$pre}sort WHERE fup='$fid' AND fmid ='$fmid'ORDER BY list DESC");
	}
	while( $rs=$db->fetch_array($query) ){
		$icon="";
		for($i=1;$i<$Class;$i++){
			$icon.="&nbsp;|&nbsp;&nbsp;";
		}
 		if($icon){
			$icon=substr($icon,0,-12);
			$icon.="--";
		}
 		if($rs[type]){
			$fid_select.="<option value='-1' style='color:red;'>$icon$rs[name]</option>";
 		}else{
 			$ckk=$ckfid==$rs[fid]?' selected ':' ';
			$fid_select.="<option value='$rs[fid]' $ckk>$icon$rs[name]</option>";
 		}
		$fid_select.=list_allsort($rs[fid],$Class,$ckfid);
	}
	return $fid_select;
}
?>