<?php 
defined('IN_DESTOON') or exit('Access Denied');
if($DT_BOT || $_POST) dhttp(403);
require DT_ROOT.'/module/'.$module.'/common.inc.php';
if(!check_group($_groupid, $MOD['group_search'])) include load('403.inc');
require DT_ROOT.'/include/post.func.php';
include load('search.lang');
$CP = $MOD['cat_property'] && $catid && $CAT['property'];
if(!$areaid && $cityid && strpos($DT_URL, 'areaid') === false) {
	$areaid = $cityid;
	$ARE = $AREA[$cityid];
}
$typeid = isset($typeid) ? intval($typeid) : 99;
$sfields = array($L['by_auto'], $L['by_title'], $L['by_content'], $L['by_introduce']);
$dfields = array('keyword', 'title', 'content', 'introduce');
$sorder  = array($L['order'], $L['order_auto'], $L['order_reward'], $L['order_answer'], $L['order_hits']);
//$dorder  = array($MOD['order'], '', 'credit DESC', 'answer DESC', 'hits DESC');
$gamename = strip_tags($gamename);
$mode = isset($mode) ? intval($mode) : 0;
$leixing = isset($leixing) ? intval($leixing) : 0;
$fuwu = isset($fuwu) ? intval($fuwu) : 0;
$qita = isset($qita) ? intval($qita) : 0;
$jianyi = isset($jianyi) ? intval($jianyi) : 0;
$order= isset($order) ? intval($order) : 0;
if($order<3){
$dorder = array('".$MOD[order]."',"addtime desc","hits desc");
}else{
$catid = $order;	
}
if(!$MOD['fulltext']) unset($sfields[2], $dfields[2]);
isset($fields) && isset($dfields[$fields]) or $fields = 0;
//isset($order) && isset($dorder[$order]) or $order = 0;
isset($username) && check_name($username) or $username = '';
$order_select  = dselect($sorder, 'order', '', $order);
$category_select = category_select('catid', $L['all_category'], $catid, $moduleid);
$area_select = $DT['city'] ? ajax_area_select('areaid', $L['all_area'], $areaid) : '';
$tags = array();
if($DT_QST) {
	if($kw) {
		if(strlen($kw) < $DT['min_kw'] || strlen($kw) > $DT['max_kw']) message(lang($L['word_limit'], array($DT['min_kw'], $DT['max_kw'])), $MOD['linkurl'].'search.php');
		if($DT['search_limit'] && $page == 1) {
			if(($DT_TIME - $DT['search_limit']) < get_cookie('last_search')) message(lang($L['time_limit'], array($DT['search_limit'])), $MOD['linkurl'].'search.php');
			set_cookie('last_search', $DT_TIME);
		}
	}

	$pptsql = '';
	if($CP) {
		require DT_ROOT.'/include/property.func.php';
		$PPT = property_condition($catid);
		foreach($PPT as $k=>$v) {
			$PPT[$k]['select'] = '';
			$oid = $v['oid'];
			$tmp = 'ppt_'.$oid;
			if(isset($$tmp)) {
				$PPT[$k]['select'] = $tmp = $$tmp;
				if($tmp && in_array($tmp, $v['options'])) {
					$tmp = 'O'.$oid.':'.$tmp.';';
					$pptsql .= " AND pptword LIKE '%$tmp%'";
				}
			}
		}
	}
	$fds = $MOD['fields'];
	$condition = '';
	if(isset($gamename)) $condition .= " and gamename='$gamename'";
	if($mode>0){
	$dmode = str_split($mode);
    $mode = implode(',',$dmode);
    $condition .= " and mode in ($mode)";
}   
if($leixing>0){
	$dleixing = str_split($leixing);
    $leixing = implode(',',$dleixing);
    $condition .= " and leixing in ($leixing)";
}
if($fuwu>0){
	$dfuwu = str_split($fuwu);
    $fuwu = implode(',',$dfuwu);
    $condition .= " and fuwu in ($fuwu)";
}
if($qita>0){
	$dqita = str_split($qita);
    $qita = implode(',',$dqita);
    $condition .= " and qita in ($qita)";
}
if($jianyi>0){
	$djianyi = str_split($jianyi);
    $jianyi = implode(',',$djianyi);
    $condition .= " and jianyi in ($jianyi)";
}
	if($catid) $condition .= ($CAT['child']) ? " AND catid IN (".$CAT['arrchildid'].")" : " AND catid=$catid";
	if($areaid) $condition .= ($ARE['child']) ? " AND areaid IN (".$ARE['arrchildid'].")" : " AND areaid=$areaid";
	if($typeid == 0) {
		$condition .= " AND process=3";
	} else if($typeid == 1) {
		$condition .= " AND process=1";
	} else if($typeid == 2) {
		$condition .= " AND process=2";
	} else if($typeid == 3) {
		$condition .= " AND process=1 AND answer=0";
	} else if($typeid == 4) {
		$condition .= " AND level>0";
	} else if($typeid == 5) {
		$hc = intval($MOD['highcredit']);
		if($hc > 0) $condition .= " AND process=1 AND credit>$hc";
	} else if($typeid == 6) {
		$condition .= " AND process=3 AND expert<>''";
	}
	if($username) $condition .= " AND username='$username'";
	if($dfields[$fields] == 'content') {
		if($keyword && $MOD['fulltext'] == 1) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
		$condition = str_replace('AND ', 'AND i.', $condition);
		$condition = str_replace('i.content', 'd.content', $condition);
		$condition = "i.status=3 AND i.itemid=d.itemid".$condition;
		if($keyword && $MOD['fulltext'] == 2) $condition .= " AND MATCH(`content`) AGAINST('$kw'".(preg_match("/[+-<>()~*]/", $kw) ? ' IN BOOLEAN MODE' : '').")";
		$table = $table.' i,'.$table_data.' d';
		$fds = 'i.'.str_replace(',', ',i.', $fds);
	} else {
		if($keyword) $condition .= " AND $dfields[$fields] LIKE '%$keyword%'";
		if($pptsql) $condition .= $pptsql;//PPT
		$condition = "status=3".$condition;
	}
	$pagesize = $MOD['pagesize'];
	$offset = ($page-1)*$pagesize;
	$items = $db->count($table, $condition, $DT['cache_search']);
	$pages = pages($items, $page, $pagesize);
	if($items) {
		$order = $dorder[$order] ? " ORDER BY $dorder[$order]" : '';
		$result = $db->query("SELECT $fds FROM {$table} WHERE {$condition}{$order} LIMIT {$offset},{$pagesize}", $DT['cache_search'] && $page == 1 ? 'CACHE' : '', $DT['cache_search']);
		if($kw) {
			$replacef = explode(' ', $kw);
			$replacet = array_map('highlight', $replacef);
		}
		while($r = $db->fetch_array($result)) {
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['editdate'] = timetodate($r['edittime'], 5);
			if($lazy && isset($r['thumb']) && $r['thumb']) $r['thumb'] = DT_SKIN.'image/lazy.gif" original="'.$r['thumb'];
			$r['alt'] = $r['title'];
			$r['title'] = set_style($r['title'], $r['style']);
			if($kw) $r['title'] = str_replace($replacef, $replacet, $r['title']);
			$r['linkurl'] = $MOD['linkurl'].$r['linkurl'];
			$tags[] = $r;
		}
		$db->free_result($result);
		if($page == 1 && $kw) keyword($kw, $items, $moduleid);
	}
}
$showpage = 1;
$datetype = 5;
$target = '_blank';
$cols = 5;
$class = '';
$seo_file = 'search';
include DT_ROOT.'/include/seo.inc.php';
include template('search', $module);
?>