<?php
require("../class/connect.php");
include("../class/db_sql.php");
include("../class/config.php");
include("../class/class.php");
include("../class/q_functions.php");
$link=db_connect();
$empire=new mysqlquery();
$ip=egetip();
$searchtime=time();
$getvar=$_POST['getvar'];
if(empty($getvar))
{
	$getfrom="history.go(-1)";
}
else
{
	$getfrom="../../search";
	$dogetvar="&getvar=1";
}
//搜索用户组
if($public_r['searchgroupid'])
{
	$psearchgroupid=$public_r['searchgroupid'];
	@include("../class/MemberLevel.php");
	$searchgroupid=(int)getcvar('mlgroupid');
	if($level_r[$searchgroupid][level]<$level_r[$psearchgroupid][level])
	{
		printerror("NotLevelToSearch",$getfrom,1);
	}
}
$add="";
$addprice="";
$keyboard=trim($_POST['keyboard']);
$s_tbname=RepPostVar($_POST['tbname']);
$s_tempid=(int)$_POST['tempid'];
$classid=(int)$_POST['classid'];
$searchclass=$_POST['show'];
$myorder=(int)$_POST['myorder'];
$orderby=(int)$_POST['orderby'];
$starttime=RepPostVar($_POST['starttime']);
if(empty($starttime))
{$starttime="0000-00-00";}
$endtime=RepPostVar($_POST['endtime']);
if(empty($endtime))
{$endtime="0000-00-00";}
$startprice=(int)$_POST['startprice'];
$endprice=(int)$_POST['endprice'];
//搜索间隔
$lastsearchtime=getcvar('lastsearchtime');
if($lastsearchtime)
{
	if($searchtime-$lastsearchtime<$public_r[searchtime])
	{printerror("SearchOutTime",$getfrom,1);}
}
$len=strlen($keyboard);
if(empty($keyboard))
{printerror("EmptyKeyboard",$getfrom,1);}
//关键字限制
if($len<$public_r[min_keyboard]||$len>$public_r[max_keyboard])
{printerror("MinKeyboard",$getfrom,1);}
$keyboard=addslashes(stripSlashes($keyboard));
//时间范围
if($endtime!="0000-00-00")
{
	$starttimea=$starttime." 00:00:00";
	$endtimea=$endtime." 11:59:59";
	$add=" and newstime>='$starttimea' and newstime<='$endtimea'";
}
//价格
if($endprice)
{
	$addprice=" and price>=".$startprice." and price<=".$endprice;
}
if($classid)
{
	//终极栏目
	if($class_r[$classid][islast])
	{
		$add.=" and classid='$classid'";
	}
	else
	{
		$where=ReturnClass($class_r[$classid][sonclass]);
		$add.=" and (".$where.")";
	}
	$tbname=$class_r[$classid][tbname];
	$modid=$class_r[$classid][modid];
}
elseif($s_tbname)
{
	$tbnamenum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$s_tbname' limit 1");
	if(!$tbnamenum)
	{
		Header("Location:result?searchid=0".$dogetvar);
		exit();
	}
	$tbname=$s_tbname;
	//模型id
	$thestemp_r=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where tempid='$s_tempid'");
	if(empty($thestemp_r['modid']))
	{
		Header("Location:result?searchid=0".$dogetvar);
		exit();
	}
	$modid=$thestemp_r['modid'];
}
else
{
	$tbname=$public_r['tbname'];
}
//表不存在
if(empty($tbname))
{
	Header("Location:result?searchid=0".$dogetvar);
	exit();
}
//搜索字段
if(empty($searchclass)||strstr($searchclass," "))
{
	Header("Location:result?searchid=0".$dogetvar);
	exit();
}
//取得字段
if(empty($class_r[$classid][searchtempid]))
{
	if(empty($modid))
	{
		$tempr=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where isdefault=1 limit 1");
	}
	else
	{
		$tempr[modid]=$modid;
	}
}
else
{
	$tempr[modid]=$modid;
	//$tempr=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where tempid='".$class_r[$classid][searchtempid]."' limit 1");
}
//完全
$asame="";
$sfsql=" like '%[!--key--]%'";
if($_POST['allsame']==1)
{
	$asame=1;
	$sfsql="='[!--key--]'";
}
$mr=$empire->fetch1("select searchvar,tbname from {$dbtbpre}enewsmod where mid='$tempr[modid]'");
$fr=explode(",",$searchclass);
$sf="";
for($j=0;$j<count($fr);$j++)
{
	//验证参数是否正确
	if(!strstr($mr[searchvar],",".$fr[$j].","))
	{
		continue;
	}
	if(empty($newsearchclass))
	{
		$or="";
		$dh="";
	}
	else
	{
		$or=" or ";
		$dh=",";
	}
	$newsearchclass.=$dh.$fr[$j];
	$sf.=$or.$fr[$j].$sfsql;
}
$searchclass=$newsearchclass;
//是否包含价格
if(!strstr($mr[searchvar],",price,"))
{
	$addprice="";
	$startprice=0;
	$endprice=0;
}
//参数错
if(empty($sf))
{
	Header("Location:result?searchid=0".$dogetvar);
	exit();
}
//---------------多个关键字
$kr=explode(" ",$keyboard);
for($i=0;$i<count($kr);$i++)
{
	if(empty($kr[$i]))
	{continue;}
	$add.=" and (".str_replace("[!--key--]",$kr[$i],$sf).")";
}
//验证码
if($s_tempid)
{
	$mtempid=$s_tempid;
}
else
{
	$mtempid="";
}
$checkpass=md5($keyboard.$searchclass.$classid.$starttime.$endtime.$startprice.$endprice.$s_tbname.$mtempid.$asame);
$search_r=$empire->fetch1("select searchid,searchtime from {$dbtbpre}enewssearch where checkpass='$checkpass' limit 1");
$todaytime=date("Y-m-d H:i:s");
$query="select count(*) as total from {$dbtbpre}ecms_".$tbname." where checked=1".$add.$addprice;
$searchid=$search_r[searchid];
//是否有历史记录
if($searchid)
{
    $search_num=$empire->gettotal($query);
	$sql=$empire->query("update {$dbtbpre}enewssearch set searchtime=$searchtime,result_num=$search_num,onclick=onclick+1,orderby=$orderby,myorder=$myorder,tempid=$s_tempid,allsame='$asame' where searchid='$searchid'");
	if(empty($search_num))
	{$searchid=0;}
}
else
{
	$search_num=$empire->gettotal($query);
	if(empty($search_num))
	{$searchid=0;}
	else
	{
		$sql=$empire->query("insert into {$dbtbpre}enewssearch(searchtime,keyboard,searchclass,result_num,searchip,classid,onclick,orderby,myorder,starttime,endtime,startprice,endprice,checkpass,tbname,tempid,allsame) values($searchtime,'$keyboard','$searchclass',$search_num,'$ip',$classid,1,$orderby,$myorder,'$starttime','$endtime',$startprice,$endprice,'$checkpass','$s_tbname',$s_tempid,'$asame')");
		$searchid=$empire->lastid();
	}
}
//设置最后搜索时间
$set1=esetcookie("lastsearchtime",$searchtime,$searchtime+3600*24);
db_close();
$empire=null;
Header("Location:result?searchid=$searchid".$dogetvar);
?>