<?php
include(dirname(__FILE__)."/require.php");
if($ppurview[14]!=1)
{
  Header("Location: jump.php?id=25&jump=main.php");
  exit();
}
$phpcolor->table=$core_mysql_prefix."topic";
$phpcolor->where="id = '".$_GET['id']."'";
if(isset($_GET['action']) and $_GET['action'] == "del")
{
  if(isset($_GET['id']))
    $_POST['id'][0]=$_GET['id'];
  $time=date('Y-m-d H:i:s',strtotime(date('Y-m-d H:i:s'))+$core_time*60*60);
  for($i=0;$i<count($_POST['id']);$i++)
  {
    $phpcolor->table=$core_mysql_prefix."topic";
    $phpcolor->field="state = 0";
    $phpcolor->where="id = '".$_POST['id'][$i]."'";
    $phpcolor->upgrade();
    $phpcolor->table=$core_mysql_prefix."recover";
    $phpcolor->field="tid, operate, operatetime, ip, type";
    $phpcolor->where="'".$_POST['id'][$i]."', '".$_COOKIE["core_user_name"]."','".$time."','".$_SERVER['REMOTE_ADDR']."' , '1'";
    $phpcolor->write();
  } 
  Header("Location: jump.php?id=24&jump=topic.php");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "edit")
{
  $phpcolor->field="*";
  $post = $phpcolor->exist(1);
  $post['content'] = str_replace("<br />","",$post['content']);
  if($post['vote']=="1")
  {
    $phpcolor->table=$core_mysql_prefix."vote";
    $phpcolor->field="options,result";
    $phpcolor->where="tid = '".$_GET['id']."'";
    $vote = $phpcolor->exist(1);
    $voteo = split("\|", $vote['options']);
    $voter = split("\|", $vote['result']);
	$post['num']=count($voteo);
    $tpl -> assign("voteo", $voteo);
    $tpl -> assign("voter", $voter);
  }
  $tpl -> assign("post", $post);
  $tpl -> display("topic_edit.htm");
}
elseif(isset($_GET['action']) and isset($_GET['id']) and $_GET['action'] == "editdo")
{
  $phpcolor->field="vote";
  $post = $phpcolor->exist(1);
  if($post['vote']=="1")
  {
    for($i=0;$i<$_POST['num'];$i++)
    {
	  $rs1='option'.($i+1);
      if(!isset($option))
        $option=$_POST[$rs1];
      else
	    $option.="|".$_POST[$rs1];
    }
    $rs=split("\|", $option);
  }
  if($post['vote']=="1" && count($rs)<2)
    Header("Location: jump.php?id=22&jump=topic.php?action=edit=and=id=".$_GET['id']);
  elseif(strlen($_POST['theme'])<$core_themeshort or strlen($_POST['theme'])>$core_themelong or strlen($_POST['content'])<$core_contentshort or strlen($_POST['content'])>$core_contentlong)
    Header("Location: jump.php?id=8&jump=topic.php?action=edit=and=id=".$_GET['id']);
  else
  {
    $_POST['theme']=html_replace($_POST['theme']);
    $_POST['content']=nl2br(html_replace(autolink($_POST['content'])));
    $phpcolor->field="theme='".$_POST['theme']."', content='".$_POST['content']."', belong='".$_POST['belong']."', top='".$_POST['top']."', boutique='".$_POST['boutique']."', locks='".$_POST['locks']."', recommend='".$_POST['recommend']."', state='".$_POST['state']."'";
    $phpcolor->upgrade();
    if($post['vote']=="1")
    {
      for($i=0;$i<$_POST['num'];$i++)
      {
	    $rs1='result'.($i+1);
        if(!isset($result))
          $result=$_POST[$rs1];
        else
	      $result.="|".$_POST[$rs1];
	  }
	  $phpcolor->table=$core_mysql_prefix."vote";
	  $phpcolor->field="options='".$option."', result='".$result."'";
	  $phpcolor->where="tid = '".$_GET['id']."'";
      $phpcolor->upgrade();
	}
    Header("Location: jump.php?id=24&jump=topic.php");
  }
}
else
{
  if(!isset($_GET['page']))
    $_GET['page']=0;
  $phpcolor->field="*";
  if(isset($_GET['search']) and isset($_GET['theme']))
  {
    if($_GET['search']==1)
    {
      $phpcolor->where="theme like '%".$_GET['theme']."%' and state != '0'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="theme like '%".$_GET['theme']."%' and state != '0' order by published desc limit ".($_GET['page']*20).",20";
    }
    else
    {
      $phpcolor->where="author like '%".$_GET['theme']."%' and state != '0'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="author like '%".$_GET['theme']."%' and state != '0' order by published desc limit ".($_GET['page']*20).",20";
	}
    $page=pages($count,20,$_GET['page'],'topic','&theme='.$_GET['theme'].'&search=',$_GET['search'],'0');
  }
  else
  {
    if(isset($_GET['type']) and $_GET['type']=="recommend")
    {
	  $phpcolor->where="state != '0' and recommend != '0'";
      $count = $phpcolor->exist(2);
	  $phpcolor->where="state != '0' and recommend != '0' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','&type=','recommend','0');
	}
    elseif(isset($_GET['type']) and $_GET['type']=="boutique")
    {
      $phpcolor->where="state != '0' and boutique = '1'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="state != '0' and boutique = '1' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','&type=','boutique','0');
	}
    elseif(isset($_GET['type']) and $_GET['type']=="top")
    {
      $phpcolor->where="state != '0' and top = '1'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="state != '0' and top = '1' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','&type=','top','0');
	}
	else
	{
	  $phpcolor->where="state != '0'";
      $count = $phpcolor->exist(2);
      $phpcolor->where="state != '0' order by published desc limit ".($_GET['page']*20).",20";
	  $page=pages($count,20,$_GET['page'],'topic','','','0');
	}
  }
  $post=$phpcolor->assignment();
  $phpcolor->table=$core_mysql_prefix."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]['authorip']=is_ip($post[$count]['author']);
  }
  $tpl -> assign("post", $post);
  $tpl -> assign("page", $page);
  $tpl -> display("topic.htm");
}
?>