<?php
include("require.php");
if(!isset($_GET['soname']) || $_GET['soname']=="")
{
  if(!isset($_GET['type']))
    $_GET['type']=1;
  $phpcolor->table="topic";
  $phpcolor->field="id,theme";
  $phpcolor->where="boutique = 1 and state!=0 order by click desc limit 10";
  $hotpost=$phpcolor->assignment();
  for ($count=0;$count<count($hotpost);$count++)
  {
    $hotpost[$count]['url']=htmlurl($core_url,"read",$hotpost[$count]['id'],0,$core_rewrite);
	$hotpost[$count]['theme']=$phpcolor->mysubstr($hotpost[$count]['theme'],0,34); 
  }
  $phpcolor->table="topic";
  $phpcolor->field="id,theme";
  $phpcolor->where="boutique = 1 and state!=0 order by published desc limit 10";
  $newpost=$phpcolor->assignment();
  for ($count=0;$count<count($newpost);$count++)
  {
    $newpost[$count]['url']=htmlurl($core_url,"read",$newpost[$count]['id'],0,$core_rewrite);
	$newpost[$count]['theme']=$phpcolor->mysubstr($newpost[$count]['theme'],0,34); 
  }
  $phpcolor->table="topic";
  $phpcolor->field="id,theme";
  $phpcolor->where="state!=0 order by published desc limit 10";
  $newposts=$phpcolor->assignment();
  for ($count=0;$count<count($newposts);$count++)
  {
    $newposts[$count]['url']=htmlurl($core_url,"read",$newposts[$count]['id'],0,$core_rewrite);
	$newposts[$count]['theme']=$phpcolor->mysubstr($newposts[$count]['theme'],0,34); 
  }
  $phpcolor->table="hotkey";
  $phpcolor->field="hotkey";
  $phpcolor->where="status=1 order by click desc limit 10";
  $hotkey=$phpcolor->assignment();
  $tpl -> assign("hotpost", $hotpost);
  $tpl -> assign("newpost", $newpost);
  $tpl -> assign("newposts", $newposts);
  $tpl -> assign("hotkey", $hotkey);
  $tpl -> assign("type", $_GET['type']);
  $tpl -> display("$core_style/so_index.htm");
  exit();
}
//搜索关键词记录
$time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
$phpcolor->table="hotkey";
$phpcolor->field="*";
$phpcolor->where="hotkey='".$_GET['soname']."'";
$yzxr=$phpcolor->exist(1);
if($yzxr!="0")
{
  $phpcolor->field="click=click+1,hottime='".$time."'";
  $phpcolor->upgrade();
}
else
{
  $phpcolor->field="hotkey, click, status, hottime";
  $phpcolor->where="'".$_GET['soname']."', '1', '1','".$time."'";
  $phpcolor->write();
}
$users=$_SERVER['REMOTE_ADDR'];
if(isset($_COOKIE["core_user_name"]))
  $users = $_COOKIE["core_user_name"];
$phpcolor->table="hotkey_so";
$phpcolor->field="*";
$phpcolor->where="hotkey='".$_GET['soname']."' and users='".$users."'";
$yzxr=$phpcolor->exist(1);
if($yzxr!="0")
{
  $phpcolor->field="click=click+1,hottime='".$time."'";
  $phpcolor->upgrade();
}
else
{
  $phpcolor->field="users, hotkey, click, hottime";
  $phpcolor->where="'".$users."', '".$_GET['soname']."', '1', '".$time."'";
  $phpcolor->write();
}
if(!isset($_GET['site']) || $_GET['site']=="")
  jump("create.php?barname=".$_GET['soname']); 
else
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  if($_GET['site']=="1" or $_GET['site']=="2")
  {
	$phpcolor->table="topic";
    $phpcolor->field="*";
	if($_GET['site']=="1")
    {
      $phpcolor->where="theme like '%".$_GET['soname']."%' and state != '0' order by republished desc";
      $count=$phpcolor->exist(2);
      $phpcolor->where="theme like '%".$_GET['soname']."%' and state != '0' order by republished desc limit ".($_GET['page']*20).",20";
	}
	else
	{
	  $phpcolor->where="author = '".$_GET['soname']."' and state != '0' order by republished desc";
      $count=$phpcolor->exist(2);
      $phpcolor->where="author = '".$_GET['soname']."' and state != '0' order by republished desc limit ".($_GET['page']*20).",20";
	}
	$page=pages($count,20,$_GET['page'],'so','&site='.$_GET['site'].'&soname=',$_GET['soname'],'0');
	$post=$phpcolor->assignment();
    $phpcolor->table="bar";
    for ($count=0;$count<count($post);$count++)       
    {
	  $phpcolor->field="barname";
      $phpcolor->where="id = '".$post[$count]['belong']."'";
      $rs=$phpcolor->exist(1);
      $post[$count]['belongbn']=$rs['barname'];
	  $post[$count]['url']=htmlurl($core_url,"read",$post[$count]['id'],0,$core_rewrite);
	  $post[$count]['burl']=htmlurl($core_url,"list",$post[$count]['belong'],0,$core_rewrite);
	  $post[$count]['content']=preg_replace("/\[img\](.*?)\[\/img\]/","",$post[$count]['content']);
      $post[$count]['content']=str_replace("<br />","",$post[$count]['content']);
	  $post[$count]['content']=$phpcolor->mysubstr($post[$count]['content'],0,220);
	  if($_GET['site']=="1")
      {
        $post[$count]['theme']=str_replace($_GET['soname'],"<span style='color:#F00'>".$_GET['soname']."</span>",$post[$count]['theme']);
	    $post[$count]['content']=str_replace($_GET['soname'],"<span style='color:#F00'>".$_GET['soname']."</span>",$post[$count]['content']);
	  }
	  if($post[$count]['authorip'] = is_ip($post[$count]['author']))
        $post[$count]['author'] = ip_three($post[$count]['author']);
    }
	$tpl -> assign("post", $post); 
  }
  else
  {
    $phpcolor->table="book";
    $phpcolor->field="*";
    $phpcolor->where="bookname like '%".$_GET['soname']."%' and status >= 1 order by uptime desc";
    $count=$phpcolor->exist(2);
    $phpcolor->where="bookname like '%".$_GET['soname']."%' and status >= 1 order by uptime desc limit ".($_GET['page']*20).",20";
	$page=pages($count,20,$_GET['page'],'so','&site='.$_GET['site'].'&soname=',$_GET['soname'],'0');
	$book=$phpcolor->assignment();
	for ($count=0;$count<count($book);$count++)
    {
      $book[$count]['words']=floor($book[$count]['words']/2)."字";
      $book[$count]['introduction']=str_replace("<br/>"," ",$book[$count]['introduction']);
	  $picfile=ceil(($book[$count]['id']/5000));
      if(file_exists("upload/bookpic/".$picfile."/".$book[$count]['id'].".jpg"))
        $book[$count]['pic']="upload/bookpic/".$picfile."/".$book[$count]['id'].".jpg";
      else
        $book[$count]['pic']="images/img/booknopic.jpeg";
      if($book[$count]['belong']!="0")
      {
        $phpcolor->table="book_class";
        $phpcolor->field="classname";
        $phpcolor->where="id='".$book[$count]['belong']."'";
        $bookclass=$phpcolor->exist(1);
		$book[$count]["classname"]=$bookclass['classname'];
      }
      else
        $book[$count]["classname"]="暂未分类";
    }
    $tpl -> assign("book", $book);
  }
  //推荐小说
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="bookname like '%".$_GET['soname']."%' and status >= 1 order by rand() limit 1";
  if(!$books=$phpcolor->exist(1))
  {
    $phpcolor->where="1 = 1 order by click desc limit 1";
    $books=$phpcolor->exist(1);
  }
  $picfile=ceil(($books['id']/5000));
  if(file_exists("upload/bookpic/".$picfile."/".$books['id'].".jpg"))
    $books['pic']="upload/bookpic/".$picfile."/".$books['id'].".jpg";
  else
    $books['pic']="images/img/booknopic.jpeg";
  $books['introduction']=str_replace("<br/>","",$books['introduction']);
  $books['introduction']=str_replace("<br>","",$books['introduction']);
  $books['introduction']=str_replace("　","",$books['introduction']);
  $books['introduction']=str_replace("&nbsp;"," ",$books['introduction']);
  $books['introduction']=str_replace("  "," ",$books['introduction']);
  $books['introduction']=$phpcolor->mysubstr($books['introduction'],0,100)." <a href='book.php?action=book&id=".$books['id']."'>[详情]</a>";
  //相关吧
  $phpcolor->table="bar";
  $phpcolor->field="*";
  $phpcolor->where="barname like '%".$_GET['soname']."%' order by rand() limit 3";
  $bar=$phpcolor->assignment();
  for ($count=0;$count<count($bar);$count++)
  {
    $bar[$count]['barname']=$phpcolor->mysubstr($bar[$count]['barname'],0,14); 
    $bar[$count]['url']=htmlurl($core_url,"list",$bar[$count]['id'],0,$core_rewrite);
  }
  //相关用户
  $phpcolor->table="user";
  $phpcolor->field="*";
  $phpcolor->where="username like '%".$_GET['soname']."%' order by rand() limit 3";
  $user=$phpcolor->assignment();
  $tpl -> assign("user", $user); 
  $tpl -> assign("bar", $bar);
  $tpl -> assign("books", $books);
  $tpl -> assign("page", $page);
  $tpl -> assign("site", $_GET['site']);
  $tpl -> assign("core_header_title", $_GET['soname']." - ");
}
$tpl -> display("$core_style/so.htm");
?>