<?php
	require '../include/common.inc.php';
	$mod=preg_replace('/[^a-z0-9_]/i','',substr(dirname(__FILE__),strlen(SYSTEM_ROOT)));
	$moduleid=$moduleobj->getmoduleid($mod);
	$_mod=$modconfig=$moduleobj->get(intval($moduleid));
	$uppermod=strtoupper($mod);
	$$uppermod=$settings=$configobj->getconfig($_SESSION['siteid'],$moduleid);
	$action=empty($action)?'index':preg_replace('/[^0-9a-z_]/i','',$action);
	$page=isset($page)?max(1,intval($page)):1;

	switch($action)
	{			
			case 'show':
				$contentid=isset($contentid)?intval($contentid):0;
				$coninfo=$conobj->get($contentid);
				
				if(!$coninfo || $coninfo['ishidden'])
				{
					header('location:'.LA_PATH);exit();
				}

				if($coninfo['isdeleted'] && !$preview)
				{
					header('location:'.LA_PATH);exit();
				}
				
				$_model=$modelinfo=$modelobj->info(intval($coninfo['modelid']));
				
				$hash_table=LA_PRE.get_hash_table($modelinfo['tbname'].'_data',$contentid,$modelobj->subformcount2(intval($coninfo['modelid'])));
				$baseinfo=$db->fetch_one("SELECT * FROM `".LA_PRE.$modelinfo['tbname']."` WHERE `".LA_PRE.$modelinfo['tbname']."`.`id`=$contentid");
				$moreinfo=$db->fetch_one("SELECT * FROM `$hash_table` WHERE `$hash_table`.`mid`=$contentid");
				$_con=array_merge($coninfo,$baseinfo,$moreinfo);
				$_type=$conobj->typeinfo(intval($_con['typeid']));
				$_mod=$configobj->getconfig($_SESSION['siteid'],$coninfo['moduleid']);
				$_mod['comment_membergrade']=explode(',',$_mod['comment_membergrade']);
				$_mod['commentpass_membergrade']=explode(',',$_mod['commentpass_membergrade']);
				$_mod['viewcontact_membergrade']=explode(',',$_mod['viewcontact_membergrade']);
				$_mod['duoshuo_code']=$configobj->getconfigcode($_mod['duoshuo_code']);

				if($do_comment)
				{
					if(!in_array($_gradeid,$_mod['comment_membergrade']))
					{
						showjsmsg('评论权限不够！');
					}

					$_comment=array();
					$_comment['contentid']=$contentid;
					$_comment['moduleid']=$_con['moduleid'];
					$_comment['userid']=$_userid;
					$_comment['content']=sub_string(trim(htmlspecialchars($content)),255);
					$_comment['inputtime']=TIME;
					$_comment['ip']=IP;
					$_comment['siteid']=$_SESSION['siteid'];
					$_comment['status']=in_array($_gradeid,$_mod['commentpass_membergrade'])?1:0;
					$conobj->comment_submit($_comment);
					$conobj->baseset(array('comments'=>max(0,$_con['comments']+1)),$contentid);
					showjsmsg('评论成功！',formatconurl($contentid));
				}	
				else
				{
					$conobj->click($contentid);
					$_pagecon=!isset($_GET['nopage'])?explode('_baidu_page_break_tag_',$_con['content']):array(str_replace('_baidu_page_break_tag_','',$_con['content']));
					$_con['content']=(isset($_pagecon[max(0,$page-1)]) && !isset($_GET['nopage']))?$_pagecon[max(0,$page-1)]:$_pagecon[0];
					@extract($_con,EXTR_SKIP);

					/**
					分页
						$PAGE['content']
					**/
					$PAGE['content']='';
					$query=array();

					if(sizeof($_pagecon)>1)
					{
						for($i=1;$i<=sizeof($_pagecon);$i++)
						{
							$_GET['page']=$i;
							foreach($_GET as $key => $value)
							{
								$_GET[$key]=preg_replace('/[^a-z0-9_]/i','',$value);
								$query[$key]=$key.'='.$_GET[$key];
							}

							if(!$_mod['is_rewrite'])
							{
								$PAGE['content'].='<a '.($page==$i?'class="hover"':'').' href="'.LA_PATH.$mod.'/index.php?'.implode('&',$query).'">'.$i.'</a>';
							}
							else
							{
								$PAGE['content'].='<a '.($page==$i?'class="hover"':'').' href="'.LA_PATH.$mod.'/show-'.$contentid.'-'.$i.'.html">'.$i.'</a>';
							}
						}
						
						$PAGE['content'].='<a '.(isset($_GET['nopage'])?'class="hover"':'').' href="'.LA_PATH.'index.php?'.implode('&',$query).'&nopage=1">阅读全文</a>';
					}
					////兼容其他产品
					$content_page=$PAGE['content'];
					////兼容其他产品
					$_fields=$db->fetch_all("SELECT * FROM `".LA_PRE."field` WHERE `".LA_PRE."field`.`modelid`=".intval($_con['modelid']));
					include template($_type['article_tlp'],$mod);
				}
				break;
			case 'list':
				$typeid=isset($typeid)?intval($typeid):0;
				$_type=$typeinfo=$conobj->typeinfo($typeid);
				$_mod=$configobj->getconfig($_SESSION['siteid'],$_type['moduleid']);

				@extract($_type,EXTR_SKIP); // 兼容 niubicms 产品
				if(!$typeinfo)
				{
					header('location:'.LA_PATH);exit();
				}
				
				$_GET=la_htmlspecialchars($_GET);
				ksort($_GET);
				$orderby=' ORDER BY a.updatetime DESC';
				$pagesize=isset($pagesize)?max(1,intval($pagesize)):36;
				$areaid=intval($_GET['areaid']);

				$k=isset($k) && trim($k)?htmlspecialchars($k):'';

				if(SITETYPE && $_SESSION['siteid'])
				{
					$where='a.siteid IN('.implode(',',$siteidobj->getchildid($_SESSION['siteid'])).') AND a.status=1 AND a.isdeleted=0 AND a.ishidden=0';
				}
				else
				{
					$where='a.status=1 AND a.isdeleted=0 AND a.ishidden=0';
				}
				$where.=$areaid?' AND a.areaid IN('.implode(',',$areaobj->getchildid($areaid)).')':'';

				$where.=$typeid?' AND a.typeid IN('.implode(',',getchildtypeid($typeid)).')':'';

				$modelinfo=$modelobj->info($_type['modelid']);
				$modelfields=array();

				if($modelinfo)
				{
					$modelfields=$modelobj->getmodelfields($_type['modelid']);
					foreach($modelfields as $r)
					{
						if(isset($_GET[$r['enname']]) && in_array($r['form'],array('select','radio')) && intval($_GET[$r['enname']]))
						{
							$where.=' AND c.'.$r['enname'].'='.intval($_GET[$r['enname']]);
						}
					}
					$sql="SELECT * FROM ".LA_PRE."content a ,".LA_PRE.$modelinfo['tbname']." c WHERE a.contentid=c.id AND ".$where.$orderby." LIMIT ".($page-1)*$pagesize.",$pagesize";
				}
				else
				{
					$sql="SELECT * FROM ".LA_PRE."content a WHERE ".$where.$orderby." LIMIT ".($page-1)*$pagesize.",$pagesize";
				}

				$count_sql=preg_replace("/^SELECT([^(]+)\s*FROM(.+)(ORDER BY.+)$/i", "SELECT COUNT(*) AS `count` FROM\\2", $sql);
				$totalnum=get_cache_counts($count_sql); 
				
				$pagenum=ceil($totalnum/$pagesize);
				$page=isset($page)?min($pagenum,max(1,intval($page))):1;
				$pagearray=range(1,$pagenum);
				$pagearray=array_slice($pagearray,($page/10 < 1?0:floor($page/10)*10) ,10);

				$data=$cacheobj->get($sql);
				if(!$data)
				{
					$data=$db->fetch_all($sql);
					if($data)
					{
						$cacheobj->set($sql,$data);
					}
				}

				$list_page=array();

				$querystring=array();

				if($_GET)
				{
					if(isset($_GET['page']))unset($_GET['page']);
					foreach($_GET as $key => $val)
					{
						if($_mod['is_rewrite'])
						{
							$querystring[]=$key.'-'.$val;
						}
						else
						{
							$querystring[]=$key.'='.$val;
						}
					}
				}
				if($page!=1)
				{
					
					if($_mod['is_rewrite'])
					{
						$querystring['page']='page-'.max($page-1,1).'.html';
						$list_page[]='<a class="page_end" href="'.LA_PATH.$mod.'/'.implode('-',$querystring).'">上一页</a>';
					}
					else
					{
						$querystring['page']='page='.max($page-1,1);
						$list_page[]='<a class="page_end" href="'.LA_PATH.'index.php?'.implode('&',$querystring).'">上一页</a>';
					}
				}

				foreach($pagearray as $_page)
				{
					if($page==$_page)
					{
						$list_page[]='<a href="javascript:void(0);" class="page_active">'.$_page.'</a>';
					}
					else
					{
						if($_mod['is_rewrite'])
						{
							$querystring['page']='page-'.$_page.'.html';
							$list_page[]='<a class="page_mo" href="'.LA_PATH.$mod.'/'.implode('-',$querystring).'">'.$_page.'</a>';
						}
						else
						{
							$querystring['page']='page='.$_page;
							$list_page[]='<a class="page_mo" href="'.LA_PATH.'index.php?'.implode('&',$querystring).'">'.$_page.'</a>';
						}
					}
				}

				if($page!=$pagenum)
				{
					if($_mod['is_rewrite'])
					{
						$querystring['page']='page-'.min($page+1,$pagenum).'.html';
						$list_page[]='<a class="page_end" href="'.LA_PATH.$mod.'/'.implode('-',$querystring).'">下一页</a>';
					}
					else
					{
						$querystring['page']='page='.min($page+1,$pagenum);
						$list_page[]='<a class="page_end" href="'.LA_PATH.'index.php?'.implode('&',$querystring).'">下一页</a>';
					}
				}
				
				$PAGE['list']=$data?implode(' ',$list_page):'';
				if(isfinaltypeid($typeid))
				{
					include template($_type['list_tlp'],$mod);
				}
				else
				{
					include template($_type['index_tlp'],$mod);
				}
				break;
			default:
				include template('index',$mod);
				break;
	}
?>
