<?php
require_once (dirname(__FILE__) . "/include/common.inc.php");
header("Content-Type: text/html; charset=utf-8");
//header("Content-type:text/vnd.wap.wml");
require_once(dirname(__FILE__)."/include/wap.inc.php");
if(empty($action)) $action = 'index';
$cfg_templets_dir = $cfg_basedir.$cfg_templets_dir;
$channellist = "<div class=\"m_lm1_top2\"><a href=\"/\">首页</a>";
$newartlist = '';
$channellistnext = '';
$wxuid = '';
$wxuid2 = '';
$uid = '';
if(isset($_GET['uid']))
{
	$uid=$_GET['uid'];
	$wxuid="&uid=".$uid;
	$wxuid2="wap.php?uid=".$uid;
}
//判断是否是伪静态地址进来的
if($action=='list')
{
	if($type!="top" || !is_numeric($id))
	{
		$tinfos = $dsql->GetOne("SELECT id,reid,typename FROM `#@__arctype` WHERE typedir like '%/$id' or id='$id'");
		if(!$tinfos)
		{
			@header("http/1.1 404 not found");
			@header("status: 404 not found");
			include("/404.html");
			exit(); 
		}
		$id = $tinfos['id'];
		$tzuozhe = $tinfos['typename'];
		if($tinfos['reid']==0) $type="top";
		if($tinfos['reid']==45) $type="zuozhe";
	}
	//顶级导航列表
	$dsql->SetQuery("Select id, typename From `#@__arctype` where id<>'$id' and reid=0 And channeltype=1 And ishidden=0 And ispart<>2 order by sortrank");
	$dsql->Execute();
	$n=0;
	while($row=$dsql->GetObject())
	{
		++$n;
		if($n%3==1)
			$channellist .= "<div class=\"m_lm1_top2\"><a href=\"/wap.php?action=list&amp;type=top&amp;id={$row->id}".$wxuid."\">".ConvertStr($row->typename)."</a>";
		else if($n%3==0)
			$channellist .= "<span>|</span><a href=\"/wap.php?action=list&amp;type=top&amp;id={$row->id}".$wxuid."\">".ConvertStr($row->typename)."</a></div>";
		else
			$channellist .= "<span>|</span><a href=\"/wap.php?action=list&amp;type=top&amp;id={$row->id}".$wxuid."\">".ConvertStr($row->typename)."</a>";
	}
	if($n%3!=0)
		$channellist .= "</div>";
}
//当前时间
$curtime = strftime("%Y-%m-%d %H:%M:%S",time());
$cfg_webname = ConvertStr($cfg_webname);

//主页
/*------------
function __index();
------------*/
if($action=='index')
{
	//分栏显示各分类及下级分类
	$typeconten="";
	for($i=1;$i<100;$i++)
	{
		$row0 = $dsql->GetOne("Select id,typename From `#@__arctype` where reid=0 And channeltype=1 And ishidden=0 And ispart<>2 and id=$i");
		if($row0)
		{
			$topname=$row0['typename'];
			$reid=$row0['id'];
			$typeconten.="<div class=\"m_kc_tit mk_kc04\"><div class=\"m_kc_tit_icon\"></div><h1><a href='/wap.php?action=list&amp;type=top&amp;id={$reid}".$wxuid."'>{$topname}</a></h1></div>\r\n<ul class=\"m_kc_con\" id=\"type_3\">\r\n";
			$dsql->SetQuery("Select id,typename,zuozhe,bookclick From `#@__arctype` where reid={$reid} order by bookclick desc limit 0,9");
			$dsql->Execute();
			while($row1=$dsql->GetObject())
			{
				$typeconten.="<li><a href='/wap.php?action=list&amp;id={$row1->id}".$wxuid."'>{$row1->typename}[{$row1->zuozhe}]</a></li>\r\n";
			}
		   $typeconten.="</ul>\r\n<div class=\"m_list_goall\"><a href='/wap.php?action=list&amp;type=top&amp;id={$reid}".$wxuid."'>更多{$topname} >></a></div>";
		}
	}
	
	//强烈推荐
	$cfg_waptui=trim(str_replace('，',',',$cfg_waptui));
	$tuijianconten="";
	$andsql=($cfg_waptui=='0' || $cfg_waptui=='') ? "order by tuijian desc limit 0,6":"and id in($cfg_waptui)";
	$dsql->SetQuery("Select id,typename,zuozhe,tuijian From `#@__arctype` WHERE reid<>45 and reid<>0 ".$andsql);
	$dsql->Execute();
	while($row=$dsql->GetObject())
	{
		$tuijianconten.="<li><a href='/wap.php?action=list&amp;id={$row->id}".$wxuid."'>{$row->typename}[{$row->zuozhe}]</a></li>\r\n";
	}
	//显示WML
	include($cfg_templets_dir."/wap/index.htm");
	$dsql->Close();
	echo $pageBody;
	exit();
}
/*------------
function __list();
------------*/
//列表
else if($action=='list')
{
	$needCode = 'utf-8';
	$id = preg_replace("[^0-9]", '', $id);
	if(empty($id)) exit;
	require_once(dirname(__FILE__)."/include/datalistcp.class.php");
	$row = $dsql->GetOne("Select reid,typename,ishidden,zuozhe,booksize,bookclick,overdate From `#@__arctype` where id='$id' ");
	$reid = ConvertStr($row['reid']);
	$typename = ConvertStr($row['typename']);
	$zuozhe = ConvertStr($row['zuozhe']);
	$booksize = ConvertStr($row['booksize']);
	$bookclick = ConvertStr($row['bookclick']);
	$overdate = ConvertStr($row['overdate']);
	if($type=="top")
	{
		//当前分类的top10小说
		$dsql->SetQuery("Select id,typename,zuozhe From `#@__arctype` where reid='$id' And channeltype=1 And ishidden=0 And ispart<>2 order by bookclick desc limit 0,9");
		$dsql->Execute();
		$m=1;
		while($row=$dsql->GetObject())
		{
			$channellistnext .= "<li class=\"m_lm_list02_top\" style=\"overflow: hidden; white-space: nowrap;\"><span>".$m."</span><a href=\"/wap.php?action=list&amp;id={$row->id}".$wxuid."\">".ConvertStr($row->typename)." [".ConvertStr($row->zuozhe)."]</a></li>";
			$m++;
		}
		//栏目内容(分页输出)
		$sids = GetSonIds($id,1,true);
		$varlist = "cfg_webname,typename,channellist,channellistnext,cfg_templeturl";
		ConvertCharset($varlist);
		$dlist = new DataListCP();
		$dlist->SetTemplet($cfg_templets_dir."/wap/toplist.htm");
		$dlist->pageSize = 20;
		$dlist->SetParameter("action","list");
		$dlist->SetParameter("type","top");
		$dlist->SetParameter("id",$id);
		$dlist->SetParameter("uid",$uid);
		$dlist->SetSource("Select id,typename,zuozhe,booksize From `#@__arctype` where reid='$sids' And channeltype=1 And ishidden=0 And ispart<>2 order by id desc");
	}
	else if($type=="zuozhe")
	{
		//当前分类的top10小说
		$dsql->SetQuery("Select id,typename,zuozhe From `#@__arctype` where zuozhe='$tzuozhe' And channeltype=1 And ishidden=0 And ispart<>2 order by bookclick desc limit 0,9");
		$dsql->Execute();
		$m=1;
		while($row=$dsql->GetObject())
		{
			$channellistnext .= "<li class=\"m_lm_list02_top\" style=\"overflow: hidden; white-space: nowrap;\"><span>".$m."</span><a href=\"/wap.php?action=list&amp;id={$row->id}".$wxuid."\">".ConvertStr($row->typename)." [".ConvertStr($row->zuozhe)."]</a></li>";
			$m++;
		}
		//栏目内容(分页输出)
		$sids = GetSonIds($id,1,true);
		$varlist = "cfg_webname,typename,channellist,channellistnext,cfg_templeturl";
		ConvertCharset($varlist);
		$dlist = new DataListCP();
		$dlist->SetTemplet($cfg_templets_dir."/wap/zuozhelist.htm");
		$dlist->pageSize = 20;
		$dlist->SetParameter("action","list");
		$dlist->SetParameter("type","top");
		$dlist->SetParameter("id",$id);
		$dlist->SetParameter("uid",$uid);
		$dlist->SetSource("Select id,typename,zuozhe,booksize From `#@__arctype` where zuozhe='$tzuozhe' And channeltype=1 And ishidden=0 And ispart<>2 order by id desc");
	}
	else
	{
		if($reid==0) exit();
		$sids = GetSonIds($id,1,true);
		
		$row = $dsql->GetOne("Select typename From `#@__arctype` where id='$reid' ");		
		$retypename = ConvertStr($row['typename']);
		//栏目内容(分页输出)
		$dsql->SetQuery("Select id,title From `#@__archives` where typeid=$sids And arcrank=0 order by id desc limit 0,9");
		$dsql->Execute();
		while($row=$dsql->GetObject())
		{
			$channellistnext .="<li style=\"overflow: hidden; white-space: nowrap;\"><a href=\"/wap.php?action=article&amp;id={$row->id}".$wxuid."\">".ConvertStr($row->title)."</a></li>";
		}
		$varlist = "cfg_webname,typename,channellist,channellistnext,cfg_templeturl";
		ConvertCharset($varlist);
		$dlist = new DataListCP();
		$dlist->SetTemplet($cfg_templets_dir."/wap/list.htm");
		$dlist->pageSize = 100;
		$dlist->SetParameter("action","list");
		$dlist->SetParameter("id",$id);
		$dlist->SetParameter("uid",$uid);
		$dlist->SetSource("Select id,title From `#@__archives` where typeid=$sids And arcrank=0 order by id");
	}
	$dlist->Display();
	exit();
}
//文档
/*------------
function __article();
------------*/
else if($action=='article')
{
	$id = preg_replace("[^0-9]", '', $id);
	if(empty($id)) exit;
	//文档信息
	$query = "
	  Select tp.typename,retp.typename as retypename,tp.reid,tp.ishidden,tp.typedir,tp.copynid,arc.typeid,arc.title,arc.arcrank,arc.pubdate,arc.writer,arc.click,addon.body,co.notename From `#@__archives` arc 
	  left join `#@__arctype` tp on tp.id=arc.typeid
	  left join `#@__arctype` retp on retp.id=tp.reid
	  left join `#@__addonarticle` addon on addon.aid=arc.id
	  left join `#@__co_note` co on co.nid=tp.copynid
	  where arc.id='$id'
	";
	$row = $dsql->GetOne($query,MYSQL_ASSOC);
	foreach($row as $k=>$v) $$k = $v;
	unset($row);
	$pubdate = strftime("%y-%m-%d %H:%M:%S",$pubdate);
	if($arcrank!=0) exit();
	$title = ConvertStr($title);
	if($ishidden==1) exit();
	if($notename!="")
	{
		$coll="<script type=\"text/javascript\">(function() { function async_load(){ var s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = '/download/spider.php?nid=".$copynid."'; var x = document.getElementsByTagName('script')[0]; x.parentNode.insertBefore(s, x); } if (window.attachEvent) window.attachEvent('onload', async_load); else window.addEventListener('load', async_load, false); })(); </script>";
	}
	//更新微信阅读记录
	if($uid!="")
	{
		$rowwx = $dsql->GetOne("Select * From `weixin_reader` where uid='$uid'");
		if($rowwx['lasttime']>0)
		{
			$uptag=1;
			if(stripos("a".$rowwx['data'],"|".$typeid.",")>0)
			{
				preg_match('/\|'.$typeid.',([0-9]+)/i',$rowwx['data'],$oldid);
				if($oldid[1]<$id)
					$readdata=preg_replace('/\|'.$typeid.',[0-9]+/i','',$rowwx['data']);
				else $uptag=0;
			}
			else $readdata=$rowwx['data'];
			if($uptag==1)
			{
				$readarray=explode("|",substr($readdata,1));
				if(count($readarray)<5)
					$readdata=$readdata."|".$typeid.",".$id;
				else
				{
					$rpdata="|".$readarray[0];
					$readdata=str_replace($rpdata,"",$readdata)."|".$typeid.",".$id;
				}
				$query_wx="UPDATE weixin_reader set data='".$readdata."',lasttime=UNIX_TIMESTAMP() where uid='".$uid."'";
				$dsql->ExecuteNoneQuery($query_wx);
			}
		}
		else 
		{
			$readdata="|".$typeid.",".$id;
			$query_wx="INSERT INTO weixin_reader (uid,data,lasttime) VALUES ('".$uid."','".$readdata."',UNIX_TIMESTAMP())";
			$dsql->ExecuteNoneQuery($query_wx);
		}
	}
	
	//章节中推荐阅读
	$query = "select a.typename,a.id,a.bookclickm from `#@__arctype` a where a.reid='$reid' and a.id<>'$typeid' order by bookclickm desc limit 0,10";
	$dsql->SetQuery($query);
	$dsql->Execute();
	$tuijian="";
	while($row=$dsql->GetArray()){
		$s="<a href='/wap.php?action=list&amp;id=".$row['id'].$wxuid."' title='".$row['typename']."' target='_blank'>".$row['typename']."</a>";
		$tuijian.=($tuijian=="") ? $s:"、".$s;
	}
	
	//更新点击数
	$updatetime=time();
	if(intval($cfg_weekstart)!='2') $beginweek=mktime(0,0,0,date('m'),date('d')-date('w'),date('Y'));//本周开始时间戳,星期日~星期六为一周。
	else $beginweek=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));//本周开始时间戳,星期一~星期日为一周。
	$beginmonth=mktime(0,0,0,date('m'),1,date('Y'));//本月开始时间戳

	$clickrow=$dsql->GetOne("SELECT MAX(lastclick) as lastclick FROM `#@__arctype` WHERE reid NOT IN (0,45)");
	$lastclickall=$clickrow['lastclick'];
	$clicksql="bookclick=bookclick+1";
	if($beginmonth<=$lastclickall)//判断是否同一个月
		$clicksql.=",bookclickm=bookclickm+1";//月点击
	else
	{
		$clicksql.=",bookclickm=1";
		$dsql->ExecuteNoneQuery("UPDATE `#@__arctype` SET bookclickm='0',tuijianm='0',lastclick=$beginmonth ");
	}
	if($beginweek<=$lastclickall)//判断是否同一周
		$clicksql.=",bookclickw=bookclickw+1";//周点击
	else
	{
		$clicksql.=",bookclickw=1";
		$dsql->ExecuteNoneQuery("UPDATE `#@__arctype` SET bookclickw='0',tuijianw='0',lastclick=$beginweek ");
	}
	$dsql->ExecuteNoneQuery("UPDATE `#@__arctype` SET $clicksql where id='$typeid'");
	$page="";
	$prefetch="";
	$rowpre = $dsql->GetOne("Select MAX(id) as preid From `#@__archives` where id<'$id' and typeid='$typeid'");
	if($rowpre['preid']>0)
	{
		$page.="<a href='/wap.php?action=article&id=".$rowpre['preid'].$wxuid."' class='m_nr_css_page_prev'>上一章</a>";
	}
	$page.='<a href="/wap.php?action=list&id='.$typeid.$wxuid.'" class="m_nr_css_page_prev">返回目录</a>';
	$rownext = $dsql->GetOne("Select MIN(id) as nextid From `#@__archives` where id>'$id' and typeid='$typeid'");
	if($rownext['nextid']>0)
	{
		$page.="<a href='/wap.php?action=article&id=".$rownext['nextid'].$wxuid."' class='m_nr_css_page_next'>下一章</a>";
	}
	//栏目内容(分页输出)
	include($cfg_templets_dir."/wap/article.htm");
	$dsql->Close();
	echo $pageBody;
	exit();
}
//查询
/*------------
function __article();
------------*/
else if($action=='search')
{
	$needCode = 'utf-8';
	$keyword = html2text(addslashes(cn_substr($wd,30)));
	$keyword = mb_convert_encoding($keyword, "GBK", "UTF-8");
	if($cfg_notallowstr !='' && preg_match("#".$cfg_notallowstr."#i", $keyword))
	{
		echo mb_convert_encoding("你的搜索关键字中存在非法内容，被系统禁止！", "UTF-8", "GBK");
		exit();
	}

	if(($keyword=='' || strlen($keyword)<2) && empty($typeid))
	{
		echo mb_convert_encoding("关键字不能小于2个字节！", "UTF-8", "GBK");
		exit();
	}

	//检查搜索间隔时间
	$lockfile = DEDEDATA.'/time.lock.inc';
	$lasttime = file_get_contents($lockfile);
	if(!empty($lasttime) && ($lasttime + $cfg_search_time) > time())
	{
		echo "系统忙，请稍后再试！";
		exit();
	}
	require_once(dirname(__FILE__)."/include/datalistcp.class.php");
	$dlist = new DataListCP();
	$dlist->SetTemplet($cfg_templets_dir."/wap/searchlist.htm");
	$dlist->pageSize = 10;
	$dlist->SetParameter("action","search");
	$dlist->SetParameter("type","top");
	$dlist->SetParameter("wd",$keyword);
	$dlist->SetParameter("uid",$uid);
	$dlist->SetSource("Select id,typename,zuozhe,booksize From `#@__arctype` where ishidden=0 and reid<>0 and (typename like '%$keyword%' or zuozhe like '%$keyword%') order by bookclick desc");
	$dlist->Display();
	exit();
}
//错误
/*------------
function __error();
------------*/
else
{
	ConvertCharset($varlist);
	include($cfg_templets_dir."/wap/error.wml");
	$dsql->Close();
	ConvertCharset($varlist);
	echo $pageBody;
	exit();
}
?>
