index.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. require("../class/connect.php");
  3. require("../class/db_sql.php");
  4. require("../data/dbcache/class.php");
  5. require("../class/q_functions.php");
  6. eCheckCloseMods('search');//关闭模块
  7. $link=db_connect();
  8. $empire=new mysqlquery();
  9. //处理关键字
  10. function SearchDoKeyboardVar($keyboard){
  11. $keyboard=str_replace(' ','',$keyboard);
  12. $keyboard=RepPostVar2(trim($keyboard));
  13. return $keyboard;
  14. }
  15. //返回SQL
  16. function SearchDoKeyboard($f,$hh,$keyboard){
  17. $where='';
  18. $keyboard=SearchDoKeyboardVar($keyboard);
  19. if(empty($keyboard))
  20. {
  21. return "";
  22. }
  23. if(!empty($hh))
  24. {
  25. if($hh=='LT')//小于
  26. {
  27. $where=$f."<'".$keyboard."'";
  28. }
  29. elseif($hh=='GT')//大于
  30. {
  31. $where=$f.">'".$keyboard."'";
  32. }
  33. elseif($hh=='EQ')//等于
  34. {
  35. $where=$f."='".$keyboard."'";
  36. }
  37. elseif($hh=='LE')//小于等于
  38. {
  39. $where=$f."<='".$keyboard."'";
  40. }
  41. elseif($hh=='GE')//大于等于
  42. {
  43. $where=$f.">='".$keyboard."'";
  44. }
  45. elseif($hh=='NE')//不等于
  46. {
  47. $where=$f."<>'".$keyboard."'";
  48. }
  49. elseif($hh=='IN')//包含
  50. {
  51. $kr=explode(' ',$keyboard);
  52. $kcount=count($kr);
  53. $kbs='';
  54. $dh='';
  55. for($i=0;$i<$kcount;$i++)
  56. {
  57. $kr[$i]=(float)$kr[$i];
  58. if(empty($kr[$i]))
  59. {
  60. continue;
  61. }
  62. if($kbs)
  63. {
  64. $dh=',';
  65. }
  66. $kbs.=$dh."'".$kr[$i]."'";
  67. }
  68. if($kbs)
  69. {
  70. $where=$f." IN (".$kbs.")";
  71. }
  72. else
  73. {
  74. return '';
  75. }
  76. }
  77. elseif($hh=='BT')//范围
  78. {
  79. $keyboard=ltrim($keyboard);
  80. if(!strstr($keyboard,' '))
  81. {
  82. return '';
  83. }
  84. $kr=explode(' ',$keyboard);
  85. $kr[0]=(float)$kr[0];
  86. $kr[1]=(float)$kr[1];
  87. if(!trim($kr[0])||!trim($kr[1]))
  88. {
  89. return '';
  90. }
  91. $where=$f." BETWEEN '".$kr[0]."' and '".$kr[1]."'";
  92. }
  93. else//相似
  94. {
  95. $where=$f." LIKE '%".$keyboard."%'";
  96. }
  97. }
  98. else
  99. {
  100. $where=$f." LIKE '%".$keyboard."%'";
  101. }
  102. return $where;
  103. }
  104. //变量
  105. if($_GET['searchget']==1)
  106. {
  107. $_POST=$_GET;
  108. }
  109. $ip=egetip();
  110. $searchtime=time();
  111. $getvar=$_POST['getvar'];
  112. if(empty($getvar))
  113. {
  114. $getfrom="history.go(-1)";
  115. $dogetvar='';
  116. }
  117. else
  118. {
  119. $getfrom="../../search/";
  120. $dogetvar="&getvar=1";
  121. }
  122. //返回
  123. $getfrom=DoingReturnUrl($getfrom,$_POST['ecmsfrom']);
  124. //搜索用户组
  125. if($public_r['searchgroupid'])
  126. {
  127. $psearchgroupid=$public_r['searchgroupid'];
  128. @include("../data/dbcache/MemberLevel.php");
  129. $searchgroupid=(int)getcvar('mlgroupid');
  130. if($level_r[$searchgroupid][level]<$level_r[$psearchgroupid][level])
  131. {
  132. printerror("NotLevelToSearch",$getfrom,1);
  133. }
  134. }
  135. //搜索间隔
  136. $lastsearchtime=getcvar('lastsearchtime');
  137. if($lastsearchtime)
  138. {
  139. if($searchtime-$lastsearchtime<$public_r[searchtime])
  140. {
  141. printerror("SearchOutTime",$getfrom,1);
  142. }
  143. }
  144. //搜索字段
  145. $searchclass=$_POST['show'];
  146. if(empty($searchclass)||@strstr($searchclass," "))
  147. {
  148. printerror("SearchNotRecord",$getfrom,1);
  149. }
  150. //时间范围
  151. $add='';
  152. $addtime='';
  153. $starttime=RepPostVar($_POST['starttime']);
  154. if(empty($starttime))
  155. {
  156. $starttime="0000-00-00";
  157. }
  158. $endtime=RepPostVar($_POST['endtime']);
  159. if(empty($endtime))
  160. {
  161. $endtime="0000-00-00";
  162. }
  163. if($endtime!="0000-00-00")
  164. {
  165. $addtime=" and (newstime BETWEEN '".to_time($starttime." 00:00:00")."' and '".to_time($endtime." 23:59:59")."')";
  166. }
  167. //价格
  168. $addprice='';
  169. $startprice=(int)$_POST['startprice'];
  170. $endprice=(int)$_POST['endprice'];
  171. if($endprice)
  172. {
  173. $addprice=" and (price BETWEEN ".$startprice." and ".$endprice.")";
  174. }
  175. //搜索栏目及表
  176. $classid=RepPostVar($_POST['classid']);
  177. $s_tbname=RepPostVar($_POST['tbname']);
  178. $s_tempid=(int)$_POST['tempid'];
  179. $trueclassid=0;
  180. if($classid)//按栏目
  181. {
  182. if(strstr($classid,","))//多栏目
  183. {
  184. $son_r=sys_ReturnMoreClass($classid,1);
  185. $trueclassid=$son_r[0];
  186. $add.=' and ('.$son_r[1].')';
  187. }
  188. else
  189. {
  190. $trueclassid=intval($classid);
  191. $add.=$class_r[$trueclassid][islast]?" and classid='$trueclassid'":" and ".ReturnClass($class_r[$trueclassid][sonclass]);
  192. }
  193. $tbname=$class_r[$trueclassid][tbname];
  194. $modid=$class_r[$trueclassid][modid];
  195. }
  196. elseif($s_tbname)//按数据表
  197. {
  198. $tbnamenum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$s_tbname' limit 1");
  199. if(!$tbnamenum)
  200. {
  201. printerror("SearchNotRecord",$getfrom,1);
  202. }
  203. $tbname=$s_tbname;
  204. //模型id
  205. $thestemp_r=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where tempid='$s_tempid'");
  206. if(empty($thestemp_r['modid']))
  207. {
  208. printerror("SearchNotRecord",$getfrom,1);
  209. }
  210. $modid=$thestemp_r['modid'];
  211. }
  212. else
  213. {
  214. $tbname=$public_r['tbname'];
  215. $modid=0;
  216. }
  217. //表不存在
  218. if(empty($tbname)||InfoIsInTable($tbname))
  219. {
  220. printerror("SearchNotRecord",$getfrom,1);
  221. }
  222. //标题分类
  223. $ttid=RepPostVar($_POST['ttid']);
  224. $truettid=0;
  225. if($ttid)
  226. {
  227. if(strstr($ttid,","))//多标题分类
  228. {
  229. $son_r=sys_ReturnMoreTT($ttid);
  230. $truettid=$son_r[0];
  231. $add.=' and ('.$son_r[1].')';
  232. }
  233. else
  234. {
  235. $truettid=intval($ttid);
  236. $add.=" and ttid='$truettid'";
  237. }
  238. }
  239. //会员
  240. $member=$_POST['member'];
  241. if($member==1)
  242. {
  243. $add.=' and ismember=1';
  244. }
  245. elseif($member==2)
  246. {
  247. $add.=' and ismember=0';
  248. }
  249. //模型
  250. $tempr=array();
  251. if(empty($class_r[$trueclassid][searchtempid]))
  252. {
  253. if(empty($modid))
  254. {
  255. $tempr=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where isdefault=1 limit 1");
  256. }
  257. else
  258. {
  259. $tempr[modid]=$modid;
  260. }
  261. }
  262. else
  263. {
  264. $tempr[modid]=$modid;
  265. }
  266. //关键字
  267. $keyboard=$_POST['keyboard'];
  268. $keyboardone=0;
  269. if(is_array($keyboard))
  270. {}
  271. elseif(strstr($keyboard,','))
  272. {
  273. $keyboard=explode(',',$keyboard);
  274. }
  275. else
  276. {
  277. $keyboard=trim($keyboard);
  278. $len=strlen($keyboard);
  279. if($len<$public_r[min_keyboard]||$len>$public_r[max_keyboard])
  280. {
  281. printerror("MinKeyboard",$getfrom,1);
  282. }
  283. $keyboardone=1;
  284. }
  285. //符号
  286. $hh=$_POST['hh'];
  287. $hhone=0;
  288. if(is_array($hh))
  289. {}
  290. elseif(strstr($hh,','))
  291. {
  292. $hh=explode(',',$hh);
  293. }
  294. else
  295. {
  296. $hhone=1;
  297. }
  298. //字段
  299. if(!is_array($searchclass))
  300. {
  301. $searchclass=explode(',',$searchclass);
  302. }
  303. $andor=$_POST['andor'];
  304. $andor=$andor=='and'?'and':'or';
  305. $mr=$empire->fetch1("select searchvar,tbname from {$dbtbpre}enewsmod where mid='$tempr[modid]'");
  306. if(!strstr($mr[searchvar],",price,"))//是否包含价格
  307. {
  308. $addprice="";
  309. $startprice=0;
  310. $endprice=0;
  311. }
  312. //搜索特殊字段
  313. $mr[searchvar].='id,keyboard,userid,username,';
  314. $where='';
  315. $newsearchclass='';
  316. $count=count($searchclass);
  317. for($i=0;$i<$count;$i++)
  318. {
  319. if(empty($searchclass[$i]))
  320. {
  321. continue;
  322. }
  323. $searchclass[$i]=str_replace(',','',$searchclass[$i]);
  324. if(!strstr($mr[searchvar],",".$searchclass[$i].","))
  325. {
  326. continue;
  327. }
  328. $searchclass[$i]=RepPostVar($searchclass[$i]);
  329. if(stristr(','.$newsearchclass.',',','.$searchclass[$i].','))
  330. {
  331. continue;
  332. }
  333. $dh=empty($newsearchclass)?'':',';
  334. $newsearchclass.=$dh.$searchclass[$i];
  335. $dohh=$hhone==1?$hh:$hh[$i];
  336. $dokeyboard=$keyboardone==1?$keyboard:$keyboard[$i];
  337. if(strlen($dokeyboard)>$public_r['max_keyboard'])
  338. {
  339. printerror("MinKeyboard",$getfrom,1);
  340. }
  341. $onewhere=SearchDoKeyboard($searchclass[$i],$dohh,$dokeyboard);
  342. if($onewhere)
  343. {
  344. $or=empty($where)?'':' '.$andor.' ';
  345. $where.=$or.'('.$onewhere.')';
  346. }
  347. }
  348. //参数错
  349. if(empty($newsearchclass))
  350. {
  351. printerror("SearchNotRecord",$getfrom,1);
  352. }
  353. if($where)
  354. {
  355. $add.=' and ('.$where.')';
  356. }
  357. $allwhere=$add.$addtime.$addprice;
  358. $keyboard=$keyboardone==1?SearchDoKeyboardVar($keyboard):'';
  359. $andsql=addslashes($allwhere);
  360. if(strlen($newsearchclass)>250||strlen($classid)>200||strlen($andsql)>3000||strlen($keyboard)>100||strlen($ttid)>200)
  361. {
  362. printerror("SearchNotRecord",$getfrom,1);
  363. }
  364. //验证码
  365. $checkpass=md5($allwhere.$tbname);
  366. $query="select count(*) as total from {$dbtbpre}ecms_".$tbname.($allwhere?' where '.substr($allwhere,5):'');
  367. $search_r=$empire->fetch1("select searchid from {$dbtbpre}enewssearch where checkpass='$checkpass' limit 1");
  368. $searchid=$search_r[searchid];
  369. //排序
  370. $orderby=RepPostVar($_POST['orderby']);
  371. $myorder=(int)$_POST['myorder'];
  372. if($orderby)
  373. {
  374. $orderr=ReturnDoOrderF($tempr[modid],$orderby,$myorder);
  375. $orderby=$orderr['returnf'];
  376. }
  377. else
  378. {
  379. $orderby='newstime';
  380. }
  381. //是否有历史记录
  382. if($searchid)
  383. {
  384. $search_num=$empire->gettotal($query);
  385. $sql=$empire->query("update {$dbtbpre}enewssearch set searchtime='$searchtime',result_num='$search_num',onclick=onclick+1,orderby='$orderby',myorder='$myorder',tempid='$s_tempid' where searchid='$searchid'");
  386. if(empty($search_num))
  387. {
  388. $searchid=0;
  389. }
  390. }
  391. else
  392. {
  393. $search_num=$empire->gettotal($query);
  394. if(empty($search_num))
  395. {
  396. $searchid=0;
  397. }
  398. else
  399. {
  400. $iskey=$keyboardone==1?0:1;
  401. $sql=$empire->query("insert into {$dbtbpre}enewssearch(searchtime,keyboard,searchclass,result_num,searchip,classid,onclick,orderby,myorder,checkpass,tbname,tempid,iskey,andsql,trueclassid) values('$searchtime','$keyboard','$newsearchclass','$search_num','$ip','$classid',1,'$orderby','$myorder','$checkpass','$tbname','$s_tempid','$iskey','$andsql','$trueclassid')");
  402. $searchid=$empire->lastid();
  403. }
  404. }
  405. //设置最后搜索时间
  406. $set1=esetcookie("lastsearchtime",$searchtime,$searchtime+3600*24);
  407. if(!$searchid)
  408. {
  409. printerror("SearchNotRecord",$getfrom,1);
  410. }
  411. else
  412. {
  413. Header("Location:result/?searchid=$searchid".$dogetvar);
  414. }
  415. db_close();
  416. $empire=null;
  417. ?>