<?php
include("require.php");
$phpcolor->table="book_class";
$phpcolor->field="*";
$phpcolor->where="1=1 order by queue";
$class=$phpcolor->assignment();
$tpl -> assign("action", $_GET['action']);
if($_GET['action']=="config")
{
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  setcookie($_POST['type'], $_POST['zhi'], time()+$core_time*3600+86400);
  if(isset($_COOKIE['core_user_name']))
  {
    
  }
  exit;
}
elseif($_GET['action']=="book")
{
  submitfilter($_GET['id']);
  $phpcolor->table="book";
  $phpcolor->where="id='".$_GET['id']."'";
  if(!isset($_COOKIE[$_GET['id']]))
  {
    $phpcolor->field="click=click+1";
    $phpcolor->upgrade();
	setcookie($_GET['id'], "1", time()+$core_time*3600+86400);
  }
  $phpcolor->field="*";
  $book=$phpcolor->exist(1);
  if($book['belong']!="0")
  {
    $phpcolor->table="book_class";
    $phpcolor->field="classname";
    $phpcolor->where="id='".$book['belong']."'";
    $bookclass=$phpcolor->exist(1);
  }
  else
    $bookclass["classname"]="暂未分类";
  $book['words']=$book['words']."字";
  if($book['status']=="1")
    $book['status']="连载中";
  elseif($book['status']=="2")
    $book['status']="完本";
  $picfile=ceil(($book['id']/5000));
  if(file_exists("upload/bookpic/".$picfile."/".$book['id'].".jpg"))
    $book['pic']="upload/bookpic/".$picfile."/".$book['id'].".jpg";
  else
    $book['pic']="images/img/booknopic.jpeg";
  $classid=$book['belong'];
  $book['introduction']="<br/>".$book['introduction'];
  $book['introduction']=str_replace("<br/><br/>","<br/>",$book['introduction']);
  $tpl -> assign("book", $book);
  $tpl -> assign("bookclass", $bookclass);
  $tpl -> assign("core_header_title", $book['bookname']." - 多彩小说 - ");
}
elseif($_GET['action']=="chapter")
{
  submitfilter($_GET['id']);
  $phpcolor->table="book";
  $phpcolor->where="id='".$_GET['id']."'";
  if(!isset($_COOKIE[$_GET['id']]))
  {
    $phpcolor->field="click=click+1";
    $phpcolor->upgrade();
	setcookie($_GET['id'], "1", time()+$core_time*3600+86400);
  }
  $phpcolor->field="*";
  $book=$phpcolor->exist(1);
  $phpcolor->table="book_sql";
  $phpcolor->field="*";
  $phpcolor->where="did='".$book['sqldata']."'";
  $booksql=$phpcolor->exist(1);
  //连接远程数据库
  $phpcolor->close();
  $phpcolor = new mysql();
  $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  $phpcolor->table="book_volume";
  $phpcolor->field="*";
  $phpcolor->where="belong='".$book['id']."' order by queue";
  $volume=$phpcolor->assignment();
  for ($count=0;$count<count($volume);$count++)
  {
	$phpcolor->table="book_chapter_".$book['sqltable'];
    $phpcolor->field="*";
    $phpcolor->where="belong='".$book['id']."' and volume='".$volume[$count]['id']."' order by queue";
    $chapter[$count]=$phpcolor->assignment();
  }
  $tpl -> assign("book", $book);
  $tpl -> assign("volume", $volume);
  $tpl -> assign("chapter", $chapter);
  $tpl -> assign("core_header_title", $book['bookname']."章节目录 - 多彩小说 - ");
  $tpl -> display("$core_style/bookread.htm");
  exit();
}
elseif($_GET['action']=="read")
{
  submitfilter($_GET['id']);
  $phpcolor->table="book";
  $phpcolor->where="id='".$_GET['id']."'";
  if(!isset($_COOKIE[$chapter['belong']]))
  {
    $phpcolor->field="click=click+1";
    $phpcolor->upgrade();
	setcookie($chapter['belong'], "1", time()+$core_time*3600+86400);
  }
  $phpcolor->field="*";
  $book=$phpcolor->exist(1);
  $phpcolor->table="book_sql";
  $phpcolor->field="*";
  $phpcolor->where="did='".$book['sqldata']."'";
  $booksql=$phpcolor->exist(1);
  //连接远程数据库
  $phpcolor->close();
  $phpcolor = new mysql();
  $phpcolor->open($booksql['sqlhost'],$booksql['sqluser'],$booksql['sqlpw'],$booksql['sqldb']);
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['rid']."'";
  $chapter=$phpcolor->exist(1);
  $phpcolor->where="belong='".$chapter['belong']."' and queue<'".$chapter['queue']."' order by queue desc limit 1";
  $schapter=$phpcolor->exist(1);
  $chapter['schapter']=$schapter['id'];
  $phpcolor->where="belong='".$chapter['belong']."' and queue>'".$chapter['queue']."' order by queue limit 1";
  $xchapter=$phpcolor->exist(1);
  $chapter['xchapter']=$xchapter['id'];
  $wzsize="16";
  if(isset($_COOKIE['bookwzsize']))
    $wzsize=$_COOKIE['bookwzsize'];
  $bgcolor="#FFF";
  if(isset($_COOKIE['bookbgcolor']))
    $bgcolor=$_COOKIE['bookbgcolor'];
  if(isset($_COOKIE['core_user_name']))
  {
    if($user['bookwzsize']!="")
      $wzsize=$user['bookwzsize'];
    if($user['bookbgcolor']!="")
      $bgcolor=$user['bookbgcolor'];
  }
  $tpl -> assign("book", $book);
  $tpl -> assign("chapter", $chapter);
  $tpl -> assign("wzsize", $wzsize);
  $tpl -> assign("bgcolor", $bgcolor);
  $tpl -> assign("core_header_title", $chapter['theme']." - ".$book['bookname']." - 多彩小说 - ");
  $tpl -> display("$core_style/bookread.htm");
  exit();
}
elseif($_GET['action']=="class")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $where="";
  $order="uptime";
  if(isset($_GET['sx']))
  {
	if($_GET['sx']=="1")
      $where="status=1 and ";
	elseif($_GET['sx']=="2")
      $where="status=2 and ";
	elseif($_GET['sx']=="3")
      $order="jointime ";
	elseif($_GET['sx']=="4")
      $order="words ";
	elseif($_GET['sx']=="5")
      $order="click ";
  }
  if($_GET['id']==0)
  {
    $phpcolor->where=$where."1=1";
    $count = $phpcolor->exist(2);
    $phpcolor->where=$where."1=1 order by ".$order." desc limit ".($_GET['page']*30).",30";
  }
  else
  {
	submitfilter($_GET['id']);
    $phpcolor->where=$where."belong = '".$_GET['id']."'";
    $count = $phpcolor->exist(2);
    $phpcolor->where=$where."belong = '".$_GET['id']."' order by ".$order." desc limit ".($_GET['page']*30).",30";
  }
  if(isset($_GET['sx']))
    $page=pages($count,'30',$_GET['page'],'book','&action=class&sx='.$_GET['sx'].'&id=',$_GET['id'],'0');
  else
    $page=pages($count,'30',$_GET['page'],'book','&action=class&id=',$_GET['id'],'0');
  $book=$phpcolor->assignment();
  for ($count=0;$count<count($book);$count++)
  {
    $book[$count]['uptime']=substr($book[$count]['uptime'],0,10);
    $book[$count]['words']=floor($book[$count]['words']/2)."字";
	if($book[$count]['status']=="1")
      $book[$count]['status']="连载中";
    elseif($book[$count]['status']=="2")
      $book[$count]['status']="完本";
	if(strlen($book[$count]['author'])>10)
	  $book[$count]['author']=$phpcolor->mysubstr($book[$count]['author'],0,10)."…"; 
  }
  $classid=$_GET['id'];
  $tpl -> assign("book", $book);
  $tpl -> assign("page", $page);
  if($_GET['id']==0)
    $tpl -> assign("core_header_title", "多彩小说 - ");
  else
  {
	$phpcolor->table="book_class";
    $phpcolor->field="classname";
    $phpcolor->where="id='".$_GET['id']."'";
    $sort=$phpcolor->exist(1);
    $tpl -> assign("core_header_title", $sort['classname']." - 多彩小说 - ");
  }
}
else
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="recommendb='1' order by uptime desc limit 3";
  $tjbook=$phpcolor->assignment();
  for ($count=0;$count<count($tjbook);$count++)
  {
    $tjbook[$count]['introduction']=str_replace("<br />","<br>",$tjbook[$count]['introduction']);
	$tjbook[$count]['introduction']=str_replace("<br>","。",$tjbook[$count]['introduction']);
    $tjbook[$count]['introduction']=$phpcolor->mysubstr($tjbook[$count]['introduction'],0,110); 
    $picfile=ceil(($tjbook[$count]['id']/5000));
    if(file_exists("upload/bookpic/".$picfile."/".$tjbook[$count]['id'].".jpg"))
      $tjbook[$count]['pic']="upload/bookpic/".$picfile."/".$tjbook[$count]['id'].".jpg";
    else
      $tjbook[$count]['pic']="images/img/booknopic.jpeg";
  }
  //点击榜第一
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="1=1 order by click desc";
  $djbook=$phpcolor->exist(1);
  $djbook['introduction']=str_replace("<br />","<br>",$djbook['introduction']);
  $djbook['introduction']=str_replace("<br>","。",$djbook['introduction']);
  $djbook['introduction']=$phpcolor->mysubstr($djbook['introduction'],0,200); 
  $picfile=ceil(($djbook['id']/5000));
  if(file_exists("upload/bookpic/".$picfile."/".$djbook['id'].".jpg"))
    $djbook['pic']="upload/bookpic/".$picfile."/".$djbook['id'].".jpg";
  else
    $djbook['pic']="images/img/booknopic.jpeg";
  //点击榜第二-五
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="1=1 order by click desc limit 2 , 4";
  $djbooks=$phpcolor->assignment();
  //更新榜第一
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="1=1 order by uptime desc";
  $gxbook=$phpcolor->exist(1);
  $gxbook['introduction']=str_replace("<br />","<br>",$gxbook['introduction']);
  $gxbook['introduction']=str_replace("<br>","。",$gxbook['introduction']);
  $gxbook['introduction']=$phpcolor->mysubstr($gxbook['introduction'],0,200); 
  $picfile=ceil(($gxbook['id']/5000));
  if(file_exists("upload/bookpic/".$picfile."/".$gxbook['id'].".jpg"))
    $gxbook['pic']="upload/bookpic/".$picfile."/".$gxbook['id'].".jpg";
  else
    $gxbook['pic']="images/img/booknopic.jpeg";
  //更新榜第二-五
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="1=1 order by uptime desc limit 2 , 4";
  $gxbooks=$phpcolor->assignment();
  for ($count=0;$count<count($gxbooks);$count++)
  {
    $gxbooks[$count]['uptime']=date('Y-m-d',strtotime($gxbooks[$count]['uptime']));
  }
  $tpl -> assign("gxbook", $gxbook);
  $tpl -> assign("djbook", $djbook);
  $tpl -> assign("tjbook", $tjbook);
  $tpl -> assign("djbooks", $djbooks);
  $tpl -> assign("gxbooks", $gxbooks);
  $tpl -> assign("core_header_title", "多彩小说 - ");
}
$tpl -> assign("class", $class);
$tpl -> assign("id", $classid);
$tpl -> display("$core_style/book.htm");
?>