<?php
if(!defined('IN_MYMPS')) exit('FORBIDDEN');
define('CURSCRIPT','index');

$city=='all' && msetcookie('fcityid',0,3600*24);
$city=='all' && msetcookie('cityid',0,3600*24);

if(!$cityid && !is_robot() && $city!='all' && in_array($mymps_global['cfg_redirectpage'],array('nchome','ncchangecity'))){

	if($fcityid > 0){
		$fcity = get_city_caches($fcityid);
		if($fcity[domain]) write_msg("",$fcity[domain]);
		exit;
	}
	
	if(is_array($fromcity = get_ip2city($ip))){
		@header("location:".$fromcity['domain'],TRUE,301);
		exit;
	} elseif($mymps_global['cfg_redirectpage'] == 'ncchangecity') {
		$loc					= get_location('index');
		$page_title				= $loc['page_title'];
		$seo = get_seoset();
		$seo['seo_keywords'] = str_replace('{city}','',$seo['seo_keywords']);
		$seo['seo_description'] = str_replace('{city}','',$seo['seo_description']);
		$mymps_global = array_merge($mymps_global,$seo);
		$lastupdate=$db->getOne("SELECT begintime FROM `{$db_mymps}information` ORDER BY begintime DESC LIMIT 0,1");
		if($lastupdate+172800<$timestamp) $lastupdate=$timestamp;
		globalassign();
		$city=get_city_caches(0);
		include mayi_tpl('changecity');
		exit;
	}

}elseif(!$cityid && $mymps_global['cfg_redirectpage'] == 'changecity') {
	$loc					= get_location('index');
	$page_title				= $loc['page_title'];
	$seo = get_seoset();
	$seo['seo_keywords'] = str_replace('{city}','',$seo['seo_keywords']);
	$seo['seo_description'] = str_replace('{city}','',$seo['seo_description']);
	$mymps_global = array_merge($mymps_global,$seo);
	$lastupdate=$db->getOne("SELECT begintime FROM `{$db_mymps}information` ORDER BY begintime DESC LIMIT 0,1");
	if($lastupdate+172800<$timestamp) $lastupdate=$timestamp;
	globalassign();
	$city=get_city_caches(0);
	include mayi_tpl('changecity');
	exit;
}elseif(is_numeric($mymps_global['cfg_redirectpage'])){
	$r = get_city_caches($mymps_global['cfg_redirectpage']);
	if($ifmobile){
		@header("location:".$r['mdomain'],TRUE,301);
	}else{
		@header("location:".$r['domain'],TRUE,301);
	}
	unset($r);
	exit;
}


$city = get_city_caches($cityid,$areaid);

$city['title'] = str_replace('{city}',$city['cityname'],$city['title']);
$city['keywords'] = str_replace('{city}',$city['cityname'],$city['keywords']);
$city['description'] = str_replace('{city}',$city['cityname'],$city['description']);

$cache = get_cache_config();
require MYMPS_INC.'/cachepages.class.php';
$cachepages = new cachepages($cache['site']['time'],'index_'.$cityid,$areaid);
$cachetime = $cache['site']['time'] > 0 ? true : false;
$cachepages->cacheCheck();
unset($cache);

if($city['cityid']) $city_limit = "AND a.cityid = '$city[cityid]'";
if($areaid) $city_limit .= "AND a.areaid = '$areaid'";//区县分站新增

$tpl_index = get_tpl_index();
$tpl_index = $tpl_index['tpl_set'];

$maincity = get_city_caches(0);
$independency = explode(',',$mymps_global['cfg_independency']);
$independency = is_array($independency) ? $independency : array();

if(!$cityid || in_array('friendlink',$independency)){
	$friendlink = $city['flink'] ? $city['flink'] : $maincity['flink'];
}elseif($cityid){
	$friendlink = $city['flink'];
}
unset($city['flink'],$maincity['flink']);

$focus = mymps_get_focus('index',5,$cityid,$areaid);
if(!$cityid || in_array('focus',$independency)){
	$focus = $focus ? $focus : mymps_get_focus('index',5,NULL);
}

$announce = mymps_get_announce($tpl_index['announce'],$cityid,$areaid);
if(!$cityid || in_array('announce',$independency)){
	$announce = $announce ? $announce : mymps_get_announce($tpl_index['announce'],NULL);
}

$telephone = mymps_get_telephone($tpl_index[telephone],$cityid,$areaid);
if(!$cityid || in_array('telephone',$independency)){
	$telephone = $telephone ? $telephone : mymps_get_telephone($tpl_index['telephone'],NULL);
}

$lifebox = mymps_get_lifebox($tpl_index[lifebox],$cityid,$areaid);
if(!$cityid || in_array('lifebox',$independency)){
	$lifebox = $lifebox ? $lifebox : mymps_get_lifebox($tpl_index['lifebox'],NULL);
}

if(in_array('advertisement',$independency)){
	$city['advertisement'] = empty($city['advertisement']) ? $maincity['advertisement'] : $city['advertisement'];
}
$maincity = NULL;
$loc					= get_location('index');
$location				= $loc['location'];
$page_title				= $loc['page_title'];
unset($loc);

$seo = get_seoset();
if($city['cityid']>0){
	$city['title'] = $city['title'] ? $city['title'] : $page_title;
	$city['keywords'] = $city['keywords'] ? $city['keywords'] : str_replace('{city}',$city['cityname'],$seo['seo_keywords']);
	$city['description'] = $city['description'] ? $city['description'] : str_replace('{city}',$city['cityname'],$seo['seo_description']);
} else {
	$city['title'] = $page_title;
	$city['keywords'] = str_replace('{city}','',$seo['seo_keywords']);
	$city['description'] = str_replace('{city}','',$seo['seo_description']);
}
$city['title']=$page>1?$city['title'].' - 第'.$page.'页':$city['title'];
$mymps_global = array_merge($mymps_global,$seo);

$advertisement			= get_advertisement('index');
$adveritems				= $city['advertisement'];
$advertisement['type']	= $advertisement['all'] ? (is_array($advertisement['type']) ? array_merge($advertisement['all']['type'],$advertisement['type']) : $advertisement['all']['type']) : $advertisement['type'];

globalassign();

if($tpl_index['banmian'] == 'portal'){
	
	$index_cat = get_categories_tree(0,'category');

	if(ifplugin('group')){
		require MYMPS_ROOT.'/plugin/group/include/functions.php';
		$groups = mymps_get_groups(3,NULL,$cityid,$areaid);
		$groupclass = get_group_class();
	}
	if(ifplugin('goods')){
		require MYMPS_ROOT.'/plugin/goods/include/functions.php';
		$goods = mymps_get_goods($tpl_index['goods'],1,NULL,NULL,NULL,NULL,$cityid,$areaid);
	}
	$in = $tpl_index['portal']['ershou'].','.$tpl_index['portal']['zhaopin'].','.$tpl_index['portal']['ershoufang'].','.$tpl_index['portal']['jianzhi'].','.$tpl_index['portal']['zufang'];
	if($query = $db->query("SELECT catid,dir_typename FROM `{$db_mymps}category` WHERE catid IN($in)")){
		while($row = $db->fetchRow($query)){
			$hd[$row['catid']]['dir_typename'] = $row['dir_typename'];
		}
	}
	
	$portaluri['moreershou'] = Rewrite('category',array('catid'=>$tpl_index['portal']['ershou'],'dir_typename'=>$hd[$tpl_index['portal']['ershou']]['dir_typename']));
	$portaluri['catidershou'] = $tpl_index['portal']['ershou'];

	$portaluri['morezufang'] = Rewrite('category',array('catid'=>$tpl_index['portal']['zufang'],'dir_typename'=>$hd[$tpl_index['portal']['zufang']]['dir_typename']));
	$portaluri['catidzufang'] = $tpl_index['portal']['zufang'];
	$portaluri['postzhaopin'] = $mymps_global['cfg_postfile'].'?catid='.$tpl_index['portal']['zhaopin'];
	$portaluri['morezhaopin'] = Rewrite('category',array('catid'=>$tpl_index['portal']['zhaopin'],'dir_typename'=>$hd[$tpl_index['portal']['zhaopin']]['dir_typename']));
	$portaluri['catidzhaopin'] = $tpl_index['portal']['zhaopin'];
	$portaluri['moreershoufang'] = Rewrite('category',array('catid'=>$tpl_index['portal']['ershoufang'],'dir_typename'=>$hd[$tpl_index['portal']['ershoufang']]['dir_typename']));
	$portaluri['catidershoufang'] = $tpl_index['portal']['ershoufang'];
	$portaluri['morejianzhi'] = Rewrite('category',array('catid'=>$tpl_index['portal']['jianzhi'],'dir_typename'=>$hd[$tpl_index['portal']['jianzhi']]['dir_typename']));
	$portaluri['catidjianzhi'] = $tpl_index['portal']['jianzhi'];
	$arr = $row = $query = NULL;
	$zhaopin = array();
	$query = $db -> query("SELECT a.cityid,a.id,a.title,a.ifred,a.ifbold,a.dir_typename,g.{$tpl_index[portali][company]} FROM `{$db_mymps}information` AS a LEFT JOIN `{$db_mymps}information_{$tpl_index[portal][zhaopinmod]}` AS g ON a.id = g.id WHERE ".get_children($tpl_index[portal][zhaopin])." AND (a.info_level = 1 OR a.info_level = 2) {$city_limit} ORDER BY a.begintime DESC LIMIT 0,12");
	
	while($row = $db -> fetchRow($query)){
		$arr['id']        = $row['id'];
		$arr['title']     = $row['title'];
		$arr['ifred']     = $row['ifred'];
		$arr['ifbold']    = $row['ifbold'];
		$arr[$tpl_index['portali']['company']] = $row[$tpl_index['portali']['company']];
		$arr['uri']       = Rewrite('info',array('id'=>$row['id'],'dir_typename'=>$row['dir_typename'],'cityid'=>$row['cityid']));
		$zhaopin[$row['id']]      = $arr;
	}
	$arr = $row = $query = NULL;
	$jianzhi = array();
	$query = $db -> query("SELECT a.id,a.title,a.ifred,a.ifbold,a.contact_who,a.dir_typename,a.cityid FROM `{$db_mymps}information` AS a WHERE ".get_children($tpl_index[portal][jianzhi])." AND (a.info_level = 1 OR a.info_level = 2) {$city_limit} ORDER BY a.begintime DESC LIMIT 0,4");
	if($query){
		while($row = $db -> fetchRow($query)){
			$arr['id']        = $row['id'];
			$arr['title']     = $row['title'];
			$arr['ifred']     = $row['ifred'];
			$arr['ifbold']    = $row['ifbold'];
			$arr['contact_who']    = $row['contact_who'];
			$arr['uri']       = Rewrite('info',array('id'=>$row['id'],'dir_typename'=>$row['dir_typename'],'cityid'=>$row['cityid']));
			$jianzhi[$row['id']]      = $arr;
		}
	}
	$arr = $row = $query = NULL;
	$esf = array();
	$query = $db -> query("SELECT a.cityid,a.id,a.title,a.ifred,a.ifbold,a.img_path,a.dir_typename,g.{$tpl_index[portali][acreage]},g.{$tpl_index[portali][prices]} FROM `{$db_mymps}information` AS a LEFT JOIN `{$db_mymps}information_{$tpl_index[portal][ershoufangmod]}` AS g ON a.id = g.id WHERE ".get_children($tpl_index[portal][ershoufang])." AND (a.info_level = 1 OR a.info_level = 2) {$city_limit} ORDER BY a.begintime DESC LIMIT 0,12");
	while($row = $db -> fetchRow($query)){
		$arr['id']        = $row['id'];
		$arr['title']     = $row['title'];
		$arr['ifred']     = $row['ifred'];
		$arr['ifbold']    = $row['ifbold'];
		$row['img_path']   = $row['img_path']?$row['img_path']:'/images/nophoto.gif';
		$arr['img_path']  = !strstr($row['img_path'],'http')?$mymps_global['SiteUrl'].$row['img_path']:$row['img_path'];
		$arr[$tpl_index[portali][prices]] = $row[$tpl_index[portali][prices]];
		$arr[$tpl_index[portali][acreage]]    = $row[$tpl_index[portali][acreage]];
		$arr['uri']       = Rewrite('info',array('id'=>$row['id'],'dir_typename'=>$row['dir_typename'],'cityid'=>$row['cityid']));
		$esf[$row['id']]      = $arr;
	}
	
	$arr = $row = $query = NULL;
	$czf = array();
	$query = $db -> query("SELECT a.cityid,a.id,a.title,a.ifred,a.ifbold,a.img_path,a.dir_typename FROM `{$db_mymps}information` AS a WHERE ".get_children($tpl_index[portal][zufang])." AND (a.info_level = 1 OR a.info_level = 2) {$city_limit} ORDER BY a.begintime DESC LIMIT 0,4");
	while($row = $db -> fetchRow($query)){
		$arr['id']        = $row['id'];
		$arr['title']     = $row['title'];
		$arr['ifred']     = $row['ifred'];
		$arr['ifbold']    = $row['ifbold'];
		$row['img_path']   = $row['img_path']?$row['img_path']:'/images/nophoto.gif';
		$arr['img_path']  = !strstr($row['img_path'],'http')?$mymps_global['SiteUrl'].$row['img_path']:$row['img_path'];
		$arr['uri']       = Rewrite('info',array('id'=>$row['id'],'dir_typename'=>$row['dir_typename'],'cityid'=>$row['cityid']));
		$czf[$row['id']]      = $arr;
	}
	
	unset($arr);
	$ershou = $ershou_img = array();
	$inershou = get_children($tpl_index[portal][ershou]);
	$query	 = $db -> query("SELECT a.cityid,a.id,a.title,a.ifred,a.ifbold,a.begintime,a.catname,a.dir_typename FROM {$db_mymps}information AS a  WHERE ".$inershou." AND (a.info_level = 1 OR a.info_level = 2) {$city_limit} ORDER BY a.begintime DESC LIMIT 0,10");
	while($row   = $db -> fetchRow($query)){
		$arr['id'] 	      = $row['id'];
		$arr['title'] 	  = $row['title'];
		$arr['begintime'] = $row['begintime'];
		$arr['catname']	  = $row['catname'];
		$arr['ifred']	  = $row['ifred'];
		$arr['ifbold']	  = $row['ifbold'];
		$arr['uri']		  = Rewrite('info',array('id'=>$row['id'],'dir_typename'=>$row['dir_typename'],'cityid'=>$row['cityid']));
		$ershou[$row['id']] = $arr;
	}
	$arr = $row = $query = NULL;
	$query = $db -> query("SELECT a.cityid,a.id,a.title,a.img_path,a.dir_typename FROM `{$db_mymps}information` AS a WHERE ".$inershou." AND (a.info_level = 1 OR a.info_level = 2) AND img_path != '' {$city_limit} ORDER BY a.begintime DESC LIMIT 0,6");
	if($query){
		while($row = $db -> fetchRow($query)){
			$arr['id']        = $row['id'];
			$arr['title']     = $row['title'];
			$row['img_path']   = $row['img_path']?$row['img_path']:'/images/nophoto.gif';
			$arr['img_path']  = !strstr($row['img_path'],'http')?$mymps_global['SiteUrl'].$row['img_path']:$row['img_path'];
			$arr['uri']       = Rewrite('info',array('id'=>$row['id'],'dir_typename'=>$row['dir_typename'],'cityid'=>$row['cityid']));
			$ershou_img[$row['id']]      = $arr;
		}
	}

} elseif($tpl_index['banmian'] == 'classic'){

	$index_cat = get_categories_tree(0,'category');
	if(is_array($index_cat)){
		foreach($index_cat as $firstcatkey => $firstcatval){
			$idin = get_page_idin2("id","SELECT a.id FROM `{$db_mymps}information` AS a WHERE a.gid='$firstcatval[catid]' AND (a.info_level = 1 OR a.info_level = 2) {$city_limit} ORDER BY a.begintime DESC",$tpl_index['foreachinfo']);
			if($idin){
				$query	= $db -> query("SELECT a.cityid,a.img_count,a.catname,a.dir_typename,a.id,a.userid,a.catid,a.title,a.ifred,a.ifbold,a.begintime,a.endtime,b.areaname FROM `{$db_mymps}information` AS a LEFT JOIN `{$db_mymps}area` AS b ON a.areaid = b.areaid WHERE a.id IN ($idin) ORDER BY a.begintime DESC");
				$index_cat[$firstcatval['catid']]['information'] = array();
				while($irow   = $db -> fetchRow($query)){
					$arr['id'] 	      = $irow['id'];
					$arr['title'] 	  = $irow['title'];
					$arr['begintime'] = $irow['begintime'];
					$arr['endtime'] = $irow['endtime'];
					$arr['catname']	  = $irow['catname'];
					$arr['areaname']  = $irow['areaname'];
					$arr['ifred']	  = $irow['ifred'];
					$arr['ifbold']	  = $irow['ifbold'];
					$arr['img_count']	  = $irow['img_count'];
					$arr['caturi']	  = Rewrite('category',array('dir_typename'=>$irow['dir_typename'],'catid'=>$irow['catid'],'domain'=>$city['domain']));
					$arr['uri']		  = Rewrite('info',array('id'=>$irow['id'],'dir_typename'=>$irow['dir_typename'],'cityid'=>$irow['cityid']));
					$index_cat[$firstcatval['catid']]['information'][] = $arr;
				}
			}
		}
	}
	$tpl_index[classic][cats] = $tpl_index[classic][cats] ? $tpl_index[classic][cats]+1 : '12';	
	
} elseif($tpl_index['banmian'] == 'simpleT'){
		
	if(ifplugin('goods')){
		require MYMPS_ROOT.'/plugin/goods/include/functions.php';
		$goods = mymps_get_goods($tpl_index['goods'],1,NULL,NULL,NULL,NULL,$cityid,$areaid);
	}
	$index_cat = get_categories_tree(0,'category');
	$firstcats = read_static_cache('tpl_smp_first');
	$secondcats = read_static_cache('tpl_smp_second');
	$thirdcats = read_static_cache('tpl_smp_third');
	$fourthcats = read_static_cache('tpl_smp_fourth');
	$lastupdate=$db->getOne("SELECT a.begintime FROM `{$db_mymps}information` AS a WHERE 1 {$city_limit} ORDER BY a.begintime DESC LIMIT 0,1");
	if($lastupdate+172800<$timestamp) $lastupdate=$timestamp;
}elseif($tpl_index['banmian'] == 'gezi'){
	
	$where .= " WHERE 1 ";
	$where .= " AND (a.info_level = 1 OR a.info_level = 2)";
	$where .= $city_limit;
	$orderby	= " ORDER BY a.upgrade_type_index DESC,a.begintime DESC" ;

	$param = setParam(array('catid'),'active','news-');
	$rows_num   =  $db -> getOne("SELECT COUNT(a.id) FROM `{$db_mymps}information` AS a  {$where}");
	
	$idin = get_page_idin("id","SELECT a.id FROM `{$db_mymps}information` AS a {$where}{$orderby}",$tpl_index[newinfo]);

	$sql = "SELECT a.*,b.areaname FROM {$db_mymps}information AS a LEFT JOIN `{$db_mymps}area` AS b ON a.areaid = b.areaid WHERE a.id IN ($idin) {$orderby}";
	$info_list = array();
	
	$page1 = $idin ? $db -> getAll($sql) : array();
	foreach($page1 as $key => $val){
		$infolist['id']				= $val['id'];
		$infolist['begintime']		= $val['begintime'];
		$infolist['title']			= $val['title'];
		$infolist['ifred']			= $val['ifred'];
		$infolist['ifbold']			= $val['ifbold'];
		$infolist['tel']			= $val['tel'];
		$infolist['img_count']		= $val['img_count'];
		$infolist['content']		= clear_html($val['content']);
		$infolist['areaname']		= $val['areaname'];
		$infolist['catname']		= $val['catname'];
		$infolist['hit']			= $val['hit'];
		$infolist['contact_who']	= $val['contact_who'];
		$infolist['poster']			= !empty($val['userid']) ? '<a target="black" href='.Rewrite('space',array("user"=>$val["userid"])).'>'.$val["userid"].'</a>' : ($val['contact_who'] ? $val['contact_who'] : '游客');
		$infolist['img_path']		= $val['img_path'];
		$infolist['img_count']		= $val['img_count'];
		$infolist['uri']			= Rewrite('info',array('id'=>$val['id'],'dir_typename'=>$val['dir_typename'],'cityid'=>$val['cityid']));
		$infolist['info_level']		= $val['info_level'];
		$infolist['upgrade_type_index']	= $val['upgrade_type_index'];
		$infolist['certify']		= $val['certify'];
		$info_list[$val['id']]		= $infolist;
		$ids = true;
	}
	
	$idin = $ids ? " AND a.id IN ($idin) " : "";
	
	$pageline = NULL;
	$pageview	= page2('active');
	
} elseif($tpl_index['banmian'] == 'simple'){
		
	if(ifplugin('goods')){
		require MYMPS_ROOT.'/plugin/goods/include/functions.php';
		$goods = mymps_get_goods($tpl_index['goods'],1,NULL,NULL,NULL,NULL,$cityid,$areaid);
	}
	$index_cat = get_categories_tree(0,'category');
	$firstcats = read_static_cache('tpl_smp_first');
	$secondcats = read_static_cache('tpl_smp_second');
	$thirdcats = read_static_cache('tpl_smp_third');
	$fourthcats = read_static_cache('tpl_smp_fourth');
	$lastupdate=$db->getOne("SELECT a.begintime FROM `{$db_mymps}information` AS a WHERE 1 {$city_limit} ORDER BY a.begintime DESC LIMIT 0,1");
	if($lastupdate+172800<$timestamp) $lastupdate=$timestamp;
}

include mayi_tpl('index_'.$tpl_index['banmian']);

is_object($db) && $db->Close();
$cachetime && $cachepages->caching();

$city = $maincity = $advertisement =NULL;
unset($city,$maincity,$advertisement);
?>