<?php
require_once("inc/common.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
$searchLimitFlag = 0;$searchtime = 5; //searchLimitFlag为显示搜索控制标识(0不开启,1开启),searchtime为搜索间隔秒数
$searchword = getForm("searchword","both");$searchType = getForm("searchtype","both");$page = getForm("page","both");
if (empty($searchType)) { 
	$searchType=-1;
} else {
	if (is_numeric($searchType)){ $searchType=intval($searchType); } else { echoSaveStr("safe");exit;}
}

if (empty($page)) {
	$page=1;
} else { 
	if (is_numeric($page)){ $page=intval($page); } else { echoSaveStr("safe");exit;}
}
$searchword=$searchword ; if (strlen($searchword)>20) $searchword=(msubstr($searchword,20));
echoSearchPage();

function echoSearchPage() { 
	global $page,$searchLimitFlag,$searchtime,$searchType,$searchword;
	if (empty($searchword)){ echoMsgAndGo('请输入搜索关键字',$searchtime);}
	if ($searchLimitFlag == 1) checkSearchTimes($searchtime);
	$searchTemplatePath = DJCMS_TEMPLATE."newssearch.html";
	$pSize = getPageSize(loadFile($searchTemplatePath),"newssearch") ; if (empty($pSize)) $pSize=12;
	switch ($searchType){
		case -1 : $whereStr=" WHERE m_recycle=0 AND m_title LIKE '%$searchword%' or m_keyword LIKE '%$searchword%'";break;
		case 0 : $whereStr=" WHERE m_recycle=0 AND m_title '%$searchword%'";break;
		case 1 : $whereStr=" WHERE m_recycle=0 AND m_author LIKE '%$searchword%'";break;
		case 2 : $whereStr=" WHERE m_recycle=0 AND m_from LIKE '%$searchword%'";break;
		case 3 : $whereStr=" WHERE m_recycle=0 AND m_outline LIKE '%$searchword%'";break;
		case 4 : $whereStr=" WHERE m_recycle=0 AND m_letter = '".strtoupper($searchword)."'" ;break;
	}
	if ("".$searchword=="") $whereStr=" WHERE 1=2";
	$recordcount = DB::result_first("SELECT count(*) FROM ".DB::table('news')." ".$whereStr);
	$cacheName="parse_newssearch_";
	if (cacheStart == 1) {
		if (djcms::$content = memory('get', $cacheName)) {
        } else {
            parseSearchPart($searchTemplatePath);
            memory('set', $cacheName, djcms::$content);
        }
	} else {
	    parseSearchPart($searchTemplatePath);
	}
	$content = replaceStr(djcms::$content,"{djcms:newssearchword}",$searchword);
	$content = replaceStr($content,"{newssearchpage:page}",$page);
	djcms::$content = replaceStr($content,"{djcms:newssearchnum}",$recordcount);
	djcms::parseNewsPartList("",$page,"newssearch");
	djcms::parseIf();
	echo replaceStr(djcms::$content,"{djcms:runinfo}",getRunTime()) ;
}

function parseSearchPart($x) {
	djcms::$content=loadFile($x);
	djcms::parseTopAndFoot();
	djcms::parseSelf();
	djcms::parseGlobal();
	djcms::$content = replaceCurrentTypeId(djcms::$content);
	djcms::parseMenuList("");
	djcms::parseNewsList();
	djcms::parseAreaList();
	djcms::parseVideoList();
	djcms::parseTopicList();
}

function checkSearchTimes($searchtime) {
	global $searchword,$searchtime;
	if (!empty($searchword)) {
		if (getcookie("djcms_newssearch")=="ok") echoMsgAndGo('搜索限制为'.$searchtime.'秒一次',$searchtime);
		dsetcookie("djcms_newssearch", "ok", $searchtime);
	}
}
?>
