<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[48]!=1)
  jump("jump.php?id=25&jump=main.php");
$phpcolor->table="book";
$phpcolor->where="id = '".$_GET['id']."'";
if(isset($_GET['action']) and ($_GET['action'] == "del" or $_GET['action'] == "clear") and isset($_GET['id']))
{
  $phpcolor->field="*";
  $book=$phpcolor->exist(1);
  if($_GET['action'] == "clear")
  {
    $phpcolor->field="words='0', click='0', rule='', ruleid='', cid='',ctheme=''";
    $phpcolor->upgrade();
  }
  else
  {
    $phpcolor->field="*";
    $phpcolor->delete();
  }
  if($book['sqldata']!=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 = '".$_GET['id']."'";
  $phpcolor->delete();
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$_GET['id']."'";
  $phpcolor->delete();
  $picfile=ceil(($_GET['id']/5000));
  unlink('../upload/bookpic/'.$picfile.'/'.$_GET['id'].".jpg");
  jump("jump.php?id=24&jump=book.php");
}
elseif(isset($_GET['action']) and $_GET['action'] == "tihuan")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $book=$phpcolor->exist(1);
  $tpl -> assign("book", $book);
  $tpl -> display("book_tihuan.htm");
}
elseif(isset($_GET['action']) and $_GET['action'] == "tihuanadd")
{
  if($_POST['yuankey']=="")
    jump("jump.php?id=2&jump=book.php");
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=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="belong='".$_GET['id']."'";
  $chapter=$phpcolor->assignment();
  for ($count=0;$count<count($chapter);$count++)
  {
    $chapter[$count]['content']=str_replace($_POST['yuankey'],$_POST['xiankey'],$chapter[$count]['content']);
	$phpcolor->field="content='".$chapter[$count]['content']."'";
    $phpcolor->where="id='".$chapter[$count]['id']."'";
	$phpcolor->upgrade();
  }
  jump("jump.php?id=24&jump=book.php");
}
elseif(isset($_GET['action']) and $_GET['action'] == "ok")
{
  $phpcolor->table="book";
  $phpcolor->field="status='2'";
  $phpcolor->where="id = '".$_GET['id']."'";
  $sort=$phpcolor->upgrade();
  jump("jump.php?id=24&jump=book.php");
}
elseif(isset($_GET['action']) and $_GET['action'] == "add")
{
  $phpcolor->table="book_class";
  $phpcolor->field="id,classname";
  $phpcolor->where="1 = 1  order by queue";
  $sort=$phpcolor->assignment();
  $phpcolor->table="book_sql";
  $phpcolor->field="*";
  $phpcolor->where="1=1 order by did";
  $booksql=$phpcolor->assignment();
  $tpl -> assign("sort", $sort);
  $tpl -> assign("booksql", $booksql);
  $tpl -> display("book_add.htm");
}
elseif(isset($_GET['action']) and $_GET['action'] == "adddo")
{
  if($_POST['bookname']=="" || $_POST['author']=="")
    jump("jump.php?id=2&jump=book.php?action=add");
  $phpcolor->field="*";
  $phpcolor->where="bookname = '".$_POST['bookname']."'";
  if($phpcolor->exist(2))
    jump("jump.php?id=27&jump=book.php");
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
  $_POST['introduction']=nl2br($_POST['introduction']);
  $phpcolor->field="bookname, author, introduction, jointime, belong, status, recommend, recommendb, rule, ruleid, sqldata, sqltable";
  $phpcolor->where="'".$_POST['bookname']."', '".$_POST['author']."', '".$_POST['introduction']."', '".$time."', '".$_POST['belong']."', '".$_POST['status']."', '".$_POST['recommend']."', '".$_POST['recommendb']."', '".$_POST['rule']."', '".$_POST['ruleid']."', '".$_POST['sqldata']."', '".$_POST['sqltable']."'";
  $phpcolor->write();
  $bookid=mysql_insert_id();
  $picfile=ceil(($bookid/5000));
  if(!file_exists('../upload/bookpic/'.$picfile))
    mkdir("../upload/bookpic/".$picfile, 0777);
  move_uploaded_file($_FILES["file"]["tmp_name"],"../upload/bookpic/".$picfile."/".$bookid.".jpg");
  jump("jump.php?id=24&jump=book.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "edit")
{
  $phpcolor->field="*";
  $book = $phpcolor->exist(1);
  $phpcolor->table="book_class";
  $phpcolor->field="id,classname";
  $phpcolor->where="1 = 1  order by queue";
  $sort=$phpcolor->assignment();
  $tpl -> assign("sort", $sort);
  $tpl -> assign("book", $book);
  $tpl -> display("book_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editdo")
{
  if($_POST['bookname']=="" || $_POST['author']=="" || $_POST['words']=="" || $_POST['click']=="")
    jump("jump.php?id=2&jump=book.php?action=add=and=id=".$_GET['id']);
  if(isset($_FILES["file"]["tmp_name"]) and $_FILES["file"]["tmp_name"]!="")
  {
    $picfile=ceil(($_GET['id']/5000));
    if(file_exists("../upload/bookpic/".$picfile."/".$_GET['id'].".jpg"))
      unlink("../upload/bookpic/".$picfile."/".$_GET['id'].".jpg");
    if(!file_exists('../upload/bookpic/'.$picfile))
      mkdir("../upload/bookpic/".$picfile, 0777);
    move_uploaded_file($_FILES["file"]["tmp_name"],"../upload/bookpic/".$picfile."/".$_GET['id'].".jpg");
  }
  $phpcolor->field="bookname='".$_POST['bookname']."', author='".$_POST['author']."', introduction='".$_POST['introduction']."', words='".$_POST['words']."', click='".$_POST['click']."', belong='".$_POST['belong']."', status='".$_POST['status']."', recommend='".$_POST['recommend']."', recommendb='".$_POST['recommendb']."', rule='".$_POST['rule']."', ruleid='".$_POST['ruleid']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=book.php");
}
elseif(isset($_GET['action']) && isset($_GET['id']) && $_GET['action']=="chaptergx")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id='".$_GET['id']."'";
  $book=$phpcolor->exist(1);
  include(dirname(__FILE__)."/./configs/".$book['rule']."_book.php");
  $content=collect_chapters($readpage);
  if($content[1][0]=="")
    jump("jump.php?id=0&jump=book.php");
  if($book['sqldata']!=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']."'";
  $num=$phpcolor->exist(2);
  $num++;
  for($i=0;$i<count($content[4]);$i++)
  {
    $phpcolor->field="*";
    $phpcolor->where="belong = '".$book['id']."' and volumename = '".$content[4][$i]."'";
    if(!$phpcolor->exist(2))
	{
      $nums=$num+$i;
      $phpcolor->field="volumename,belong,queue";
      $phpcolor->where="'".$content[4][$i]."', '".$book['id']."','".$nums."'";
      $phpcolor->write();
	}
  }
  $phpcolor->field="id";
  $phpcolor->where="belong = '".$book['id']."' order by queue";
  $volume=$phpcolor->assignment();
  //章节入库
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="*";
  $phpcolor->where="belong = '".$book['id']."'";
  $num=$phpcolor->exist(2);
  $num++;
  $words=0;
  $volumeid=$volume[0]['id'];
  $volumers=1;
  for($i=0;$i<count($content[1]);$i++)
  {
    $phpcolor->field="*";
    $phpcolor->where="belong = '".$book['id']."' and theme = '".$content[1][$i]."' and ruleid = '".$content[0][$i]."'";
	if($content[5][$volumers]==$content[1][$i] || $content[5][$volumers]==" ".$content[1][$i])
	{
      $volumeid=$volume[$volumers]['id'];
	  $volumers++;
    }
    if(!$phpcolor->exist(2))
	{
      $nums=$num+$i;
      $words=$words+strlen($content[2][$i])/2;
	  $word=strlen($content[2][$i])/2;
      $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*3600);
      $phpcolor->field="theme,content,words,uptime,belong,volume,status,queue,rule,ruleid";
      $phpcolor->where="'".$content[1][$i]."', '".$content[2][$i]."','".$word."','".$time."','".$book['id']."','".$volumeid."','1','".$nums."','".$book['rule']."','".$content[0][$i]."'";
      $phpcolor->write();
	  if(mysql_affected_rows() < 0)
	  {
		$words=$words-strlen($content[2][$i])/2;
		$content[3]="5";
		break;
      }
	}
  }
  $phpcolor->table="book_chapter_".$book['sqltable'];
  $phpcolor->field="id,theme";
  $phpcolor->where="belong = '".$book['id']."' order by queue desc limit 1";
  $chapter=$phpcolor->exist(1);
  if($book['sqldata']!=1)
  {
	$phpcolor->close();
	$phpcolor = new mysql();
    $phpcolor->open();
  }
  $phpcolor->table="book";
  $phpcolor->where="id = '".$book['id']."'";
  $phpcolor->field="words = words+".$words.", uptime = '".$time."', cid='".$chapter['id']."',ctheme = '".$chapter['theme']."'";
  $phpcolor->upgrade();
  jump("jump.php?id=24&jump=book.php");
}
elseif(isset($_GET['action']) && isset($_GET['id']) && $_GET['action']=="tongbu")
{
  $phpcolor->table="book";
  $phpcolor->field="*";
  $phpcolor->where="id = '".$_GET['id']."'";
  $book = $phpcolor->exist(1);
  if($book['sqldata']!=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="sum(words)";
  $phpcolor->where="belong = '".$book['id']."'";
  $words=$phpcolor->exist(1);
  $phpcolor->field="id,theme,uptime";
  $phpcolor->where="belong = '".$book['id']."' order by queue desc limit 1";
  $chapter=$phpcolor->exist(1);
  $phpcolor->close();
  $phpcolor = new mysql();
  $phpcolor->open();
  if($words[0]!="")
  {
    $phpcolor->table="book";
    $phpcolor->field="words = '".$words[0]."', uptime = '".$chapter['uptime']."', cid='".$chapter['id']."',ctheme = '".$chapter['theme']."'";
    $phpcolor->where="id = '".$book['id']."'";
    $phpcolor->upgrade();
  }
  jump("jump.php?id=24&jump=book.php");
}
else
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  if(isset($_GET['theme']))
  {
    $phpcolor->where="bookname like '%".$_GET['theme']."%'";
    $count = $phpcolor->exist(2);
    $phpcolor->where="bookname like '%".$_GET['theme']."%' order by jointime desc limit ".($_GET['page']*28).",28";
    $page=pages($count,28,$_GET['page'],'book','&theme=',$_GET['theme'],'0');
  }
  elseif(isset($_GET['type']))
  {
    $phpcolor->where="status = '".$_GET['type']."'";
    $count = $phpcolor->exist(2);
    $phpcolor->where="status = '".$_GET['type']."' order by jointime desc limit ".($_GET['page']*28).",28";
	$page=pages($count,28,$_GET['page'],'book','&type=',$_GET['type'],'0');
  }
  else
  {
    $phpcolor->where="1 = 1";
    $count = $phpcolor->exist(2);
    $phpcolor->where="1 = 1 order by jointime desc limit ".($_GET['page']*28).",28";
    $page=pages($count,28,$_GET['page'],'book','','','0');
  }
  $book=$phpcolor->assignment();
  $bookrs=count($book);
  $books=4-$bookrs%4;
  $phpcolor->table="book_class";
  for($count=0;$count<count($book);$count++)
  {
    $phpcolor->field="classname";
    $phpcolor->where="id = '".$book[$count]['belong']."'";
    $rs=$phpcolor->exist(1);
    $book[$count]['belongbn']=$rs['classname'];
    if($book[$count]['status']=="1")
      $book[$count]['status']="正在更新";
	elseif($book[$count]['status']=="2")
      $book[$count]['status']="完本";
  }
  $tpl -> assign("book", $book);
  $tpl -> assign("books", $books);
  $tpl -> assign("page", $page);
  $tpl -> display("book.htm");
}
?>